str::trim_ascii_start¶
Level: reference · for working programmers
One line: trim_ascii at the front only — const, ASCII-only, leading bytes removed.
Stable since 1.80.0. Usable in a const context.
The ASCII, const-capable counterpart of trim_start. Same five bytes, same compile-time availability.
Reach for it in const contexts and in byte-oriented parsing where the input alphabet is known to be ASCII — a protocol header, a log format. Anywhere a person typed the text, prefer trim_start.
Example¶
str_trim_ascii_start.rs in full — pasted here by tools/run_examples.py from the file CI compiles and runs.
fn main() {
let s = " \t value ";
println!("{:?}", s.trim_ascii_start());
println!("{:?}", s.trim_ascii_end());
println!("{:?}", s.trim_ascii());
// const, which the Unicode version cannot be.
const HEADER: &str = " Content-Type";
const NAME: &str = HEADER.trim_ascii_start();
println!("{NAME:?}");
// Parsing a header value: split, then trim the ASCII padding.
let line = "Accept: text/plain ";
if let Some((k, v)) = line.split_once(':') {
println!("{:?} = {:?}", k, v.trim_ascii());
}
}
Verified output of str_trim_ascii_start.rs — regenerated by tools/run_examples.py, never hand-typed.
See also¶
str::trim_ascii— both endsstr::trim_ascii_end— the other endstr::trim_start— the Unicode-aware versionstr::split_ascii_whitespace— the same ASCII restriction, for splitting
str::trim_ascii_start in the standard library ↗
Po polsku¶
trim_ascii_start ścina z początku wycinka łańcucha (string slice) wyłącznie pięć bajtów białych znaków ASCII — spację, \t, \n, \r i \x0C — a w zamian daje coś, czego unikodowy trim_start nie potrafi: działa w kontekście const, więc nagłówek można przyciąć już na etapie kompilacji. Dla polskiego tekstu wynika z tego jedna bardzo konkretna pułapka: polska typografia każe stawiać twardą spację (U+00A0, non-breaking space) po spójnikach jednoliterowych, a tekst wklejony z edytora czy ze strony bywa nią poprzedzony — trim_ascii_start takiej spacji nie widzi i zostawi ją na początku. Ograniczenie do ASCII dotyczy przy tym samych białych znaków, nie treści: ą, ć, ę i reszta ogonków przechodzą przez tę metodę bez szwanku. Stąd prosty podział: format, którego alfabet znasz z góry (nagłówek protokołu, linia logu) — trim_ascii_start; tekst, który pisał człowiek — trim_start.
Szukaj po polsku: twarda spacja w tekście · białe znaki ASCII · funkcje const w Ruscie · rust trim_ascii_start const fn · rust ascii whitespace vs unicode whitespace