Managing garbage collector lifetime in Rust: how to implement garbage collection for long-lived objects
Hello, colleagues! I'm new to Rust and trying to understand memory management. I come from Java, where the garbage collector solves all memory problems, and now I'm having difficulty getting used to the ownership and lifetime system.
I need to implement a data structure where objects can live long and reference each other. For example, a graph with cyclic references. In Java, I would just create `HashMap>` and forget about it. In Rust, the compiler complains about borrows and lifetimes.
I'm trying to do something like:
```rust
struct Node {
id: u32,
children: Vec<&Node>,
}
fn main() {
let mut nodes = Vec::new();
let node1 = Node { id: 1, children: vec![] };
let node2 = Node { id: 2, children: vec![] };
node1.children.push(&node2);
node2.children.push(&node1);
```