Rust Tutorial

Rust - arithmetic operators example



The example below shows the usage of arithmetic operators - addition(+), subtraction(-), multiply(*), division(/), and modulo(%) operators.

fn main() {
  let a = 9.0;
  let b = 2.0;

  println!("a = {}, b = {}\n", a, b);

  //Add a and b
  let result_add = a + b;
  println!("a + b = {}", result_add);

  //Subtract b from a
  let result_sub = a - b;
  println!("a - b = {}", result_sub);

  //Multiply a and b
  let result_mul = a * b;
  println!("a * b = {}", result_mul);

  //Divide a by b
  let result_div = a / b;
  println!("a / b = {}", result_div);

  //return division remainder
  let result_modulo = a % b;
  println!("a % b = {}", result_modulo);
}

The output of the above code will be:

a = 9, b = 2 

a + b = 11
a - b = 7
a * b = 18
a / b = 4.5
a % b = 1

❮ Rust - Operators

5