I have the following function that's supposed to find and return the longest length of a String
given an Iterator
:
fn max_width(strings: &Iterator<Item = &String>) -> usize {
let mut max_width = 0;
for string in strings {
if string.len() > max_width {
max_width = string.len();
}
}
return max_width;
}
However, the compiler gives me the following error:
error[E0277]: the trait bound `&std::iter::Iterator<Item=&std::string::String>: std::iter::Iterator` is not satisfied
--> src/main.rs:3:19
|
3 | for string in strings {
| ^^^^^^^ `&std::iter::Iterator<Item=&std::string::String>` is not an iterator; maybe try calling `.iter()` or a similar method
|
= help: the trait `std::iter::Iterator` is not implemented for `&std::iter::Iterator<Item=&std::string::String>`
= note: required by `std::iter::IntoIterator::into_iter`
I'm new to Rust, and terribly confused by this, since I thought I was explicitly passing in an iterator. Calling strings.iter()
tells me that it is not implemented, and calling strings.into_iter()
sends me down a mutability rabbit-hole, and I certainly don't want to mutate the passed argument.
How can I iterate over my strings?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…