xianzhe
V2EX  ›  问与答

读 Rust 实战疑问

  •  
  •   xianzhe · May 24, 2023 · 1807 views
    This topic created in 1102 days ago, the information mentioned may be changed or developed.

    image.png 请问这话啥意思啊?下面的代码我跑起来也没任何问题啊

    fn main() {
        let container = [1, 2, 3, 4, 5];
        for x in container {
            println!("{}", x)
        }
        println!("{}", container.is_empty());
        for x in container {
            println!("{}", x)
        }
    }
    
    2 replies    2023-05-24 21:17:25 +08:00
    misdake
        1
    misdake  
       May 24, 2023
    可以把数组换成 Vec 试试看。他会抱怨 container.is_empty()使用了 move 后的数据。
    你这个例子没问题是因为 container 是[i32;5]类型是 Copy 的,所以所有权没被吃掉。
    xianzhe
        2
    xianzhe  
    OP
       May 24, 2023 via Android
    @misdake 感谢感谢,换成 vector 确实如你所说
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2906 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 42ms · UTC 08:56 · PVG 16:56 · LAX 01:56 · JFK 04:56
    ♥ Do have faith in what you're doing.