In this code, sref1
and sref2
are the addresses of s
, and the addresses are the same. What is the difference between ref
and &
?
fn main() {
let s = String::from("hello");
let sref1 = &s;
let ref sref2 = s;
println!("{:p}", sref1);
println!("{:p}", sref2);
f1(&s);
f2(s);
}
fn f1(_s: &String) {
println!("{:p}", _s);
}
fn f2(ref _s: String) {
println!("{:p}", _s);
}
_s
in f1
and f2
is also the address of the string, f2
will take ownership, but the address printed by f2
is not the same as the address printed by f1
. Why?