let v:[i32;9] = [0i32; 9];
let v_ref :&[i32;9]= &v;
let v_p :*const [i32]= v_ref as *const [i32];
I want to convert v_p to v_ref again, how to do it?
and one more thing, I read from rust nomicon, it says " casting *const [u16] to *const [u8] will result in a raw pointer which refers to an object of half the size of the original", but how can I get the lenght of the "*const [u16]"?
You can cast the pointer to *const [i32; 9] and dereference it:
let v_ref_2 = unsafe { &*v_p.cast::<[i32; 9]>() };
Note that if you're not literally converting a pointer obtained from a reference, then there are various safety invariants to uphold. The pointer must indeed point to a live (=properly allocated and not free) buffer holding fully initialized bytes. Rust doesn't have typed memory, so it doesn't matter how you have initialized the buffer, but you must make sure that there are no uninitialized bytes. Dereferencing a pointer pointing to uninitialized memory instantly violates memory safety. You also need to make sure that the pointer is sufficiently aligned for a value of your type. Finally, note that you cannot use this trick to turn a &T into a &mut T. Creating a &mut T or mutating through a raw pointer memory which is assumed immutable (including immutable bindings) is not permitted.