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[..]
}
This approach avoids parsing UTF-8 characters manually and lets you work with raw bytes directly for performance-critical string operations.