I'm new to rust and trying to collect files into a string vector.
use glob::glob;
use std::env;
fn main() {
let mut file_names: Vec<&str> = Vec::new();
let cwd = env::current_dir().unwrap();
for entry in glob(cwd.to_str().unwrap()).expect("Failed to read directory.") {
match entry {
Ok(file) => {
println!("{:?}", file.file_name());
let file_name = file.file_name().unwrap().to_str();
file_names.push(file_name.unwrap());
}
Err(e) => println!("{:?}", e),
}
}
}
Receiving the following error.
error[E0597]: `file` does not live long enough
--> src/main.rs:13:33
|
13 | let file_name = file.file_name().unwrap().to_str();
| ^^^^ borrowed value does not live long enough
14 | file_names.push(file_name.unwrap());
| ---------- borrow later used here
15 | }
16 | Err(e) => println!("{:?}", e),
| - `file` dropped here while still borrowed
error: aborting due to previous error
I'm not sure how to push the file names into the vector. Thanks!