Hello, I'm currently doing a personal project inspired by Kondo, to clean projects dependencies & artifacts.
The goal is to improve my knowledge of the Rust language.
I was wondering about a portion of the code in Kondo's codebase : https://github.com/tbillington/kondo/blob/master/kondo-lib/src/lib.rs#L326
After building an OsString iterator, they try to turn it into a Option<&str>, and if it gives None they just skip this iteration. So I was wondering, could it be possible to turn the iteration variable directly to a &str, directly into the functions chain used in the for loop ? That way we wouldn't have to create file_name from the OsString.
My first attempt to do that failed though, as the compiler won't allow me to use filter_map and to_str() directly in a closure. The message, more precisely, is cannot return reference to temporary value returns a reference to data owned by the current function.
Attempt :
for file_name in rd
.filter_map(|rd| rd.ok())
.filter(|de| de.file_type().map(|ft| ft.is_file()).unwrap_or(false))
.filter_map(|de| de.file_name().to_str())
{
//...
}
Then, I started to try weird things with to_owned or String::from, but didn't succeed. I think I misunderstand something about the Rust compiler.
I hope this is clear, and can give more info if needed. Thank you !