From 5764d1991748f10d37592c7e0b732d6035ff9e57 Mon Sep 17 00:00:00 2001 From: Brad Clawsie Date: Sat, 31 Oct 2026 15:07:15 -0500 Subject: fix: feat5 commit ff2f7e19fce1174d0b6796c11d4fec2314e17657 Author: Brad Clawsie Date: Sat Oct 31 15:06:06 2026 -0500 feat: ex5 --- ex5/src/main.rs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 ex5/src/main.rs (limited to 'ex5/src') diff --git a/ex5/src/main.rs b/ex5/src/main.rs new file mode 100644 index 0000000..4ee9764 --- /dev/null +++ b/ex5/src/main.rs @@ -0,0 +1,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); + } +} -- cgit v1.2.3