Skip to content

Define fn [_]::try_split_at(&self, usize) -> Option<(&Self, &Self)> #54887

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 85 additions & 1 deletion src/libcore/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,59 @@ impl<T> [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
Expand All @@ -820,7 +873,7 @@ impl<T> [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;
Expand All @@ -842,6 +895,37 @@ impl<T> [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.
///
Expand Down