How to use bytes crate in Rust byte buffer

Use the .as_bytes() method to convert a string to a byte slice for efficient character searching.

Use the .as_bytes() method on a &String or &str to get a byte slice, then iterate over it to find specific byte values like spaces.

fn first_word(s: &str) -> &str {
    let bytes = s.as_bytes();

    for (i, &item) in bytes.iter().enumerate() {
        if item == b' ' {
            return &s[0..i];
        }
    }

    &s[..]
}