Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Generate Combinations Using Itertools in Rust

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

Tips and Traps

IterTools.combinations returns an iterator adaptor that iterates over the k-length combinations of the elements from an iterator. The iterator element type is Vec<Self::Item>. This means that

  1. Element of the original iterator need to be clonable.

  2. For copy/clone-expensive types, it is better to use collect elements into a vector and generate indexes instead.

Combinations { indices: [0, 1, 2], pool: LazyBuffer { it: 4..5, done: false, buffer: [1, 2, 3] }, first: true }
[[1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]]
[[1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]]

Note: Combinations does not take into account the equality of the iterated values!

[[1, 2], [1, 2], [2, 2]]
[[4, 3, 2], [4, 3, 1], [4, 2, 1], [3, 2, 1]]