There's an object that only accepts &[&str]
as input, but I need to generate this list dynamically. I tried many ways but I always get errors.
My code:
use std::fs;
fn example() {
let paths = fs::read_dir("./").unwrap();
let mut input_files = Vec::<&str>::new();
for path in paths {
let x = ["../", path.unwrap().path().to_str().unwrap()]
.concat()
.as_str();
input_files.push(x);
}
}
Error:
error[E0716]: temporary value dropped while borrowed
--> src/lib.rs:8:17
|
8 | let x = ["../", path.unwrap().path().to_str().unwrap()]
| _________________^
9 | | .concat()
| |_____________________^ creates a temporary which is freed while still in use
10 | .as_str();
| - temporary value is freed at the end of this statement
11 | input_files.push(x);
| - borrow later used here
|
= note: consider using a `let` binding to create a longer lived value
I think the error is because concat
creates a temporary, but why? Why can it not return a string and how should I fix this?
I can't use String
.