Coroutines, Self-Referential Structs, and Pinning 236 This is very much like futures that are not self-referential until they're polled, as we saw in our previous example. Let's write the impl block for Maybe Self Ref and take a look at the code:: ch09/d-pin/src/main. rs impl Maybe Self Ref { fn init(self: Pin<&mut Self>) { unsafe { let Self { a, b,.. } = self. get_unchecked_mut(); *b = Some(a); } } fn b(self: Pin<&mut Self>)-> Option<&mut usize> { unsafe { self. get_unchecked_mut(). b. map(|b| &mut *b) } } } As you can see, Maybe Stelf Ref will only be self-referential after we call init on it. We also define one more method that casts the pointer stored in b to Option<&mut usize>, which is a mutable reference to a. One thing to note is that both our functions require unsafe. Without Pin, the only method requiring unsafe would be b since we dereference a pointer there. Acquiring a mutable reference to a pinned value always require unsafe, since there is nothing preventing us from moving the pinned value at that point. Pinning to the heap is usually done by pinning a Box. There is even a convenient method on Box that allows us to get Pin<Box<... >>. Let's look at a short example: ch09/d-pin/src/main. rs fn main() { let mut x = Box:: pin(Maybe Self Ref::default()); x. as_mut(). init(); println!("{}", x. as_ref(). a); *x. as_mut(). b(). unwrap() = 2; println!("{}", x. as_ref(). a); }