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