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
struct Counter {
count: u32,
}
impl Counter {
fn new() -> Counter {
Counter { count: 0 }
}
}
impl Iterator for Counter {
type Item = u32;
fn next(&mut self) -> Option<Self::Item> {
self.count += 1;
if self.count < 6 {
Some(self.count)
} else {
None
}
}
}
#[no_mangle]
pub extern "C" fn add_one(a: u32) -> u32 {
a + 1
}
#[no_mangle]
pub extern "C" fn iterators() -> u32 {
Counter::new()
.zip(vec![1, 2, 3, 4, 5].iter().skip(1))
.map(|(a, b)| a * b)
.filter(|x| x % 3 == 0)
.sum()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn add_one_test() {
assert_eq!(add_one(3), 4);
}
#[test]
fn iterators_test() {
assert_eq!(iterators(), 18);
}
}