Rust - 关系运算符



关系运算符用来测试或定义两个实体之间的关系类型。关系运算符用来比较两个或更多个值。关系运算符会返回一个布尔值 - 真或假。

假设 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
广告