Core Beginner
04 Strings & slices
Two string types: String owns its text, &str borrows a view of it. Slice out the first word, then build a fresh String from a borrowed one.
Return the &str slice up to the first space in first_word -- the whole string if there's no space.
Hints
s.find(' ')givesOption<usize>-- the index of the first space.- Slice up to it with
&s[..i]; onNone, the whole string is the word.
Reveal solution
fn first_word(s: &str) -> &str {
match s.find(' ') {
Some(i) => &s[..i],
None => s,
}
}