Skip to content
Tutorials Center
My learning

Topics

Rust Reference ยท Iterators

Comparing iterators: cmp, eq, lt and is_sorted

Reference for comparing iterators in Rust: eq, ne, lt, le, gt, ge, cmp, partial_cmp, is_sorted and is_sorted_by_key, with lexicographic rules and examples.

Iterators can be compared with each other without collecting them first. eq checks that two iterators yield equal items in the same order and have the same length. cmp and partial_cmp compare them lexicographically (item by item, the way a dictionary orders words), and lt, le, gt, ge are the boolean shortcuts.

The two sides may be different iterator types, such as chars() and chars().rev(), as long as their items can be compared. is_sorted and friends answer a related question about a single iterator.

Rust
let same = [1, 2, 3].iter().eq([1, 2, 3].iter()); // true
let order = [1, 2].iter().cmp([1, 3].iter());      // Ordering::Less

Try it

Rust Equality and ordering

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Rust Checking sortedness

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Syntax

Rust
fn eq<I>(self, other: I) -> bool where I: IntoIterator, Self::Item: PartialEq<I::Item>;
fn ne<I>(self, other: I) -> bool where I: IntoIterator, Self::Item: PartialEq<I::Item>;
fn lt<I>(self, other: I) -> bool where I: IntoIterator, Self::Item: PartialOrd<I::Item>;
fn le<I>(self, other: I) -> bool where I: IntoIterator, Self::Item: PartialOrd<I::Item>;
fn gt<I>(self, other: I) -> bool where I: IntoIterator, Self::Item: PartialOrd<I::Item>;
fn ge<I>(self, other: I) -> bool where I: IntoIterator, Self::Item: PartialOrd<I::Item>;
fn cmp<I>(self, other: I) -> Ordering where I: IntoIterator<Item = Self::Item>, Self::Item: Ord;
fn partial_cmp<I>(self, other: I) -> Option<Ordering>
    where I: IntoIterator, Self::Item: PartialOrd<I::Item>;
fn is_sorted(self) -> bool where Self::Item: PartialOrd;
fn is_sorted_by<F>(self, compare: F) -> bool where F: FnMut(&Self::Item, &Self::Item) -> bool;
fn is_sorted_by_key<F, K>(self, f: F) -> bool where F: FnMut(Self::Item) -> K, K: PartialOrd;
Name Description
other Anything that implements IntoIterator: another iterator, a Vec, an array, a reference to either.
compare For is_sorted_by: returns true when the pair (previous, next) is in the accepted order.
f For is_sorted_by_key: derives the key that must be non-decreasing.

Returns: bool for the predicates, Ordering for cmp, Option<Ordering> for partial_cmp.

Errors / panics / edge cases: nothing panics. Iterators of different length are never equal. The comparison stops at the first difference, so infinite iterators work if they differ somewhere. partial_cmp returns None when it meets an incomparable pair such as NaN.

Usage notes

  • Lexicographic order: the first differing pair decides; if one iterator is a prefix of the other, the shorter one is smaller.
  • is_sorted means non-decreasing. Equal neighbours are fine. For strictly increasing data, use is_sorted_by(|a, b| a < b).
  • Consumption: eq and the others take self by value, so clone or recreate the iterator if you need it again. Slices compare directly with == and < and also have is_sorted(), so use those when both sides are slices.
  • Short-circuit: eq stops at the first mismatch and does not allocate, unlike comparing two collected vectors.
  • Types of items can differ as long as PartialEq or PartialOrd is implemented between them, for example comparing &i32 with i32 items does not compile, but .copied() makes it work.
  • Complexity: O(min(n, m)) time, O(1) memory.