Rust data structures with circular references
To implement its safety guarantees, the Rust compiler keeps careful track
of ownership and references throughout a program. This makes writing certain
kinds of data structures challenging; in particular, data structures that have
circular references.
Let's start with a simple binary tree:
struct Tree {
root: Option<Node>,
}
struct Node {
data: i32,
left: Option<Box<Node>>,
right: Option<Box<Node>>,
}
Since the Rust compiler should be able to calculate the size of a struct at
compile-time, left a...
Read more at eli.thegreenplace.net