Mut semantics in destructed function arguments

Why does mut inside a struct dereference trigger a copy? In the example below what are the semantics behind the additional mut included in the last function?

struct Wrapper {
    data: [u32; 1],
}

fn main() {
    let mut wrapper = Wrapper { data: [1] };
    println!("Initial value: {:?}", wrapper.data); // Initial value: [1]
    modify_wrapper(&mut wrapper);
    println!("Value after non-mut fn: {:?}", wrapper.data); // Value after non-mut fn: [2]
    modify_wrapper_mut(&mut wrapper);
    println!("Value after mut fn: {:?}", wrapper.data); // Value after mut fn: [2]
}

fn modify_wrapper(Wrapper { data }: &mut Wrapper) {
    data[0] += 1;
}

fn modify_wrapper_mut(Wrapper { mut data }: &mut Wrapper) {
    data[0] += 1;
}

See this previous topic. Definitely unexpected and not very well documented

2 Likes

Thanks! Annoying that the linting suggestions in the other thread haven't got to the top of the list to implement yet. Glad to see I'm not being totally thick.

1 Like

It might be addressed in the next edition.

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.