Skip to content

Commit bcc9012

Browse files
committed
add slice::replace
1 parent c6ab8e5 commit bcc9012

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

library/core/src/slice/mod.rs

+24
Original file line numberDiff line numberDiff line change
@@ -3059,6 +3059,30 @@ impl<T> [T] {
30593059

30603060
left
30613061
}
3062+
3063+
/// Moves `value` into `self[index]`, returning the old value.
3064+
///
3065+
/// Neither value is dropped.
3066+
///
3067+
/// # Panics
3068+
///
3069+
/// Panics if `index` is out of bounds.
3070+
///
3071+
/// # Examples
3072+
///
3073+
/// ```
3074+
/// #![feature(slice_replace)]
3075+
///
3076+
/// let mut v = [1, 2, 3];
3077+
/// let r = v.replace(1, 20);
3078+
///
3079+
/// assert_eq!(v, [1, 20, 3]);
3080+
/// assert_eq!(r, 2);
3081+
/// ```
3082+
#[unstable(feature = "slice_replace", reason = "new API", issue = "none")]
3083+
pub fn replace(&mut self, index: usize, value: T) -> T {
3084+
mem::replace(&mut self[index], value)
3085+
}
30623086
}
30633087

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

0 commit comments

Comments
 (0)