HNHacker News
TopNewBestAskShowJobs

Jarqwiz

25 karma · joined September 24, 2019

submissionscomments
Jarqwiz··on Ask HN: What is the most beautiful piece of code you've ever read?
For me it's a function that calculates factorial using iterators in Rust:

  fn factorial(i: u64) -> u64 {
      (1..=i).product()
  }
In almost every other language this code would look messy or use some terrible recursion.

For example in C it would look something like this:

  long factorial(int n)
  {
      int c;
      long result = 1;

      for (c = 1; c <= n; c++)
          result = result * c;
 
      return result;
  }
Or with recursion:

  long factorial(int n)
  {
      if (n == 0)
          return 1;
      else
          return(n * factorial(n-1));
  }
In any case, I thin Rust looks better in every way with its cleaner syntax.