一个比”vec![a,b,c].into_iter()”更好的创建临时迭代器的方式是什么?

huangapple go评论63阅读模式
英文:

A nicer way to create an ad hoc iterator than "vec![a,b,c].into_iter()"?

问题

The expression vec![a, b, c].into_iter() seems unnecessarily long. Is there something better?

The doc for itertools kmerge gives this example:

use itertools::Itertools;

let a = (0..6).step(3);
let b = (1..6).step(3;
let c = (2..6).step(3);
let it = vec![a, b, c].into_iter().kmerge();
itertools::assert_equal(it, vec![0, 1, 2, 3, 4, 5]);

When you just want to create an ad hoc iterator of values, is there anything better? Maybe something like iter![a,b,c] (if iter is taken, then some other word). If this is possible, I've got to believe that someone's already done it, but I can't find anything.

英文:

The expression vec![a, b, c].into_iter() seems unnecessarily long.Is there something better?

The doc for itertools kmerge gives this example:

use itertools::Itertools;

let a = (0..6).step(3);
let b = (1..6).step(3);
let c = (2..6).step(3);
let it = vec![a, b, c].into_iter().kmerge();
itertools::assert_equal(it, vec![0, 1, 2, 3, 4, 5]);

When you just want to create an ad hoc iterator of values, is there anything better?
Maybe something like iter![a,b,c] (if iter is taken, then some other word).
If this is possible, I've got to believe that someone's already done it, but I can't find anything.

答案1

得分: 4

数组与Vec同样有效,避免了堆分配。

[a, b, c].into_iter()

也许文档是在Rust 1.53之前编写的,当时这个功能已经稳定

英文:

An array works just as well as a Vec and avoids the heap allocation.

[a, b, c].into_iter()

Perhaps the documentation predates Rust 1.53 when this was stabilized.

答案2

得分: 0

感谢您的所有帮助!将所有内容放在一起,我们可以使[a, b, c].kmerge()正常工作:

    use itertools::Itertools;

    let a = (0..6).step_by(3);
    let b = (1..6).step_by(3);
    let c = (2..6).step_by(3);
    let it = [a, b, c].kmerge();
    itertools::assert_equal(it, [0, 1, 2, 3, 4, 5]);

额外的步骤是扩展所有IntoIterator的:

use itertools::kmerge;
use itertools::KMerge;

impl<I: IntoIterator> IntoItertoolsPlus for I {}
pub trait IntoItertoolsPlus: IntoIterator {
    fn kmerge(self) -> KMerge<<Self::Item as IntoIterator>::IntoIter>
    where
        Self: IntoIterator + Sized,
        Self::Item: IntoIterator,
        <<Self as IntoIterator>::Item as IntoIterator>::Item: PartialOrd,
    {
        kmerge(self)
    }
}

我将在我的两个函数中使用这个方法。它们都接受多个相同类型的输入变量。

英文:

Thanks for all the help! Putting it all together, we can make [a, b, c].kmerge() work:

    use itertools::Itertools;

    let a = (0..6).step_by(3);
    let b = (1..6).step_by(3);
    let c = (2..6).step_by(3);
    let it = [a, b, c].kmerge();
    itertools::assert_equal(it, [0, 1, 2, 3, 4, 5]);

The extra step is to extend all IntoIterator's:

use itertools::kmerge;
use itertools::KMerge;

impl&lt;I: IntoIterator&gt; IntoItertoolsPlus for I {}
pub trait IntoItertoolsPlus: IntoIterator {
    fn kmerge(self) -&gt; KMerge&lt;&lt;Self::Item as IntoIterator&gt;::IntoIter&gt;
    where
        Self: IntoIterator + Sized,
        Self::Item: IntoIterator,
        &lt;&lt;Self as IntoIterator&gt;::Item as IntoIterator&gt;::Item: PartialOrd,
    {
        kmerge(self)
    }
}

I'm going to use this method for two of my own functions. They both accept a variable number of same-type inputs.

huangapple
  • 本文由 发表于 2023年2月16日 09:25:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75466969.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定