How to avoid writing this boilerplate code all the time?

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?

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

If you’re trying to change the file extension, use Path in std::path - Rust first and then convert to whatever end type you want.