Skip to content

Commit

Permalink
ideomatic way
Browse files Browse the repository at this point in the history
  • Loading branch information
Volodymyr Patuta committed Oct 30, 2020
1 parent a37ac71 commit 158c322
Showing 1 changed file with 29 additions and 34 deletions.
63 changes: 29 additions & 34 deletions exercises/conversions/try_from_into.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// You can read more about it at https://doc.rust-lang.org/std/convert/trait.TryFrom.html
use std::convert::{TryFrom, TryInto};

#[derive(Debug)]
#[derive(Debug, PartialEq)]
struct Color {
red: u8,
green: u8,
Expand Down Expand Up @@ -76,17 +76,15 @@ mod tests {
}
#[test]
fn test_tuple_correct() {
let c: Color = match (183, 65, 14).try_into() {
Ok(color) => color,
Err(_) => Color {
red: 0,
green: 0,
blue: 0,
},
};
assert_eq!(c.red, 183);
assert_eq!(c.green, 65);
assert_eq!(c.blue, 14);
let c: Result<Color, String> = (183, 65, 14).try_into();
assert_eq!(
c,
Ok(Color {
red: 183,
green: 65,
blue: 14
})
);
}
#[test]
fn test_array_out_of_range_positive() {
Expand All @@ -104,18 +102,17 @@ mod tests {
assert!(c.is_err());
}
#[test]
#[test]
fn test_array_correct() {
let c: Color = match [183, 65, 14].try_into() {
Ok(color) => color,
Err(_) => Color {
red: 0,
green: 0,
blue: 0,
},
};
assert_eq!(c.red, 183);
assert_eq!(c.green, 65);
assert_eq!(c.blue, 14);
let c: Result<Color, String> = [183, 65, 14].try_into();
assert_eq!(
c,
Ok(Color {
red: 183,
green: 65,
blue: 14
})
);
}
#[test]
fn test_slice_out_of_range_positive() {
Expand All @@ -135,17 +132,15 @@ mod tests {
#[test]
fn test_slice_correct() {
let v = vec![183, 65, 14];
let c = match Color::try_from(&v[..]) {
Ok(color) => color,
Err(_) => Color {
red: 0,
green: 0,
blue: 0,
},
};
assert_eq!(c.red, 183);
assert_eq!(c.green, 65);
assert_eq!(c.blue, 14);
let c: Result<Color, String> = Color::try_from(&v[..]);
assert_eq!(
c,
Ok(Color {
red: 183,
green: 65,
blue: 14
})
);
}
#[test]
fn test_slice_excess_length() {
Expand Down

0 comments on commit 158c322

Please sign in to comment.