Equivilent of find from right from python?


#1

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?


#2

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.


#3

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


#4

Even slicker - nice.