0% found this document useful (0 votes)
9 views1 page

Rust

The document introduces Rust in 50 lines by defining a recursive factorial function, calling it in the main function, and printing the result. It defines a factorial function that takes a number and returns its factorial, handles the base case, and recursively calls itself to calculate other values. It then defines a main function, calls the factorial function with an input of 5, stores the result, and prints it along with a message.

Uploaded by

anyasopa7684
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views1 page

Rust

The document introduces Rust in 50 lines by defining a recursive factorial function, calling it in the main function, and printing the result. It defines a factorial function that takes a number and returns its factorial, handles the base case, and recursively calls itself to calculate other values. It then defines a main function, calls the factorial function with an input of 5, stores the result, and prints it along with a message.

Uploaded by

anyasopa7684
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

Here's a basic introduction to Rust in 50 lines:

```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);

// Print the result


println!("Factorial of 5 is: {}", result);
}
```

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`.

You might also like