How to use fold with rayon

    use rayon::prelude::*;
    let (wrapping_paper, ribbon_area): (i32, i32) = arg
        .par_lines()
        .map(|line: &str| Box::parse(line, delim))
        .filter(Option::is_some)
        .map(Option::unwrap)
        .map(|b: Box| (b.gift_wrap_surface(), b.ribbon_area()))
        .fold((0, 0), |acc, cur| (acc.0 + cur.0, acc.1 + cur.1));
$ cargo run  -q --release
error[E0277]: expected a `Fn<()>` closure, found `({integer}, {integer})`
  --> src/d2.rs:66:15
   |
66 |         .fold((0, 0), |acc, cur| (acc.0 + cur.0, acc.1 + cur.1));
   |               ^^^^^^ expected an `Fn<()>` closure, found `({integer}, {integer})`
   |
   = help: the trait `Fn<()>` is not implemented for `({integer}, {integer})`
   = note: wrap the `({integer}, {integer})` in a closure with no arguments: `|| { /* code */ }`

error[E0308]: mismatched types
  --> src/d2.rs:60:53
   |
60 |       let (wrapping_paper, ribbon_area): (i32, i32) = arg
   |  ________________________________________----------___^
   | |                                        |
   | |                                        expected due to this
61 | |         .par_lines()
62 | |         .map(|line: &str| Box::parse(line, delim))
63 | |         .filter(Option::is_some)
64 | |         .map(Option::unwrap)
65 | |         .map(|b: Box| (b.gift_wrap_surface(), b.ribbon_area()))
66 | |         .fold((0, 0), |acc, cur| (acc.0 + cur.0, acc.1 + cur.1));
   | |________________________________________________________________^ expected tuple, found struct `Fold`
   |
   = note: expected tuple `(i32, i32)`
             found struct `Fold<rayon::iter::Map<rayon::iter::Map<rayon::iter::Filter<rayon::iter::Map<rayon::str::Lines<'_>, [closure@src/d2.rs:62:14: 62:50]>, for<'r> fn(&'r Option<d2::Box>) -> bool {Option::<d2::Box>::is_some}>, fn(Option<d2::Box>) -> d2::Box {Option::<d2::Box>::unwrap}>, [closure@src/d2.rs:65:14: 65:63]>, ({integer}, {integer}), _>`

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0277, E0308.
For more information about an error, try `rustc --explain E0277`.
error: could not compile `aoc15`

To learn more, run the command again with --verbose.

Parallel fold expects an initializer function, not an initial value.

See the examples in the doc here.

fold doesn't give you a value, if you need to consolidate your parallel iterator, then you need to use reduce or one of its variants

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.