204. take_while / skip_while — Act on the Leading Run, Not Every Match
Reaching for .filter() to drop leading blank lines? It’ll drop the blank lines in the middle too. take_while and skip_while work on the leading run and stop the moment the predicate flips.
The Problem
You want everything after the leading comment/blank lines in a config — but filter has no concept of “leading”:
| |
filter tests every element independently, so it strips matches wherever they appear. That’s rarely what “skip the header” means.
The Fix: skip_while
skip_while discards elements while the predicate holds, then yields the rest untouched — including later elements that would have matched:
| |
The blank line between the keys survives because skip_while already stopped skipping at "key = 1".
Its Mirror: take_while
take_while yields the leading run and stops at the first non-match — perfect for parsing a prefix:
| |
take_while halts at 'p', so even a trailing "9" in the unit wouldn’t sneak back into digits. Unlike filter, both adapters care about position: they describe the boundary between a leading run and everything after it.