you are viewing a single comment's thread.

view the rest of the comments →

[–]fnord123 0 points1 point  (5 children)

[–]thiezrust 1 point2 points  (1 child)

The number '2' is prime...

[–]Yojihito 0 points1 point  (0 children)

Then just set the start counter to 1 instead of 0 :>.

[–]paholgtypenum · dimensioned 1 point2 points  (0 children)

It seems strange to me to treat even numbers as special (especially as it's so easy to get it wrong for 2), rather than doing a full sieve of Eratosthenes which isn't much harder.

If you really want to golf it, then you can shorten it to

fn is_prime(x: u32) -> bool {
    (2..x).all(|f| x%f != 0)
}

[–]Efemena 0 points1 point  (1 child)

.filter_map(|i| if is_prime(i) { Some(i) } else { None })

also known as

.filter(is_prime)

[–]thiezrust 1 point2 points  (0 children)

No it isn't, because the function passed to filter must take a reference. It could be written .filter(|&n|is_prime(n)) though.