[Solved] Moving and refererencing in a struct instanciation

Hi ! I have an issue that I haven't been able to solve for a long time :
I have a Display struct owning a peripheral instance. 2 fields of this struct are a Layer struct which hold a reference to the peripheral instance.
However, when I instanciate the Display this way

 let disp = Display {
        dma2d,
      // ^^^ move occurs here
        layer1: Layer::new(&dma2d),
                                        // ^^^ reference moved value. But I can't write self.dma2d ...
        layer2: Layer::new(&dma2d),
    };

there is a problem because dma2d is moved into disp but I try to give a ref to the layers.
Is there an elegant way to solve this, apart from a two stage construction with Option<Layer> ?

Thanks for your help !

PS : here is the playground code

It's not possible to create a self-referential struct.

Oh so this is also a slef referential struct...

And is there a Rust-ier way to achieve the same purpose ?

You can use shared ownership with Rc or Arc.

Ok, thanks for the help !

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.