I am facing a need to create a self-referential structure Foo, and to date I've managed to come up with the following:
use std::pin::Pin;
struct Foo<'a> {
    x: u32,
    xp: Option<&'a u32>,
}
impl From<u32> for Pin<Box<Foo<'_>>> {
    fn from(x: u32) -> Self {
        let mut res = Box::pin(Foo { x, xp: None });
        let x = &res.x;
        let xp = &mut res.xp;
        *xp = Some(x);
        res
    }
}
That looks like a book example of splitting a borrow, but I am probably missing a point here, because the compiler complains with a message that does not make it transparent what exactly is wrong:
error[E0502]: cannot borrow `res` as mutable because it is also borrowed as immutable
  --> src/lib.rs:12:23
   |
9  |     fn from(x: u32) -> Self {
   |                        ---- return type is Pin<Box<Foo<'1>>>
10 |         let mut res = Box::pin(Foo { x, xp: None });
11 |         let x = &res.x;
   |                  --- immutable borrow occurs here
12 |         let xp = &mut res.xp;
   |                       ^^^ mutable borrow occurs here
13 |         *xp = Some(x);
14 |         res
   |         --- returning this value requires that `res` is borrowed for `'1`
I am aware of this question, but why does the compiler thinks that a move of the referred to data occurs here?
Is it possible to express the same idea, probably using unsafe code?
