V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
azev
V2EX  ›  问与答

rust 中 ownership 的小问题

  •  
  •   azev · 2020-04-03 12:51:17 +08:00 · 597 次点击
    这是一个创建于 1497 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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 类型变量的复制体不释放?
    2 条回复    2020-04-03 15:04:53 +08:00
    jmc891205
        1
    jmc891205  
       2020-04-03 13:42:33 +08:00
    some_integer 是 i32 类型 进入函数的时候是 copy 进来而不是 move 进来的
    他的内存完全都是分配在栈上 所以函数退出的时候不需要显式地调用 drop 因为销毁函数栈的时候自然就把他使用的内存销毁了
    azev
        2
    azev  
    OP
       2020-04-03 15:04:53 +08:00
    @jmc891205 多谢
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1032 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 49ms · UTC 22:06 · PVG 06:06 · LAX 15:06 · JFK 18:06
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.