I was trying to manipulate the field x of the struct Foo by borrowing a mutable reference from its instance foo.
If I try to print the field x using the moved binding y of the instance foo after the move of the original instance, it keeps printing the value that haven't changed.
Simplified example below:
struct Foo {
x: i32,
}
fn main() {
let mut foo = Foo { x: 42 };
let x = &mut foo.x;
*x = 13;
let y = foo;
println!("{}", y.x); // -> 42; expected result: 13
}
Instead, if I print the moved binding y itself, it prints the changed value.
println!("{:?}", y); // -> Foo { x: 13 }
Or, if I print something else like x or foo.x before the move, it prints the thing as expected.
println!("{}", x); // -> 13
let y = foo;
println!("{}", y.x); // -> 13
Is this an intended behavior?
println!("{:?}", (&y).x);prints expected result and if you executeprintln!("{:?}", y.x);after the previous println it also prints expected result : play.rust-lang.org/…