For loop: cannot assign to data in a `&` reference [E0389]


I’m trying to write a piece of code more concise, but I hit this roadblock:
Minimal (non)-working example:

error: cannot assign to data in a & reference [E0389]
x.v = 0;

struct S {
    v: i32,

struct P {
    k: S,
    l: S,

fn test() {
    // Given:
    let mut data = P {
        k: S {v:1},
        l: S {v:1}, // and possibly more ...

    // Want to reset these, something like this:
    for x in &[&mut data.k, &mut data.l] {
        // Possibly do something here: func(x)
        // Then reset:
        x.v = 0;           // <----------------  ERROR

I’m missing something here and I really can’t find out what it is.
Any insight is very much appreciated!


Have you tried “for x in &mut [&mut data.k, &mut data.l] {”?


Thank you!
I learned something that I’ll not forget again :slight_smile:

I was so sure that I had tried that first, but obviously no.