Rust – 操作符
运算符通常用于对值和变量执行操作。这些是用于逻辑和算术运算的标准符号。在本文中,我们将研究不同类型的 Rust运算符。运算符告诉编译器或解释器执行特定的数学、逻辑或关系运算。
以下是 Rust 中的运算符类型:
- 算术运算符
- 比较运算符
- 逻辑运算符
- 按位运算符
- 复合赋值运算符
算术运算符
算术运算运算符用于执行数学运算,如加法、减法、乘法和除法。
让我们假设 A=40 和 B=20 S.No Operator Explanation Example 1 + (Addition) Returns the addition of two operands A+B=60 2 -(Subtraction) Returns the difference of the values(subtract right operand from left) A-B=20 3 *(Multiplication) Returns the product of the values A*B=800 4 /(Division) Divide left operand by right one and returns the quotient A/B=2 5 %(Modulus) Divide left operand by right one and returns the remainder A%B=0
例子:
Rust
fn main() {
let x = 20 ;
let y = 4;
let mut result:i32;
// addition of two operands
result = x + y;
println!("Sum: {} ",result);
// the difference of the values
result = x - y;
println!("Difference: {} ",result) ;
// the product of the values
result = x*y ;
println!("Product: {} ",result) ;
// Divide left operand by right one
// and returns the quotient
result = x/y ;
println!("Quotient: {} ",result);
// Divide left operand by right one
// and returns the remainder
result = x%y ;
println!("Remainder: {} ",result);
}
Rust
fn main() {
let a = 4;
let b = 5;
// performing a series of comparisions
// using comparision operators
// is equal to
let c = a == b;
// is not equal to
let d = a != b;
// is less than
let e = a < b;
// is greater than
let f = a > b;
// is less than or equa to
let g = a <= a;
// is greater than or equal to
let h = a >= a;
println!("a: {}, b: {},
c: {0} == {1} is {},
d: {0} != {1} is {},
e: {0}<{1} is {},
f: {0}>{1} is {},
g: {0}<={0} is {},
h: {0}>={0} is {}",
a, b, c, d, e, f, g, h);
}
Rust
fn main() {
let a = false;
let b = true;
let c = !a;
let d = a && b;
let e = a || b;
println!("
a: {}, b: {},
c: !{0} is {},
d: {0} && {1} is {},
e: {0} || {1} is {}",
a, b, c, d, e);
}
Rust
fn main() {
let a = 1;
let b = 2;
// Bitwise AND, 0 (01 && 10 -> 00)
let c = a & b;
// Bitwise OR, 3 (01 || 10 -> 11)
let d = a | b;
// Bitwise exclusive OR, 3 (01 != 10 -> 11)
let e = a ^ b;
// Left-shift, 4
let f = a << b;
// 16 a->'01'+"0000" ->'10000'
let f2 = a << 4;
// Right-shift, 0
let g = a >> b;
// 0 '11111' -> remove 4 bits from the end ->'1'
let g2 = 31 >> 4;
// Bitwise NOT
let h = !a;
println!("
a: {a}, b: {b},
c: {a} & {b} is {c},
d: {a} | {b} is {d},
e: {a} ^ {b} is {e},
f: {a} << {b} is {f},
f2: {a} << 4 is {f2},
g: {a} >> {b} is {g},
g2: {a} >> {b} is {g2},
h: !{a} = {h}",
a=a, b=b, c=c, d=d, e=e, f=f, f2=f2, g=g, g2=g2, h=h);
}
Rust
fn main() {
let mut a = 4;
println!("a is {}", a);
// Arithmetic addition and assignment, 4 + 5 = 7
a += 5;
println!("1: a += 5 is {}", a);
// Arithmetic subtraction and assignment, 9 - 2 = 7
a -= 2;
println!("2: a -= 2 is {}", a);
// Arithmetic multiplication and assignment, 7 * 5 = 35
a *= 5;
println!("3: a *= 5 is {}", a);
// Arithmetic division and assignment, 35 / 2 = 17 not 17.5
a /= 2;
println!("4: a /= 2 is {}", a);
// Arithmetic remainder and assignment, 17 % 5 = 2
a %= 5;
println!("5: a %= 5 is {}", a);
// Bitwise AND and assignment, 10 && 10 -> 10 -> 2
a &= 2;
println!("6: a &= 2 is {}", a);
// Bitwise OR and assignment, 010 || 101 -> 111 -> 7
a |= 5;
println!("7: a |= 5 is {}", a);
// Bitwise exclusive OR and assignment, 111 ^= 010 -> 101 -> 5
a ^= 2;
println!("8: a ^= 2 is {}", a);
// Left-shift and assignment, '101'+'0' -> 1010 -> 10
a <<= 1;
println!("9: a <<= 1 is {}", a);
// Right-shift and assignment, 101̶0̶ -> 10 ->
a >>= 2; 2
println!("10: a >>= 2 is {}", a);
}
输出:
Sum: 24
Difference: 16
Product: 80
Quotient: 5
Remainder: 0
比较运算符
运算符是比较值并根据条件返回真或假的运算符。
让我们假设 A=40 和 B=20
S.No Operator Explanation Example 1 > Greater than (A>B) is True 2 < Less than (A 3 == Equal to (A==B) is False 4 != Not equal to (A!=B) is True 5 >= Greater than and equal to (A>=B) is True 6 <= Less than and equal to (A<=B) is False
例子:
锈
fn main() {
let a = 4;
let b = 5;
// performing a series of comparisions
// using comparision operators
// is equal to
let c = a == b;
// is not equal to
let d = a != b;
// is less than
let e = a < b;
// is greater than
let f = a > b;
// is less than or equa to
let g = a <= a;
// is greater than or equal to
let h = a >= a;
println!("a: {}, b: {},
c: {0} == {1} is {},
d: {0} != {1} is {},
e: {0}<{1} is {},
f: {0}>{1} is {},
g: {0}<={0} is {},
h: {0}>={0} is {}",
a, b, c, d, e, f, g, h);
}
输出:
a: 4, b: 5,
c: 4 == 5 is false,
d: 4 != 5 is true,
e: 4<5 is true,
f: 4>5 is false,
g: 4<=4 is true,
h: 4>=4 is true
逻辑运算符
逻辑运算符用于组合两个或多个条件。逻辑运算符也返回布尔值。
让我们假设 A=1 和 B=0
The operator returns the inverse of the expression’s result. For E.g.: !(>5) returns false !(A >10 ) is True Bitwise Operators S.No Operator Explanation Example 1 && The operator returns true only if all the expressions specified return true (A&&B) is False 2 || The operator returns true if at least one of the expressions specified return true (A||B) is True 3 ! !(A||B) is False
例子:
锈
fn main() {
let a = false;
let b = true;
let c = !a;
let d = a && b;
let e = a || b;
println!("
a: {}, b: {},
c: !{0} is {},
d: {0} && {1} is {},
e: {0} || {1} is {}",
a, b, c, d, e);
}
输出:
a: false, b: true,
c: !false is true,
d: false && true is false,
e: false || true is true
按位运算符
按位是涉及处理单个位的操作级别,这些位是计算机中最小的数据单位。每个位都有一个二进制值:0 或 1。
让我们假设 A=2 和 B=3
S.No | Operator | Explanation | Example |
---|---|---|---|
1 | & (Bitwise AND) | It performs a Boolean AND operation on each bit of its integer arguments. | (A&B) is 2 |
2 | | (BitWise OR) | It performs a Boolean OR operation on each bit of its integer arguments. | (A|B) is 3 |
3 | ^ (Bitwise XOR) | It performs a Boolean exclusive OR operation on each bit of its integer arguments. Exclusive OR means that either operand one is true or operand two is true, but not both. | (A^B) is 1 |
4 | ! (Bitwise Not) | It is a unary operator and operates by reversing all the bits in the operand. | !A is 2 |
5 | << (Left Shift) | It moves all the bits in its first operand to the left by the number of places specified in the second operand. New bits are filled with zeros. Shifting a value left by one position is equivalent to multiplying it by 2, shifting two positions is equivalent to multiplying by 4, and so on. | (B<<1) is 6 |
6 | >> (Right Shift) | Binary Right Shift Operator. The left operand’s value is moved right by the number of bits specified by the right operand. | (B >>1) is 1 |
7 | >>> (Right shift with Zero) | This operator is just like the >> operator, except that the bits shifted to the left are always zero. | (A>>>1) is 1 |
例子:
锈
fn main() {
let a = 1;
let b = 2;
// Bitwise AND, 0 (01 && 10 -> 00)
let c = a & b;
// Bitwise OR, 3 (01 || 10 -> 11)
let d = a | b;
// Bitwise exclusive OR, 3 (01 != 10 -> 11)
let e = a ^ b;
// Left-shift, 4
let f = a << b;
// 16 a->'01'+"0000" ->'10000'
let f2 = a << 4;
// Right-shift, 0
let g = a >> b;
// 0 '11111' -> remove 4 bits from the end ->'1'
let g2 = 31 >> 4;
// Bitwise NOT
let h = !a;
println!("
a: {a}, b: {b},
c: {a} & {b} is {c},
d: {a} | {b} is {d},
e: {a} ^ {b} is {e},
f: {a} << {b} is {f},
f2: {a} << 4 is {f2},
g: {a} >> {b} is {g},
g2: {a} >> {b} is {g2},
h: !{a} = {h}",
a=a, b=b, c=c, d=d, e=e, f=f, f2=f2, g=g, g2=g2, h=h);
}
输出:
a: 1, b: 2,
c: 1 & 2 is 0,
d: 1 | 2 is 3,
e: 1 ^ 2 is 3,
f: 1 << 2 is 4,
f2: 1 << 4 is 16,
g: 1 >> 2 is 0,
g2: 1 >> 2 is 1,
h: !1 = -2
复合赋值运算符
复合赋值运算符执行由附加运算符指定的操作,然后将结果赋值给左操作数。例如,复合赋值表达式,如
expression1 += expression2
让我们假设 A=4
S.No Operator Explanation Example 1 += Arithmetic addition and assignment A+=2 is 6 2 -= Arithmetic subtraction and assignment A-=2 is 2 3 *= Arithmetic multiplication and assignment A*=2 is 8 4 /= Arithmetic division and assignment A/=2 is 2 5 %= Arithmetic remainder and assignment A%=2 is 0 6 <<= Left-shift and assignment A<<=1 is 0 7 >>= Right-shift and assignment A>>=1 is 2 8 &= Bitwise AND and assignment A&=2 is 0 9 |= Bitwise OR and assignment A|=2 is 6 10 ^= Bitwise exclusive OR and assignment A^=2 is 1
例子:
锈
fn main() {
let mut a = 4;
println!("a is {}", a);
// Arithmetic addition and assignment, 4 + 5 = 7
a += 5;
println!("1: a += 5 is {}", a);
// Arithmetic subtraction and assignment, 9 - 2 = 7
a -= 2;
println!("2: a -= 2 is {}", a);
// Arithmetic multiplication and assignment, 7 * 5 = 35
a *= 5;
println!("3: a *= 5 is {}", a);
// Arithmetic division and assignment, 35 / 2 = 17 not 17.5
a /= 2;
println!("4: a /= 2 is {}", a);
// Arithmetic remainder and assignment, 17 % 5 = 2
a %= 5;
println!("5: a %= 5 is {}", a);
// Bitwise AND and assignment, 10 && 10 -> 10 -> 2
a &= 2;
println!("6: a &= 2 is {}", a);
// Bitwise OR and assignment, 010 || 101 -> 111 -> 7
a |= 5;
println!("7: a |= 5 is {}", a);
// Bitwise exclusive OR and assignment, 111 ^= 010 -> 101 -> 5
a ^= 2;
println!("8: a ^= 2 is {}", a);
// Left-shift and assignment, '101'+'0' -> 1010 -> 10
a <<= 1;
println!("9: a <<= 1 is {}", a);
// Right-shift and assignment, 101̶0̶ -> 10 ->
a >>= 2; 2
println!("10: a >>= 2 is {}", a);
}
输出:
a is 4
1: a += 5 is 9
2: a -= 2 is 7
3: a *= 5 is 35
4: a /= 2 is 17
5: a %= 5 is 2
6: a &= 2 is 2
7: a |= 5 is 7
8: a ^= 2 is 5
9: a <<= 1 is 10
10: a >>= 2 is 2