連鎖提領
就像在 C 中一樣,Rust 原始指標可以指向其他原始指標(這反過來可能指向更多的原始指標)。
// Take a regular string slice
let planet: &str = "Earth";
// Create a constant pointer pointing to our string slice
let planet_ptr: *const &str = &planet as *const &str;
// Create a constant pointer pointing to the pointer
let planet_ptr_ptr: *const *const &str = &planet_ptr as *const *const &str;
// This can go on...
let planet_ptr_ptr_ptr = &planet_ptr_ptr as *const *const *const &str;
unsafe {
// Direct usage
println!("The name of our planet is: {}", planet);
// Single dereference
println!("The name of our planet is: {}", *planet_ptr);
// Double dereference
println!("The name of our planet is: {}", **planet_ptr_ptr);
// Triple dereference
println!("The name of our planet is: {}", ***planet_ptr_ptr_ptr);
}
這將輸出:The name of our planet is: Earth
四次。