如何转换 Option<T>到零或一个元素的迭代器? [英] How to convert an Option&lt;T&gt; to an iterator of zero or one element?

查看:26
本文介绍了如何转换 Option<T>到零或一个元素的迭代器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个数字解码为一个整数,如果它不是一个数字,则获得一个仅针对该数字的迭代器或一个空迭代器.我试着这样做:

let ch = '1';ch.to_digit(10).map(once).unwrap_or(empty())

这不会编译.我收到以下错误消息:

错误[E0308]:类型不匹配-->src/lib.rs:6:41|6 |ch.to_digit(10).map(once).unwrap_or(empty());|^^^^^^^ 预期结构体`std::iter::Once`,找到结构体`std::iter::Empty`错误[E0308]:类型不匹配-->src/lib.rs:6:41|6 |ch.to_digit(10).map(once).unwrap_or(empty());|^^^^^^^ 预期结构体`std::iter::Once`,找到结构体`std::iter::Empty`||= 注意:预期类型`std::iter::Once`找到类型`std::iter::Empty<_>`= 注意:预期类型`std::iter::Once`找到类型`std::iter::Empty<_>`

我有什么办法告诉 .unwrap_or(...) 我不关心实际的类型,但我会得到一个 Iterator?

解决方案

IntoIterator trait 的存在仅仅是为了能够将类型转换为迭代器:

<块引用>

转换为迭代器.

通过为类型实现 IntoIterator,您可以定义如何将其转换为迭代器.这对于描述某种集合的类型很常见.

实施 IntoIterator 的一个好处是您的类型将 使用 Rust 的 for 循环语法.

<块引用>

如何转换 Option到零元素或一个元素的迭代器?

Option 实现 IntoIterator:

impl<'a, T>IntoIterator for &'a mut Option<T>实施<T>Option 的 IntoIteratorimpl'a,T>IntoIterator for &'a Option<T>

Result 也是如此.

您需要做的就是调用 into_iter(或在调用 IntoIterator 的地方使用该值,就像 for 循环一样):

fn x() ->impl Iterator{让 ch = '1';ch.to_digit(10).into_iter()}

另见:

I'm trying to decode a digit to an integer and either get an iterator over just this digit or an empty iterator if it wasn't a digit. I tried to do it like that:

let ch = '1';
ch.to_digit(10).map(once).unwrap_or(empty())

This doesn't compile. I get the following error message:

error[E0308]: mismatched types
 --> src/lib.rs:6:41
  |
6 |     ch.to_digit(10).map(once).unwrap_or(empty());
  |                                         ^^^^^^^ expected struct `std::iter::Once`, found struct `std::iter::Empty`
error[E0308]: mismatched types
 --> src/lib.rs:6:41
  |
6 |     ch.to_digit(10).map(once).unwrap_or(empty());
  |                                         ^^^^^^^ expected struct `std::iter::Once`, found struct `std::iter::Empty`
  |
  |
  = note: expected type `std::iter::Once<u32>`
             found type `std::iter::Empty<_>`

  = note: expected type `std::iter::Once<u32>`
             found type `std::iter::Empty<_>`

I there any way to tell the .unwrap_or(...) that I don't care of the actual type, but just that I will get an implementation of Iterator?

解决方案

The IntoIterator trait exists solely for the purpose of being able to convert types into iterators:

Conversion into an Iterator.

By implementing IntoIterator for a type, you define how it will be converted to an iterator. This is common for types which describe a collection of some kind.

One benefit of implementing IntoIterator is that your type will work with Rust's for loop syntax.

How to convert an Option<T> to an iterator of zero or one element?

Option implements IntoIterator:

impl<'a, T> IntoIterator for &'a mut Option<T>
impl<T> IntoIterator for Option<T>
impl<'a, T> IntoIterator for &'a Option<T>

The same is true for Result.

All you need to do is call into_iter (or use the value in a place that calls IntoIterator like a for loop):

fn x() -> impl Iterator<Item = u32> {
    let ch = '1';
    ch.to_digit(10).into_iter()
}

See also:

这篇关于如何转换 Option<T>到零或一个元素的迭代器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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