rust 中 ownership 的小问题

2020-04-03 12:51:17 +08:00
 azev
fn makes_copy(some_integer: i32) { // some_integer comes into scope
println!("{}", some_integer);
} // Here, some_integer goes out of scope. Nothing special happens.

官方教程实例 Listing 4-3: Functions with ownership and scope annotated
这里说函数结束后 some_integer 不做处理

是 scalar 类型变量的复制体不释放?
604 次点击
所在节点    问与答
2 条回复
jmc891205
2020-04-03 13:42:33 +08:00
some_integer 是 i32 类型 进入函数的时候是 copy 进来而不是 move 进来的
他的内存完全都是分配在栈上 所以函数退出的时候不需要显式地调用 drop 因为销毁函数栈的时候自然就把他使用的内存销毁了
azev
2020-04-03 15:04:53 +08:00
@jmc891205 多谢

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/658982

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX