Rust - Relational Operators



Relational Operators test or define the kind of relationship between two entities. Relational operators are used to compare two or more values. Relational operators return a Boolean value − true or false.

Assume the value of A is 10 and B is 20.

Sr.No Operator Description Example
1 > Greater than (A > B) is False
2 < Lesser than (A < B) is True
3 >= Greater than or equal to (A >= B) is False
4 <= Lesser than or equal to (A <= B) is True
5 == Equality (A == B) is fals
6 != Not equal (A != B) is True

Illustration

fn main() {
   let A:i32 = 10;
   let B:i32 = 20;

   println!("Value of A:{} ",A);
   println!("Value of B : {} ",B);

   let mut res = A>B ;
   println!("A greater than B: {} ",res);

   res = A<B ;
   println!("A lesser than B: {} ",res) ;

   res = A>=B ;
   println!("A greater than or equal to B: {} ",res);

   res = A<=B;
   println!("A lesser than or equal to B: {}",res) ;

   res = A==B ;
   println!("A is equal to B: {}",res) ;

   res = A!=B ;
   println!("A is not equal to B: {} ",res);
}

Output

Value of A:10
Value of B : 20
A greater than B: false
A lesser than B: true
A greater than or equal to B: false
A lesser than or equal to B: true
A is equal to B: false
A is not equal to B: true
rust_operators.htm
Advertisements