mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-03 01:09:18 +00:00
fix(try_from_into): Rewrite exercise
Instead of working with strings, the exercise suggests creating a Color structure from other simple types (tuple, array, slice). closes #392
This commit is contained in:
parent
9f75554f2a
commit
9f4be26dc4
@ -5,98 +5,100 @@
|
|||||||
use std::convert::{TryInto, TryFrom};
|
use std::convert::{TryInto, TryFrom};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Person {
|
struct Color {
|
||||||
name: String,
|
red: u8,
|
||||||
age: usize,
|
green: u8,
|
||||||
|
blue: u8,
|
||||||
}
|
}
|
||||||
|
|
||||||
// I AM NOT DONE
|
// I AM NOT DONE
|
||||||
// Your task is to complete this implementation
|
|
||||||
// in order for the line `let p = Person::try_from("Mark,20")` to compile
|
impl TryFrom<(i16, i16, i16)> for Color {
|
||||||
// and return an Ok result of inner type Person.
|
|
||||||
// Please note that you'll need to parse the age component into a `usize`
|
|
||||||
// with something like `"4".parse::<usize>()`. The outcome of this needs to
|
|
||||||
// be handled appropriately.
|
|
||||||
//
|
|
||||||
// Steps:
|
|
||||||
// 1. If the length of the provided string is 0, then return an error
|
|
||||||
// 2. Split the given string on the commas present in it
|
|
||||||
// 3. Extract the first element from the split operation and use it as the name
|
|
||||||
// 4. If the name is empty, then return an error.
|
|
||||||
// 5. Extract the other element from the split operation and parse it into a `usize` as the age
|
|
||||||
// If while parsing the age, something goes wrong, then return an error
|
|
||||||
// Otherwise, then return a Result of a Person object
|
|
||||||
impl TryFrom<&str> for Person {
|
|
||||||
type Error = String;
|
type Error = String;
|
||||||
fn try_from(s: &str) -> Result<Self, Self::Error> {
|
fn try_from(tuple: (i16, i16, i16)) -> Result<Self, Self::Error> {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TryFrom<[i16; 3]> for Color {
|
||||||
|
type Error = String;
|
||||||
|
fn try_from(arr: [i16; 3]) -> Result<Self, Self::Error> {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TryFrom<&[i16]> for Color {
|
||||||
|
type Error = String;
|
||||||
|
fn try_from(slice: &[i16]) -> Result<Self, Self::Error> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// Use the `from` function
|
// Use the `from` function
|
||||||
let p1 = Person::try_from("Mark,20");
|
let c1 = Color::try_from((183, 65, 14));
|
||||||
// Since From is implemented for Person, we should be able to use Into
|
println!("{:?}", c1);
|
||||||
let p2: Result<Person, _> = "Gerald,70".try_into();
|
|
||||||
println!("{:?}", p1);
|
// Since From is implemented for Color, we should be able to use Into
|
||||||
println!("{:?}", p2);
|
let c2: Result<Color, _> = [183, 65, 14].try_into();
|
||||||
|
println!("{:?}", c2);
|
||||||
|
|
||||||
|
let v = vec![183, 65, 14];
|
||||||
|
// With slice we should use `from` function
|
||||||
|
let c3 = Color::try_from(&v[..]);
|
||||||
|
println!("{:?}", c3);
|
||||||
|
// or take slice within round brackets
|
||||||
|
let c4: Result<Color, _> = (&v[..]).try_into();
|
||||||
|
println!("{:?}", c4);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
#[test]
|
|
||||||
fn test_bad_convert() {
|
|
||||||
// Test that error is returned when bad string is provided
|
|
||||||
let p = Person::try_from("");
|
|
||||||
assert!(p.is_err());
|
|
||||||
}
|
|
||||||
#[test]
|
|
||||||
fn test_good_convert() {
|
|
||||||
// Test that "Mark,20" works
|
|
||||||
let p = Person::try_from("Mark,20");
|
|
||||||
assert!(p.is_ok());
|
|
||||||
let p = p.unwrap();
|
|
||||||
assert_eq!(p.name, "Mark");
|
|
||||||
assert_eq!(p.age, 20);
|
|
||||||
}
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic]
|
#[should_panic]
|
||||||
fn test_panic_empty_input() {
|
fn test_bad_tuple() {
|
||||||
let p: Person = "".try_into().unwrap();
|
let _: Color = Color::try_from((-1, 0, 0)).unwrap();
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic]
|
fn test_good_tuple() {
|
||||||
fn test_panic_bad_age() {
|
let c: Color = (183, 65, 14).try_into().unwrap();
|
||||||
let p = Person::try_from("Mark,twenty").unwrap();
|
assert_eq!(c.red, 183);
|
||||||
|
assert_eq!(c.green, 65);
|
||||||
|
assert_eq!(c.blue, 14);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic]
|
#[should_panic]
|
||||||
fn test_missing_comma_and_age() {
|
fn test_bad_array() {
|
||||||
let _: Person = "Mark".try_into().unwrap();
|
let _: Color = [0, -1, 0].try_into().unwrap();
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn test_good_array() {
|
||||||
|
let c: Color = [183, 65, 14].try_into().unwrap();
|
||||||
|
|
||||||
|
assert_eq!(c.red, 183);
|
||||||
|
assert_eq!(c.green, 65);
|
||||||
|
assert_eq!(c.blue, 14);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic]
|
#[should_panic]
|
||||||
fn test_missing_age() {
|
fn test_bad_slice() {
|
||||||
let _: Person = "Mark,".try_into().unwrap();
|
let arr = [0, 0, -1];
|
||||||
|
let _ = Color::try_from(&arr[..]).unwrap();
|
||||||
}
|
}
|
||||||
|
#[test]
|
||||||
|
fn test_good_slice() {
|
||||||
|
let v = vec![183, 65, 14];
|
||||||
|
let c = Color::try_from(&v[..]).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(c.red, 183);
|
||||||
|
assert_eq!(c.green, 65);
|
||||||
|
assert_eq!(c.blue, 14);
|
||||||
|
}
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic]
|
#[should_panic]
|
||||||
fn test_missing_name() {
|
fn test_bad_slice_length() {
|
||||||
let _ : Person = ",1".try_into().unwrap();
|
let v = vec![0, 0, 0, 0];
|
||||||
|
let _ = Color::try_from(&v[..]).unwrap();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
#[test]
|
|
||||||
#[should_panic]
|
|
||||||
fn test_missing_name_and_age() {
|
|
||||||
let _: Person = ",".try_into().unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
#[should_panic]
|
|
||||||
fn test_missing_name_and_invalid_age() {
|
|
||||||
let _: Person = ",one".try_into().unwrap();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user