The Gadget/Owner example in the documentation for std::rc::Rc confuses me quite a bit.
Consider a scenario where a set of Gadget s are owned by a given Owner . We want to have our Gadget s point to their Owner . We can't do this with unique ownership, because more than one gadget may belong to the same Owner.
I don't really understand this. Can't I just define a struct like:
struct Gadget<'a> {
id: i32,
owner: &'a Owner,
}
and have the Gadgets point to the owner like that? Rust's ownership rules say that you can have multiple immutable references to one thing. Why did the example choose to use std::rc::Rc here other than to ensure that the Gadgets lived beyond the Owner? I understand that it helped when drop(gadget_owner) was called, but the "unique ownership" line confuses me.
What you did works for some situations, and leads to headache in others. Most limiting is that you can't create a new Gadget with a new Owner and freely move around the gadget, e.g. by returning it - the owner would be dropped and the reference point to nothing.
References are not pointers, but (compile time) locks on objects that make them read-only. It's not useful to have Owner permanently locked to be read-only.
Ah, I think I understand now. Rust doesn't like how gadget_owner2's lifetime is shorter than the Vec. I could move it above the Vec, but as you said, this is just complicating the issue further and can just be simplified using Rc. Thanks!