itertools & rand
itertools extends the iterator ecosystem with combinators std lacks. rand provides reproducible, testable randomness for simulations, sampling, and IDs.
Busca en todas las páginas de la documentación
itertools extends the iterator ecosystem with combinators std lacks. rand provides reproducible, testable randomness for simulations, sampling, and IDs.
itertools = "0.14"
rand = "0.9"use itertools::Itertools;
fn main() {
let v = vec![1, 2, 3, 4];
let pairs: Vec<_> = v.iter().tuple_windows().collect();
assert_eq!(pairs, vec![(&1, &2), (&2, &3), (&3, &4)]);
}use rand::Rng;
fn sample_id() -> u64 {
rand::rng().random_range(1_000_000..9_999_999)
}When to reach for this:
use itertools::iproduct;
use rand::seq::SliceRandom;
fn all_pairs(ids: &[u32]) -> Vec<(u32, u32)> {
iproduct!(ids.iter(), ids.iter())
.map(|(&a, &b)| (a, b))
.filter(|(a, b)| a < b)
.collect()
}
fn shuffled(mut items: Vec<&str>) -> Vec<&str> {
items.shuffle(&mut rand::rng());
items
}What this demonstrates:
iproduct! replaces nested loopstuple_windows for sliding viewsSliceRandom for unbiased shuffles| Adapter | Use |
|---|---|
group_by | Run-length style grouping |
chunks / batch | Fixed-size batches |
unique | Dedup in iterator pipeline |
collect_vec | Ergonomic collect (extension trait) |
use rand::SeedableRng;
use rand::rngs::StdRng;
let mut rng = StdRng::seed_from_u64(42);
let n: u32 = rng.random_range(0..100);Seed in tests for deterministic output.
thread_rng is not crypto. Fix: rand_core + ring or OS RNG for tokens.take(n) before collect.Vec or impl Iterator where possible.random_range - use library ranges, not % on integers.for_each instead of collect.| Alternative | Use When | Don't Use When |
|---|---|---|
| Manual loops | Tiny logic | Combinatorial pipelines |
uuid crate | Unique IDs | Statistical sampling |
nanoid | URL-safe IDs | Scientific RNG |
New projects use 0.9 API (rng() entry point). Align workspace on one version.
Use rayon for parallel collect; itertools stays sequential.
Shuffle input ids, then take(n) for representative samples.
Most adapters are lazy; collect allocates the result container.
Stack versions: This page was written for Rust 1.97.0 (edition 2024), Tokio 1.x, Axum 0.8, serde 1.0, sqlx 0.8, clap 4, and Polars 0.46+.
Revisado por Chris St. John·Última actualización: 16 jul 2026