Function read
unsafe const fn read<T>(src: *const T) -> T
Reads the value from src without moving it. This leaves the
memory in src unchanged.
Safety
Behavior is undefined if any of the following conditions are violated:
-
srcmust be valid for reads. -
srcmust be properly aligned. Useread_unalignedif this is not the case. -
srcmust point to a properly initialized value of typeT.
Note that even if T has size 0, the pointer must be properly aligned.
Examples
Basic usage:
let x = 12;
let y = &x as *const i32;
unsafe
Manually implement [mem::swap]:
use ptr;
let mut foo = "foo".to_owned;
let mut bar = "bar".to_owned;
swap;
assert_eq!;
assert_eq!;
Ownership of the Returned Value
read creates a bitwise copy of T, regardless of whether T is Copy.
If T is not Copy, using both the returned value and the value at
*src can violate memory safety. Note that assigning to *src counts as a
use because it will attempt to drop the value at *src.
[write()] can be used to overwrite data without causing it to be dropped.
use ptr;
let mut s = Stringfrom;
unsafe
assert_eq!;