Rust Comparison Operators

Comparison operators in Rust

In Rust, comparison operators are used to compare the value of two operands and output a Boolean value.

Comparison operators include greater than, lesser than, equal to, greater than or equal to, lesser than or equal to, and not equal to. These operators provide a good amount of flexibility when working with compatible types.

Comparison operators function differently depending on the type of the operands. The most common use case is using comparison operators with numerical types. However they can also be used with Boolean types and function as a kind of logic gate.

Table of Comparison Operators in Rust

OperatorOperationSyntaxFunction
>Greater thana > bTrue if a is greater than b
<Lesser thana < bTrue if a is less than b
==Equal toa == bTrue if a is equal to b
>=Greater than or equal toa >= bTrue if a is greater than or equal to b
<=Lesser than or equal toa <= bTrue if a is less than or equal to b
!=Not equal toa != bTrue if a is not equal to b

Using Comparison Operators

Comparison operators in Rust use a simple, intuitive syntax.

The following code example shows how each comparison operator can be used:

let a = 5;
let b = 10;

println!("a > b:{}", a > b); // false
println!("a >= b:{}", a >= b); // false
println!("a < b:{}", a < b); // true
println!("a <= b:{}", a <= b); // true
println!("a == b:{}", a == b); // false
println!("a != b:{}", a != b); // true
Evaluation of comparison operators in Rust

Using Comparison Operators with Boolean Operands

In Rust, comparison operators can be used with Boolean operands to perform logical functions. As a result, they can be used to extend the functionality provided by logical operators.

When used with Boolean operands, each comparison operator will function by regarding a Boolean value of true as the equivalent numerical value of 1. A Boolean value of false has an equivalent numerical value of 0:

Boolean ValueNumerical Value
True1
False0

The following image shows how comparison operators function with Boolean operands in Rust:

Comparison operators with Boolean operands in Rust

We can construct Truth tables to see how each operator would function as a logic gate.

Greater Than Operator

The greater than operator is true only if A is true and B is false.

Operand AOperand BA > B
TrueTrueFalse
TrueFalseTrue
FalseTrueFalse
FalseFalseFalse

Lesser Than Operator

The lesser than operator is true only if A is false and B is true.

Operand AOperand BA < B
TrueTrueFalse
TrueFalseFalse
FalseTrueTrue
FalseFalseFalse

Equal To Operator

The lesser than operator is true if A and B are either both true or false.

Operand AOperand BA == B
TrueTrueTrue
TrueFalseFalse
FalseTrueFalse
FalseFalseTrue

Greater Than or Equal To Operator

The greater than or equal to operator is false only if A is false and B is true.

Operand AOperand BA >= B
TrueTrueTrue
TrueFalseTrue
FalseTrueFalse
FalseFalseTrue

Lesser Than or Equal To Operator

The greater than or equal to operator is false only if A is true and B is false.

Operand AOperand BA <= B
TrueTrueTrue
TrueFalseFalse
FalseTrueTrue
FalseFalseTrue

Not Equal To Operator

The not equal to operator is true if A is not equal to B.

Operand AOperand BA <= B
TrueTrueFalse
TrueFalseTrue
FalseTrueTrue
FalseFalseFalse