I'm trying to build Rust code that manipulates a recursive (tree-like) data structure. Naively, one could define it as
struct ALinkedList {
value: i32,
next: Option<Box<Self>>
}
To experiment with different memory layouts and separate algorithm design from storage, I'd like to generalize the definition to something like
struct ALinkedList<D: Deref<Target=Self>> {
value: i32,
next: Option<D>
}
but when trying to construct an instance of ALinkedList, I get
64 | let t: ALinkedList<Box<_>> = ALinkedList{value: 0, next: Some(Box::new(ALinkedList{value: 0, next: None}))};
| ^^^^^^^^^^^^^^^^^^^ cyclic type of infinite size
My questions are:
- Is it possible to make these recursive type definitions work in Rust?
- If not, what other design patterns can I use to represent a tree-like struct without hardcoding how its children are stored and dereferenced in memory?