summaryrefslogtreecommitdiff
path: root/ex6/src/main.rs
diff options
context:
space:
mode:
authorBrad Clawsie <brad@b7j0c.org>2026-10-31 15:17:41 -0500
committerBrad Clawsie <brad@b7j0c.org>2026-10-31 15:17:41 -0500
commit05cb8a3fee673ff33f90b26b2c01d42de11bb5fb (patch)
treea9eb848df534cb126426bd152ada9db5dfdbfc8d /ex6/src/main.rs
parent5764d1991748f10d37592c7e0b732d6035ff9e57 (diff)
feat: ex6
Diffstat (limited to 'ex6/src/main.rs')
-rw-r--r--ex6/src/main.rs36
1 files changed, 36 insertions, 0 deletions
diff --git a/ex6/src/main.rs b/ex6/src/main.rs
new file mode 100644
index 0000000..2514b62
--- /dev/null
+++ b/ex6/src/main.rs
@@ -0,0 +1,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);
+ }
+}