特征是否可以具有由泛型参数化的超级特征?

Cli*_*ton 4 types higher-kinded-types rust

你能在 Rust 中做这样的事情吗?

trait A : forall<T> B<T> { ... }
Run Code Online (Sandbox Code Playgroud)

也就是说,如果我们想要:

impl A for D { ... }
Run Code Online (Sandbox Code Playgroud)

我们首先要落实:

impl<T> B<T> for D { ... }
Run Code Online (Sandbox Code Playgroud)

Pet*_*all 5

不可以。Rust 的类型系统当前不支持任何涉及更高种类类型的功能。但是,它确实支持与您所描述的类似的结构,但仅限于生命周期参数。例如:

trait B<'a> {}

trait A: for<'a> B<'a> {}

struct D;

impl A for D { }
Run Code Online (Sandbox Code Playgroud)

这是一个错误:

trait B<'a> {}

trait A: for<'a> B<'a> {}

struct D;

impl A for D { }
Run Code Online (Sandbox Code Playgroud)

直到您添加一揽子实施:

impl<'a> B<'a> for D { }
Run Code Online (Sandbox Code Playgroud)

Rust 最终也为类型添加类似的功能并非不可能,但我预计不会很快实现。