Module auto-imported

prelude::ops

Every operator is a trait method: a + b is Add, a == b is Eq, a < b is Lt, and format("%v", x) uses Show. The primitive types implement the ones that make sense for them. Implement a trait for your own class or enum to give it the operator:

class Money {
    pub cents: int,
}

impl Add for Money {
    pub fn add(Money a, Money b) -> Money {
        return new Money(a.cents + b.cents);
    }
}

Num and Integral bundle the arithmetic traits so generic code can ask for “any number” (fn sum<T: Num>(…)). #[derive(Eq, Ord, Show)] writes the common instances for you; see Macros. The rules for each operator are in Operators.

Built into the compiler.

Traits#

NameDescription
AddThe addition operator +.
SubThe subtraction operator -.
MulThe multiplication operator *.
DivThe division operator /.
NegThe unary negation operator -x.
RemThe remainder operator %.
PowThe exponentiation operator **.
NumThe numeric bundle: Add, Sub, Mul, Div, Neg, Rem and Pow.
ShlThe left shift operator <<.
ShrThe right shift operator >>.
BitAndThe bitwise AND operator &.
BitOrThe bitwise OR operator |.
BitXorThe bitwise XOR operator ^.
BitNotThe bitwise NOT operator ~x.
IntegralThe integer bundle: Num plus Shl, Shr, BitAnd, BitOr, BitXor and BitNot.
EqThe equality operators == and !=.
OrdThe ordering bundle: Lt, Le, Gt and Ge, with Eq.
LtThe less-than operator <.
LeThe less-or-equal operator <=.
GtThe greater-than operator >.
GeThe greater-or-equal operator >=.
ShowConversion to a display string, used by the %v format specifier.
LengthA size query: x.len().
IntoConversion from S into T, written impl Into<T> for S.
↑ ↓ navigate ↵ open