#267
Jul 18, 2026
267. Path::file_name — rsplit('/') Hands You Empty Strings and '..'
Grabbing the last path segment with rsplit('/') returns "" for logs/ and ".." for logs/.. — two values that were never file names. Path::file_name reasons in components and returns None instead.
The string version looks harmless:
| |
Feed either of those into a join or a delete-by-name and you have a bug — or worse. Path::file_name works on components, not characters:
| |
The rules it applies:
- the last component wins, so a trailing separator changes nothing:
logs/→logs - a path ending in
..has no final name to give you →None - the root itself has no name →
None
Like Path::extension from bite 266, the return type is Option<&OsStr>: the “there is no file name here” case arrives as a None the compiler makes you handle, instead of an empty string or a .. sneaking into your filesystem calls.