join().unwrap(); } } 各位老师,上面的代码我希望通过多线程去对vec进行一些操作,但是会得到错误说"v does not live long enough"。这里我不是很明白,v的生命周期明明是到main函数结束的,并且我尝试使用Arc解决这个错误,依然会有这个错误。能否麻烦大佬说明一下问题在什么地方,谢谢。 rust 有用关注2收藏 回复 ...
错误也是s does not live long enough。注释掉println!那一行,编译会通过,为什么呢?按照lifetime是引用的使用区域就非常容易理解了——因为注释掉以后,引用的使用区域小于本体的有效区域。 error[E0597]: `s` does not live long enough --> test.rs:27:26 | 27 | foo = Foo {name: &s}; | ^^ borro...
rs:5:13 | 4 | let y = 5; | - binding `y` declared here 5 | x = &y; | ^^ borrowed value does not live long enough 6 | } | - `y` dropped here while still borrowed 7 | println!("x value is: {}", x); | - borrow later used here For more information about this ...
大多数情况下,引用是隐式的、可以被推断出来的,但当引用可能以不同的方式互相关联时,则需要手动标注生命周期。 fnmain() { letr; { letx =5; r = &x; }// 此处 r 不再有效 println!("{}", r); } 执行的时候会报出如下错误:borrowed value does not live long enough,意思就是借用的值存活的时...
如果你想保留fn parse(raw: String),那么你可以使用Result::map_err()和nom::Err::map(),在错误...
| ^^ borrowed value does not live long enough 6 | }; | - `b` dropped here while still borrowed 7 | println!("a:{}", a); | - borrow later used here 1. 2. 3. 4. 5. 6. 7. 8. 9. 意思就是说变量b的生命周期不够长。变量b已经被销毁了仍然对它进行了借用。
has.lifetime = &long; assert_eq!(has.lifetime, "long"); // `short`在这里析构 } // 编译错误,`short`在析构后仍处于借用状态 assert_eq!(has.lifetime, "long"); } 报错: error[E0597]: `short` does not live long enough --> src/main.rs:11:24 ...
GenericDoesNotLiveLongEnough:表示一个泛型参数的寿命不够长的错误消息。 VarNeedNotMut:表示不需要可变引用的变量的错误消息。 FnMutError:表示不正确的mut函数类型才错误的错误消息。 LifetimeOutliveError:表示生命周期不符合要求的错误消息。 MoveBorrow<'a>:表示移动借用错误的错误消息。
//error:`y` does not live long enough{letx:&Vec<i32>;{lety=Vec::new();//---+// | y's lifetime// |x=&y;//---|---+// | |}// <---+ | x's lifetimeprintln!("x's length is {}",x.len());// |}// <---+ 如果未添加此约束,则x在println!中可能会访问无效内存,...
引用,有时又叫borrow(借用)。Rust关于引用的报错大多都是表borrowed value does not live long enough。也就是引用还存在着,被引用的对象却被销毁了。 文字还是有点复杂,一图胜千言, 变量T存活在绿色的椭圆区域,引用在黄色的圈圈’a里面使用,所以绿色椭圆为T的有效区域,'a是引用的使用区域。绿色椭圆必须包含黄色...