Ich versuche zu verstehen, wann ref
und &
sowie wenn sie austauschbar sind zu verwenden. In einem anderen Codebeispiel habe ich die falsche Menge von &
verwendet, aber bei einigen Tests habe ich gesehen, dass das manchmal funktioniert. Nehmen Sie diesen Code als Beispiel:Warum ändert sich die Adresse einer Variablen, wenn ich mehrere '&' hinzufüge?
fn main() {
let test = 5;
let ref testRef = test;
println!("&test as *const _ {:?}", &test as *const _);
println!("&testRef as *const _ {:?}", &testRef as *const _);
println!("&*testRef as *const _ {:?}", &*testRef as *const _);
println!("&&&&&&&&&&testRef as *const _ {:?}", &&&&&&&&&&testRef as *const _);
println!("&&&&&testRef as *const _ {:?}", &&&&&testRef as *const _);
println!("&&&&&&*testRef as *const _ {:?}", &&&&&&*testRef as *const _);
println!("&&&&&&&&&&testRef {:?}", &&&&&&&&&&testRef);
println!("&&&&&testRef {:?}", &&&&&testRef);
println!("&&&&&&*testRef {:?}", &&&&&&*testRef);
}
Shell:
&test as *const _ 0x7fffac2d5be4 <- this no problem I understand
&testRef as *const _ 0x7fffac2d5bd8 <- this no problem I understand
&*testRef as *const _ 0x7fffac2d5be4 <- this no problem I understand
&&&&&&&&&&testRef as *const _ 0x7fffac2d5998 <- why this found and
&&&&&testRef as *const _ 0x7fffac2d58f0 <- It changes every
&&&&&&*testRef as *const _ 0x7fffac2d5840 <- time you add &
&&&&&&&&&&testRef 5
&&&&&testRef 5
&&&&&&*testRef 5