HKTonyLee
HKTonyLee

Reputation: 3300

How to define recursive type alias in Rust?

So in Rust, I can define recursive type using enum like this:

enum Trie {
    Child(HashMap<char, Box<Trie>>)
}

However this is a little bit verbose, considering that this enum has only one member.

May I know how to make it simpler? Let's say using type alias like this?

// This code does not compile in rustc 1.55.0
type Trie = HashMap<char, Box<Trie>>;

Upvotes: 2

Views: 628

Answers (1)

Makalone LOgman
Makalone LOgman

Reputation: 980

Using a single field struct compiles:

struct Trie(HashMap<char, Box<Trie>>);

Upvotes: 4

Related Questions