What is the most consice way to check that a path has `.rs` extension?

Is it possible to simplify this check?

path.extension().and_then(|it| it.to_str()) = Some("rs")

You can just compare to an OsStr:

path.extension() == Some(OsStr::new("rs"))

Nightly would let you do

path.extension().contains(&"rs")
3 Likes

Two more options:

path.extension().map(|it| it == "rs").unwrap_or(false)

(I think that one is the most clear on intent)

path.extension().unwrap_or(OsStr::new("")) == "rs"

.extension().contains() is correct but reads wrong.

2 Likes

Perfect!

path.extension().unwrap_or_default() == "rs"
3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.