-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfind_max_recursive.rs
46 lines (36 loc) · 1018 Bytes
/
find_max_recursive.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#[allow(dead_code)]
fn find_max_recursive(coll: &Vec<i32>) -> i32 {
if coll.len() == 0 {
return 0;
}
let mut coll_clone = coll.clone();
// A find_max recursive function is defined internally to
// provide the state capabilities and protect the behavior
// of the function
fn find_max(max_num: i32, coll: &mut Vec<i32>) -> i32 {
if coll.len() == 0 {
return max_num;
}
let last = coll.pop().unwrap();
if last > max_num {
find_max(last, coll)
} else {
find_max(max_num, coll)
}
}
find_max(*coll.first().unwrap(), &mut coll_clone)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn finds_the_greatest_value() {
let items = vec![8, 12, 128, 1, 90, -1, 32];
assert_eq!(find_max_recursive(&items), 128);
}
#[test]
fn returns_zero_if_empty_vec_is_provided() {
let items = vec![];
assert_eq!(find_max_recursive(&items), 0);
}
}