135. str::strip_prefix — Trim a Prefix Without Slicing by Hand
Reaching for if s.starts_with("foo") { &s[3..] } to drop a prefix? That’s an off-by-one waiting to happen — and a panic the first time someone passes in an emoji. str::strip_prefix returns Option<&str> and gets it right by construction.
The Problem
You want the part of a string after a known prefix:
| |
Two things wrong here: the literal 7 has to stay in sync with the literal "Bearer ", and slicing by byte offset will panic if the prefix ever lands mid-codepoint. Even using prefix.len() only saves you from the first bug, not the second when the prefix is dynamic.
The Fix: strip_prefix
| |
strip_prefix returns Some(&str) if the prefix matched (giving you the rest), or None if it didn’t. No magic numbers, no slicing, no UTF-8 footguns — the prefix length comes from the prefix itself.
Pattern Matching, Not Just Strings
The argument is anything implementing Pattern, so a char, a closure, or even an array of chars all work:
| |
Note this only strips one match — the char form doesn’t loop. For “strip every leading space,” reach for trim_start_matches.
The Twin: strip_suffix
Same shape, other end:
| |
Together they replace half the manual &s[..s.len() - 3] arithmetic you’d otherwise write — and the Option return makes “did it actually have the prefix?” a value, not a separate starts_with call.