Factorial in #rust , the #functional paradigm pays off well.

#rustlang

@atamakahere Surely that would need to limit itself to u8 as the input and u128 as the output? And even then you'd need a hard limit enforced on the input value because 255! is equal to more than 3.35E104, whereas u128::MAX is less than 3.5E38.

In fact, the biggest number which fits is 34! equal to roughly 2.95E38.

Factorials grow up so fast.

#RustLang #factorials

@bobulous @atamakahere This will also crash on 0! Which should equal 1.
@zethtren @bobulous @atamakahere Just tried that, and it correctly returns 1 on input 0 (I would have been disgusted if it didn't)

@Corax42 @zethtren @bobulous that’s the neat part, it works for zero as well,

The `.product()` [1] is implemented using `fold` and starts from 1, so if it doesn’t have anything to accumulate, it returns 1, which is correct for 0 and 1 as input.

Also `RangeInclusive` doesn’t complain if the range is `(1..=0)`. because it is an fused iterator [2], it will return `None` once and `.product()` will do the job

accum.rs - source

Source of the Rust file `library/core/src/iter/traits/accum.rs`.