首页 > 解决方案 > 如何在 Rust 中定义递归类型别名?

问题描述

所以在 Rust 中,我可以像这样使用枚举定义递归类型:

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

然而,考虑到这个枚举只有一个成员,这有点冗长。

我可以知道如何使它更简单吗?假设使用这样的类型别名?

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

标签: rust

解决方案


使用单个字段结构编译:

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

推荐阅读