trim_ascii Versus Unicode Trim
Strip only ASCII whitespace with trim_ascii while trim honours Unicode spaces, and split fields with split_ascii_whitespace.
Code
fn main() {
// trim_ascii 只认 ASCII 空白:空格、\t、\n、\r、\x0c
let padded = "\t status: ok \r\n";
println!("trimmed: {:?}", padded.trim_ascii());
println!("start: {:?}", padded.trim_ascii_start());
println!("end: {:?}", padded.trim_ascii_end());
// 非 ASCII 空白:trim_ascii 保留,trim 按 char::is_whitespace 去掉
let fancy = "\u{00A0}value\u{3000}";
println!("ascii trim: {:?}", fancy.trim_ascii());
println!("unicode trim: {:?}", fancy.trim());
// 字节切片也有同名方法,适合处理还没解码的原始报文
let raw: &[u8] = b" payload \n";
println!("byte trim: {:?}", raw.trim_ascii());
// split_ascii_whitespace 按连续 ASCII 空白切分,不产生空字段
let fields: Vec<&str> = " a\tb\n c ".split_ascii_whitespace().collect();
println!("fields: {:?}", fields);
}What each trim treats as whitespace
trim_ascii Versus Unicode Trim keeps everything in the function main.
What to watch for
- Nothing third-party is pulled in; the snippet is dependency-free.
Putting it to use
Entry signature: fn main()
The run emits 7 labeled sections — trimmed:, start:, end:, ascii trim:, unicode trim:, byte trim:, fields: — one per step.
The contrast in code
let fields: Vec<&str> = " a\tb\n c ".split_ascii_whitespace().collect();
Interactive Tools for This Topic
ASCII Table ReferenceLook up any ASCII code point 0-127 by decimal, hex, or character and see the binary, octal, and control-name view.Character Frequency AnalyzerCount how often each character appears in a text, with Unicode code points, optional case folding, and whitespace exclusion.Diff Ignoring WhitespaceCompare two blocks of text line by line and separate true differences from lines that differ only in spacing, indentation, or line endings.SRI Hash GeneratorGenerate Subresource Integrity (SRI) hashes from resource content using SHA-256, SHA-384, or SHA-512, and copy a ready-to-use script tag.
FAQ
- What does trim_ascii Versus Unicode Trim print when run?
- Running the file directly is enough — expect labeled output lines such as `trimmed:`, `start:`, `end:`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.