Rust-arithmetic-operators

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

Rust-算術演算子

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

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ではサポートされていません。

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