考虑以下代码:
fn foo<'a, T: 'a>(t: T) -> Box<Fn() -> &'a T + 'a> {
Box::new(move || &t)
}
Run Code Online (Sandbox Code Playgroud)
我期待的是:
'a.t只要有效T.t 移动到关闭,所以关闭生活只要 tt移动到闭包的引用.因此只要闭包存在,引用就是有效的.实际发生了什么:
error[E0495]: cannot infer an appropriate lifetime for borrow expression due to conflicting requirements
--> src/lib.rs:2:22
|
2 | Box::new(move || &t)
| ^^
|
note: first, the lifetime cannot outlive the lifetime as defined on the body at 2:14...
--> src/lib.rs:2:14
|
2 | …Run Code Online (Sandbox Code Playgroud)