In idiomatic Rust the return type of a function that can fail should be an Option
or a Result
. In general, functions should also accept slices instead of String
s and only create a new String
where necessary. This reduces excessive copying and heap allocations.
You can use the provided extension()
method and then convert the resulting OsStr
to a &str
:
use std::path::Path;
use std::ffi::OsStr;
fn get_extension_from_filename(filename: &str) -> Option<&str> {
Path::new(filename)
.extension()
.and_then(OsStr::to_str)
}
assert_eq!(get_extension_from_filename("abc.gz"), Some("gz"));
Using and_then
is convenient here because it means you don’t have to unwrap the Option<&OsStr>
returned by extension()
and deal with the possibility of it being None
before calling to_str
. I also could have used a lambda |s| s.to_str()
instead of OsStr::to_str
– it might be a matter of preference or opinion as to which is more idiomatic.
Notice that both the argument &str
and the return value are references to the original string slice created for the assertion. The returned slice cannot outlive the original slice that it is referencing, so you may need to create an owned String
from this result if you need it to last longer.