遍历集合时如何获得静态引用?

时间:2019-03-25 17:42:12

标签: reference rust lifetime

看看这段代码:

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,但这看起来不太好。

0 个答案:

没有答案