Rust Iterator Cheat Sheet

Last updated: Rust 1.17.0, Itertools 0.6.0.

To-do:

How you can help:

I need feedback on what does and doesn't work. Can you understand the summaries? Do they mesh with how you perceive the methods in question work? What other information could/should be included?

If you have any corrections or suggestions, you can file them on the GitHub repository. The canonical source for this page is src/itercheat/itercheat.html.


Jump to: Generators, Sequences, Free-Standing Sequences, Values, Other, Notes.


This summarises the iterator-related methods defined by the following Rust libraries:

Additional filtering options:

This is intended to be used in one of a few ways:

  1. You know what an iterator method is called, but you want a quick summary of what it does.
  2. You know what you want to do with an iterator, but not what it's called.
  3. You just want to browse through a summary of the available methods because you're bored.

To keep the summary as compact as possible, various bits of notation and convention are used. An incomplete list is:

Generators

Lib Function Input Output
IT iterate x:T f:&TT {x,f(x),f(f(x)),}
IT repeat_call f:()T {f(),f(),}
IT unfold s:S, f:&mut SSome(T) | None {c0,,cl1} wh.  Some(c0)=f(&mut s),  ,  Some(cl1)=f(&mut s),  None=f(&mut s)

Sequences

Lib Method Input Output
IT batching {a0,,ai,,}:{T}, batch:&mut {T}U {c0batch({a0,}),c1batch({ai,}),}:{U}
IT cartesian_product {a0,a1,,an1}, {b0,b1,,bm1} {(a0,b0),(a0,b1),,(a0,bm1),  (a1,b0),(a1,b1),,(a1,bm1)  ,  (an1,b0),(an1,b1),,(an1,bm1)}
IT chunks {a0,}, m {{a0,,am1},{an,,a2m1},}
IT coalesce {a0,a1,a2,} f:(T,T)Ok(T) | Err((T,T)) {x0,}wh.{f(a0,a1)=Ok(x0),or{a0,x1,}wh.{f(a0,a1)=Err((a0,a1)),f(a1,a2)=Ok(x1),or{a0,a1,}wh.{f(a0,a1)=Err((a0,a1)),f(a1,a2)=Err(a1,a2),
IT collect_vec {a0,} [a0,]:VecT
IT dedup {a0,a0,,a1,a1,,} {a0,a1,}
IT dropping {a0,,ai1consumed,ai,} i wh. i<n {ai,} (consumes a0ai1 immediately)
{a0,,an1consumed} i wh. in {} (consumes a0an1 immediately)
IT dropping_back {a0,,ani1,ani,,an1consumed} i wh. i<n {a0,,ani1} (consumes anian immediately)
{a0,,an1consumed} i wh. in {} (consumes a0an immediately)
IT flatten {{a0,},{b0,},}, {a0,,b0,}
IT group_by {a0,,ai1g(ax)=g0,ai,,aj1g(ay)=g1g0,aj,,ak1g(az)=g2g1,}, g:&TG {(g0,[a0,,ai1]),  (g1,[ai,,aj1]),  (g2,[aj,,ak1]),}
IT group_by_lazy {a0,,ai1g(ax)=g0,ai,,aj1g(ay)=g1g0,aj,,ak1g(az)=g2g1,}, g:&TG {(g0,{a0,,ai1}),  (g1,{ai,,aj1}),  (g2,{aj,,ak1}),}
IT interleave {a0,a1,,ai1,ai,}, {b0,b1,,bi1} {a0,b0,a1,b1,,ai1,bi1,ai,}
{a0,a1,,ai1}, {b0,b1,,bi1} {a0,b0,a1,b1,,ai1,bi1}
{a0,a1,,ai1}, {b0,b1,,bi1,bi,} {a0,b0,a1,b1,,ai1,bi1,bi,}
IT interleave_shortest {a0,a1,,ai1,ai,_}, {b0,b1,,bi1} {a0,b0,a1,b1,,ai1,bi1,ai}
{a0,a1,,ai1} {b0,b1,,bi1,bi,_} {a0,b0,a1,b1,,ai1,bi1}
IT intersperse {a0,a1,,an1} b {a0,b,a1,b,,b,an1}
IT kmerge {{a0,},{b0,},} wh.  sorted(a), sorted(b), {c0,c1,} wh. c0c1,  ac,bc,
IT kmerge_by {{a0,},{b0,},} wh.  sorted_by(a,), sorted_by(b,),, :(&T,&T)bool {c0,c1,} wh. c0c1,  ac,bc,
IT map_fn {a0,a1,}, f:fn(T)U {f(a0),f(a1),}
IT map_results {a0,}:{Ok(T) | Err(E)}, f:(T)U {a0.map(f),}:{Ok(U) | Err(E)}
IT mend_slices {&[x0,,xi1],&[xi,,xn1],  &[y0,,ym1],  &[z0,,zl1],} {&[x0,,xi1,xi,,xn1],  &[y0,,ym1],  &[z0,,zl1],}
IT merge {a0,a1,} wh. a0a1, {b0,b1,} wh. b0b1 {c0,c1,} wh. c0c1,ac,bc
IT merge_by {a0,a1,} wh. a0a1, {b0,b1,} wh. b0b1, :(&T,&T)Ordering {c0,c1,} wh. c0c1,ac,bc
IT pad_using {a0,,an1}, m,    f:iT {a0,,an1} wh. n>=m{a0,,an1,f(n),,f(m1)} otherwise
IT partition_map {a0,a1,}, f:TLeft(L) | Right(R) ([xi for all i wh. f(ai)=Left(xi)],  [xi for all i wh. f(ai)=Right(xi)])