Skip to content

Instantly share code, notes, and snippets.

@nexxeln
Created March 3, 2023 18:19
Show Gist options
  • Save nexxeln/4b4b61b44cd1d8d19216577488bb724c to your computer and use it in GitHub Desktop.
Save nexxeln/4b4b61b44cd1d8d19216577488bb724c to your computer and use it in GitHub Desktop.

Beautiful code is just beautiful. Doesn't need to be simple.

  • Ugly factorial function
fn factorial_loop(n: u32) -> u32 {
    let mut result = 1;
    for i in 1..=n {
        result *= i;
    }
    result
}
  • Beautiful factorial function
fn factorial_match(n: u32) -> u32 {
    match n {
        0 => 1,
        _ => n * factorial_match(n - 1),
    }
}
  • I'm melting factorial function
fn factorial_comprehension(n: u32) -> u32 {
    (1..=n).into_iter().product()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment