对于lifetime标注,Rust的borrow checker只在乎能否用它静态地验证程序的内存安全。即使lifetime标注有语义上的错误,Rust也会愉快地成功编译,而后果就是程序变得不必要的严格。 下面是一个上面例子的反面:Rust的lifetime省略规则碰巧在语义上是正确的,但是我们无意识地用不必要的lifetime标注加上了过于严格的限制。 #[d...
error: lifetime may not live long enough --> src/main.rs:39:39 | 39 | let closure = |x: &i32| -> &i32 { x }; // fails | - - ^ returning this value requires that `'1` must outlive `'2` | | | | | let's call the lifetime of this reference `'2` | let's call t...
// `foo` has lifetime parameters `'a` and `'b` ``` This lifetime syntax indicates that the lifetime of `foo` may not exceed(超出) that of either `'a` or `'b`. 当然,函数有生命周期是有点抽象的,你可以理解为函数作用域的长度。将`foo` inline 到调用的位置,相信你就能理解了。 ---...
(has.lifetime, "short"); // 换回长生命周期(并不行) has.lifetime = &long; assert_eq!(has.lifetime, "long"); // `short`在这里析构 } // 编译错误,`short`在析构后仍处于借用状态 assert_eq!(has.lifetime, "long"); } 报错: error[E0597]: `short` does not live long enough --...
14:5 | 14 | Parser { context: &context }.parse() | ^^^ does not live long enough 15 | } | - temporary value only lives until here | note: borrowed value must be valid for the anonymous lifetime #1 defined on the function body at 13:1... --> src/lib.rs:13:1 | 13 | /...
error: lifetime may not live long enough --> src/main.rs:8:34 | 8 |letclosure = |upd: &Update| async move { | ___-___-_^ | | | | | | |returntypeof closure `impl Future<Output = >` contains a lifetime `'2` | | let' s call the lifetime of ...
error: lifetime may not live long enough--> src/main.rs:8:348 | let closure = |upd: &Update| async move {| | | return type of closure `impl Future` contains a lifetime `'2`| | let's call the lifetime of this reference `'1`9 | | println!("{:?}", upd);10 | | };|...
error[E0310]: the parameter type `T` may not live long enough --> src/lib.rs:10:5 | 9 | fn static_thread_print<T: Display + Send>(t: T) { | -- help: consider adding an explicit lifetime bound...: `T: 'static +` 10 | std::thread::spawn(move || { | ^^^ | note:...
各位老师,上面的代码我希望通过多线程去对vec进行一些操作,但是会得到错误说"v does not live long enough"。这里我不是很明白,v的生命周期明明是到main函数结束的,并且我尝试使用Arc解决这个错误,依然会有这个错误。能否麻烦大佬说明一下问题在什么地方,谢谢。
The only problem is, it doesn’t work. The compiler will squawk with the error:lifetime may not live long enough, because the closure’s input and output have different lifetimes. One way to get around this would be to use a static reference: ...