[英]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.