How to return a reference from a function

Return a reference in Rust by using &str slices for parameters and return types to share data without moving ownership.

Return a reference by declaring the function parameter and return type as &str slices instead of String to avoid moving ownership.

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[..]
}