我如何告诉Rust我的选择';s值实际上比传递给和的闭包更有效吗?

我如何告诉Rust我的选择';s值实际上比传递给和的闭包更有效吗?,rust,Rust,以下是我发现自己遇到的一个常见模式: let maybe_vec = Some(vec!["val"]); // I have an option with something in it maybe_vec.and_then(|vec| vec.get(0)); // then I want to transform the something 这给了我 src/lib.rs:317:34: 317:37 error: `vec` does not live long enough src/

以下是我发现自己遇到的一个常见模式:

let maybe_vec = Some(vec!["val"]); // I have an option with something in it
maybe_vec.and_then(|vec| vec.get(0)); // then I want to transform the something
这给了我

src/lib.rs:317:34: 317:37 error: `vec` does not live long enough
src/lib.rs:317         maybe_vec.and_then(|vec| vec.get(0));
                                                ^~~
src/lib.rs:317:9: 317:45 note: reference must be valid for the method call at 317:8...
src/lib.rs:317         maybe_vec.and_then(|vec| vec.get(0));
                       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/lib.rs:317:34: 317:44 note: ...but borrowed value is only valid for the block at 317:33
src/lib.rs:317         maybe_vec.and_then(|vec| vec.get(0));
                                                ^~~~~~~~~~

对我来说,这个错误似乎过于迂腐-
vec
可能活得不够长,但在这个特殊情况下,
vec
中的东西,它显然会活得足够长。我是否需要在这里提供某种类型的生命周期注释,或者我只是做错了吗?

不幸的是,编译器在这里是正确的
和_然后
使用该选项以及选项中的值。该值将提供给闭包。通过使用将变量分配给单位类型(
()
)的技巧可以看到这一点:

let a = Some(vec![1]);
a.and_then(|z| { let () = z; });
// error: expected `collections::vec::Vec<_>`,
let a = Some(vec![1]);
a.as_ref().and_then(|z| z.get(0));