blob: 2514b629901bb8139671ca4136338c2300ea6a13 (
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
35
36
|
fn factorial(n: u32) -> u32 {
if n == 0 {
return 1;
}
let mut t = n;
let mut j = n - 1;
while j > 0 {
t = t * j;
j = j - 1;
}
t
}
fn main() {
println!("{}", 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);
}
}
|