Skip to content

Commit 18f7e66

Browse files
committed
loops
1 parent 6383a3c commit 18f7e66

File tree

1 file changed

+9
-5
lines changed
  • exercises/02_basic_calculator/06_while/src

1 file changed

+9
-5
lines changed

exercises/02_basic_calculator/06_while/src/lib.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
// Rewrite the factorial function using a `while` loop.
22
pub fn factorial(n: u32) -> u32 {
3-
// The `todo!()` macro is a placeholder that the compiler
4-
// interprets as "I'll get back to this later", thus
5-
// suppressing type errors.
6-
// It panics at runtime.
7-
todo!()
3+
let mut result = 1;
4+
let mut i = 1;
5+
6+
while i <= n {
7+
result *= i;
8+
i += 1;
9+
}
10+
11+
result
812
}
913

1014
#[cfg(test)]

0 commit comments

Comments
 (0)