Derives: Comparison (reference)¶
This page specifies Eq, PartialEq, Ord, PartialOrd and Hash, and the types a set element or dict key may have. The derive catalog and the automatic derives are in Derives and traits.
A comparison operator is available on a type through a derive below, an adopted trait, or the operator's own dunder. An operator the type does not provide is refused (INCAN-T0001), and so is a derive on a type that does not meet its Requires entry.
Eq¶
- Provides:
==and!=. A type that implementsEqandHashcan be asetelement ordictkey. - Provided by:
@derive(Eq)or@derive(Ord);EqorOrdin@rust.derive(...); adoptingstd.derives.comparison.Eqand defining__eq__; adoptingstd.derives.comparison.Ordand defining__eq__and__lt__. - Behavior: two values are equal when every field is equal.
- Dunder:
__eq__(self, other: Self) -> booldefines==, and__ne__(self, other: Self) -> booldefines!=. AdoptingEqsupplies__ne__asnot self.__eq__(other). - Requires: every field type implements
Eq. A type does not both define__eq__and deriveEq(INCAN-T0001).
PartialEq¶
- Provides:
==and!=. - Provided by:
@derive(PartialEq);PartialEqin@rust.derive(...);EqandOrd, which imply it; a__eq__method; everyenumwhose payload types are numbers,bool,strorbytes, or alist,set,dict,Option,Resultor tuple of those. - Behavior: two values are equal when every field is equal.
- Dunder: as for
Eq. - Requires: every field type implements
PartialEq.
Ord¶
- Provides:
<,<=,>and>=,sorted(values)of alistof the type, and whatEqprovides. - Provided by:
@derive(Ord);Ordin@rust.derive(...); adoptingstd.derives.comparison.Ordand defining__eq__and__lt__. - Behavior: values compare field by field, in declaration order.
- Dunder:
__lt__,__le__,__gt__and__ge__, each(self, other: Self) -> bool, define<,<=,>and>=. AdoptingOrdsupplies__le__,__gt__and__ge__from__lt__and__eq__. - Requires: every field type implements
Ord. A type does not both define__lt__and deriveOrd(INCAN-T0001). - Refused:
sorted(values)over alistwhose element type does not implementOrd(INCAN-T0001).
PartialOrd¶
- Provides:
<,<=,>and>=. - Provided by:
@derive(PartialEq, PartialOrd);PartialOrdin@rust.derive(...);Ord, which implies it. - Behavior: values compare field by field, in declaration order.
- Dunder: as for
Ord. - Requires:
PartialEq, through a derive or__eq__; every field type implementsPartialOrd.
Hash¶
- Provides: with
Eq, use as asetelement ordictkey. - Provided by:
@derive(Hash);Hashin@rust.derive(...). - Behavior: hashes every field. Two values equal under a derived
Eqhave equal hashes. - Dunder: none. A method named
__hash__is an ordinary method, and a set or dict does not call it. - Requires: every field type implements
Hash. - Refused:
value.__hash__()on a value of a type parameter through itsHashbound (INCAN-T0001).
Set elements and dict keys¶
- The element type
Tof aset[T], and the key typeKof adict[K, V]orHashMap[K, V], implementEqandHash. A set or dict whose element or key type does not is refused (INCAN-T0114), whether it is written as a type or built by a literal, a comprehension orset(source). - A generic function or method that uses a value of its type parameter as a set element or dict key requires
EqandHashof that type parameter's type argument; a type argument without them is refused (INCAN-T0114). See Operations on a type parameter. - A
FrozenSetelement and aFrozenDictkey carry no requirement.
| Type | Eq |
Hash |
|---|---|---|
int, bool, str, bytes, decimal, the exact-width integers, FrozenStr, FrozenBytes |
Yes | Yes |
float, f32, f64 |
No | No |
A tuple, list[T], Option[T] or Result[T, E] |
When its type arguments do | When its type arguments do |
set[T], dict[K, V], FrozenList[T], FrozenSet[T], FrozenDict[K, V] |
When its type arguments do | No |
A model, class, enum or newtype |
When it provides Eq (see Eq) |
With @derive(Hash), or Hash in @rust.derive(...) |
A generic model, class, enum or newtype implements Eq or Hash for an instantiation whose type arguments implement it too.
from std.derives.comparison import Eq
enum Tag:
A
B
@derive(Eq, Hash)
enum Label:
A
B
@derive(Hash)
model UserKey with Eq:
id: int
def __eq__(self, other: UserKey) -> bool:
return self.id == other.id
def unique[T](items: list[T]) -> set[T]:
return set(items)
def main() -> None:
labels: set[Label] = {Label.A} # accepted
keys: set[UserKey] = {UserKey(id=1)} # accepted
tags: set[Tag] = {Tag.A} # refused: Tag does not implement Eq and Hash (INCAN-T0114)
seen = unique([Label.A]) # accepted
kinds = unique([Tag.A]) # refused: unique uses T as a set element (INCAN-T0114)