Skip to content
Tutorials Center
My learning

Topics

Rust Reference ยท Operators and Expressions

Rust comparison operators

Reference for Rust comparison operators ==, !=, <, >, <= and >=: PartialEq and PartialOrd, NaN, derived ordering, reference comparison and Ordering.

Rust's six comparison operators return bool. == and != use the PartialEq trait; <, >, <= and >= use PartialOrd. They are implemented for numbers, bool, char, strings, slices, arrays, tuples, Option, Result, references to comparable things, and any type that derives or implements the traits.

The operands must have compatible types: 1_i32 == 1_i64 does not compile. Comparison operators take their operands by reference automatically, so comparing never moves a value. Comparisons do not chain: a < b < c is a syntax error, so write a < b && b < c.

Rust
if age >= 18 && name != "guest" { /* ... */ }

Try it

Rust Comparing built-in and compound values

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 Derived and custom comparison

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
a == b    a != b
a < b     a > b     a <= b     a >= b
a.cmp(&b)            // Ordering (Ord)
a.partial_cmp(&b)    // Option<Ordering> (PartialOrd)
Operator Trait method Description
== PartialEq::eq(&a, &b) Equality.
!= PartialEq::ne(&a, &b) Inequality; defaults to !eq.
<, <=, >, >= PartialOrd::lt, le, gt, ge Ordering based on partial_cmp.

Errors: comparing different types without a PartialEq<Other> impl gives E0308 or E0277; comparing a type that does not derive the traits (for example a struct without PartialEq) gives E0369. Comparing references to different levels (&&i32 == &i32) needs dereferencing.

Usage notes

  • Equivalence traits: Eq marks that equality is reflexive (a == a always), so floats are PartialEq but not Eq; Ord is a total order, PartialOrd may say "unordered" with None.
  • NaN is unordered: every comparison with NaN except != is false. Use total_cmp to sort floats, or f64::max/min, which ignore NaN.
  • Derived order is lexicographic. For structs it compares fields in declaration order; for enums it compares by variant order first, then by payload. The derive of PartialOrd needs PartialEq too.
  • References compare their contents. &a == &b compares the values; use std::ptr::eq for identity.
  • Strings compare byte by byte, which means code point order for UTF-8, not dictionary order for any language. Use a collation crate for locale-aware sorting.
  • Option and Result: None is less than any Some; for Result, Ok is less than Err.
  • Mixed types: implement PartialEq<Rhs> to compare, as Meters and f64 do above. String == str and Vec<T> == [T; N] are already provided.
  • Sorting and keys: sort, BTreeMap, max and min need Ord; HashMap keys need Eq + Hash. A custom Ord must agree with Eq.