Skip to content

Commit

Permalink
add slice::replace
Browse files Browse the repository at this point in the history
  • Loading branch information
izik1 committed Sep 19, 2020
1 parent c6ab8e5 commit bcc9012
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions library/core/src/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3059,6 +3059,30 @@ impl<T> [T] {

left
}

/// Moves `value` into `self[index]`, returning the old value.
///
/// Neither value is dropped.
///
/// # Panics
///
/// Panics if `index` is out of bounds.
///
/// # Examples
///
/// ```
/// #![feature(slice_replace)]
///
/// let mut v = [1, 2, 3];
/// let r = v.replace(1, 20);
///
/// assert_eq!(v, [1, 20, 3]);
/// assert_eq!(r, 2);
/// ```
#[unstable(feature = "slice_replace", reason = "new API", issue = "none")]
pub fn replace(&mut self, index: usize, value: T) -> T {
mem::replace(&mut self[index], value)
}
}

#[stable(feature = "rust1", since = "1.0.0")]
Expand Down

0 comments on commit bcc9012

Please sign in to comment.