SPOILERS BELOW for Rustlings!
Rustlings is a series of small exercises "to get you used to reading and writing Rust code."
In the Conversions exercises I just got try_from_into.rs to compile and pass its tests.
I have shared my solution below. And I am looking for comments and discussion on the exercise as a whole.
Am i getting the lesson being taught here? Below, as you'll see, I've commented out an implementation that compiles but fails the tests. I still don't understand why attempting to convert a number < 0 && > 255
to u8 does not result in the kind of error required to pass the test. Is there a way of succeeding here without using the if r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255
line?
This Rustling has been updated over its lifetime. Many of the existing solutions online contain a different String
error type. As someone relatively new to coding as a whole I was confused as to what was being required. It was frustrating at the end to realize all I needed to do to make my code compile was to write my own "error"
and then .into()
to return the right Error result. Again, I feel like I'm missing the broader significance of how errors are handled here.
Really looking for discussions and comments that can show me really what this exercise is trying to bring home. I feel like I'm missing the bigger picture!
SPOILERS BELOW
Here is everything I added to the challenge to make it successfully compile and pass its tests:
// Tuple implementation
impl TryFrom<(i16, i16, i16)> for Color {
type Error = Box<dyn error::Error>;
fn try_from(tuple: (i16, i16, i16)) -> Result<Self, Self::Error> {
match tuple {
(r, g, b) if r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255 => {
Err("rgb must be 0~255".into())
},
(red, green, blue) => {
Ok(Color {
red: red as u8,
green: green as u8,
blue: blue as u8,
})
}
}
}
}
/* BUT WHY DOESN'T THIS WORK?!?
if let (r, g, b) = (u8::try_from(tuple.0).unwrap(), u8::try_from(tuple.1).unwrap(), u8::try_from(tuple.2).unwrap()) {
Ok (Color {
red: r, //u8::try_from(tuple.0).unwrap(),
green: g, //u8::try_from(tuple.1).unwrap(),
blue: b, //u8::try_from(tuple.2).unwrap(),
})} else { Err("no good!".into()) }
}
}
*/
// Array implementation
impl TryFrom<[i16; 3]> for Color {
type Error = Box<dyn error::Error>;
fn try_from(arr: [i16; 3]) -> Result<Self, Self::Error> {
arr[..].try_into()
}
}
// Slice implementation
impl TryFrom<&[i16]> for Color {
type Error = Box<dyn error::Error>;
fn try_from(slice: &[i16]) -> Result<Self, Self::Error> {
if slice.len() != 3 {
return Err("There aren't enough values for RGB here!".into());
}
(slice[0], slice[1], slice[2]).try_into()
}
}