about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDavid Herberth <github@dav1d.de>2022-07-16 17:40:43 +0200
committerDavid Herberth <github@dav1d.de>2022-07-18 08:57:43 +0200
commitc1c1abc08ab5ae90da2e51ad02246631207113e1 (patch)
tree948e1320eed90438a2d3969c6315bafeb4b20014
parentd695a497bbf4b20d2580b75075faa80230d41667 (diff)
downloadrust-c1c1abc08ab5ae90da2e51ad02246631207113e1.tar.gz
rust-c1c1abc08ab5ae90da2e51ad02246631207113e1.zip
Use split_once in FromStr docs
-rw-r--r--library/core/src/str/traits.rs14
1 files changed, 8 insertions, 6 deletions
diff --git a/library/core/src/str/traits.rs b/library/core/src/str/traits.rs
index 32c31803a51..e9649fc91fa 100644
--- a/library/core/src/str/traits.rs
+++ b/library/core/src/str/traits.rs
@@ -519,12 +519,14 @@ unsafe impl const SliceIndex<str> for ops::RangeToInclusive<usize> {
 ///     type Err = ParseIntError;
 ///
 ///     fn from_str(s: &str) -> Result<Self, Self::Err> {
-///         let coords: Vec<&str> = s.trim_matches(|p| p == '(' || p == ')' )
-///                                  .split(',')
-///                                  .collect();
-///
-///         let x_fromstr = coords[0].parse::<i32>()?;
-///         let y_fromstr = coords[1].parse::<i32>()?;
+///         let (x, y) = s
+///             .strip_prefix('(')
+///             .and_then(|s| s.strip_suffix(')'))
+///             .and_then(|s| s.split_once(','))
+///             .unwrap();
+///
+///         let x_fromstr = x.parse::<i32>()?;
+///         let y_fromstr = y.parse::<i32>()?;
 ///
 ///         Ok(Point { x: x_fromstr, y: y_fromstr })
 ///     }