我希望从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