如何枚举字符串向量?
How do I enumerate on a vector of Strings?
let output_sorted: Vec<String> = four_digit_ouput
.iter()
.map(|tok| tok.chars().sorted().collect::<String>())
.collect();
let output = 0;
for (idx, digit) in output_sorted.enumerate() {
当我尝试对字符串向量进行枚举 for 循环时出现此错误:
error[E0599]: the method `enumerate` exists for struct `Vec<String>`, but its trait bounds were not satisfied
--> src\day8.rs:185:39
|
185 | for (idx, digit) in output_sorted.enumerate() {}
| ^^^^^^^^^ method cannot be called on `Vec<String>` due to unsatisfied trait bounds
|
::: C:\Users\b\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib/rustlib/src/rust\library\alloc\src\vec\mod.rs:400:1
|
400 | pub struct Vec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global> {
| ------------------------------------------------------------------------------------------------ doesn't satisfy `Vec<String>: Iterator`
|
= note: the following trait bounds were not satisfied:
`Vec<String>: Iterator`
which is required by `&mut Vec<String>: Iterator`
`[String]: Iterator`
which is required by `&mut [String]: Iterator`
For more information about this error, try `rustc --explain E0599`.
这是怎么回事?什么特征界限不允许这个枚举?
矢量不是 Iterator
。
使用output_sorted.iter().enumerate()
(to consult), output_sorted.iter_mut().enumerate()
(to modify) oroutput_sorted.into_iter().enumerate()
(消费)。
请注意,当直接在向量上使用 for
循环时,会隐式调用 into_iter()
;这可以解释为好像向量本身就是一个 Iterator
但这只是语言的一种方便。
let output_sorted: Vec<String> = four_digit_ouput
.iter()
.map(|tok| tok.chars().sorted().collect::<String>())
.collect();
let output = 0;
for (idx, digit) in output_sorted.enumerate() {
当我尝试对字符串向量进行枚举 for 循环时出现此错误:
error[E0599]: the method `enumerate` exists for struct `Vec<String>`, but its trait bounds were not satisfied
--> src\day8.rs:185:39
|
185 | for (idx, digit) in output_sorted.enumerate() {}
| ^^^^^^^^^ method cannot be called on `Vec<String>` due to unsatisfied trait bounds
|
::: C:\Users\b\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib/rustlib/src/rust\library\alloc\src\vec\mod.rs:400:1
|
400 | pub struct Vec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global> {
| ------------------------------------------------------------------------------------------------ doesn't satisfy `Vec<String>: Iterator`
|
= note: the following trait bounds were not satisfied:
`Vec<String>: Iterator`
which is required by `&mut Vec<String>: Iterator`
`[String]: Iterator`
which is required by `&mut [String]: Iterator`
For more information about this error, try `rustc --explain E0599`.
这是怎么回事?什么特征界限不允许这个枚举?
矢量不是 Iterator
。
使用output_sorted.iter().enumerate()
(to consult), output_sorted.iter_mut().enumerate()
(to modify) oroutput_sorted.into_iter().enumerate()
(消费)。
请注意,当直接在向量上使用 for
循环时,会隐式调用 into_iter()
;这可以解释为好像向量本身就是一个 Iterator
但这只是语言的一种方便。