Created
March 1, 2017 20:59
-
-
Save Newlifer/c02ce0d4b943439cf05797d02915a0e7 to your computer and use it in GitHub Desktop.
transmute.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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