Binary Expressions¶
Binary Expressions¶
Binary expressions are expressions that operate on two operands. Each operator has an associated std::ops::XXX
class,
where the Rhs
and Out
types are specialized for the left-hand-side type. For example,
std::ops::add::Add[Rhs=std::string::Str, Out=std::string::Str
] is implemented for the std::string::Str
type:
sup std::Str ext std::ops::Add[Rhs=std::Str, Out=std::Str] {
fun add(self, rhs: std::Str) -> std::Str {
...
}
}
The only special binary operator that doesn’t have an associated std::ops::XXX
class is the is
operator. The
right-hand-side isn’t an expression, but a local variable declaration pattern. For example,
case optional_type is std::option::Some(value) { ... }
.
Operator Precedence¶
The following table shows the precedence of the binary operators, from highest to lowest:
Operator token |
Operation |
Associated type and method |
Precedence |
---|---|---|---|
|
Bitwise OR Assignment |
|
0 |
|
Bitwise XOR Assignment |
|
0 |
|
Bitwise AND Assignment |
|
0 |
|
Shift left Assignment |
|
0 |
|
Shift right Assignment |
|
0 |
|
Addition Assignment |
|
0 |
|
Subtraction Assignment |
|
0 |
|
Multiplication Assignment |
|
0 |
|
Division Assignment |
|
0 |
|
Remainder Assignment |
|
0 |
|
Power Assignment |
|
0 |
|
Logical OR |
|
1 |
|
Logical AND |
|
2 |
|
Type check |
|
3 |
|
Equality |
|
4 |
|
Inequality |
|
4 |
|
Less than |
|
4 |
|
Greater than |
|
4 |
|
Less equal |
|
4 |
|
Greater equal |
|
4 |
|
Bitwise OR |
|
5 |
|
Bitwise XOR |
|
6 |
|
Bitwise AND |
|
7 |
|
Shift left |
|
8 |
|
Shift right |
|
8 |
|
Addition |
|
9 |
|
Subtraction |
|
9 |
|
Multiplication |
|
10 |
|
Division |
|
10 |
|
Remainder |
|
10 |
|
Power |
|
10 |