相关疑难解决方法(0)

为什么Rust需要明确的生命周期?

我正在阅读Rust书的生命周章,我在这个例子中看到了命名/显式生命周期:

struct Foo<'a> {
    x: &'a i32,
}

fn main() {
    let x;                    // -+ x goes into scope
                              //  |
    {                         //  |
        let y = &5;           // ---+ y goes into scope
        let f = Foo { x: y }; // ---+ f goes into scope
        x = &f.x;             //  | | error here
    }                         // ---+ f and y go out of scope
                              //  |
    println!("{}", x);        //  |
}                             // -+ x goes out …
Run Code Online (Sandbox Code Playgroud)

static-analysis reference lifetime rust

183
推荐指数
7
解决办法
2万
查看次数

标签 统计

lifetime ×1

reference ×1

rust ×1

static-analysis ×1