
Understand it
Pin protects location-sensitive values after they are pinned; it does not make every value immovable from creation. Most users interact through pinned futures while libraries handle the unsafe construction details.
Some future state machines can contain references into their own stored state while suspended. Moving such a value after those references become valid could invalidate them. Pin<P> is a contract that the value reached through pointer P will not be moved again if the value's type relies on pinning. It is not a magical freeze of every value from birth, and it does not mean the surrounding pointer variable can never move.
Many ordinary types are Unpin, so pinning adds no movement restriction for them. A type that is !Unpin needs more care: once pinned, safe code must not move its inner value out through the pinned pointer. APIs that project pinned fields or construct self-referential structures carry subtle invariants. At this stage, use library-provided safe pinning helpers and avoid writing unsafe projections to make a compiler error disappear.
Compare a plain String, which can be moved between bindings, with a future that a runtime stores and polls repeatedly. Think of pinning as protecting the address of the future's internal state across polls. Then read an API signature accepting Pin<&mut F> and identify what it promises to the future implementation. Do not infer that every async function creates self-references or that Box::pin is required for every future. The practical rule is to follow the executor and library API, and to treat Pin as a movement guarantee with a precisely defined start.
Read the code
use std::pin::Pin;
fn main() {
let mut text = String::from("crab");
let pinned = Pin::new(&mut text);
let moved = std::mem::take(Pin::into_inner(pinned));
assert_eq!(moved, "crab");
assert!(text.is_empty());
println!("Unpin permits the move: {moved}");
}This complete safe program moves a String out through Pin because String implements Unpin. It contrasts with a location-sensitive !Unpin pointee.
Practice starter
use std::pin::Pin;
fn main() {
let mut text = String::from("crab");
let pinned = Pin::new(&mut text);
let moved = std::mem::take(Pin::into_inner(pinned));
println!("{moved}");
}Try it
Run the safe String example, then explain why the same safe extraction API is unavailable for an address-sensitive !Unpin pointee. Do not use unsafe code to bypass its contract.
What success looks like: You can say why this safe move works for String and why it is unavailable for a !Unpin pointee.
Check your understanding
Why is moving the String out through Pin safe in this example?
Reveal the answer
- String implements Unpin, so this pinning wrapper does not forbid the move
- Pin makes String thread safe
- Pin gives the borrowed String a static lifetime
Unpin opts a type out of pinning movement restrictions. The guarantee for an address-sensitive !Unpin pointee does not apply to this String.
Go deeper
Common misconception: “Pin freezes every value it wraps, so nothing can ever be moved out.”
Pin constrains moves of a pointee that relies on pinning. Unpin types such as String opt out of that restriction; moving the pointer wrapper itself is a separate matter.
- Identify the pointee: The pointee is a String, which implements Unpin. Its heap allocation and the stack slot holding String are different locations.
- Follow safe extraction: Pin::into_inner returns the mutable String reference because String is Unpin. mem::take moves its value and leaves an empty String.
- Compare a location-sensitive type: For an address-sensitive !Unpin pointee, safe extraction through this API is unavailable. Moving a Pin<Box<T>> handle still leaves the boxed pointee at its allocation.
Transfer challenge: Draw separate boxes for a String, its byte buffer, a Pin<Box<T>> handle and its pointee. Explain which address is constrained for an address-sensitive !Unpin T.
Connections
- Deadlock Avoidance: Keep a global lock order and short critical sections
- Cancellation Safety: Expect a future to be dropped at any await point
Learn this lesson with AI
Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.
I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools. Lesson: Pinning Intuition (courseId "concurrency", lessonId "pinning-intuition") Link: https://crabrix.com/learn/concurrency/pinning-intuition/ Goal: Pin constrains moves of a pointee that relies on pinning. Unpin types such as String opt out of that restriction; moving the pointer wrapper itself is a separate matter. Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=concurrency/pinning-intuition) and I will write the code there. Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.