Skip to content

Commit

Permalink
Rollup merge of rust-lang#75038 - rust-lang:Havvy-patch-1, r=stevekla…
Browse files Browse the repository at this point in the history
…bnik

See also X-Link mem::{swap, take, replace}

Since it's easy to end up at one of these functions when you really wanted the other one, cross link them with descriptions of why you'd want to use them.
  • Loading branch information
tmandry authored Aug 18, 2020
2 parents 7ae6928 + e720f42 commit 3271a75
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions library/core/src/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,9 @@ pub unsafe fn uninitialized<T>() -> T {

/// Swaps the values at two mutable locations, without deinitializing either one.
///
/// * If you want to swap with a default or dummy value, see [`take`].
/// * If you want to swap with a passed value, returning the old value, see [`replace`].
///
/// # Examples
///
/// ```
Expand All @@ -683,6 +686,9 @@ pub unsafe fn uninitialized<T>() -> T {
/// assert_eq!(42, x);
/// assert_eq!(5, y);
/// ```
///
/// [`replace`]: fn.replace.html
/// [`take`]: fn.take.html
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn swap<T>(x: &mut T, y: &mut T) {
Expand All @@ -695,6 +701,9 @@ pub fn swap<T>(x: &mut T, y: &mut T) {

/// Replaces `dest` with the default value of `T`, returning the previous `dest` value.
///
/// * If you want to replace the values of two variables, see [`swap`].
/// * If you want to replace with a passed value instead of the default value, see [`replace`].
///
/// # Examples
///
/// A simple example:
Expand Down Expand Up @@ -747,6 +756,8 @@ pub fn swap<T>(x: &mut T, y: &mut T) {
/// ```
///
/// [`Clone`]: ../../std/clone/trait.Clone.html
/// [`replace`]: fn.replace.html
/// [`swap`]: fn.swap.html
#[inline]
#[stable(feature = "mem_take", since = "1.40.0")]
pub fn take<T: Default>(dest: &mut T) -> T {
Expand All @@ -757,6 +768,9 @@ pub fn take<T: Default>(dest: &mut T) -> T {
///
/// Neither value is dropped.
///
/// * If you want to replace the values of two variables, see [`swap`].
/// * If you want to replace with a default value, see [`take`].
///
/// # Examples
///
/// A simple example:
Expand Down Expand Up @@ -810,6 +824,8 @@ pub fn take<T: Default>(dest: &mut T) -> T {
/// ```
///
/// [`Clone`]: ../../std/clone/trait.Clone.html
/// [`swap`]: fn.swap.html
/// [`take`]: fn.take.html
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
#[must_use = "if you don't need the old value, you can just assign the new value directly"]
Expand Down

0 comments on commit 3271a75

Please sign in to comment.