Rust
Rust
```rust
// Define a function to calculate the factorial
fn factorial(n: u64) -> u64 {
if n <= 1 {
1
} else {
n * factorial(n - 1)
}
}
fn main() {
// Call the factorial function and store the result
let result = factorial(5);
Explanation:
1. `fn factorial(n: u64) -> u64 {`: This line defines a function named `factorial`
that takes an unsigned 64-bit integer `n` as input and returns an unsigned 64-bit
integer.
2. `if n <= 1 {`: This line begins an if statement to handle the base case of the
factorial function.
3. `1`: This line returns 1 when the input is 0 or 1.
4. `n * factorial(n - 1)`: This line recursively calls the `factorial` function to
calculate the factorial for values greater than 1.
5. `fn main() {`: This line defines the `main` function, which is the entry point
of the Rust program.
6. `let result = factorial(5);`: This line calls the `factorial` function with an
argument of 5 and stores the result in the variable `result`.
7. `println!("Factorial of 5 is: {}", result);`: This line prints the result along
with a message to the standard output. The `{}` is a placeholder for the value of
`result`.