I think it is a common task to manipulate file names and paths to append numbers or extra extensions, etc. I have encountered this problem myself a few times, and also found this post on StackOverflow about the issue.
From code I've seen, the commonly used solution is to use format!
with .to_str().unwrap()
on the path's parts. This is understandable, because format!
won't work directly on &OsStr
. Such pattern probably makes a lot of rust code break when used with invalid UTF-8 paths.
I myself try to avoid calling .to_str()
on OsStr
, and I usually .clone()/.to_owned()
, then append the modifications in-place with .push()
, which is much more cumbersome and uglier.
I think there should be in std
a format-like macro that can format both Display
and AsRef<OsStr>
, but outputs OsString
instead of String
, thus providing a little friendlier experience with OsStr
and Path
.
What do you think? Is it feasible? A good idea?