kmd*_*eko 12
fn main() {
let a: [u8; 3] = [1, 2, 3];
println!("{:p}", a.as_ptr());
}
Run Code Online (Sandbox Code Playgroud)
0x7ffc97350edd
Run Code Online (Sandbox Code Playgroud)
数组强制切片,因此可以在数组上调用任何切片方法。
请注意,Rust 中的数组只是内存块。它们不像 C 中的数组那样指向某些存储的对象,它们是对象序列。
如果您有一些数据并想要获取指向它的指针,您通常会创建一个引用,因为只有引用(和其他指针)可以通过以下方式转换为指针as:
fn main() {
let a: [u8; 3] = [1, 2, 3]; // a blob of data on the stack...
let a_ref = &a; // a shared reference to this data...
let a_ptr = a_ref as *const u8; // and a pointer, created from the reference
println!("{:p}", a_ptr);
}
Run Code Online (Sandbox Code Playgroud)