Pin Struct
Pinning for self-referential types
Pin Basics
Section titled “Pin Basics”use std::pin::Pin;use std::marker::PhantomPinned;
fn main() { // Pin prevents moving a value // Needed for self-referential types (e.g., async futures)
println!("=== What is Pin? ==="); println!("Pin<P> wraps a pointer P (e.g., &mut T, Box<T>)"); println!("It guarantees the pointee won't move"); println!();
println!("=== Why Pin? ==="); println!("Self-referential structs have pointers to themselves"); println!("If moved, internal pointers become invalid"); println!("Async blocks can be self-referential"); println!();
// Unpin types - safe to move even when pinned let mut x = 42; let pinned = Pin::new(&mut x); println!("i32 is Unpin: can be moved freely"); println!("Pinned value: {}", *pinned);}เข้าสู่ระบบเพื่อดูเนื้อหาเต็ม
ยืนยันตัวตนด้วยบัญชี Google เพื่อปลดล็อกบทความทั้งหมด
Login with Google