Hi guys,
I'm new to rust and began to learn it by doing some examples from:
https://doc.rust-lang.org/book/ch04-03-slices.html
My confusion stems from the following code, specifically line (marked by me):
fn first_word(s: &String) -> &str {
let bytes = s.as_bytes();
for (i, &item) in bytes.iter().enumerate() {
if item == b' ' {
return &s[0..i];//HERE <<< why do I have to use return not just &s[0..i]
}
}
&s[..]//HERE I actually don't use return and it compiles!!!
}
What's more, the code will simply not compile if I don't use return!!!
I always thought that in order to indicate return statement in rust one simply doesn't end line with semicolon. That's obviously wrong as the above example is showing.
Any ideas why and how to understand this behavior?