返回Iterator(或任何其他特性)的正确方法是什么?
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(' '))}
to_words()
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`