rfind Locates The Last Occurrence
Find the last occurrence of a byte pattern with rfind, getting the byte index you need for suffix slicing in one call.
Code
fn main() {
let path = "reports/2026/nov/q4-summary.txt";
// rfind 从右往左找:拿到最后一个分隔符的字节位置
let slash = path.rfind('/').expect("path has separators");
println!("last slash at byte {slash}");
println!("name: {}", &path[slash + 1..]);
// find 只命中第一次出现,rfind 命中最后一次
let first = path.find('/').expect("first slash");
println!("first slash at byte {first}");
// 找不到时返回 None:不必先确认存在
let dot = path.rfind("no-such-token");
assert!(dot.is_none());
println!("missing token: None");
}How it works
The code’s whole job belongs to the function main.
What to watch for
- No package manager involved — every call resolves to a built-in API.
Applying it
Entry signature: fn main()
Each run prints the 4 labeled blocks last slash at byte, name:, first slash at byte, missing token: None in source order.
Statements that matter
let slash = path.rfind('/').expect("path has separators");
let first = path.find('/').expect("first slash");
let path = "reports/2026/nov/q4-summary.txt";
Interactive Tools for This Topic
Fuzzy Match TesterTest subsequence fuzzy matching: check whether pattern characters appear in order inside a text, with positions and highlighting.SRI Hash GeneratorGenerate Subresource Integrity (SRI) hashes from resource content using SHA-256, SHA-384, or SHA-512, and copy a ready-to-use script tag.
FAQ
- What does rfind Locates The Last Occurrence print when run?
- No setup needed: run the file and look for labeled lines like `last slash at byte`, `name:`, `first slash at byte` in the output.
Report a problem with this exampleOpens a pre-filled issue on GitHub.