The test code is:
use std::mem::transmute;
fn main(){
unsafe{
let mut a:*mut u16 = transmute((vec!['w' as u8, 0u8, 'h' as u8, 0u8, 'a' as u8, 0u8,'t' as u8, 0u8]).as_mut_ptr());
let mut raw = ::std::slice::from_raw_parts::<u8>(a as *mut u8, 20);
println!("{:?}", raw);
let mut raw = ::std::slice::from_raw_parts::<u8>(transmute(a), 20);
println!("{:?}", raw);
let mut b:*mut u16 = (vec!['w' as u8, 0u8, 'h' as u8, 0u8, 'a' as u8, 0u8,'t' as u8, 0u8]).as_mut_ptr() as *mut u16;
let mut raw = ::std::slice::from_raw_parts::<u8>(b as *mut u8, 20);
println!("{:?}", raw);
let mut raw = ::std::slice::from_raw_parts::<u8>(transmute(a), 20);
println!("{:?}", raw);
println!("the w is:{:?}",'w' as u8);
println!("the h is:{:?}",'h' as u8);
println!("the a is:{:?}",'a' as u8);
println!("the t is:{:?}",'t' as u8);
}
}
The output at my PC:
$ /e/rustdir/target/debug/test_cast_msvc_i686.exe
[8, 208, 3, 1, 196, 0, 95, 0, 30, 78, 157, 171, 154, 45, 0, 8, 91, 56, 44, 32]
[8, 208, 3, 1, 196, 0, 95, 0, 30, 78, 157, 171, 154, 45, 0, 8, 91, 56, 44, 32]
[196, 0, 95, 0, 196, 0, 95, 0, 69, 8, 0, 77, 154, 45, 0, 0, 196, 0, 95, 0]
[8, 208, 3, 1, 196, 0, 95, 0, 30, 78, 157, 171, 154, 45, 0, 8, 91, 56, 44, 32]
the w is119
My PC info:
Windows 64bit
rustup info:
$ rustup show
Default host: x86_64-pc-windows-gnu
installed toolchains
--------------------
nightly-i686-pc-windows-gnu
nightly-i686-pc-windows-msvc (default)
active toolchain
----------------
nightly-i686-pc-windows-msvc (default)
rustc 1.22.0-nightly (dead08cb3 2017-09-08)
The out put at Rust Playground
[16, 192, 248, 108, 156, 85, 0, 0, 80, 64, 226, 40, 42, 127, 0, 0, 0, 0, 0, 0]
[16, 192, 248, 108, 156, 85, 0, 0, 80, 64, 226, 40, 42, 127, 0, 0, 0, 0, 0, 0]
[119, 0, 104, 0, 97, 0, 116, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[16, 192, 248, 108, 156, 85, 0, 0, 80, 64, 226, 40, 42, 127, 0, 0, 119, 0, 104, 0]
the w is:119
It seem that playground's result: [119, 0, 104, 0, 97, 0, 116, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] is the correct result.