3 ms·
let mut bytes = path.bytes(); if let Some(end) = bytes.rposition(|&x| x != SEP) { if let Some(idx) = bytes.rposition(|&x| x == SEP) { &path[idx..end
by __s 9y ago
let mut bytes = path.bytes();
if let Some(end) = bytes.rposition(|&x| x != SEP) {
if let Some(idx) = bytes.rposition(|&x| x == SEP) { &path[idx..end] } else { &path[..end] }
} else { "" }
There are other ways to do this, like using a single iterator where we scan manually, but I just threw this together to demonstrate that this code needn't use unsafe, & by extension needn't use pointers. All that unsafe stuff is more so that if you need to drop down to C from safe-Rust you can instead use unsafe-Rust