From 33a6a07d586437e8d4894dc35413a21ee5cfba54 Mon Sep 17 00:00:00 2001 From: Donnie Bishop Date: Sat, 25 Mar 2017 11:56:52 -0400 Subject: [PATCH 1/3] FromStr implementation example --- src/libcore/str/mod.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/libcore/str/mod.rs b/src/libcore/str/mod.rs index dfb6936da6bda..bc5df6810a9d9 100644 --- a/src/libcore/str/mod.rs +++ b/src/libcore/str/mod.rs @@ -35,6 +35,39 @@ pub mod pattern; /// [`from_str`]: #tymethod.from_str /// [`str`]: ../../std/primitive.str.html /// [`parse`]: ../../std/primitive.str.html#method.parse +/// +/// # Examples +/// +/// Basic implementation of `FromStr` on an example `Point` type: +/// +/// ``` +/// use std::str::FromStr; +/// use std::num::ParseIntError; +/// +/// #[derive(Debug, PartialEq)] +/// struct Point { +/// x: i32, +/// y: i32 +/// } +/// +/// impl FromStr for Point { +/// type Err = ParseIntError; +/// +/// fn from_str(s: &str) -> Result { +/// let coords: Vec<&str> = s.trim_matches(|p| p == '(' || p == ')' ) +/// .split(",") +/// .collect(); +/// +/// let x_fromstr = try!(coords[0].parse::()); +/// let y_fromstr = try!(coords[1].parse::()); +/// +/// Ok(Point { x: x_fromstr, y: y_fromstr }) +/// } +/// } +/// +/// let p = Point::from_str("(1,2)"); +/// assert_eq!(p.unwrap(), Point{ x: 1, y: 2} ) +/// ``` #[stable(feature = "rust1", since = "1.0.0")] pub trait FromStr: Sized { /// The associated error which can be returned from parsing. From 64cd0bebab1a9023dc5a4bbc38f9e6820629fbb9 Mon Sep 17 00:00:00 2001 From: Donnie Bishop Date: Sat, 25 Mar 2017 12:22:23 -0400 Subject: [PATCH 2/3] Remove trailing whitespace --- src/libcore/str/mod.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/libcore/str/mod.rs b/src/libcore/str/mod.rs index bc5df6810a9d9..ae08a3d0a9a7d 100644 --- a/src/libcore/str/mod.rs +++ b/src/libcore/str/mod.rs @@ -43,28 +43,28 @@ pub mod pattern; /// ``` /// use std::str::FromStr; /// use std::num::ParseIntError; -/// +/// /// #[derive(Debug, PartialEq)] /// struct Point { /// x: i32, /// y: i32 /// } -/// +/// /// impl FromStr for Point { /// type Err = ParseIntError; -/// +/// /// fn from_str(s: &str) -> Result { /// let coords: Vec<&str> = s.trim_matches(|p| p == '(' || p == ')' ) /// .split(",") /// .collect(); -/// +/// /// let x_fromstr = try!(coords[0].parse::()); /// let y_fromstr = try!(coords[1].parse::()); -/// +/// /// Ok(Point { x: x_fromstr, y: y_fromstr }) /// } /// } -/// +/// /// let p = Point::from_str("(1,2)"); /// assert_eq!(p.unwrap(), Point{ x: 1, y: 2} ) /// ``` From fb5e63fc475996ee5c65fb1b8686b8db17eb1e63 Mon Sep 17 00:00:00 2001 From: Donnie Bishop Date: Sat, 25 Mar 2017 14:41:37 -0400 Subject: [PATCH 3/3] Change `try!` to `?` --- src/libcore/str/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libcore/str/mod.rs b/src/libcore/str/mod.rs index ae08a3d0a9a7d..f3c3994ef3150 100644 --- a/src/libcore/str/mod.rs +++ b/src/libcore/str/mod.rs @@ -58,8 +58,8 @@ pub mod pattern; /// .split(",") /// .collect(); /// -/// let x_fromstr = try!(coords[0].parse::()); -/// let y_fromstr = try!(coords[1].parse::()); +/// let x_fromstr = coords[0].parse::()?; +/// let y_fromstr = coords[1].parse::()?; /// /// Ok(Point { x: x_fromstr, y: y_fromstr }) /// }