Condition inside my_iterator.for_each

Hi ,i have working on pong game and try to make it work with rayon:

this works:
self.my_vec_balls.par_iter_mut().for_each(|ball| ball.rect.translate(ball.vel));
this doesn't work:
let par_iter_balls = self.my_vec_balls.par_iter_mut();
for ball in (self.my_vec_balls.par_iter_mut()):

i have conditions for bouncing with walls but i have no idea how to insert condition inside for_each.

Your description is rather vague. It is entirely possible to include an if expression inside the closure you pass to for each. But you probably tried that already.

You can't use for ball in parallel_iterator, because for loops are strictly for Iterator.

You can use an arbitrary block of code in the parallel for_each though:

self.my_vec_balls.par_iter_mut().for_each(|ball| {
    if ball.is_red() {
        // do red stuff
    } else {
        // do other stuff

thank you !

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.