Hey, I have a Vec<PathBuf>
, where some PathBuf
s can start with "./"
prefix. I tried to iterate them like this, but realized it would not work:
let infiles: Vec<PathBuf> = ...
let to_edit = infiles
.iter()
.map(|inf| inf.strip_prefix("./").to_string_lossy())
.collect::<Vec<_>>();
To make it work, I had to rewrite it like this:
let infiles: Vec<PathBuf> = ...
let to_edit = infiles
.iter()
.map(|inf| {
if inf.starts_with("./") {
inf.strip_prefix("./").unwrap()
} else {
inf
}
.to_string_lossy()
})
.collect::<Vec<_>>();
So, I have two questions:
- Is there a more elegant way to write that snippet?
- Do you see a good reason for
str::strip_prefix()
to return anOption<&'a str>
instead of returning&'a str
? I see it causing inconvenience, but what is the benefit?