Trait in prelude::ops auto-imported

Eq

trait Eq<T>
Source: compiler/src/builtin_docs/prelude/ops.hy

The equality operators == and !=.

a == b calls eq(a, b) and a != b calls ne(a, b) on a type with an Eq instance. The primitives have builtin instances (strings compare by content). Arrays, Vecs, tuples, records and enums compare structurally, and an element with an Eq instance (a class’s too) compares with it. Classes without an instance compare by identity. #[derive(Eq)] generates a field-by-field instance.

Required methods#

NameDescription
eqReturns true when a and b are equal.
neReturns true when a and b are not equal.

Examples#

use tests::assert;
class Money {
    pub cents: int,
}

impl Eq for Money {
    pub fn eq(Money a, Money b) -> bool {
        return a.cents == b.cents;
    }
    pub fn ne(Money a, Money b) -> bool {
        return a.cents != b.cents;
    }
}

fn main() {
    assert!(new Money(100) == new Money(100));
    assert!(new Money(1) != new Money(2));
}

See also#

  • Vec
  • Add: The addition operator +.
  • Sub: The subtraction operator -.
  • Mul: The multiplication operator *.
  • Div: The division operator /.
  • Neg: The unary negation operator -x.
  • Rem: The remainder operator %.
  • Pow: The exponentiation operator **.
↑ ↓ navigate ↵ open