initial commit
This commit is contained in:
10
exercises/02_basic_calculator/05_factorial/Cargo.toml
Normal file
10
exercises/02_basic_calculator/05_factorial/Cargo.toml
Normal file
@@ -0,0 +1,10 @@
|
||||
[package]
|
||||
name = "factorial"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[lints.rust]
|
||||
# We silence dead code warnings for the time being in order to reduce
|
||||
# compiler noise.
|
||||
# We'll re-enable them again once we explain how visibility works in Rust.
|
||||
dead_code = "allow"
|
||||
36
exercises/02_basic_calculator/05_factorial/src/lib.rs
Normal file
36
exercises/02_basic_calculator/05_factorial/src/lib.rs
Normal file
@@ -0,0 +1,36 @@
|
||||
// Define a function named `factorial` that, given a non-negative integer `n`,
|
||||
// returns `n!`, the factorial of `n`.
|
||||
//
|
||||
// The factorial of `n` is defined as the product of all positive integers up to `n`.
|
||||
// For example, `5!` (read "five factorial") is `5 * 4 * 3 * 2 * 1`, which is `120`.
|
||||
// `0!` is defined to be `1`.
|
||||
//
|
||||
// We expect `factorial(0)` to return `1`, `factorial(1)` to return `1`,
|
||||
// `factorial(2)` to return `2`, and so on.
|
||||
//
|
||||
// Use only what you learned! No loops yet, so you'll have to use recursion!
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::factorial;
|
||||
|
||||
#[test]
|
||||
fn first() {
|
||||
assert_eq!(factorial(0), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn second() {
|
||||
assert_eq!(factorial(1), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn third() {
|
||||
assert_eq!(factorial(2), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fifth() {
|
||||
assert_eq!(factorial(5), 120);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user