Is there an elegant solution to modifying a structure while iterating?
Asked Answered
E

2

26

I'm trying to build a vector of points that are changed while iterating over them:

struct Point {
    x: i16,
    y: i16,
}

fn main() {
    let mut points: Vec<Point> = vec![];
    // unsure if point is mutable
    points.push(Point { x: 10, y: 10 });
    // thus trying it explicitly
    let mut p1 = Point { x: 20, y: 20 };
    points.push(p1);

    for i in points.iter() {
        println!("{}", i.x);
        i.x = i.x + 10;
    }
}

When compiling, I get the error:

error[E0594]: cannot assign to immutable field `i.x`
  --> src/main.rs:16:9
   |
16 |         i.x = i.x + 10;
   |         ^^^^^^^^^^^^^^ cannot mutably borrow immutable field

As I learned here, Rust doesn't allow modifying the structure while iterating over it, thus the error.

How do I modify it in an elegant way? If I read this answer and get it right then the following comes to my mind:

  1. pop the item from the vector, modify it and push it back.
  2. create a temporary structure where I push the changed items to and replace original structure with the temporary one outside the loop (how?).

While I think I can get (1) working, I'm not really happy about all this pop's and push's (is this high-performance anyhow?). Concerning (2), I have no idea how to get it working - if this would work at all.

Questions:

  1. Is (2) an solution and if yes, how would it look like?
  2. Are there any other solutions?
  3. What's the advantage or disadvantage of the different solutions, especially with respect to performance?
Emersen answered 28/3, 2015 at 21:38 Comment(0)
F
34

You cannot modify the structure you are iterating over, that is, the vector points. However, modifying the elements that you get from the iterator is completely unproblematic, you just have to opt into mutability:

for i in points.iter_mut() {

Or, with more modern syntax:

for i in &mut points {

Mutability is opt-in because iterating mutably further restricts what you can do with points while iterating. Since mutable aliasing (i.e. two or more pointers to the same memory, at least one of which is &mut) is prohibited, you can't even read from points while the iter_mut() iterator is around.

Frigidaire answered 28/3, 2015 at 23:0 Comment(4)
These days you can write for i in &mut points since it uses the IntoIterator trait.Outlier
@ChrisMorgan Right, I keep forgetting that one. Thanks.Frigidaire
Works for one loop but not for nested: for i in &mut points.iter() { for j in points.iter() { // some stuff and calculate a } i.x = i.x + a; // error: cannot assign to immutable field 'i.x'Emersen
@Emersen That should give a lifetime error related to points, not anything related to i. And indeed it does when I type it up in the play pen: is.gd/uIMLKs There is no easy workaround this time (&mut pointers must never alias), so you should write a separate question with full explanation of that you're trying to do and why it requires both mutability and nested loops.Frigidaire
B
-1

Not the best option in your specific case, but option 2 would look something like this (we can iterate over the collected result directly to skip the temporary Vec variable). This would be useful if you're modifying an element not pointed at by the iterator.

    for (idx, point) in points
        .iter()
        .enumerate()
        .map(|(a, b)| (a, *b)) // Clone the Points (by dereferencing)
        .collect::<Vec<(usize, Point)>>() // collect the iterator, i.e. creating the temporary Vec
    {
        println!("{}", point.x);
        points[idx].x = point.x + 10;
    }
Brewing answered 20/7 at 15:50 Comment(3)
Why not just iterate by index, without any collect()?Gilda
@ChayimFriedman It wouldn't compile without it due to points[idx] mutably borrowing points. Although I still wouldn't recommend doing it this wayBrunt
@FilipeRodrigues It willGilda

© 2022 - 2024 — McMap. All rights reserved.