Questions tagged [rust-itertools]

For questions about the `itertools` Rust crate, a library for working with iterators. Add [rust] alongside it. For the Python module, see [python-itertools] instead.

Itertools is a library containing useful constructs for working with iterators. Its main attraction is the Itertools extension trait, which adds more adaptors to iterator types.

When using this tag, do not forget to add the tag as well. Only use if your question pertains specifically to the use of the itertools crate, rather than including it in all Rust questions with code depending on it.


The Python module lives in a different tag: .

22 questions
7
votes
2 answers

In Rust, what is the proper way to replicate Python's "repeat" parameter in itertools.product?

In Python, I can do: from itertools import product k = 3 for kmer in product("AGTC", repeat=k): print(kmer) In Rust, I can force the behavior of k=3 by: #[macro_use] extern crate itertools; for kmer in iproduct!("AGTC".chars(),…
Jessime Kirk
  • 654
  • 1
  • 6
  • 13
4
votes
3 answers

How to interlace an iterator with itself from the end?

I have an iterator in the form 0..=63, i.e. 0 1 2 3 4 ... 59 60 61 62 63. Its .count() is 64. How would I get the following iterator: 0 63 1 62 2 61 3 60 4 59 ... (of course independent of the items present in the iterator), preferably without…
leo848
  • 637
  • 2
  • 7
  • 24
3
votes
1 answer

Cartesian product of n ranges

I am rewriting a python program into rust and I am struggling to translate this line: itertools.product(range(0,8), repeat=n) What I am trying to achieve is something like this: https://pastebin.com/ceEA5E3q (but so that I can change the number of…
3
votes
1 answer

How to make Rust function `zip_map` take a binary function instead of a unary function with a 2-tuple as argument?

I have the following code: pub trait Iterx: Iterator { fn zip_map(self, other: U, f: F) -> Map, F> where Self: Sized, U: IntoIterator, F: Fn((::Item,
arkethos
  • 31
  • 3
2
votes
1 answer

Unexpected "method not found" compiler error

This question arises from my use (in a toy project to learn Rust) of cartesian_product from itertools together with into_par_iter from Rayon. My question is less about this particular code and more a question about reading rustc error messages, and…
J. Dane
  • 35
  • 4
2
votes
2 answers

How do I interleave two Rust vectors by chunks of threes into a new vector?

I need an idiomatic way to interlace these two vectors: let v1 = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]; let v2 = vec![7.0, 8.0, 9.0, 10.0, 11.0, 12.0]; The output I expect is: [1.0, 2.0, 3.0, 7.0, 8.0, 9.0, 4.0, 5.0, 6.0, 10.0, 11.0, 12.0]; I used…
Ecumene
  • 87
  • 6
2
votes
1 answer

How do I interlace two Rust vectors into a new vector?

I need an idiomatic way to interlace these two vectors: v1 = vec![1.0, 2.0, 3.0]; v2 = vec![4.0, 5.0, 6.0]; The output I expect is: v3 is [1.0, 4.0, 2.0, 5.0, 3.0, 6.0]; I attempted using itertool's interlace function, but I can't get the…
Ecumene
  • 87
  • 6
2
votes
1 answer

Can I use itertools::PutBack::put_back in a loop?

My use case: Loop over string React to each character in a lexical analyzer state machine On seeing some characters, realize that the previous character was the end of its token finish up the token transition back to the Empty state (meaning no…
Paul Chernoch
  • 5,275
  • 3
  • 52
  • 73
1
vote
4 answers

How can I intersperse a rust iterator with a value every n items?

I have an iterator of characters, and I want to add a newline every N characters: let iter = "abcdefghijklmnopqrstuvwxyz".chars(); let iter_with_newlines = todo!(); let string: String =…
asdf3.14159
  • 694
  • 3
  • 19
0
votes
0 answers

Mutably iterate over all permutations of Vec

I need to mutably iterate over the Vec model.circles, so I get all permutations of two elements. However, I can't get it to compile: for ij in (0..model.circles.len()).permutations(2) { let i = ij[0]; let j = ij[1]; let circle = &mut…
Antosser
  • 346
  • 1
  • 9
0
votes
1 answer

Does rust have a trait for a collection with `iter()` method or a resettable iterator?

It is useful for functions to accept a dyn IntoIterator object, which allows you to use into_iter(), but that requires taking ownership of the collection. It seems like Rust lacks a trait for the iter() method. Does something like the…
0
votes
1 answer

Iterate over two different iterable types with the same item types in a single loop in Rust

In a Rust function, I have two different collection types that both contain strings. I need to iterate over one of them with a loop, but the iterator types are different. How would I bring both iterators to a common denominator? use…
Yuri Astrakhan
  • 8,808
  • 6
  • 63
  • 97
0
votes
1 answer

What is the best way to "rename" a Rust `std::iter::Iterator` function for my own library?

I am trying to rename the std::iter::Iterator::scan function in a Itertools like library of my own. // A renaming of https://doc.rust-lang.org/src/core/iter/traits/iterator.rs.html#1420 fn scan_while(self, initial_state: St, f: F)…
arkethos
  • 31
  • 3
0
votes
1 answer

Mutably iterate through an iterator using Itertools' tuple_windows

I'm attempting to store a series of entries inside a Vec. Later I need to reprocess through the Vec to fill in some information in each entry about the next entry. The minimal example would be something like this: struct Entry { curr: i32, …
shouya
  • 2,863
  • 1
  • 24
  • 45
0
votes
1 answer

Calling variadic function with iterator in Rust

A variadic function takes a variable number of arguments of same type. Macro function !product from Itertools package are one of those and I'd like to compute it with a vector of ranges. Example usage of !product is following where each argument is…
Rikard Olsson
  • 841
  • 1
  • 7
  • 28
1
2