Arithmetic Operators

  • What Are Arithmetic Operators?

    • Arithmetic operators are used to perform arithmetic operations.
  • Types The table below summarizes the arithmetic operations in Rust.

Types

The table below summarizes the arithmetic operations in Rust.

operatoroperationexplanation
operand1 + operand2additionadd operand1 and operand2
operand1 - operand2subtractionsubtract operand2 from operand1
operand1 / operand2dividedivide operand1 by operand2
operand1 * operand2multiplicationmultiply operand1 with operand2
operand1 % operand2modulusget reminder of operand1 by dividing with operand2

The following example shows the use of arithmetic operators in a program:

  fn main() {
    let a = 4;
    let b = 3;
    
    println!("Operand 1:{}, Operand 2:{}", a , b);
    println!("Addition:{}", a + b);
    println!("Subtraction:{}", a - b);
    println!("Multiplication:{}", a * b);
    println!("Division:{}", a / b);
    println!("Modulus:{}", a % b);
}
  

output:

  Operand 1:4, Operand 2:3
Addition:7
Subtraction:1
Multiplication:12
Division:1
Modulus:1
  

Quiz

Test your understanding of arithmetic operators in Rust!

--- primaryColor: steelblue secondaryColor: '#e8e8e8' textColor: black shuffleQuestions: false shuffleAnswers: true locale: en --- ## What is the output of the following code? ```rust fn main() { let mut a = 4; let mut b = 3; a = a + b; a = a * b; a = a - b; b = b - a; println!("a:{}", a); println!("b:{}", b); } ``` - [ ] a:18 b:-15 - [ ] a:15 b:18 - [ ] a:18 b:15

Last updated 25 Jan 2024, 05:11 +0530 . history