I have an array of 4 X u32 types. I would like a reference to the array (pointer) instead of a slice (pointer + length). The snippet below prints 16 for both slice and reference which suggests that both forms of initialization are returning a slice.
Is there some syntax for returning a reference to the array that isn't a slice?
use std::mem;
fn main() {
let array: [u32; 4] = [1, 2, 3, 4];
let slice: &[u32] = &array[..];
let reference: &[u32; 4] = &array;
println!("size(slice) = {:?}", mem::size_of_val(slice)); // Prints: 16
println!("size(reference) = {:?}", mem::size_of_val(reference)); // Prints: 16
}
No. size_of_val()
returns the size of the value pointed by the reference, which is the same for arrays and slices. If you will do size_of_val(&reference)
, you will see reference
is a thin pointer.