Logo

Programming-Idioms

History of Idiom 31 > diff from v57 to v58

Edit summary for version 58 by Aaditya Sahay:
[Rust] Removing unnecessary match arm for 1 => 1, as it can be included with 0

Version 57

2020-03-20, 17:57:23

Version 58

2020-07-02, 15:02:29

Idiom #31 Recursive factorial (simple)

Create recursive function f which returns the factorial of non-negative integer i, calculated from f(i-1)

Idiom #31 Recursive factorial (simple)

Create recursive function f which returns the factorial of non-negative integer i, calculated from f(i-1)

Variables
f,i
Code
fn factorial(num: u64) -> u64 {
    match num {
        0 => 1,
        1 => 1,
        _ => factorial(num - 1) * num,
    }
}
Code
fn factorial(num: u64) -> u64 {
    match num {
        0 | 1 => 1,
        _ => factorial(num - 1) * num,
    }
}
Comments bubble
match arm for 0|1 checks if the numbers are either 0 or 1
Doc URL
https://doc.rust-lang.org/book/ch18-03-pattern-syntax.html#multiple-patterns