【发布时间】:2018-03-27 14:12:43
【问题描述】:
我正在尝试用字符串的索引和字符串中的每个字符编写一个向量。
在下面的例子中,0 J, 0 a, 0 n ... 1 J, 1i, ...
fn main() {
let names = vec!["Jane", "Jill", "Jack", "Johne"];
let name3 = names.iter().enumerate().flat_map(|(i, name)| {
let letters = name.chars();
let mut is = Vec::new();
for _k in 0..name.len() {
is.push(i);
}
is.iter().zip(letters).collect::<Vec<_>>()
});
for i in name3 {
println!("{:?}", i);
}
}
这给了我错误
error[E0597]: `is` does not live long enough
--> src/main.rs:9:9
|
9 | is.iter().zip(letters).collect::<Vec<_>>()
| ^^ borrowed value does not live long enough
10 | });
| - `is` dropped here while still borrowed
...
14 | }
| - borrowed value needs to live until here
我不明白这里发生了什么。我已经收集了is 值。
奇怪的是,如果我翻转letters 和is,它会起作用。
letters.zip(is)
【问题讨论】:
标签: dictionary rust flat