fn main() {
let s = S { i: 42 };
println!("{:p}", &s); // 0x7fff4c7147ec
f2(&s); // 0x7fff4c7147ec
f2(&s); // 0x7fff4c7147ec
f(s); // 0x7fff4c71476c
}
期望:
f(s); 这行也不应该是输出 0x7fff4c7147ec,怎么不一样?
1
共 2 条评论, 1 页
评论区
写评论函数参数中使用ref,相当于使用ref patterns,会bind产生一个新的引用,打印的是新引用的地址。
因为
f(s)
这里s
发生了 move。在最后加一行
println!("{:p}", &s);
, 编译报错 value borrowed here after move