简体   繁体   English

为什么我在使用FlatMap迭代器时没有为Vec <i32>实现错误FromIterator <&{integer}>?

[英]Why do I get the error FromIterator<&{integer}> is not implemented for Vec<i32> when using a FlatMap iterator?

Consider this snippet: 请考虑以下代码段:

fn main() {
    let arr_of_arr = [[1, 2], [3, 4]];
    let res = arr_of_arr
        .iter()
        .flat_map(|arr| arr.iter())
        .collect::<Vec<i32>>();
}

The compiler error is: 编译器错误是:

error[E0277]: the trait bound `std::vec::Vec<i32>: std::iter::FromIterator<&{integer}>` is not satisfied
 --> src/main.rs:6:10
  |
6 |         .collect::<Vec<i32>>();
  |          ^^^^^^^ a collection of type `std::vec::Vec<i32>` cannot be built from an iterator over elements of type `&{integer}`
  |
  = help: the trait `std::iter::FromIterator<&{integer}>` is not implemented for `std::vec::Vec<i32>`

Why does this snippet not compile? 为什么这个代码片段无法编译?

In particular, I'm not able to understand the error messages: what type represents &{integer} ? 特别是,我无法理解错误消息:什么类型代表&{integer}

{integer} is a placeholder the compiler uses when it knows something has an integer type, but not which integer type. {integer}是编译器在知道某些内容具有整数类型但不知道哪种整数类型时使用的占位符。

The problem is that you're trying to collect a sequence of "references to integer" into a sequence of "integer". 问题是你试图将一系列“对整数的引用”收集到一个“整数”序列中。 Either change to Vec<&i32> , or dereference the elements in the iterator. 要么更改为Vec<&i32> ,要么取消引用迭代器中的元素。

fn main() {
    let arr_of_arr = [[1, 2], [3, 4]];
    let res = arr_of_arr.iter()
        .flat_map(|arr| arr.iter())
        .cloned() // or `.map(|e| *e)` since `i32` are copyable
        .collect::<Vec<i32>>();
}

暂无
暂无

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

相关问题 为什么我得到 FromIterator<&T>` is not implemented for Vec<t> ?</t> - Why do I get FromIterator<&T>` is not implemented for Vec<T>? 我怎样才能建立一个`Vec<i32> ` 来自 `Iterator<item=&i32> `?</item=&i32></i32> - How can I build a `Vec<i32>` from an `Iterator<Item=&i32>`? 我如何通过Vec <Vec<i32> &gt;功能? - How do I pass a Vec<Vec<i32>> to a function? 错误类型不匹配:预期的&#39;collections :: vec :: Vec <i32> &#39;,找到了&#39;&collections :: vec :: Vec <i32> &#39; - Error mismatched types: expected 'collections::vec::Vec<i32>', found '&collections::vec::Vec<i32>' 为什么在将大于i32的数字存储到变量中时,我没有得到文字超出范围错误? - Why do I not get a literal out of range error when storing a number larger than an i32 into a variable? vec 和有什么区别<i32>和 Vec <Box<i32> &gt;? - What is the difference between Vec<i32> and Vec<Box<i32>>? 为什么不 vec![[EMPTY; 大小]; SIZEX] 产生类型 Vec&lt;[[i32; 大小]; 尺寸X]&gt;? - Why doesn't vec![[EMPTY; SIZEY]; SIZEX] produce the type Vec<[[i32; SIZEY]; SIZEX]>? 退回Rust Vec <Vec<i32> &gt;通过WebAssembly转换为JavaScript - Returning a Rust Vec<Vec<i32>> to JavaScript via WebAssembly 获取多个整数用户输入并将其存储在 Vec 中的最有效方法是什么<i32> ? - What is the most efficient way of taking a number of integer user inputs and storing it in a Vec<i32>? 如何从文件中读取整数行并将其解析为 Vec<i32> 在锈? - How do I read lines of integers from a file and parse it into a Vec<i32> in Rust?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM