DEV Community


Discussion on: Daily Challenge #225 - Square'n'Sum

qm3ster profile image
Mihail Malo • Edited

Sum of squares is the default example of the Rayon crate:

use rayon::prelude::*;
fn sum_of_squares(input: &[i32]) -> i32 {
    input.par_iter() // <-- just change that!
         .map(|&i| i * i)

(this runs in parallel)

Apparently people here are really into onelinerization.

let sum_of_squares = |nums| nums.iter().map(|&i| i * i).sum();

No parallelism, but at least it's shorter than Python.