rustlings

solving rustlings ft. dracuxan
git clone [email protected]:dracuxan/rustlings.git
Log | Files | Refs

iterators4.rs (881B)


      1 fn factorial(num: u64) -> u64 {
      2     // TODO: Complete this function to return the factorial of `num` which is
      3     // defined as `1 * 2 * 3 * … * num`.
      4     // https://en.wikipedia.org/wiki/Factorial
      5     //
      6     // Do not use:
      7     // - early returns (using the `return` keyword explicitly)
      8     // Try not to use:
      9     // - imperative style loops (for/while)
     10     // - additional variables
     11     // For an extra challenge, don't use:
     12     // - recursion
     13 }
     14 
     15 fn main() {
     16     // You can optionally experiment here.
     17 }
     18 
     19 #[cfg(test)]
     20 mod tests {
     21     use super::*;
     22 
     23     #[test]
     24     fn factorial_of_0() {
     25         assert_eq!(factorial(0), 1);
     26     }
     27 
     28     #[test]
     29     fn factorial_of_1() {
     30         assert_eq!(factorial(1), 1);
     31     }
     32     #[test]
     33     fn factorial_of_2() {
     34         assert_eq!(factorial(2), 2);
     35     }
     36 
     37     #[test]
     38     fn factorial_of_4() {
     39         assert_eq!(factorial(4), 24);
     40     }
     41 }