特质可以有一个由泛型参数化的超特质吗? [英] Can a trait have a supertrait that is parameterized by a generic?

查看:28
本文介绍了特质可以有一个由泛型参数化的超特质吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Can you do something like this in Rust?

trait A : forall<T> B<T> { ... }

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

That is, if we want:

impl A for D { ... }

首先要实现:

impl<T> B<T> for D { ... }

推荐答案

没有.Rust 的类型系统目前不支持任何涉及更高级类型的特性.但是,它确实支持与您所描述的类似的构造,但仅限于生命周期参数.例如:

No. Rust's type system doesn't currently support any features involving higher kinded types. It does, however, support a similar construction to what you described, but limited to lifetime parameters. For example:

trait B<'a> {}

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

struct D;

impl A for D { }

这是一个错误:

error[E0277]: the trait bound `for<'a> D: B<'a>` is not satisfied
 --> src/lib.rs:7:6
  |
7 | impl A for D { }
  |      ^ the trait `for<'a> B<'a>` is not implemented for `D`

直到您添加一揽子实现:

Until you add the blanket implementation:

impl<'a> B<'a> for D { }

<小时>

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


It's not impossible that Rust will eventually add similar functionality for types too, but I wouldn't expect it any time soon.

这篇关于特质可以有一个由泛型参数化的超特质吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆