Options1 question #2035
-
The compiler throws an error: "expected |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
You can either do fn maybe_icecream(time_of_day: u16) -> Option<u16> {
if time_of_day<22{
return Some(5);
}
if time_of_day<24{
return Some(0);
}
None
} or fn maybe_icecream(time_of_day: u16) -> Option<u16> {
if time_of_day<22{
Some(5)
} else if time_of_day<24{
Some(0)
} else {
None
}
} The compiler even suggests the first solution:
The reason for this is that in rust You can add Or you can combine three branches in one expression (like in my second example). Then each branch of this expression will return Footnotes |
Beta Was this translation helpful? Give feedback.
You can either do
or
The compiler even suggests the first solution:
The reason for this is that in rust
if
statements are expressions. In your example thoseif
branches are unrelated to each other. Therefore rust looks at them in isolation and tr…