Rust Iterator Cheat Sheet

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.


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
Std repeat a {a,a,}
IT RepeatCall::new f:()T {f(),f(),}

Sequences

Lib Method Input Output
IT batching {a0,,ai,,}:{T}, batch:&mut {T}U {c0batch({a0,}),c1batch({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)}
Std chain {a0,,an1}, {b0,,bm1} {a0,,an1,b0,,bm1}
IT coalesce {a0,a1,a2,} f:(T,T)Ok(T) | Err((T,T)) oror{x0,}{a0,x1,}{a0,a′′1,}wh.wh.wh.{f(a0,a1)=Ok(x0),f(a0,a1)=Err((a0,a1)),f(a1,a2)=Ok(x1),f(a0,a1)=Err((a0,a1)),f(a1,a2)=Err(a′′1,a2),
Std collect {a0,} [a0,]
IT collect_vec {a0,} [a0,]:VecT
IT combinations {a0,a1,a2,,an2,an1} {(a0,a1),(a0,a2),,(a0,an2),(a0,an1),  (a1,a2),,(a1,an2),(a1,an1),  ,  (an2,an1)}
Std cycle {a0,,an1} {a0,,an1,a0,,an1,}
IT dedup {a0,a0,,a1,a1,,} {a0,a1,}
IT dropping {a0,,ai1consumed,ai,} i wh. i<n {ai,} (consumes a0ai1 immediately)
{a0,,an1consumed} i wh. in {} (consumes a0an1 immediately)
IT dropping_back {a0,,ani1,ani,,an1consumed} i wh. i<n {a0,,ani1} (consumes anian immediately)
{a0,,an1consumed} i wh. in {} (consumes a0an immediately)
Std enumerate {a0,a1,} {(0,a0),(1,a1),}
Std filter {a0,a1,}, p:&Tbool {ai for all i wh. p(&ai)}
Std filter_map {a0,a1,}, f:TSome(U) | None {xi for all i where f(ai)=Some(xi)}
Std flat_map {a0,a1,}, f:T{U} {x0,x1,x=f(a0),y0,y1,y=f(a1),}
IT group_by {a0,,ai1g(ax)=g0,ai,,aj1g(ay)=g1g0,aj,,ak1g(az)=g2g1,}, g:&TG {(g0,[a0,,ai1]),  (g1,[ai,,aj1]),  (g2,[aj,,ak1]),}
IT group_by_lazy {a0,,ai1g(ax)=g0,ai,,aj1g(ay)=g1g0,aj,,ak1g(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}
Std map {a0,a1,}, f:TU {f(a0),f(a1),}
IT map_fn {a0,a1,}, f:fn(T)U {f(a0),f(a1),}
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
Std partition {a0,a1,} f:&Tbool ([ai for all i wh. f(&ai)=true],  [ai for all i wh. f(&ai)=false])
Std rev {a0,a1,,an2,an1} {an1,an2,,a1,a0}
Std scan {a0,a1,}, s:&mut S, f:(&mut S,T)Some(U) | None {xi for all i wh. f(s,ai)=Some(xi)}
Std skip {a0,,ai1,ai,}, i {ai,}
{a0,,an1}, i wh. in {}
Std skip_while {a0,,ai,}, p:&Tbool {ai,} wh. i is first wh. p(&ai)
IT slice {a0,,ai,,aj,}, ..j {a0,,ai,,aj1}
i.. {ai,,aj1,}
{a0,,ai,,aj,}, i..j {ai,,aj1}
IT step {a0,,am,,a2m,}, m {a0,am,a2m,}
Std take {a0,,ai1,ai,}, i {a0,,ai1}
{a0,,an1}, i wh. in {a0,,an1}
Std take_while {a0,,ai1,ai,}, p:&Tbool {a0,,ai1} wh. i is first wh. p(&ai)=false
IT take_while_ref &mut {a0,,ai1,ai,}, p:&Tbool {a0,,ai1} wh. i is first wh. p(&ai)=false
IT tee {a0,a1,} ({a0,a1,},{a0,a1,})
Std unzip {(a0,b0),(a1,b1),} ([a0,a1,],[b0,b1,])
IT while_some {a0,,ai1,ai,}, {xj for all j<i wh. aj=Some(xj)} wh. i is the first wh. ai=None
Std zip {a0,,ai}, {b0,,bi,bi+1,} {(a0,b0),,(ai,bi)}
{a0,,ai,ai+1,}, {b0,,bi}
IT zip_longest {a0,,ai1,ai,} {b0,,bi1} {Both(a0,b0),,Both(ai1,bi1),Left(ai),}
{a0,,ai1} {b0,,bi1} {Both(a0,b0),,Both(ai1,bi1)}
{a0,,ai1} {b0,,bi1,bi,} {Both(a0,b0),,Both(ai1,bi1),Right(bi),}

Values

Lib Method Input Output
Std all &mut {a0,,ai1,ai,} p:Tbool p(a0)p(ai1) wh. i is first wh. p(ai)=false
&mut {} true
Std any &mut {a0,,ai1,ai,} p:Tbool p(a0)p(ai1) wh. i is first wh. p(ai)
&mut {} false
Std count {n} n
IT dropn &mut {a0,,ai1consumed,ai,} i wh. in i
&mut {a0,,an1consumed} i wh. i>n n
Std find &mut {a0,,ai,}, p:Tbool Some(ai)Nonewh. i is first wh. p(&ai)otherwise
IT find_position &mut {a0,,ai,}, p:Tbool Some((i,ai))Nonewh. i is first wh. p(&ai)otherwise
Std fold {a0,a1,}, init:U, :(U,T)U (((inita0)a1))
{} init
IT fold1 {a0,a1,a2,}, :(T,T)T Some(((a0a1)a2))
{a0} Some(a0)
{} None
IT fold_results &mut {a0,}, init:U, :(U,T)U Ok(fold(x,)) wh. x={xi wh. all ai=Ok(xi)}
&mut {,ai,} wh. ai=Err(..), Err(e) wh. i is first wh. ai=Err(e)
&mut {} Ok(init)
Std last {,an1} Some(an1)
{} None
IT join {a0,a1,an1}, sep:&str d(a0)sepd(a1)sepd(an1) wh.  d is Display::fmt,  is string concatenation
Std max {a0,a1,a2,} Some(max(max(max(a0,a1),a2),))
{a0} Some(a0)
{} None
Std min {a0,a1,a2,} Some(min(min(min(a0,a1),a2),))
{a0} Some(a0)
{} None