简体   繁体   English

struct中可变元素的生命周期

[英]Lifetime of a mutable element in struct

How can I define a mutable element in a struct ? 如何在struct定义可变元素? If I have the following example: 如果我有以下示例:

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

}

...it produces an error: ...它会产生错误:

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

...and if I take compiler's suggestion help: consider using a let binding to increase its lifetime : ...如果我接受编译器的建议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
    });

}

...I still get a similar error: ......我仍然得到类似的错误:

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

What's the issue here? 这是什么问题?

UPDATE: So this situation is actually closer to my problem: 更新:所以这种情况实际上更接近我的问题:

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

}

...and it's causing this error: ......它导致了这个错误:

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

I know this has been answered before, but I can't find it... feel free to mark this as duplicate if you find it. 我知道以前已经回答了这个问题,但我找不到它...如果你发现它,请随意将其标记为重复。

The problem is that you are attempting to store a reference in a container that will outlive the reference . 问题是您试图将引用存储在容器中,该容器将超过引用 Here's a MCVE of your problem: 这是你问题的MCVE

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

Items are dropped in the reverse order they are created, so a is dropped before things . 按照创建它们的相反顺序删除项目,因此在things之前删除a However, things has a reference to a , which means that there would be a point in time where there's a dangling reference, which Rust doesn't allow. 然而, things有一个参考a ,这意味着有一个时间点,有一个悬挂引用,Rust不允许。 Reorder your statements: 重新排序您的陈述:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有可变引用的递归结构中的生命周期 - Lifetime in recursive struct with mutable reference 闭包的常量数组,在Rust中使用寿命参数对结构进行可变引用 - Const array of closures taking a mutable reference to a struct with lifetime parameter in Rust For 循环 - 具有生命周期 &#39;a 的结构不能借用为可变的,因为它也被借用为不可变的 - For loop - Struct with lifetime 'a cannot borrow as mutable because it is also borrowed as immutable 如何获得结构(受生命周期约束)字段的可变引用? - How to get mutable reference of struct(constrained with lifetime) field? 可变寿命与不可变寿命 - Mutable versus immutable lifetime 具有 HashSet 的可变结构中的生命周期 - Lifetime in mutable structure with HashSet 当我在结构中使用可变引用而不是不可变引用时,为什么会出现生命周期错误? - Why do I get a lifetime error when I use a mutable reference in a struct instead of an immutable reference? 从 trait 方法返回对结构字段的可变引用时,如何修复生命周期不匹配? - How do I fix the lifetime mismatch when returning a mutable reference to the struct's field from a trait method? Rust - 返回对结构拥有的 Vec 中元素的可变引用 - Rust - return a mutable reference to an element in Vec owned by a struct 双重可变借款的终身不匹配 - Lifetime mismatch in a double mutable borrow
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM