Skip to content

Commit 03530fa

Browse files
committed
add example for moving out of pointer
1 parent 68e766a commit 03530fa

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/libcore/marker.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -609,16 +609,27 @@ unsafe impl<'a, T: ?Sized> Freeze for &'a mut T {}
609609
/// this trait cannot prevent types from moving by itself.
610610
///
611611
/// Instead it can be used to prevent moves through the type system,
612-
/// by controlling the behavior of special pointers types like [`PinMut`],
613-
/// which "pin" the type in place by wrapping it in a type which can only be dereferenced immutably.
612+
/// by controlling the behavior of special pointer types like [`PinMut`],
613+
/// which "pin" the type in place by not allowing it to be moved out via mutable references.
614614
///
615615
/// Implementing this trait lifts the restrictions of pinning off a type,
616-
/// which then allows it to move out of said pointers with functions such as [`swap`].
616+
/// which then allows it to move out of said pointers, with functions such as [`replace`].
617+
///
618+
/// So this, for example, can only be done on types implementing `Unpin`:
619+
///
620+
/// ```rust
621+
/// #![feature(pin)]
622+
/// use std::mem::{PinMut, replace};
623+
///
624+
/// let mut string = "this".to_string();
625+
/// let mut pinned_string = PinMut::new(&mut string);
626+
/// replace(&mut *pinned_string, "other".to_string());
627+
/// ```
617628
///
618629
/// This trait is automatically implemented for almost every type.
619630
///
620631
/// [`PinMut`]: ../mem/struct.PinMut.html
621-
/// [`swap`]: ../mem/fn.swap.html
632+
/// [`replace`]: ../mem/fn.replace.html
622633
#[unstable(feature = "pin", issue = "49150")]
623634
pub auto trait Unpin {}
624635

0 commit comments

Comments
 (0)