rustlings/exercises/23_conversions/from_into.rs

131 lines
3.3 KiB
Rust
Raw Normal View History

2024-07-01 16:09:48 -04:00
// The `From` trait is used for value-to-value conversions. If `From` is
// implemented, an implementation of `Into` is automatically provided.
// You can read more about it in the documentation:
// https://doc.rust-lang.org/std/convert/trait.From.html
2022-07-15 06:34:10 -04:00
#[derive(Debug)]
struct Person {
name: String,
2024-07-01 16:09:48 -04:00
age: u8,
}
2024-07-01 16:09:48 -04:00
// We implement the Default trait to use it as a fallback when the provided
// string is not convertible into a `Person` object.
impl Default for Person {
2024-07-01 16:09:48 -04:00
fn default() -> Self {
Self {
name: String::from("John"),
age: 30,
}
}
}
2024-07-01 16:09:48 -04:00
// TODO: Complete this `From` implementation to be able to parse a `Person`
// out of a string in the form of "Mark,20".
// Note that you'll need to parse the age component into a `u8` with something
// like `"4".parse::<u8>()`.
//
// Steps:
2024-07-01 16:09:48 -04:00
// 1. Split the given string on the commas present in it.
// 2. If the split operation returns less or more than 2 elements, return the
// default of `Person`.
// 3. Use the first element from the split operation as the name.
// 4. If the name is empty, return the default of `Person`.
// 5. Parse the second element from the split operation into a `u8` as the age.
// 6. If parsing the age fails, return the default of `Person`.
impl From<&str> for Person {
2024-07-01 16:09:48 -04:00
fn from(s: &str) -> Self {}
}
fn main() {
2024-07-01 16:09:48 -04:00
// Use the `from` function.
let p1 = Person::from("Mark,20");
2024-07-01 16:09:48 -04:00
println!("{p1:?}");
// Since `From` is implemented for Person, we are able to use `Into`.
let p2: Person = "Gerald,70".into();
2024-07-01 16:09:48 -04:00
println!("{p2:?}");
}
#[cfg(test)]
mod tests {
use super::*;
2024-07-01 16:09:48 -04:00
#[test]
fn test_default() {
let dp = Person::default();
assert_eq!(dp.name, "John");
assert_eq!(dp.age, 30);
}
2024-07-01 16:09:48 -04:00
#[test]
fn test_bad_convert() {
let p = Person::from("");
assert_eq!(p.name, "John");
assert_eq!(p.age, 30);
}
2024-07-01 16:09:48 -04:00
#[test]
fn test_good_convert() {
let p = Person::from("Mark,20");
assert_eq!(p.name, "Mark");
assert_eq!(p.age, 20);
}
2024-07-01 16:09:48 -04:00
#[test]
fn test_bad_age() {
let p = Person::from("Mark,twenty");
assert_eq!(p.name, "John");
assert_eq!(p.age, 30);
}
2020-05-02 20:41:11 -04:00
#[test]
fn test_missing_comma_and_age() {
let p: Person = Person::from("Mark");
assert_eq!(p.name, "John");
assert_eq!(p.age, 30);
}
#[test]
fn test_missing_age() {
let p: Person = Person::from("Mark,");
assert_eq!(p.name, "John");
assert_eq!(p.age, 30);
}
#[test]
fn test_missing_name() {
let p: Person = Person::from(",1");
assert_eq!(p.name, "John");
assert_eq!(p.age, 30);
}
#[test]
fn test_missing_name_and_age() {
let p: Person = Person::from(",");
assert_eq!(p.name, "John");
assert_eq!(p.age, 30);
}
#[test]
fn test_missing_name_and_invalid_age() {
let p: Person = Person::from(",one");
assert_eq!(p.name, "John");
assert_eq!(p.age, 30);
}
#[test]
fn test_trailing_comma() {
let p: Person = Person::from("Mike,32,");
2024-03-15 08:48:57 -04:00
assert_eq!(p.name, "John");
assert_eq!(p.age, 30);
}
#[test]
fn test_trailing_comma_and_some_string() {
2024-03-15 08:48:57 -04:00
let p: Person = Person::from("Mike,32,dog");
assert_eq!(p.name, "John");
assert_eq!(p.age, 30);
}
}