How to avoid writing this boilerplate code all the time?


#1

In some project I have such a code:

path.file_stem().unwrap_or(&OsStr::new("")).to_str().unwrap_or("").to_string() + ".srt"

It looks very bad and ugly. Is there a better way to write this?


#2

I would probably extract this to a method which returns Result and add some ? operator magic.


#3

If you’re trying to change the file extension, use https://doc.rust-lang.org/std/path/struct.Path.html#method.with_extension first and then convert to whatever end type you want.