Pinning in Rust 239 In this example, we create two instances of Maybe Self Ref called x and y. Then, we create a scope where we pin x and set the value of x. a to 2 by dereferencing the self-reference in b, as we did previously. Now, when we exit the scope, x isn't pinned anymore, which means we can take a mutable reference to it without needing unsafe. Since this is safe Rust and we should be able to do what we want, we swap x and y. The output prints out the pointer address of the a field of both structs and the value of the pointer stored in b. When we look at the output, we should see the problem immediately: Finished dev [unoptimized + debuginfo] target(s) in 0. 58s Running `target\debug\x-pin-experiments. exe` x: { +----->a: 0xe45fcff558, | b: None, | } | | y: { | a: 0xe45fcff570, +-----|b: Some(0xe45fcff558), } Although the pointer values will differ from run to run, it's pretty evident that y doesn't hold a pointer to self anymore. Right now, it points somewhere in x. This is very bad and will cause the exact memory safety issues Rust is supposed to prevent. Note For this reason, the standard library has a pin! macro that helps us with safe stack pinning. The macro uses unsafe under the hood but makes it impossible for us to reach the pinned value again. Now that we've seen all the pitfalls of stack pinning, my clear recommendation is to avoid it unless you need to use it. If you have to use it, then use the pin! macro so that you avoid the issues we've described here.