简体   繁体   English

对迭代器进行分区时,没有为 Vec 实现 trait Extend

[英]The trait Extend is not implemented for Vec when partitioning an iterator

I am running into the error when calling .partition() on a vector iterator:在向量迭代器上调用.partition().partition()错误:

error[E0277]: the trait bound `std::vec::Vec<std::result::Result<std::collections::HashSet<&std::string::String>, std::boxed::Box<dyn std::error::Error>>>: std::iter::Extend<&std::result::Result<std::collections::HashSet<std::string::String>, std::boxed::Box<dyn std::error::Error>>>` is not satisfied
 --> src/main.rs:9:24
  |
9 |         results.iter().partition(|r| r.is_ok());
  |                        ^^^^^^^^^ the trait `std::iter::Extend<&std::result::Result<std::collections::HashSet<std::string::String>, std::boxed::Box<dyn std::error::Error>>>` is not implemented for `std::vec::Vec<std::result::Result<std::collections::HashSet<&std::string::String>, std::boxed::Box<dyn std::error::Error>>>`
  |
  = help: the following implementations were found:
            <std::vec::Vec<T> as std::iter::Extend<&'a T>>
            <std::vec::Vec<T> as std::iter::Extend<T>>

When running the following code:运行以下代码时:

use std::collections::HashSet;

type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;

fn main() {
    let mut results: Vec<Result<HashSet<String>>> = Default::default();

    let (okays, errors): (Vec<Result<HashSet<&String>>>, Vec<_>) =
        results.iter().partition(|r| r.is_ok());
}

See playground for example.游乐场为例。

As the error message states (with namespacing removed):正如错误消息所述(删除了命名空间):

the trait Extend<&Result<HashSet<String>, Box<dyn Error>>> is not implemented for Vec<Result<HashSet<&String>, Box<dyn Error>>>特征Extend<&Result<HashSet<String>, Box<dyn Error>>>没有为Vec<Result<HashSet<&String>, Box<dyn Error>>>

You can't extend a Vec<T> with elements of type &T because they aren't the same type .您不能使用&T类型的元素扩展Vec<T> ,因为它们不是相同的 type

Instead, you can do one of these:相反,您可以执行以下操作之一:

  1. Change the type of the destination collection to Vec<&Result<HashSet<String>>> (or just Vec<_> , like your second destination type, to allow the compiler to infer the inner type).将目标集合的类型更改为Vec<&Result<HashSet<String>>> (或者只是Vec<_> ,就像您的第二个目标类型一样,以允许编译器推断内部类型)。

  2. Convert the reference to an owned value, perhaps via clone or to_owned .可能通过cloneto_owned将引用转换为拥有的值。

  3. Don't iterate over references to start with, using into_iter or drain instead.不要在开始时迭代引用,而是使用into_iterdrain

However, your current type will be very hard or expensive to achieve, as you state that you want an owned Result with an owned HashMap but a reference the String .但是,您当前的类型将很难实现或成本很高,因为您声明您想要一个拥有Result和一个拥有的HashMap引用String

I think the best thing is to use Itertools::partition_map and into_iter :我认为最好的方法是使用Itertools::partition_mapinto_iter

use itertools::Itertools; // 0.9.0
use std::collections::HashSet;

type Error = Box<dyn std::error::Error>;
type Result<T, E = Error> = std::result::Result<T, E>;

fn main() {
    let mut results: Vec<Result<HashSet<String>>> = Default::default();

    let (errors, okays): (Vec<_>, Vec<_>) = results.into_iter().partition_map(Into::into);
    // let (errors, okays): (Vec<Error>, Vec<HashSet<String>>)
}

See also:也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM