I'm reading The Rust Programming Language and one thing is not clear:
let mut mut_value = 6;
match mut_value {
ref mut m => {
*m += 10;
println!("We added 10. `mut_value`: {:?}", m);
},
}
Why do we need to dereference it to change it? We already have a mutable reference.
A reference is an address pointer. If you were to just do m += 10
, you'd be changing the memory address (Rust doesn't let you do this without unsafe
). What you want to do is change the value at m
. So where's the value? Follow the pointer! You do this by dereferencing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With