返回Iterator(或任何其他特性)的正確方法是什么?下面的RUST代碼編譯并運行,沒有任何問題。fn main() {
let text = "abc";
println!("{}", text.split(' ').take(2).count());}在那之后,我嘗試過這樣的.但它沒有編譯fn main() {
let text = "word1 word2 word3";
println!("{}", to_words(text).take(2).count());}fn to_words(text: &str) -> &Iterator<Item = &str> {
&(text.split(' '))}主要問題是我不確定函數(shù)的返回類型是什么to_words()應(yīng)該是的。編譯器說:error[E0599]: no method named `count` found for type `std::iter::Take<std::iter::Iterator<Item=&str>>` in the current scope
--> src/main.rs:3:43
|
3 | println!("{}", to_words(text).take(2).count());
| ^^^^^
|
= note: the method `count` exists but the following trait bounds were not satisfied:
`std::iter::Iterator<Item=&str> : std::marker::Sized`
`std::iter::Take<std::iter::Iterator<Item=&str>> : std::iter::Iterator`什么才是正確的代碼使這個運行?.我的知識差距在哪里?
返回Iterator(或任何其他特性)的正確方法是什么?
胡子哥哥
2019-06-18 17:10:56