Equivilent of find from right from python?

Crazy I know but I have a path in a String and I want to knock the filename off the end of it.

In python path[:path.rfind("/")] would do the job in a jiffy. What would be a vaguely eligant way to do the same in Rust?

Ah ha, the Rust equivilent of ‘rfind’ is ‘rfind’. Ironic I didn’t find that earlier!

This seems pleasingly succinct:

let (head, _) = path.split_at(path.rfind("/").unwrap());

Well done Rust.

https://doc.rust-lang.org/std/path/struct.Path.html#method.parent might be more appropriate in this case I think :slight_smile:

1 Like

Even slicker - nice.