看看这段代码:
fn foo(line: &'static String) {
// Do some stuff e.g. spawning threads
}
fn main() {
let lines: Vec<String> = vec![];
for line in lines.iter() {
foo(line);
}
}
编译器告诉我:
error[E0597]: `lines` does not live long enough
--> src/main.rs:8:17
|
8 | for line in lines.iter() {
| ^^^^^-------
| |
| borrowed value does not live long enough
| argument requires that `lines` is borrowed for `'static`
...
11 | }
| - `lines` dropped here while still borrowed
如何遍历集合并获取静态引用?还是这不可能?
我可以复制String
,但这看起来不太好。