fn expensive_sum(v: Vec<i32>) -> i32 {
fun_name(v)
.filter(|&x| x % 2 == 0)
.map(|&x| x*x)
.sum()
}
fn fun_name(v: Vec<i32>) -> std::slice::Iter<'_, i32> {
v.iter()
}
使用https://depth-first.com/articles/2020/06/22/returning-rust-iterators/作为参考,询问编译器获得的迭代器的返回类型为std::slice::Iter<'_, i32>
,但在编译时:
error[E0106]: missing lifetime specifier
--> src/main.rs:25:46
|
25 | fn fun_name(v: Vec<i32>) -> std::slice::Iter<'_, i32> {
| ^^ expected named lifetime parameter
|
= help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
help: consider using the `'static` lifetime
|
25 | fn fun_name(v: Vec<i32>) -> std::slice::Iter<'static, i32> {
|
那是什么意思?为什么我要考虑使用static
岁的生命周期 ?