Lobste.rs

What is `std::pin::Pin` in Rust?

social_card_bg_hu_f0cc9d1c2ff17ee8

What is std::pin::Pin in Rust?29 Jun 2026std::pin::Pin is a pointer wrapper that represents the guarantee that the pointee will not be moved through that pointer.Why is it required?The need for pinning arises from self-referential types. Self-referential structs are the most common example of address-sensitive types, and are the primary motivation for Pin. Take the following struct for example:1struct SelfRef { 2 data: i32, 3 ptr: *const i32, // points to self.data 4}If we move an instance of this struct (for example, by moving ownership into another variable or returning it), its memory address changes. However, the raw pointer ptr still refers to the old memory location, leaving a dangling pointer. Hence, we need a way to prevent moving SelfRef once those self-references have been…

Continue reading at Lobste.rs vrong.me
Advertisement