How do I reference a variable used in an unsafe block?

Viewed 1085

I'm trying to read 4 bytes from a socket and then transmute those bytes into a single u32.

let mut length: u32 = 0;

// Transmute 4 byte array into u32
unsafe {
    let length = mem::transmute::<[u8; 4], u32>(buf); // buf is the 4 item array of u8 that the socket was read into
    println!("1: {:}", length);
}

println!("Length: {:}", length);

However, length has the original value of 0 once outside of the unsafe block. How can I get around this?

2 Answers
Related