Skip to content

str::split_ascii_whitespace

str methods · Strings

Level: reference · for working programmers

One line: split_whitespace restricted to the five ASCII whitespace bytes — faster, and deliberately blind to Unicode spaces.

pub fn split_ascii_whitespace(&self) -> SplitAsciiWhitespace<'_>

Stable since 1.34.0.

The separators are exactly space, \t, \n, \r and form feed. Everything else — including the non-breaking space U+00A0, which is what a word processor inserts and what a user pastes out of one — is an ordinary character here.

That blindness is the whole trade. It can work byte-at-a-time instead of decoding UTF-8, which is measurably quicker on large ASCII inputs; and it will happily hand you "a\u{00A0}b" as one word.

Choose it for machine-generated ASCII (log lines, protocol text, /proc files). Choose split_whitespace for anything a person typed or pasted.

Example

str_split_ascii_whitespace.rs in full — pasted here by tools/run_examples.py from the file CI compiles and runs.

fn main() {
    let ascii = "  the   quick \t brown \n fox  ";
    println!("{:?}", ascii.split_ascii_whitespace().collect::<Vec<&str>>());
    println!("same as unicode: {}",
             ascii.split_ascii_whitespace().eq(ascii.split_whitespace()));

    // Where they part company: a non-breaking space.
    let nbsp = "a\u{00A0}b";
    println!("{:?}", nbsp.split_whitespace().collect::<Vec<&str>>());
    println!("{:?}", nbsp.split_ascii_whitespace().collect::<Vec<&str>>());

    // The five ASCII separators.
    let all = "a b\tc\nd\re\u{000C}f";
    println!("{:?}", all.split_ascii_whitespace().collect::<Vec<&str>>());

    // A log line: machine-generated ASCII, which is what this is for.
    let line = "2026-08-29  INFO   started";
    println!("{:?}", line.split_ascii_whitespace().collect::<Vec<&str>>());
}

Verified output of str_split_ascii_whitespace.rs — regenerated by tools/run_examples.py, never hand-typed.

["the", "quick", "brown", "fox"]
same as unicode: true
["a", "b"]
["a\u{a0}b"]
["a", "b", "c", "d", "e", "f"]
["2026-08-29", "INFO", "started"]

See also

str::split_ascii_whitespace in the standard library ↗

Po polsku

split_ascii_whitespace zna dokładnie pięć separatorów — spację, \t, \n, \r i wysuw strony — a każdy inny znak traktuje jak zwykłą literę, w tym twardą spację U+00A0: dlatego "a\u{00A0}b" wraca jako jedno słowo, podczas gdy split_whitespace widzi w tym samym miejscu dwa. Dla polskiego tekstu to nie ciekawostka, tylko codzienność, bo nasza typografia każe stawiać spację nierozdzielającą po jednoliterowych przyimkach i spójnikach („w”, „i”, „z”, „a”), żeby nie zostawiać sierotek na końcu wiersza — edytory wstawiają ją same, więc wszystko wklejone z Worda albo z PDF-a jest pełne U+00A0. Ta ślepota jest jednak celowa i opłacona prędkością: metoda idzie bajt po bajcie, bez dekodowania UTF-8. Stąd reguła wyboru — split_ascii_whitespace do tekstu generowanego maszynowo (logi, protokoły, pliki z /proc), split_whitespace do wszystkiego, co napisał albo wkleił człowiek.

Szukaj po polsku: twarda spacja w tekście · spacja nierozdzielająca U+00A0 · rust split_ascii_whitespace vs split_whitespace · rust non-breaking space not whitespace