Rust-operators

提供:Dev Guides
移動先:案内検索

Rust-オペレーター

演算子は、データに対して実行される機能を定義します。 演算子が機能するデータはオペランドと呼ばれます。 次の式を考慮してください-

7 + 5 = 12

ここで、値7、5、および12はオペランドであり、+および=は演算子です。

Rustの主要な演算子は次のように分類できます-

  • 算術
  • ビット単位
  • 比較
  • 論理的
  • ビット単位
  • 条件付き

算術演算子

変数aおよびbの値がそれぞれ10および5であると仮定します。

link:/rust/rust_arithmetic_operators [例を表示]

Sr.No Operator Description Example
1 +(Addition) returns the sum of the operands a+b is 15
2 -(Subtraction) returns the difference of the values a-b is 5
3 * (Multiplication) returns the product of the values a*b is 50
4 /(Division) performs division operation and returns the quotient a/b is 2
5 % (Modulus) performs division operation and returns the remainder a % b is 0

注意-++および-演算子はRustではサポートされていません。

関係演算子

関係演算子は、2つのエンティティ間の関係の種類をテストまたは定義します。 関係演算子は、2つ以上の値を比較するために使用されます。 関係演算子はブール値-trueまたはfalseを返します。

Aの値が10で、Bが20であると仮定します。

link:/rust/rust_relational_operators [例を表示]

Sr.No Operator Description Example
1 > Greater than (A > B) is False
2 < Lesser than (A < B) is True
3 >= Greater than or equal to (A >= B) is False
4 Lesser than or equal to (A ⇐ B) is True
5 == Equality (A == B) is fals
6 != Not equal (A != B) is True

論理演算子

論理演算子は、2つ以上の条件を結合するために使用されます。 論理演算子もブール値を返します。 変数Aの値が10で、Bが20であると仮定します。

link:/rust/rust_logical_operators [例を表示]

Sr.No Operator Description Example
1 && (And) The operator returns true only if all the expressions specified return true (A > 10 && B > 10) is False
2 (OR)
The operator returns true if at least one of the expressions specified return true (A > 10 B >10) is True
3 ! (NOT) The operator returns the inverse of the expression’s result. For E.g.: !(>5) returns false !(A >10 ) is True

ビット演算子

変数A = 2およびB = 3と仮定します。

link:/rust/rust_bitwise_operators [例を表示]

Sr.No Operator Description 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. (!B) is -4 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. (A << 1) is 4 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. (A >> 1) is 1 7 >>> (Right shift with Zero)