简体   繁体   English

为什么我在使用连接时得到 Vec<&&str> 的“不满意的特征界限”?

[英]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`
  1. Why is combos[0] a Vec<&&str> , rather than a Vec<&str> ?为什么combos[0]Vec<&&str> ,而不是Vec<&str>
  2. What are the unsatisfied trait bounds here?这里不满足的特征界限是什么?
  3. How do I resolve the error?如何解决错误?

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.

相关问题 Rust 迭代 Vec&lt;"CustomStruct"&gt; =&gt; 不满足的特征边界 - Rust iterate over Vec<"CustomStruct"> => unsatisfied trait bounds 当使用ReadBytesExt从字节片中读取整数时,为什么会出现错误“特征边界未得到满足”? - Why do I get the error “trait bounds were not satisfied” when using ReadBytesExt to read an integer from a slice of bytes? 为什么在使用 .await 或在像 `join!` 这样的宏中会出现“不满足 trait bound Future”? - Why do I get "the trait bound Future is not satisfied" when using .await or in a macro like `join!`? 为什么在将高级特征边界与关联类型结合时会出现 Rust 编译错误? - Why do I get a Rust compilation error when combining higher-rank trait bounds with associated types? 扩展失败类型的结果时,为什么会得到“该方法存在,但以下特征范围不满足”的信息? - Why do I get “the method exists but the following trait bounds were not satisfied” when extending Result for failure types? NetworkBehaviour 未满足的特征界限 - NetworkBehaviour unsatisfied trait bounds 为什么我在使用FlatMap迭代器时没有为Vec <i32>实现错误FromIterator <&{integer}>? - Why do I get the error FromIterator<&{integer}> is not implemented for Vec<i32> when using a FlatMap iterator? 为什么在过滤 Vec 时会得到双重引用? - Why do I get double references when filtering a Vec? 为什么在特化特征时出现冲突的实现错误? - Why do I get a conflicting implementations error when specializing a trait? 为什么在使用 `dyn Trait` 类型别名时会出现大小错误? - Why do I get a size error when using a type alias of `dyn Trait`?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM