Pointer Primitives
Raw pointers (*const T และ *mut T)
Creating Raw Pointers
Section titled “Creating Raw Pointers”fn main() { let x = 42;
// Create raw pointer from reference let ptr: *const i32 = &x as *const i32; println!("Pointer: {:p}", ptr);
// Shorthand with coercion let ptr: *const i32 = &x;
// Mutable pointer let mut y = 100; let ptr_mut: *mut i32 = &mut y;
// Dereference requires unsafe unsafe { println!("*ptr = {}", *ptr); *ptr_mut = 200; println!("*ptr_mut = {}", *ptr_mut); }
// Null pointers let null: *const i32 = std::ptr::null(); let null_mut: *mut i32 = std::ptr::null_mut();
println!("null.is_null(): {}", null.is_null()); println!("null_mut.is_null(): {}", null_mut.is_null());
// From integer (very unsafe!) let addr = 0x1234usize; let ptr_from_int = addr as *const i32; println!("From addr: {:p}", ptr_from_int);}เข้าสู่ระบบเพื่อดูเนื้อหาเต็ม
ยืนยันตัวตนด้วยบัญชี Google เพื่อปลดล็อกบทความทั้งหมด
Login with Google