# Function core::mem::transmute_copy [−] [src]

    pub unsafe fn transmute_copy<T, U>(src: &T) -> U

1.0.0

Interprets `src` as `&U`, and then reads `src` without moving the contained value.

This function will unsafely assume the pointer `src` is valid for `sizeof(U)` bytes by transmuting `&T` to `&U` and then reading the `&U`. It will also unsafely create a copy of the contained value instead of moving out of `src`.

It is not a compile-time error if `T` and `U` have different sizes, but it is highly encouraged to only invoke this function where `T` and `U` have the same size. This function triggers undefined behavior if `U` is larger than `T`.

# Examples

fn main() { use std::mem; #[repr(packed)] struct Foo { bar: u8, } let foo_slice = [10u8]; unsafe { // Copy the data from 'foo_slice' and treat it as a 'Foo' let mut foo_struct: Foo = mem::transmute_copy(&foo_slice); assert_eq!(foo_struct.bar, 10); // Modify the copied data foo_struct.bar = 20; assert_eq!(foo_struct.bar, 20); } // The contents of 'foo_slice' should not have changed assert_eq!(foo_slice, [10]); }

    use std::mem;

    #[repr(packed)]
    struct Foo {
        bar: u8,
    }

    let foo_slice = [10u8];

    unsafe {
        // Copy the data from 'foo_slice' and treat it as a 'Foo'
        let mut foo_struct: Foo = mem::transmute_copy(&foo_slice);
        assert_eq!(foo_struct.bar, 10);

        // Modify the copied data
        foo_struct.bar = 20;
        assert_eq!(foo_struct.bar, 20);
    }

    // The contents of 'foo_slice' should not have changed
    assert_eq!(foo_slice, [10]);

# Help

## Keyboard Shortcuts

?
  Show this help dialog
S
  Focus the search field
⇤
  Move up in search results
⇥
  Move down in search results
⏎
  Go to active search result

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)