#266
Jul 18, 2026
266. Path::extension — Splitting on '.' Breaks on .gitignore
Grabbing a file extension with split('.') works right up until it meets .gitignore — which suddenly has the “extension” gitignore. Path::extension knows the actual rules.
The tempting one-liner:
| |
A dotfile’s leading dot is part of its name, not an extension separator. Your homemade splitter doesn’t know that. Path::extension and Path::file_stem do:
| |
The rules extension applies:
- everything after the last dot, so
backup.tar.gz→gz(and the stem keepsbackup.tar) - a leading dot with no other dot means dotfile, not extension:
.gitignore→None - no dot at all →
None, instead of handing you the whole filename back
Both methods return Option<&OsStr>, so the “there is no extension” case is a None you must handle — not an empty string or a full filename silently pretending to be one.