这段代码:
struct Foo<'a> {
value: Option<&'a int>,
parent: Option<&'a Foo<'a>>
}
impl<'a> Foo<'a> {
fn bar<'a, 'b, 'c: 'a + 'b>(&'a self, other:&'b int) -> Foo<'c> {
return Foo { value: Some(other), parent: Some(self) };
}
}
fn main() {
let e = 100i;
{
let f = Foo { value: None, parent: None };
let g:Foo;
{
g = f.bar(&e);
}
// <--- g should be valid here
}
// 'a of f is now expired, so g should not be valid here.
let f2 = Foo { value: None, parent: None };
{
let e2 = 100i;
let g:Foo;
{
g = f2.bar(&e2);
}
// <--- g should be valid here
}
// 'b of e2 is now expired, so g should not be valid here.
}
编译失败,错误:
<anon>:8:30: 8:35 error: cannot infer an appropriate lifetime due to conflicting requirements
<anon>:8 return Foo { value: Some(other), parent: Some(self) };
^~~~~
<anon>:7:3: 9:4 note: consider using an explicit lifetime parameter as shown: fn bar<'a, 'b>(&'a self, other: &'b int) -> Foo<'b>
<anon>:7 fn bar<'a, 'b, 'c: 'a + 'b>(&'a self, other:&'b int) -> Foo<'c> {
<anon>:8 return Foo { value: Some(other), parent: Some(self) };
<anon>:9 }
(围栏:http://is.gd/vAvNFi)
这显然是一个人为的例子,但这是我偶尔想做的事情。
所以...
1) 如何组合生命周期? (即返回一个生命周期至少为 'a 或 'b 的 Foo,以较短者为准)
2) 有什么方法可以编写 Assets 生命周期编译失败的测试吗? (例如,尝试编译一个以错误方式使用该函数并因生命周期错误而失败的#[test])
最佳答案
边界 'c: 'a + 'b
意味着 'c
至少与 'a
一样长只要'b
.但是,在这种情况下,Foo
值恰好对 'a
中最短的一个有效和 'b
: 一旦任何一个引用背后的数据超出范围,整个 Foo
必须作废。 (这是说对 'c
有效的数据在 'a
和 'b
的并集中也是有效的。)
更具体地说,比如说 'b = 'static
, 然后 'c: 'a + 'static
意味着 'c
也必须是 'static
,所以返回值为 Foo<'static>
.这显然是不对的,因为它会“升级”有限的 'a
self
成为永恒的引用。
此处正确的行为是采用生命周期的交集:Foo
仅在两个函数参数都有效时才有效。交集操作只是标记具有相同名称的引用:
fn bar<'a>(&'a self, other: &'a int) -> Foo<'a>
关于rust - 你如何结合使用rust 的生命周期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25677402/