diff --git a/src/libcore/slice/mod.rs b/src/libcore/slice/mod.rs index a50426ba886bb..c239ce5491ca8 100644 --- a/src/libcore/slice/mod.rs +++ b/src/libcore/slice/mod.rs @@ -804,6 +804,59 @@ impl [T] { (&self[..mid], &self[mid..]) } + /// Divides one slice into two at an index. + /// + /// The first will contain all indices from `[0, mid)` (excluding + /// the index `mid` itself) and the second will contain all + /// indices from `[mid, len)` (excluding the index `len` itself). + /// + /// Returns `None` if `mid > len`. + /// + /// # Examples + /// + /// ``` + /// let v = [1, 2, 3, 4, 5, 6]; + /// + /// { + /// match v.try_split_at(0) { + /// Some((left, right)) => { + /// assert!(left == []); + /// assert!(right == [1, 2, 3, 4, 5, 6]); + /// }, + /// None => assert!(false), + /// } + /// } + /// + /// { + /// match v.try_split_at(2) { + /// Some((left, right)) => { + /// assert!(left == [1, 2]); + /// assert!(right == [3, 4, 5, 6]); + /// }, + /// None => assert!(false), + /// } + /// } + /// + /// { + /// match v.try_split_at(6) { + /// Some((left, right)) => { + /// assert!(left == [1, 2, 3, 4, 5, 6]); + /// assert!(right == []); + /// }, + /// None => assert!(false), + /// } + /// } + /// + /// { + /// assert!(v.split_at(7).is_none()) + /// } + /// ``` + #[unstable(feature = "try_split_at", issue = "54886")] + #[inline] + pub fn try_split_at(&self, mid: usize) -> Option<(&[T], &[T])> { + if mid > self.len() { None } else { Some(self.split_at(mid)) } + } + /// Divides one mutable slice into two at an index. /// /// The first will contain all indices from `[0, mid)` (excluding @@ -820,7 +873,7 @@ impl [T] { /// let mut v = [1, 0, 3, 0, 5, 6]; /// // scoped to restrict the lifetime of the borrows /// { - /// let (left, right) = v.split_at_mut(2); + /// let (left, right) = v.try_split_at_mut(2); /// assert!(left == [1, 0]); /// assert!(right == [3, 0, 5, 6]); /// left[1] = 2; @@ -842,6 +895,37 @@ impl [T] { } } + /// Divides one mutable slice into two at an index. + /// + /// The first will contain all indices from `[0, mid)` (excluding + /// the index `mid` itself) and the second will contain all + /// indices from `[mid, len)` (excluding the index `len` itself). + /// + /// Returns `None` if `mid > len`. + /// + /// # Examples + /// + /// ``` + /// let mut v = [1, 0, 3, 0, 5, 6]; + /// // scoped to restrict the lifetime of the borrows + /// { + /// match v.split_at_mut(2) { + /// Some((left, right)) => { + /// assert!(left == [1, 0]); + /// assert!(right == [3, 0, 5, 6]); + /// left[1] = 2; + /// right[1] = 4; + /// }, + /// None => assert!(false), + /// } + /// assert!(v == [1, 2, 3, 4, 5, 6]); + /// ``` + #[unstable(feature = "try_split_at", issue = "54886")] + #[inline] + pub fn try_split_at_mut(&mut self, mid: usize) -> Option<(&mut [T], &mut [T])> { + if mid > self.len() { None } else { Some(self.split_at_mut(mid)) } + } + /// Returns an iterator over subslices separated by elements that match /// `pred`. The matched element is not contained in the subslices. ///