Skip to content

Commit 5c4b888

Browse files
committed
// from_str.rs
// // This is similar to from_into.rs, but this time we'll implement `FromStr` and // return errors instead of falling back to a default value. Additionally, upon // implementing FromStr, you can use the `parse` method on strings to generate // an object of the implementor type. You can read more about it at // https://doc.rust-lang.org/std/str/trait.FromStr.html // // Execute `rustlings hint from_str` or use the `hint` watch subcommand for a // hint. use std::num::ParseIntError; use std::str::FromStr; #[derive(Debug, PartialEq)] struct Person { name: String, age: usize, } #[derive(Debug, PartialEq)] enum ParsePersonError { Empty, BadLen, NoName, ParseInt(ParseIntError), } impl FromStr for Person { type Err = ParsePersonError; fn from_str(s: &str) -> Result<Person, Self::Err> { // 1. 检查输入字符串是否为空 if s.is_empty() { return Err(ParsePersonError::Empty); } // 2. 按逗号分割字符串并收集结果 let parts: Vec<&str> = s.split(',').collect(); // 3. 检查分割后的部分数量是否为2 if parts.len() != 2 { return Err(ParsePersonError::BadLen); } // 4. 提取姓名并检查是否为空 let name = parts[0].trim(); if name.is_empty() { return Err(ParsePersonError::NoName); } // 5. 解析年龄并处理可能的错误 let age = parts[1].trim().parse().map_err(ParsePersonError::ParseInt)?; // 6. 成功解析,返回Person实例 Ok(Person { name: name.to_string(), age, }) } } fn main() { let p = "Mark,20".parse::<Person>().unwrap(); println!("{:?}", p); } #[cfg(test)] mod tests { use super::*; #[test] fn empty_input() { assert_eq!("".parse::<Person>(), Err(ParsePersonError::Empty)); } #[test] fn good_input() { let p = "John,32".parse::<Person>(); assert!(p.is_ok()); let p = p.unwrap(); assert_eq!(p.name, "John"); assert_eq!(p.age, 32); } #[test] fn missing_age() { assert!(matches!( "John,".parse::<Person>(), Err(ParsePersonError::ParseInt(_)) )); } #[test] fn invalid_age() { assert!(matches!( "John,twenty".parse::<Person>(), Err(ParsePersonError::ParseInt(_)) )); } #[test] fn missing_comma_and_age() { assert_eq!("John".parse::<Person>(), Err(ParsePersonError::BadLen)); } #[test] fn missing_name() { assert_eq!(",1".parse::<Person>(), Err(ParsePersonError::NoName)); } #[test] fn missing_name_and_age() { assert!(matches!( ",".parse::<Person>(), Err(ParsePersonError::NoName | ParsePersonError::ParseInt(_)) )); } #[test] fn missing_name_and_invalid_age() { assert!(matches!( ",one".parse::<Person>(), Err(ParsePersonError::NoName | ParsePersonError::ParseInt(_)) )); } #[test] fn trailing_comma() { assert_eq!("John,32,".parse::<Person>(), Err(ParsePersonError::BadLen)); } #[test] fn trailing_comma_and_some_string() { assert_eq!( "John,32,man".parse::<Person>(), Err(ParsePersonError::BadLen) ); } }
1 parent a86f117 commit 5c4b888

File tree

1 file changed

+27
-23
lines changed

1 file changed

+27
-23
lines changed

exercises/conversions/from_str.rs

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,40 +18,44 @@ struct Person {
1818
age: usize,
1919
}
2020

21-
// We will use this error type for the `FromStr` implementation.
2221
#[derive(Debug, PartialEq)]
2322
enum ParsePersonError {
24-
// Empty input string
2523
Empty,
26-
// Incorrect number of fields
2724
BadLen,
28-
// Empty name field
2925
NoName,
30-
// Wrapped error from parse::<usize>()
3126
ParseInt(ParseIntError),
3227
}
3328

34-
// I AM NOT DONE
35-
36-
// Steps:
37-
// 1. If the length of the provided string is 0, an error should be returned
38-
// 2. Split the given string on the commas present in it
39-
// 3. Only 2 elements should be returned from the split, otherwise return an
40-
// error
41-
// 4. Extract the first element from the split operation and use it as the name
42-
// 5. Extract the other element from the split operation and parse it into a
43-
// `usize` as the age with something like `"4".parse::<usize>()`
44-
// 6. If while extracting the name and the age something goes wrong, an error
45-
// should be returned
46-
// If everything goes well, then return a Result of a Person object
47-
//
48-
// As an aside: `Box<dyn Error>` implements `From<&'_ str>`. This means that if
49-
// you want to return a string error message, you can do so via just using
50-
// return `Err("my error message".into())`.
51-
5229
impl FromStr for Person {
5330
type Err = ParsePersonError;
5431
fn from_str(s: &str) -> Result<Person, Self::Err> {
32+
// 1. 检查输入字符串是否为空
33+
if s.is_empty() {
34+
return Err(ParsePersonError::Empty);
35+
}
36+
37+
// 2. 按逗号分割字符串并收集结果
38+
let parts: Vec<&str> = s.split(',').collect();
39+
40+
// 3. 检查分割后的部分数量是否为2
41+
if parts.len() != 2 {
42+
return Err(ParsePersonError::BadLen);
43+
}
44+
45+
// 4. 提取姓名并检查是否为空
46+
let name = parts[0].trim();
47+
if name.is_empty() {
48+
return Err(ParsePersonError::NoName);
49+
}
50+
51+
// 5. 解析年龄并处理可能的错误
52+
let age = parts[1].trim().parse().map_err(ParsePersonError::ParseInt)?;
53+
54+
// 6. 成功解析,返回Person实例
55+
Ok(Person {
56+
name: name.to_string(),
57+
age,
58+
})
5559
}
5660
}
5761

0 commit comments

Comments
 (0)