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.
if age >= 18 && name != "guest" { /* ... */ }
Try it
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.
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
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:
Eqmarks that equality is reflexive (a == aalways), so floats arePartialEqbut notEq;Ordis a total order,PartialOrdmay say "unordered" withNone. - NaN is unordered: every comparison with NaN except
!=is false. Usetotal_cmpto sort floats, orf64::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
PartialOrdneedsPartialEqtoo. - References compare their contents.
&a == &bcompares the values; usestd::ptr::eqfor 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.
OptionandResult:Noneis less than anySome; forResult,Okis less thanErr.- Mixed types: implement
PartialEq<Rhs>to compare, asMetersandf64do above.String == strandVec<T> == [T; N]are already provided. - Sorting and keys:
sort,BTreeMap,maxandminneedOrd;HashMapkeys needEq + Hash. A customOrdmust agree withEq.