diff --git a/library/core/src/slice/mod.rs b/library/core/src/slice/mod.rs index 5ec12b436e84b..20b051f98b585 100644 --- a/library/core/src/slice/mod.rs +++ b/library/core/src/slice/mod.rs @@ -1832,7 +1832,7 @@ impl [T] { /// /// # Panics /// - /// Panics if `mid > len`. For non-panicking alternative see + /// Panics if `mid > len`. For a non-panicking alternative see /// [`split_at_checked`](slice::split_at_checked). /// /// # Examples @@ -1879,7 +1879,7 @@ impl [T] { /// /// # Panics /// - /// Panics if `mid > len`. For non-panicking alternative see + /// Panics if `mid > len`. For a non-panicking alternative see /// [`split_at_mut_checked`](slice::split_at_mut_checked). /// /// # Examples @@ -2021,7 +2021,7 @@ impl [T] { unsafe { (from_raw_parts_mut(ptr, mid), from_raw_parts_mut(ptr.add(mid), len - mid)) } } - /// Divides one slice into two at an index returning `None` if slice is too + /// Divides one slice into two at an index returning, `None` if slice is too /// short. /// /// The first will contain all indices from `[0, mid)` (excluding @@ -2072,7 +2072,8 @@ impl [T] { } } - /// Divides one mutable slice into two at an index. + /// Divides one mutable slice into two at an index, returning `None` if + /// slice is too short. /// /// The first will contain all indices from `[0, mid)` (excluding /// the index `mid` itself) and the second will contain all diff --git a/library/core/src/str/mod.rs b/library/core/src/str/mod.rs index e3150f3b9421c..ebda6e994a773 100644 --- a/library/core/src/str/mod.rs +++ b/library/core/src/str/mod.rs @@ -659,7 +659,10 @@ impl str { #[must_use] #[stable(feature = "str_split_at", since = "1.4.0")] pub fn split_at(&self, mid: usize) -> (&str, &str) { - self.split_at_checked(mid).unwrap_or_else(|| slice_error_fail(self, 0, mid)) + match self.split_at_checked(mid) { + None => slice_error_fail(self, 0, mid), + Some(pair) => pair, + } } /// Divide one mutable string slice into two at an index. @@ -792,8 +795,8 @@ impl str { /// /// # Safety /// - /// The caller must ensure `mid` is a byte offset at a UTF-8 character - /// boundary. + /// The caller must ensure that `mid` is a valid byte offset from the start + /// of the string and falls on the boundary of a UTF-8 code point. unsafe fn split_at_mut_unchecked(&mut self, mid: usize) -> (&mut str, &mut str) { let len = self.len(); let ptr = self.as_mut_ptr();