Skip to content

Instantly share code, notes, and snippets.

@Newlifer
Created March 1, 2017 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Newlifer/c02ce0d4b943439cf05797d02915a0e7 to your computer and use it in GitHub Desktop.
Save Newlifer/c02ce0d4b943439cf05797d02915a0e7 to your computer and use it in GitHub Desktop.
transmute.rs
use std::mem;
#[derive(Debug)]
struct Rec {
x: u32
}
fn main() {
let array = [8 as u8, 0 as u8, 0 as u8, 0 as u8];
let rec: Rec;
unsafe {
rec = mem::transmute::<[u8; 4], Rec>(array);
}
let mut array_out = [1 as u8, 2 as u8, 4 as u8, 7 as u8];
let rec_out = Rec{ x: 8 };
unsafe {
array_out = mem::transmute::<Rec, [u8; 4]>(rec_out);
}
println!("{:?}", array_out);
println!("{:?}", rec);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment