【发布时间】:2017-06-23 18:12:49
【问题描述】:
我看过多个帖子,例如 this 或 this,但我认为这不是重复的。我想我还不太明白如何利用生命来延长彼此的寿命。这是一个 MWE:
struct Point;
pub struct Line<'a> {
pub start: &'a Point,
pub end: &'a Point,
}
impl<'a> Line<'a> {
pub fn new(start: &Point, end: &Point) -> Self {
Line {
start: start,
end: end,
}
}
}
fn main() {}
我收到错误消息
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
--> src/main.rs:10:9
|
10 | Line {
| ^^^^
|
note: first, the lifetime cannot outlive the anonymous lifetime #2 defined on the body at 9:51...
--> src/main.rs:9:52
|
9 | pub fn new(start: &Point, end: &Point) -> Self {
| ____________________________________________________^
10 | | Line {
11 | | start: start,
12 | | end: end,
13 | | }
14 | | }
| |_____^
note: ...so that reference does not outlive borrowed content
--> src/main.rs:12:18
|
12 | end: end,
| ^^^
note: but, the lifetime must be valid for the lifetime 'a as defined on the body at 9:51...
--> src/main.rs:9:52
|
9 | pub fn new(start: &Point, end: &Point) -> Self {
| ____________________________________________________^
10 | | Line {
11 | | start: start,
12 | | end: end,
13 | | }
14 | | }
| |_____^
note: ...so that expression is assignable (expected Line<'a>, found Line<'_>)
--> src/main.rs:10:9
|
10 | / Line {
11 | | start: start,
12 | | end: end,
13 | | }
| |_________^
我完全不知道如何解释它。
【问题讨论】:
标签: rust