-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathfind_max.rs
171 lines (150 loc) · 3.89 KB
/
find_max.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#![crate_name = "find_max"]
#![cfg_attr(feature = "nightly", feature(test))]
//! Test various syntaxes to find the maximum value of a vector using generic function.
//!
//! Tested with rust-1.41.0 and rust-1.41.0-nightly
//! To run the benchmark, use nightly version and
//! `cargo bench --bin find_max --features=nightly`
//!
//! @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<T: Ord>(lst: &Vec<T>) -> Option<&T> {
lst.iter().max_by(|x, y| x.cmp(y))
}
#[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 self::test::Bencher;
use super::*;
#[cfg(test)]
static SIZE: i32 = 100;
#[bench]
fn bench_find_max1(b: &mut Bencher) {
let v = (0..SIZE).collect::<Vec<i32>>();
b.iter(|| {
find_max1(&v);
});
}
#[bench]
fn bench_find_max2(b: &mut Bencher) {
let v = (0..SIZE).collect::<Vec<i32>>();
b.iter(|| {
find_max2(&v);
});
}
#[bench]
fn bench_find_max3(b: &mut Bencher) {
let v = (0..SIZE).collect::<Vec<i32>>();
b.iter(|| {
find_max3(&v);
});
}
#[bench]
fn bench_find_maxstd(b: &mut Bencher) {
let v = (0..SIZE).collect::<Vec<i32>>();
b.iter(|| {
find_maxstd(&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));
#[cfg(feature = "nightly")]
{
println!("find_maxstd -> {:?}", find_maxstd(&int_v));
}
let v = vec!["qehgesrhsetha", "bqthst", "cthersth"];
let b = find_max3(&v);
println!("{:?}", b);
println!("{:?}", v);
println!("{:?}", b);
}