Context
Having trouble getting this function working. I believe I understand what the errors are trying to tell me, but I don't understand how I'm committing the errors / how to resolve them.
The function is a recursive function that looks through a directory and its subdirectories and accumulates a vector of the path and an Axum MethodRouter (which I don't believe has any bearing on the actual issue I'm facing).
Error
The compiler error states "cannot return value referencing local variable path."
If I replace path.to_str() with path.clone().to_str(), the error becomes, "cannot return value referencing temporary value."
Code
fn assign_handlers(dir: Option<&str>) -> Vec<(&str, MethodRouter)> {
//Check root dir validity
let dir = match dir {
Some(s) => s,
None => return vec![],
};
//Look for routes in dir
let dir_path = fs::read_dir(dir).expect(&format!("Unable to access directory '{}'", &dir));
let mut routes = Vec::new();
for entry in dir_path {
match entry {
Ok(entry) => {
let path = entry.path();
if path.is_dir() {
//Recurse into subdirectories
let subroutes = assign_handlers(path.to_str());
for route in subroutes {
routes.push(route)
}
} else if path.is_file() {
// Cut for brevity (and because I don't know what goes here yet)
}
},
Err(_) => {},
}
}
routes
}
Vec<String, MethodRouter>to perform a copy so the borrow checker is happy.entry.file_type().is_dir()thanentry.path().is_dir()as this saves a syscall (usually).