azev
V2EX  ›  问与答

rust 中 ownership 的小问题

  •  
  •   azev · Apr 3, 2020 · 1049 views
    This topic created in 2272 days ago, the information mentioned may be changed or developed.
    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 replies    2020-04-03 15:04:53 +08:00
    jmc891205
        1
    jmc891205  
       Apr 3, 2020
    some_integer 是 i32 类型 进入函数的时候是 copy 进来而不是 move 进来的
    他的内存完全都是分配在栈上 所以函数退出的时候不需要显式地调用 drop 因为销毁函数栈的时候自然就把他使用的内存销毁了
    azev
        2
    azev  
    OP
       Apr 3, 2020
    @jmc891205 多谢
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1233 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 23:34 · PVG 07:34 · LAX 16:34 · JFK 19:34
    ♥ Do have faith in what you're doing.