std-method

str::bytes

Iterate over raw UTF-8 bytes as u8 values. Use this for byte-level protocols, not text segmentation. Expected stdout (not executed): [65, 216, 180] Common mistake: Casting each UTF-8 byte to char to decode text.

Formal Syntax & Structure

rust syntaxrust
fn bytes(&self) -> Bytes<'_>

Working Example

Example Snippetrust
fn main() {    let data: Vec<u8> = "Aش".bytes().collect();    println!("{data:?}");}
Rust Reference