Rust 生命周期 - 书籍示例解决方案

问题描述

在 Rust Book 上有这个生命周期示例:

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);        //  |
}   

将其简化为:

fn main() {
    let x;
    {
        let y = 42;
        x = &y;
    }

    println!("The value of 'x' is {}.",x);
}

是否可以在没有 clone 的情况下工作?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)