24: Add solutions for section "conversions".

[?]
Aaw9nJhsNmfzFih9mKyNw9mV8CgERXJkRa1kK1Kx3LQH
Aug 14, 2021, 12:53 PM
ZN4JRLPZVM5MVVBIFPKDXDP2FAHCD5F2I7EYHCBOVL3HAJTSSCHQC

Dependencies

Change contents

  • edit in exercises/conversions/using_as.rs at line 7
    [2.145160][2.145160:145178]()
    // I AM NOT DONE
  • replacement in exercises/conversions/using_as.rs at line 10
    [2.145270][2.145270:145295]()
    total / values.len()
    [2.145270]
    [2.145295]
    total / values.len() as f64
  • replacement in exercises/conversions/try_from_into.rs at line 5
    [2.145938][2.145938:145992]()
    use std::convert::{TryFrom, TryInto};
    use std::error;
    [2.145938]
    [2.145992]
    use std::{convert::{TryFrom, TryInto}, error, fmt};
  • edit in exercises/conversions/try_from_into.rs at line 13
    [2.146080][2.146080:146098]()
    // I AM NOT DONE
  • replacement in exercises/conversions/try_from_into.rs at line 26
    [2.146663][2.146663:146735]()
    fn try_from(tuple: (i16, i16, i16)) -> Result<Self, Self::Error> {}
    [2.146663]
    [2.146735]
    fn try_from(tuple: (i16, i16, i16)) -> Result<Self, Self::Error> {
    Ok(Color {
    red: u8::try_from(tuple.0)?,
    green: u8::try_from(tuple.1)?,
    blue: u8::try_from(tuple.2)?
    })
    }
  • replacement in exercises/conversions/try_from_into.rs at line 38
    [2.146837][2.146837:146900]()
    fn try_from(arr: [i16; 3]) -> Result<Self, Self::Error> {}
    [2.146837]
    [2.146900]
    fn try_from(arr: [i16; 3]) -> Result<Self, Self::Error> {
    Ok(Color {
    red: u8::try_from(arr[0])?,
    green: u8::try_from(arr[1])?,
    blue: u8::try_from(arr[2])?
    })
    }
  • edit in exercises/conversions/try_from_into.rs at line 46
    [2.146902]
    [2.146902]
    #[derive(Debug, Clone)]
    struct IncorrectSliceLength;
  • edit in exercises/conversions/try_from_into.rs at line 50
    [2.146903]
    [2.146903]
    impl fmt::Display for IncorrectSliceLength {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    write!(f, "Incorrect slice length.")
    }
    }
    impl error::Error for IncorrectSliceLength {}
  • replacement in exercises/conversions/try_from_into.rs at line 61
    [2.147000][2.147000:147063]()
    fn try_from(slice: &[i16]) -> Result<Self, Self::Error> {}
    [2.147000]
    [2.147063]
    fn try_from(slice: &[i16]) -> Result<Self, Self::Error> {
    if slice.len() == 3 {
    Ok(Color {
    red: u8::try_from(slice[0])?,
    green: u8::try_from(slice[1])?,
    blue: u8::try_from(slice[2])?
    })
    } else { Err(Box::new(IncorrectSliceLength)) }
    }
  • replacement in exercises/conversions/from_str.rs at line 5
    [2.150438][2.150438:150477]()
    use std::error;
    use std::str::FromStr;
    [2.150438]
    [2.150477]
    use std::{error, str::FromStr, fmt};
  • edit in exercises/conversions/from_str.rs at line 13
    [2.150548][2.150548:150566]()
    // I AM NOT DONE
  • edit in exercises/conversions/from_str.rs at line 23
    [2.151184]
    [2.151184]
    #[derive(Debug, Clone)]
    struct InvalidInput;
    impl fmt::Display for InvalidInput {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    write!(f, "Could not parse input.")
    }
    }
    impl error::Error for InvalidInput {}
  • edit in exercises/conversions/from_str.rs at line 37
    [2.151304]
    [2.151304]
    let v: Vec<&str> = s.split(',').collect();
    if v.len() != 2 || v[0].is_empty() {
    Err(Box::new(InvalidInput))
    } else {
    Ok(Person {
    name: v[0].to_string(),
    age: v[1].parse::<usize>()?
    })
    }
  • edit in exercises/conversions/from_into.rs at line 35
    [2.154035][2.154035:154053]()
    // I AM NOT DONE
  • edit in exercises/conversions/from_into.rs at line 38
    [2.154116]
    [2.154116]
    let mut s = s.split(',');
    if let (Some(name), Some(age), None) = (s.next(), s.next(), s.next()) {
    if let (false, Ok(age)) = (name.is_empty(), age.parse()) {
    return Person {
    name: String::from(name),
    age,
    };
    }
    }
    Person::default()
  • edit in exercises/conversions/as_ref_mut.rs at line 4
    [2.156741][2.156741:156759]()
    // I AM NOT DONE
  • replacement in exercises/conversions/as_ref_mut.rs at line 7
    [2.156883][2.156883:156921]()
    fn byte_counter<T>(arg: T) -> usize {
    [2.156883]
    [2.156921]
    fn byte_counter<T: AsRef<str>>(arg: T) -> usize {
  • replacement in exercises/conversions/as_ref_mut.rs at line 13
    [2.157081][2.157081:157119]()
    fn char_counter<T>(arg: T) -> usize {
    [2.157081]
    [2.157119]
    fn char_counter<T: AsRef<str>>(arg: T) -> usize {