Rust recursive functions: factorial
examples/functions/factorial/src/main.rs
fn main() { let n = 10; let fact = recursive_factorial(n); println!("Factorial of {n} is {fact}"); let fact = factorial(n); println!("Factorial of {n} is {fact}"); } fn recursive_factorial(n: i64) -> i64 { if n == 0 { return 1; } n * recursive_factorial(n - 1) } fn factorial(n: i64) -> i64 { let mut fact = 1; for i in 1..=n { fact *= i; } fact }
Factorial of 10 is 3628800 Factorial of 10 is 3628800