2023-05-29 18:39:08 +01:00
|
|
|
// The From trait is used for value-to-value conversions. If From is implemented
|
|
|
|
// correctly for a type, the Into trait should work conversely. You can read
|
|
|
|
// more about it at https://doc.rust-lang.org/std/convert/trait.From.html
|
2022-07-15 11:34:10 +01:00
|
|
|
|
2019-12-16 13:34:30 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
struct Person {
|
|
|
|
name: String,
|
|
|
|
age: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
fn default() -> Person {
|
|
|
|
Person {
|
|
|
|
name: String::from("John"),
|
|
|
|
age: 30,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-19 10:18:54 +00:00
|
|
|
// Your task is to complete this implementation in order for the line `let p1 =
|
2024-02-04 02:51:06 +00:00
|
|
|
// Person::from("Mark,20")` to compile. Please note that you'll need to parse the
|
2023-05-29 18:39:08 +01:00
|
|
|
// age component into a `usize` with something like `"4".parse::<usize>()`. The
|
|
|
|
// outcome of this needs to be handled appropriately.
|
2019-12-16 13:34:30 +00:00
|
|
|
//
|
|
|
|
// Steps:
|
2023-05-29 18:39:08 +01:00
|
|
|
// 1. If the length of the provided string is 0, then return the default of
|
|
|
|
// Person.
|
|
|
|
// 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 the default of Person.
|
|
|
|
// 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 the default of
|
|
|
|
// Person Otherwise, then return an instantiated Person object with the results
|
2020-07-11 03:01:38 +01:00
|
|
|
|
2019-12-16 13:34:30 +00:00
|
|
|
impl From<&str> for Person {
|
2024-03-15 12:48:57 +00:00
|
|
|
fn from(s: &str) -> Person {}
|
2019-12-16 13:34:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
// Use the `from` function
|
|
|
|
let p1 = Person::from("Mark,20");
|
|
|
|
// Since From is implemented for Person, we should be able to use Into
|
|
|
|
let p2: Person = "Gerald,70".into();
|
|
|
|
println!("{:?}", p1);
|
|
|
|
println!("{:?}", p2);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
#[test]
|
|
|
|
fn test_default() {
|
|
|
|
// Test that the default person is 30 year old John
|
|
|
|
let dp = Person::default();
|
|
|
|
assert_eq!(dp.name, "John");
|
|
|
|
assert_eq!(dp.age, 30);
|
|
|
|
}
|
|
|
|
#[test]
|
|
|
|
fn test_bad_convert() {
|
|
|
|
// Test that John is returned when bad string is provided
|
|
|
|
let p = Person::from("");
|
|
|
|
assert_eq!(p.name, "John");
|
|
|
|
assert_eq!(p.age, 30);
|
|
|
|
}
|
|
|
|
#[test]
|
|
|
|
fn test_good_convert() {
|
|
|
|
// Test that "Mark,20" works
|
|
|
|
let p = Person::from("Mark,20");
|
|
|
|
assert_eq!(p.name, "Mark");
|
|
|
|
assert_eq!(p.age, 20);
|
|
|
|
}
|
2020-03-10 22:21:37 +00:00
|
|
|
#[test]
|
|
|
|
fn test_bad_age() {
|
2023-05-29 18:39:08 +01:00
|
|
|
// Test that "Mark,twenty" will return the default person due to an
|
|
|
|
// error in parsing age
|
2020-03-10 22:21:37 +00:00
|
|
|
let p = Person::from("Mark,twenty");
|
|
|
|
assert_eq!(p.name, "John");
|
|
|
|
assert_eq!(p.age, 30);
|
|
|
|
}
|
2020-05-03 01:41:11 +01: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);
|
|
|
|
}
|
2021-01-08 15:08:38 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_trailing_comma() {
|
|
|
|
let p: Person = Person::from("Mike,32,");
|
2024-03-15 12:48:57 +00:00
|
|
|
assert_eq!(p.name, "John");
|
|
|
|
assert_eq!(p.age, 30);
|
2021-01-08 15:08:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_trailing_comma_and_some_string() {
|
2024-03-15 12:48:57 +00:00
|
|
|
let p: Person = Person::from("Mike,32,dog");
|
|
|
|
assert_eq!(p.name, "John");
|
|
|
|
assert_eq!(p.age, 30);
|
2021-01-08 15:08:38 +00:00
|
|
|
}
|
2019-12-16 13:34:30 +00:00
|
|
|
}
|