expression

BytesMut split and freeze

Separate a known prefix into an immutable byte value. Check the length first because splitting past the buffer panics. Remaining bytes stay available for the next frame. Tokio is an external dependency; see the proposed Cargo manifest. Expected stdout (not executed): abc def Common mistake: Splitting based on an unchecked size from input.

Formal Syntax & Structure

rust syntaxrust
buffer.split_to(n).freeze()

Working Example

Example Snippetrust
fn main() {    use bytes::BytesMut;    let mut b = BytesMut::from(&b"abcdef"[..]);    let head = b.split_to(3).freeze();    println!("{} {}", std::str::from_utf8(&head).unwrap(), std::str::from_utf8(&b).unwrap());}
Rust Reference