Skip to content
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
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: don't bother making this change until we find out whether libs wants to move forward, but note that mem::replace is must_use, so this one probably should be too -- maybe something like

#[must_use = "if you don't need the old value, you can just assign the new value directly with `*slice[index] = value;`"]

mem::replace(&mut self[index], value)
}
}

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