expression

Normalize record endings without removing data

A record format may permit LF and CRLF endings. Strip only the defined suffix, keeping meaningful spaces and preserving a bare final carriage return. This works on bytes before UTF-8 validation. Fixture: Expected stdout (not executed): "hi " Expected stderr: (empty) Expected exit: 0 Common mistake: Using trim() on every record and losing meaningful whitespace.

Formal Syntax & Structure

rust syntaxrust
strip_suffix(b"\n")

Working Example

Example Snippetrust
fn main() {    fn payload(bytes: &[u8]) -> &[u8] {        match bytes.strip_suffix(b"\n") {            Some(rest) => rest.strip_suffix(b"\r").unwrap_or(rest),            None => bytes,        }    }    assert_eq!(payload(b"hi \r\n"), b"hi ");    assert_eq!(payload(b"hi\r"), b"hi\r");    println!("{:?}", std::str::from_utf8(payload(b"hi \n")).unwrap());}
Rust Reference