write a code in rust
- User
write a code in rust
- Assistant
Sure! Let's write a simple Rust program that calculates the factorial of a number. The factorial of a number (n) is defined as the product of all positive integers less than or equal to (n). For example, the factorial of 5 (denoted as (5!)) is (5 \times 4 \times 3 \times 2 \times 1 = 120).
// Define a function to calculate the factorial of a number fn factorial(n: u64) -> u64 { match n { 0 | 1 => 1, _ => n * factorial(n - 1), } } fn main() { let num = 5; println!("The factorial of {} is {}", num, factorial(num)); }Here is a breakdown of what this code does: