blob: 4ee9764284660674af82e5277a9609744a2ebaa3 (
plain)
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
|
fn factorial(n: u32) -> u32 {
if n == 0 {
return 1;
}
let mut t = n;
for j in (1..n).rev() {
t = t * j;
}
return t;
}
fn main() {
println!("answer:{}", factorial(5));
}
#[cfg(test)]
mod tests {
use crate::factorial;
#[test]
fn t0 () {
assert_eq!(factorial(0), 1);
}
#[test]
fn t1 () {
assert_eq!(factorial(1), 1);
}
#[test]
fn t2 () {
assert_eq!(factorial(5), 120);
}
}
|