Rollup merge of #75038 - rust-lang:Havvy-patch-1, r=steveklabnik

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.
This commit is contained in:
Yuki Okushi 2020-08-19 15:54:24 +09:00 committed by GitHub
commit 17d3ce457f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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
///
/// ```
@ -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) {
@ -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:
@ -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 {
@ -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:
@ -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"]