Rust - Logical Operators



Logical Operators are used to combine two or more conditions. Logical operators too, return a Boolean value. Assume the value of variable A is 10 and B is 20.

Sr.No Operator Description Example
1 && (And) The operator returns true only if all the expressions specified return true (A > 10 && B > 10) is False
2 ||(OR) The operator returns true if at least one of the expressions specified return true (A > 10 || B >10) is True
3 ! (NOT) The operator returns the inverse of the expression’s result. For E.g.: !(>5) returns false !(A >10 ) is True

Illustration

fn main() {
   let a = 20;
   let b = 30;
   
   if (a > 10) && (b > 10) {
      println!("true");
   }
   let c = 0;
   let d = 30;
   
   if (c>10) || (d>10){
      println!("true");
   }
   let is_elder = false;
   
   if !is_elder {
      println!("Not Elder");
   }
}

Output

true
true
Not Elder
rust_operators.htm
Advertisements