我试图用Higher-Rank Trait Bound定义一个trait Foo
,在&'a Self
类型IntoIterator
上.换句话说,我想要求实现类型T
可以通过不可变借位迭代(即,&T: IntoIterator
).
我还想要一个函数bar
,它接受类型为T: Foo
的参数.但这给了我一个错误.
以下是MRE:(playground在夜间1.79)
trait Foo
where for<'a> &'a Self: IntoIterator
{}
fn bar<T: Foo>(_: T) {}
这就是错误:
error[E0277]: `&'a T` is not an iterator
--> src/lib.rs:5:11
|
5 | fn bar<T: Foo>(_: T) {}
| ^^^ `&'a T` is not an iterator
|
= help: the trait `for<'a> Iterator` is not implemented for `&'a T`, which is required by `for<'a> &'a T: IntoIterator`
= help: the trait `Iterator` is implemented for `&mut I`
= note: required for `&'a T` to implement `for<'a> IntoIterator`
note: required by a bound in `Foo`
--> src/lib.rs:2:25
|
1 | trait Foo
| --- required by a bound in this trait
2 | where for<'a> &'a Self: IntoIterator
| ^^^^^^^^^^^^ required by this bound in `Foo`
For more information about this error, try `rustc --explain E0277`.
我不明白这个错误.为什么要用for<'a> Iterator
来实现&'a T
?Iterator
trait 似乎不是IntoIterator
的超级trait .
为什么他说是"required for 100 to implement 101"?毕竟,这正是我为Foo
定义的trait边界,也就是我为bar
参数的类型设置的边界,不是吗?
我觉得我在这里错过了一些基本的东西,但这是一种罕见的情况,当我发现编译器的错误信息根本没有帮助时.
PS:
这句话更让人困惑:"the trait 100 is implemented for 101".I
到底是什么?