I want to define a HashMap with a key type of String and the value type is itself.
I tried to write something like:
HashMap<String, HashMap<String, ...>>
I found this requires recursion and I don't know how to write recursion in a type.
After reading Recursive generic types, I tried:
type HashToHash = HashMap<String, HashToHash>
However I got error:
error[E0391]: cycle detected when processing `HashToHash`
--> src/lib.rs:3:35
|
3 | type HashToHash = HashMap<String, HashToHash>;
| ^^^^^^^^^^
|
= note: ...which again requires processing `HashToHash`, completing the cycle
Is there a way to define this kind of type in Rust?
struct HashToHash(HashMap<String, HashToHash>);.HashTableacts like a box, so you can have a recursive struct.HashMaphas a size known at compile time which is independent of the key and value types, so the problem of an infinitely sized type mentioned in the question marked as duplicate does not occur here. There is still a question how useful this type is, since you can't have any values in the leaf nodes, but I can't rule out that there are use cases for this data structure.