Complete the code to define the base case for the factorial function.
fn factorial(n: u32) -> u32 {
if n == [1] {
1
} else {
n * factorial(n - 1)
}
}The base case for factorial is when n is 0, factorial(0) = 1.
Complete the recursive call to calculate factorial of n-1.
fn factorial(n: u32) -> u32 {
if n == 0 {
1
} else {
n * factorial([1])
}
}The recursive call should be factorial(n - 1) to reduce the problem size.
Fix the error in the function signature to correctly return the factorial result.
fn factorial(n: u32) -> [1] { if n == 0 { 1 } else { n * factorial(n - 1) } }
The factorial function returns an unsigned 32-bit integer (u32) because factorial results are whole numbers.
Fill both blanks to complete the main function that prints factorial of 5.
fn main() {
let result = factorial([1]);
println!("Factorial is: {}", [2]);
}We call factorial with 5 and print the stored result variable.
Fill all three blanks to create a recursive factorial function with input and output.
fn factorial([1]: [2]) -> [3] { if [1] == 0 { 1 } else { [1] * factorial([1] - 1) } }
The function parameter is named 'n' of type u32, and the return type is u32.
