我有一段有效的代码:
for line in content
.split('n')
.collect::<Vec<_>>()
.iter()
.chain((&["output"]).iter())
我认为必须有一种方法可以使用split提供的迭代器来获得相同的结果,而不必将((收集到向量中。
当我尝试将.collect::<Vec<_>>().iter()
更改为into_iter()
时,我得到以下错误:
error[E0271]: type mismatch resolving `<std::slice::Iter<'_, &str> as IntoIterator>::Item == &str`
--> src/main.rs:251:53
|
251 | for line in content.split('n').into_iter().chain((&["output"]).iter()) {
| ^^^^^ expected `&str`, found `str`
|
= note: expected reference `&&str`
found reference `&str`
还有其他错误指的是基本上说了同样的话的同一行。
问题的核心是:
(&["output"]).iter()
是具有Item=&&str
的类型std::slice::Iter<'_, &str>
- CCD_ 6是具有CCD_ 8的类型CCD_
因此项目类型不匹配。如果你让类型匹配它编译,IMO最简单的方法是在数组上使用map来取消引用每个字符串。
for line in content
.split('n')
.chain((&["output"]).iter().map(|s|*s)) {
println!("line={}",line);
}
我通过插入let q:() = ...
形式的行找到了这一点,这给了我
21 | let q:() = (&["output"]).iter();
| -- ^^^^^^^^^^^^^^^^^^^^ expected `()`, found struct `std::slice::Iter`
| |
| expected due to this
|
= note: expected unit type `()`
found struct `std::slice::Iter<'_, &str>`
22 | let q:() = content.split('n');
| -- ^^^^^^^^^^^^^^^^^^^ expected `()`, found struct `std::str::Split`
| |
| expected due to this
|
= note: expected unit type `()`
found struct `std::str::Split<'_, char>`
然后进行let q: <X as Iterator>::Item = ()
23 | let q: <std::slice::Iter<&str> as Iterator>::Item = ();
| ------------------------------------------ ^^ expected `&&str`, found `()`
和
24 | let q: <std::str::Split<char> as Iterator>::Item = ();
| ----------------------------------------- ^^ expected `&str`, found `()`