为什么我不能使用 `&amp;Iterator<Item = &amp;String&gt;` 作为迭代器? [英] Why can&#39;t I use `&amp;Iterator&lt;Item = &amp;String&gt;` as an iterator?

查看:42
本文介绍了为什么我不能使用 `&amp;Iterator<Item = &amp;String&gt;` 作为迭代器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下函数,它应该在给定 Iterator 的情况下查找并返回 String 的最长长度:

fn max_width(strings: &Iterator) ->使用{让 mut max_width = 0;对于字符串中的字符串{如果 string.len() >最大宽度{max_width = string.len();}}返回最大宽度;}

但是,编译器给了我以下错误:

error[E0277]: trait bound `&std::iter::Iterator<Item=&std::string::String>: std::iter::Iterator` 不满足-->src/main.rs:3:19|3 |对于字符串中的字符串{|^^^^^^^ `&std::iter::Iterator<Item=&std::string::String>` 不是迭代器;也许尝试调用`.iter()` 或类似的方法|= help: 特性 `std::iter::Iterator` 没有为 `&std::iter::Iterator<Item=&std::string::String>` 实现= 注意:`std::iter::IntoIterator::into_iter` 需要

我是 Rust 的新手,对此非常困惑,因为我认为我明确地传入了一个迭代器.调用 strings.iter() 告诉我它没有实现,而调用 strings.into_iter() 会让我陷入一个可变的兔子洞,我当然没有想改变传递的参数.

如何迭代我的字符串?

解决方案

您的代码失败,因为 Iterator&Iterator 不同.如果您将 Iterator 传递给您的函数,您可以解决此问题,但由于 Iterator 是一个特征,因此无法确定大小(您不知道什么是 Iterator 你正在通过).解决方案是传递任何实现 Iterator 的东西:

fn max_width<'a>(strings: impl Iterator) ->使用

游乐场<小时>

对于更有经验的 Rust 用户:

最通用的方法大概是这样:

fn max_width>(strings: impl IntoIterator) ->使用{让 mut max_width = 0;对于字符串中的字符串{让字符串 = string.as_ref();如果 string.len() >最大宽度{max_width = string.len();}}最大宽度}

游乐场

不过,你也可以使用

fn max_width>(strings: impl IntoIterator) ->使用{字符串.into_iter().map(|s| s.as_ref().len()).最大限度().unwrap_or(0)}

游乐场

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?

解决方案

Your code fails because Iterator is not the same as &Iterator. You can fix this if you pass Iterator to your function, but since Iterator is a trait, the size cannot be determined (You can't know, what Iterator you are passing). The solution is to pass anything that implements Iterator:

fn max_width<'a>(strings: impl Iterator<Item = &'a String>) -> usize

playground


For more experienced Rust users:

The most generic way is probably this:

fn max_width<T: AsRef<str>>(strings: impl IntoIterator<Item = T>) -> usize {
    let mut max_width = 0;
    for string in strings {
        let string = string.as_ref();
        if string.len() > max_width {
            max_width = string.len();
        }
    }
    max_width
}

playground

However, you can also use

fn max_width<T: AsRef<str>>(strings: impl IntoIterator<Item = T>) -> usize {
    strings
        .into_iter()
        .map(|s| s.as_ref().len())
        .max()
        .unwrap_or(0)
}

playground

这篇关于为什么我不能使用 `&amp;Iterator<Item = &amp;String&gt;` 作为迭代器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆