Function std::ptr::replace

pub unsafe fn replace<T>(dst: *mut T, src: T) -> T

Moves src into the pointed dst, returning the previous dst value.

Neither value is dropped.

This function is semantically equivalent to mem::replace except that it operates on raw pointers instead of references. When references are available, mem::replace should be preferred.


Behavior is undefined if any of the following conditions are violated:

  • dst must be valid for writes.

  • dst must be properly aligned.

Note that even if T has size 0, the pointer must be non-NULL and properly aligned.


use std::ptr;

let mut rust = vec!['b', 'u', 's', 't'];

// `mem::replace` would have the same effect without requiring the unsafe
// block.
let b = unsafe {
    ptr::replace(&mut rust[0], 'r')

assert_eq!(b, 'b');
assert_eq!(rust, &['r', 'u', 's', 't']);

© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.