Rust - 关系运算符


关系运算符测试或定义两个实体之间的关系类型。关系运算符用于比较两个或多个值。关系运算符返回一个布尔值 - true 或 false。

假设A的值为10,B的值为20。

先生编号 操作员 描述 例子
1 > 比...更棒 (A > B) 为假
2 < 小于 (A < B) 为真
3 >= 大于或等于 (A >= B) 为假
4 <= 小于或等于 (A <= B) 为真
5 == 平等 (A == B) 为假
6 != 不等于 (A != B) 为真

插图

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

输出

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