0

there, I am still quite new to rust. I ran into a problem where I need to take ownership of a variable, and at the same time create a different variable, that gets a borrow of the newly owned one. Is there an elegant way to do such a thing?

struct Object<'a> {
    owned: SecondObject,
    borrowed: Wrapper<&'a SecondObject>
}

impl<'a> Object<'a> {
    pub fn new(second: SecondObject) -> Object<'a> {
        Object {
            owned: second,
            borrowed: Wrapper::new(&second); // This line breaks 
        }
    }
}

I think I tried all variants I can think of, but the actual move is always done either first, so I cannot get the reference, or there is a reference to the original variable so it can not be moved at all. And of course I can not reference to owned before it is actually created.

Thank you for any hints if this can be achieved, or if there is a different way to make this work.

0 Answers0