forked from eliovir/rust-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind_max.rs
More file actions
149 lines (131 loc) · 3.08 KB
/
find_max.rs
File metadata and controls
149 lines (131 loc) · 3.08 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#![crate_name="find_max"]
//! Test various syntaxes to find the maximum value of a vector using generic function.
//!
//! @license MIT license <http://www.opensource.org/licenses/mit-license.php>
/**
* Code from Rust MeetUp (March, 31st 2014) at Mozilla Space, Paris.
* https://reps.mozilla.org/e/meetup-rust-paris-02/
*/
fn find_max1<'a, T: Ord>(lst: &'a Vec<T>) -> Option<&'a T> {
let mut max = None;
for i in lst.iter() {
max = match max {
None => Some(i),
Some(ref m) if i > *m => Some(i),
_ => max
}
}
max
}
/**
* Using a closure.
*
* Inspired by tutorial "15 Closure"
* http://doc.rust-lang.org/tutorial.html#closures
*/
fn find_max2<'a, T: Ord>(lst: &'a Vec<T>) -> Option<&'a T> {
let mut max = None;
// huon said:
// place the closure and the loop into its own scope
// to constrain the borrow from the closure.
// (to be able to modify `max` it has to take an `&mut` borrow to it)
{
let mut find_max = |i: &'a T| {
max = match max {
None => Some(i),
Some(ref m) if i > *m => Some(i),
_ => max
}
};
for x in lst.iter() {
find_max(x);
}
}
max
}
/**
* Using a closure and .map().
*/
fn find_max3<'a, T: Ord>(lst: &'a Vec<T>) -> Option<&'a T> {
let mut max = None;
let find_max = |i: &'a T| {
max = match max {
None => Some(i),
Some(ref m) if i > *m => Some(i),
_ => max
};
max
};
lst.iter().map(find_max).last().unwrap()
}
/**
* Using std lib
*/
#[cfg(feature = "nightly")]
fn find_maxstd<'a, T: Ord>(lst: &'a Vec<T>) -> Option<&'a T> {
lst.iter().max_by(|x| *x)
}
#[test]
fn test_find_max1() {
let v = vec!(0i32, 1, 2, 3, 4, 5, 6, 7, 8, 9);
let nine = 9i32;
assert_eq!(Some(&nine), find_max1(&v));
}
#[test]
fn test_find_max2() {
let v = vec!(0i32, 1, 2, 3, 4, 5, 6, 7, 8, 9);
let nine = 9i32;
assert_eq!(Some(&nine), find_max2(&v));
}
#[test]
fn test_find_max3() {
let v = vec!(0i32, 1, 2, 3, 4, 5, 6, 7, 8, 9);
let nine = 9i32;
assert_eq!(Some(&nine), find_max3(&v));
}
#[test]
#[cfg(feature = "nightly")]
fn test_find_maxstd() {
let v = vec!(0i32, 1, 2, 3, 4, 5, 6, 7, 8, 9);
let nine = 9i32;
assert_eq!(Some(&nine), find_maxstd(&v));
}
#[cfg(feature = "nightly")]
#[cfg(test)]
mod bench {
extern crate test;
use test::Bencher;
#[bench]
fn bench_find_max1(b: &mut Bencher) {
let v = vec!(0i32, 1, 2, 3, 4, 5, 6, 7, 8, 9);
b.iter(|| {
find_max1(&v);
});
}
#[bench]
fn bench_find_max2(b: &mut Bencher) {
let v = vec!(0i32, 1, 2, 3, 4, 5, 6, 7, 8, 9);
b.iter(|| {
find_max2(&v);
});
}
#[bench]
fn bench_find_max3(b: &mut Bencher) {
let v = vec!(0i32, 1, 2, 3, 4, 5, 6, 7, 8, 9);
b.iter(|| {
find_max3(&v);
});
}
}
#[cfg(not(test))]
fn main () {
let int_v = vec!(5i32, 2, 0, 8, 2);
println!("find_max1 -> {:?}", find_max1(&int_v));
println!("find_max2 -> {:?}", find_max2(&int_v));
println!("find_max3 -> {:?}", find_max3(&int_v));
let v = vec!("qehgesrhsetha", "bqthst", "cthersth");
let b = find_max3(&v);
println!("{:?}", b);
println!("{:?}", v);
println!("{:?}", b);
}