rustlings icon indicating copy to clipboard operation
rustlings copied to clipboard

wrong compiler error

Open ghost opened this issue 2 years ago • 2 comments

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.

ghost avatar Dec 18 '22 10:12 ghost

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.

manyinsects avatar Dec 23 '22 15:12 manyinsects

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.

i just thought the expection always should be the return type. maybe it does not always mean it. im not sure about it tho.

ghost avatar Dec 23 '22 16:12 ghost

Closing because of long inactivity. Feel free to reopen if you still need help :)

mo8it avatar Mar 30 '24 16:03 mo8it