struct中可变元素的生命周期

时间:2015-06-16 16:13:40

标签: rust

如何在struct中定义可变元素?如果我有以下示例:

struct User<'a> {
    reference: String,
    email: String,
    addresses: &'a mut Vec<Address>
}

struct Address {
    street: String,
    city: String
}

fn main() {

    let mut users = Vec::new();
    users.push(User {
        reference: "ref".to_string(),
        email: "test@test.com".to_string(),
        addresses: &mut Vec::new()
    });

}

...它会产生错误:

src/main.rs:18:19: 18:29 error: borrowed value does not live long enough
src/main.rs:18      addresses: &mut Vec::new()
                                    ^~~~~~~~~~
src/main.rs:14:29: 21:2 note: reference must be valid for the block suffix following statement 0 at 14:28...
src/main.rs:14  let mut users = Vec::new();
src/main.rs:15  users.push(User {
src/main.rs:16      reference: "ref".to_string(),
src/main.rs:17      email: "test@test.com".to_string(),
src/main.rs:18      addresses: &mut Vec::new()
src/main.rs:19  });
               ...
src/main.rs:15:2: 19:5 note: ...but borrowed value is only valid for the statement at 15:1
src/main.rs:15  users.push(User {
src/main.rs:16      reference: "ref".to_string(),
src/main.rs:17      email: "test@test.com".to_string(),
src/main.rs:18      addresses: &mut Vec::new()
src/main.rs:19  });
src/main.rs:15:2: 19:5 help: consider using a `let` binding to increase its lifetime
src/main.rs:15  users.push(User {
src/main.rs:16      reference: "ref".to_string(),
src/main.rs:17      email: "test@test.com".to_string(),
src/main.rs:18      addresses: &mut Vec::new()
src/main.rs:19  });
error: aborting due to previous error

...如果我接受编译器的建议help: consider using a let binding to increase its lifetime

fn main() {

    let mut users = Vec::new();
    let mut addresses = Vec::new();
    users.push(User {
        reference: "ref".to_string(),
        email: "test@test.com".to_string(),
        addresses: &mut addresses
    });

}

......我仍然遇到类似的错误:

src/main.rs:19:19: 19:28 error: `addresses` does not live long enough
src/main.rs:19      addresses: &mut addresses
                                    ^~~~~~~~~
src/main.rs:14:29: 22:2 note: reference must be valid for the block suffix following statement 0 at 14:28...
src/main.rs:14  let mut users = Vec::new();
src/main.rs:15  let mut addresses = Vec::new();
src/main.rs:16  users.push(User {
src/main.rs:17      reference: "ref".to_string(),
src/main.rs:18      email: "test@test.com".to_string(),
src/main.rs:19      addresses: &mut addresses
               ...
src/main.rs:15:33: 22:2 note: ...but borrowed value is only valid for the block suffix following statement 1 at 15:32
src/main.rs:15  let mut addresses = Vec::new();
src/main.rs:16  users.push(User {
src/main.rs:17      reference: "ref".to_string(),
src/main.rs:18      email: "test@test.com".to_string(),
src/main.rs:19      addresses: &mut addresses
src/main.rs:20  });
               ...
error: aborting due to previous error

这里有什么问题?

更新:所以这种情况实际上更接近我的问题:

struct User<'a> {
    reference: String,
    email: String,
    addresses: &'a mut Vec<Address>
}

struct Address {
    street: String,
    city: String
}

fn main() {

    let mut users = get_users();

}

fn get_users<'a>() -> Vec<User<'a>> {

    let mut addresses = Vec::new();
    let mut users = Vec::new();
    users.push(User {
        reference: "ref".to_string(),
        email: "test@test.com".to_string(),
        addresses: &mut addresses
    });

    users

}

......它导致了这个错误:

src/main.rs:26:25: 26:34 error: `addresses` does not live long enough
src/main.rs:26         addresses: &mut addresses
                                       ^~~~~~~~~
src/main.rs:19:37: 31:2 note: reference must be valid for the lifetime 'a as defined on the block at 19:36...
src/main.rs:19 fn get_users<'a>() -> Vec<User<'a>> {
src/main.rs:20 
src/main.rs:21  let mut addresses = Vec::new();
src/main.rs:22     let mut users = Vec::new();
src/main.rs:23     users.push(User {
src/main.rs:24         reference: "ref".to_string(),
               ...
src/main.rs:21:33: 31:2 note: ...but borrowed value is only valid for the block suffix following statement 0 at 21:32
src/main.rs:21  let mut addresses = Vec::new();
src/main.rs:22     let mut users = Vec::new();
src/main.rs:23     users.push(User {
src/main.rs:24         reference: "ref".to_string(),
src/main.rs:25         email: "test@test.com".to_string(),
src/main.rs:26         addresses: &mut addresses
               ...
error: aborting due to previous error

1 个答案:

答案 0 :(得分:7)

我知道之前已经回答了这个问题,但我找不到它...如果你发现它,请随意将其标记为重复。

问题在于您尝试将引用存储在将超过引用的容器中。以下是您问题的MCVE

fn main() {
    let mut things = vec![];
    let a = 42;
    things.push(&a);
}

按照创建项目的相反顺序删除项目,因此athings之前被删除。但是,things引用了a,这意味着有一个时间点会有一个悬挂引用,Rust不允许这样做。重新排序您的陈述:

fn main() {
    let a = 42;
    let mut things = vec![];
    things.push(&a);
}