操作员定义一些将对数据执行的函数。运算符所作用的数据称为操作数。
Rust的主要操作符可以分类为-
假设变量a =10 和b=5。
Sr.No | 运算符 | 描述 | 示例 |
---|---|---|---|
1 | +(加法) | 返回操作数之和 | a + b =15 |
2 | -(减法) | 返回值的差异 | a-b=5 |
3 | *(乘法) | 返回值的乘积 | a * b =50 |
4 | /(除法) | 执行除法运算并返回商数 | a/b=2 |
5 | %(模量) | 执行除法运算并返回余数 | a%b=0 |
注意-Rust不支持++和-运算符。
fn main() { let num1 = 10 ; let num2 = 2; let mut res:i32; res = num1 + num2; println!("Sum: {} ",res); res = num1 - num2; println!("Difference: {} ",res) ; res = num1*num2 ; println!("Product: {} ",res) ; res = num1/num2 ; println!("Quotient: {} ",res); res = num1%num2 ; println!("Remainder: {} ",res); }
Sum: 12 Difference: 8 Product: 20 Quotient: 5 Remainder: 0
关系运算符测试或定义两个实体之间的关系类型,关系运算符用于比较两个或多个值,关系运算符返回布尔值-true或false。
假设A=10,B=20。
Sr.No | 运算符 | 描述 | 示例 |
---|---|---|---|
1 | > | 大于 | (A > B) is False |
2 | < | 小于 | (A < B) is True |
3 | >= | 大于或等于 | (A >= B) is False |
4 | <= | 小于或等于 | (A <= B) is True |
5 | == | 相等 | (A == B) is fals |
6 | != | 不相等 | (A != B) is True |
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
关系运算符测试或定义两个实体之间的关系类型。关系运算符用于比较两个或多个值。关系运算符返回布尔值-true或false。
假设A=10,B=20。
Sr.No | 运算符 | 描述 | 示例 |
---|---|---|---|
1 | && (And) | 仅当指定的所有表达式均返回true时,运算符才返回true | (A > 10 && B > 10) is False |
2 | ||(OR) | 如果指定的至少一个表达式返回true,则运算符返回true | (A > 10 || B >10) is True |
3 | ! (NOT) | 运算符返回表达式结果的倒数。例如:!(>5)返回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
假设变量A=2且B=3。
Sr.No | 运算符 | 描述 | 示例 |
---|---|---|---|
1 | & (Bitwise AND) | 它对其整数参数的每一位执行布尔“与”运算。 | (A & B) is 2 |
2 | | (BitWise OR) | 它对其整数参数的每一位执行布尔或运算。 | (A | B) is 3 |
3 | ^ (Bitwise XOR) | 它对其整数参数的每一位执行布尔异或运算。 | (A ^ B) is 1 |
4 | ! (Bitwise Not) | 它是一元运算符,通过反转操作数中的所有位进行操作。 | (!B) is -4 |
5 | << (Left Shift) | 左移一个位置等同于将其乘以2,将两个位置左移等同于乘以4,依此类推。 | (A << 1) is 4 |
6 | >> (Right Shift) | 右移运算符,右移一位除以2,依此类推。 | (A >> 1) is 1 |
7 | >>> (Right shift with Zero) | 该运算符类似于>>运算符,只是向左移位的位始终为零。 | (A >>> 1) is 1 |
fn main() { let a:i32 = 2; let b:i32 = 3; let mut result:i32; result = a & b; println!("(a & b) => {} ",result); result = a | b; println!("(a | b) => {} ",result) ; result = a ^ b; println!("(a ^ b) => {} ",result); result = !b; println!("(!b) => {} ",result); result = a << b; println!("(a << b) => {}",result); result = a >> b; println!("(a >> b) => {}",result); }
(a & b) => 2 (a | b) => 3 (a ^ b) => 1 (!b) => -4 (a << b) => 16 (a >> b) => 0
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)