[英]Why do I get "unsatisfied trait bounds" for a Vec<&&str> when using join?
I'm confused about this:我对此感到困惑:
use itertools::Itertools; // 0.10.0
fn main() {
let combos = ["a", "b", "c"].iter().combinations(2).collect::<Vec<_>>();
println!("{:#?}", combos[0].join(""));
}
error[E0599]: the method `join` exists for struct `Vec<&&str>`, but its trait bounds were not satisfied
--> src/main.rs:5:33
|
5 | println!("{:#?}", combos[0].join(""));
| ^^^^ method cannot be called on `Vec<&&str>` due to unsatisfied trait bounds
|
= note: the following trait bounds were not satisfied:
`Vec<&&str>: Iterator`
which is required by `Vec<&&str>: Itertools`
`<[&&str] as Join<_>>::Output = _`
`[&&str]: Iterator`
which is required by `[&&str]: Itertools`
combos[0]
a Vec<&&str>
, rather than a Vec<&str>
?为什么combos[0]
是Vec<&&str>
,而不是Vec<&str>
?I'm sure these answers are derivable from the book , but I was hoping not to have to read the whole book to write this trivial program.我确信这些答案可以从书中得出,但我希望不必阅读整本书来编写这个琐碎的程序。 Perhaps one can not merely dabble in Rust...或许Rust不能只涉猎...
TL;DR the fix: TL;DR 修复:
["a", "b", "c"].iter().copied().combinations(2).collect::<Vec<_>>()
Parts of your question have existing answers:您的部分问题已有答案:
Concentrating on the remaining piece, we look at the error message:专注于剩下的部分,我们查看错误消息:
= note: the following trait bounds were not satisfied:
`Vec<&&str>: Iterator`
which is required by `Vec<&&str>: Itertools`
`<[&&str] as Join<_>>::Output = _`
`[&&str]: Iterator`
which is required by `[&&str]: Itertools`
You've created a Vec<&&str>
.你已经创建了一个Vec<&&str>
。 You are presumably trying to call slice::join
:您可能正在尝试调用slice::join
:
pub fn join<Separator>(
&self,
sep: Separator
) -> <[T] as Join<Separator>>::Output
where
[T]: Join<Separator>,
The trait bounds for that method are not met:不满足该方法的特征界限:
fn satisfies<T: std::borrow::Borrow<str>>() {}
satisfies::<&&str>();
error[E0277]: the trait bound `&&str: Borrow<str>` is not satisfied
--> src/main.rs:9:5
|
8 | fn satisfies<T: std::borrow::Borrow<str>>() {}
| ------------------------ required by this bound in `satisfies`
9 | satisfies::<&&str>();
| ^^^^^^^^^^^^^^^^^^ the trait `Borrow<str>` is not implemented for `&&str`
The compiler has instead selected Itertools::join
:编译器改为选择Itertools::join
:
pub fn join(&mut self, sep: &str) -> String
where
Self::Item: Display,
This also fails to resolve, as neither Vec<&&str>
nor [&&str]
implement Iterator
, which would be required for Itertools
to be implemented for those types.这也无法解决,因为Vec<&&str>
和[&&str]
都没有实现Iterator
,这对于为这些类型实现Itertools
是必需的。
I don't know why the compiler chooses to only report one of the failures.我不知道为什么编译器选择只报告其中一个失败。 Perhaps it considers one more likely to be what you intended, or one is closer to being implemented, or maybe it's a bug or oversight in the trait resolution algorithm.也许它认为一个更有可能是你想要的,或者一个更接近于实现,或者它可能是特征解析算法中的一个错误或疏忽。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.