rustlings
rustlings copied to clipboard
wrong compiler error
exercises > conversions > try_from_into.rs
impl TryFrom<(i16, i16, i16)> for Color { type Error = IntoColorError; fn try_from(tuple: (i16, i16, i16)) -> Result<Self, Self::Error> { let (r, g, b) = tuple; if r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255 { return Err(IntoColorError::IntConversion); } // i didn't write else yet } }
compiler error:
! Compiling of exercises/conversions/try_from_into.rs failed! Please try again. Here's the output:
error[E0317]: if may be missing an else clause
--> exercises/conversions/try_from_into.rs:41:9
|
41 | / if r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255 {
42 | | return Err(IntoColorError::IntConversion);
43 | | }
| |_________^ expected (), found enum Result ( << this part )
|
= note: expected unit type ()
found enum Result<Color, IntoColorError>
= note: if expressions without else evaluate to ()
= help: consider adding an else block that evaluates to the expected type
error: aborting due to previous error
it says that it expected (), not Result.
seems like something is wrong.
when I write else {}, it says it expected Result and found (), and this is correct error.
If the condition doesn't go into the if block, it'll return () (the unit type) at the end of a function by default. The easiest way to fix this would be to add a like containing Ok(self) at the end of the function.
If the condition doesn't go into the
ifblock, it'll return()(the unit type) at the end of a function by default. The easiest way to fix this would be to add a like containingOk(self)at the end of the function.
i just thought the expection always should be the return type. maybe it does not always mean it. im not sure about it tho.
Closing because of long inactivity. Feel free to reopen if you still need help :)