Beautiful code is just beautiful. Doesn't need to be simple.
- Ugly factorial function
fn factorial_loop(n: u32) -> u32 {
let mut result = 1;
for i in 1..=n {
result *= i;
}
result
}
- Beautiful factorial function
fn factorial_match(n: u32) -> u32 {
match n {
0 => 1,
_ => n * factorial_match(n - 1),
}
}
- I'm melting factorial function
fn factorial_comprehension(n: u32) -> u32 {
(1..=n).into_iter().product()
}