Content-Length: 8503 | pFad | https://doc.rust-lang.org/stable/std/ptr/fn.replace.html

replace in std::ptr - Rust

replace

Function replace 

1.0.0 (const: 1.83.0) · Source
pub const unsafe fn replace<T>(dst: *mut T, src: T) -> T
Expand description

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.

§Safety

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

  • dst must be valid for both reads and writes.

  • dst must be properly aligned.

  • dst must point to a properly initialized value of type T.

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

§Examples

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']);








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://doc.rust-lang.org/stable/std/ptr/fn.replace.html

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy