Skip to content

Commit c1c1abc

Browse files
committed
Use split_once in FromStr docs
1 parent d695a49 commit c1c1abc

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

library/core/src/str/traits.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -519,12 +519,14 @@ unsafe impl const SliceIndex<str> for ops::RangeToInclusive<usize> {
519519
/// type Err = ParseIntError;
520520
///
521521
/// fn from_str(s: &str) -> Result<Self, Self::Err> {
522-
/// let coords: Vec<&str> = s.trim_matches(|p| p == '(' || p == ')' )
523-
/// .split(',')
524-
/// .collect();
525-
///
526-
/// let x_fromstr = coords[0].parse::<i32>()?;
527-
/// let y_fromstr = coords[1].parse::<i32>()?;
522+
/// let (x, y) = s
523+
/// .strip_prefix('(')
524+
/// .and_then(|s| s.strip_suffix(')'))
525+
/// .and_then(|s| s.split_once(','))
526+
/// .unwrap();
527+
///
528+
/// let x_fromstr = x.parse::<i32>()?;
529+
/// let y_fromstr = y.parse::<i32>()?;
528530
///
529531
/// Ok(Point { x: x_fromstr, y: y_fromstr })
530532
/// }

0 commit comments

Comments
 (0)