azev
V2EX  ›  问与答

rust 中 ownership 的小问题

  •  
  •   azev · Apr 3, 2020 · 987 views
    This topic created in 2232 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   ·   5493 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 82ms · UTC 09:01 · PVG 17:01 · LAX 02:01 · JFK 05:01
    ♥ Do have faith in what you're doing.