Rust-logical-operators

提供:Dev Guides
移動先:案内検索

Rust-論理演算子

論理演算子は、2つ以上の条件を結合するために使用されます。 論理演算子もブール値を返します。 変数Aの値が10で、Bが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

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");
   }
}

出力

true
true
Not Elder