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
#![feature(test)]
extern crate test;
use test::Bencher;
#[allow(dead_code)]
pub fn compute_add(a:i32,b:i32) -> i32 {
let s = a + b;
println!("用Rust计算add的结果是:{}",s);
s
}
#[allow(dead_code)]
pub fn compute_sub(a:i32,b:i32) -> i32 {
let s = a - b;
println!("用Rust计算sub的结果是:{}",s);
s
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_compute_add() {
assert_eq!(compute_add(1000,999), 1999);
}
#[test]
fn test_compute_sub() {
assert_eq!(compute_sub(999,1000), -1);
}
#[bench]
fn bench_compute_add(b:&mut Bencher) {
b.iter(|| compute_add(1000,999));
}
#[bench]
fn bench_compute_sub(b:&mut Bencher) {
b.iter(|| compute_sub(1000,999));
}
}