Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Rust ends_with and strip_prefix behavior differences in Path and str

development rust programming #rust #Path #str #std #debugging

While writing an ignore library, I encountered subtle bugs caused by Rust’s Path behavior.

  • ends_with in Path is different from ends_with in str: If you try to check whether a Path is a directory by its final character, you’ll find that path.ends_with("/") returns false.

  • Path::strip_prefix also consumes the final slash: If you have a directory marker at the end, such as /Users/emre/mydir/, and pass it to strip_prefix("/Users/emre"), you’ll get mydir instead of mydir/.