返回从闭包内部借来的值

时间:2017-09-01 20:18:46

标签: rust borrow-checker

我希望从Vec<&Metadata>获得files

use std::fs::File;

fn example(files: Vec<&File>) {
    let files_metadata: Vec<_> = files
        .iter()
        .map(|f| &f.metadata().unwrap())
        .collect();
}

map中创建并删除临时值,它需要与files_metadata一样长。

error[E0597]: borrowed value does not live long enough
 --> src/main.rs:6:39
  |
6 |         .map(|f| &f.metadata().unwrap())
  |                   --------------------^
  |                   |                   |
  |                   |                   temporary value dropped here while still borrowed
  |                   temporary value created here
7 |         .collect();
8 | }
  | - temporary value needs to live until here

0 个答案:

没有答案