2.6 Math operators

Rust contains all the regular mathematical operators one can use in order to do mathematical operations on numbers. These are addition +, subtraction -, multiplication *, division / and remainder %.

Addition

Adding an amount to another number is rather simple:

let mut x = 2;

x = x + 5;

println!("x is {}.", x);

This is going to say that the mutable binding x is seven upon being outputted into the terminal, but it feels somewhat akward having to tyhpe x twice in order for the mathematical operation to occur. Luckily, there’s an easier way we can do this—using an operator to increase the value of a variable binding by a specified amount:

let mut x = 2;

x += 5;

println!("x is {}.", x);

The result of this is going to be the exact same thing—only we didn’t have to type x twice in order for it to work. This works due to the reason we’re using an operator which serves as a shorthand for the former example using only +. The += will add a number to the binding that comes before it.

Subtraction

Subtraction in Rust takes place in a similar manner to that of addition:

let mut x = 7;

x -= 5;

println!("x is {}.", x);

The above will output:

x is 2.

This is due to the fact that we’re removing five from x which is seven, resulting in a value of two.

Multiplication

This—too—looks very similar to that of the other mathematical operations:

let mut x = 5;

x *= 3;

println!("x is {}.", x);

After the operation, the value of x will be fifteen, since five times three is—indeed—fifteen.

Division

And there’s of course division:

let mut x = 9;

x /= 3;

println!("x is {}.", x);

Depending on whether we’re using division on integers or other numbers the results may vary.

Integer division

Let’s imagine a situation where we divide ten by three. If the numbers we’re attempting to divide are integers, we’re not going to be able to use up all of the numbers and put them into equally big stacks. This means that there’ll also be a remainder.

ten divided by three is three, with 1 as a remainder—due to the fact that three times three is nine and nine and one is ten.

Remainder

Let’s go retrieve the remainder from the division in the previous example, which divided ten by three:

let mut x = 10;

x %= 3;

println!("x is {}.", x);

This will output:

x is 1

The remainder is the remaining number left over from the operation.

Modulo?

In some higher-level languages the remainder operator is what is known as a modulo operator. With Rust, this is not the case. Rust is using more of a C-like style when it comes to the %, rendering it a simple means of retrieving the remainder of a number.

Order of operations

Just like in regular mathematics, there’s an order of operations, which is used to know what number gets evaluated first.

let mut x = 9 / 3 + 5 * 7;

println!("x is {}.", x);

This will first evaluate 9 / 3 and 5 * 7—finally adding their results together:

x is 38

Parenthesis

You may also write out parenthesis to change the order of operations—following the same trend of regular mathematics:

let mut x = 9 / (3 + 6);

println!("x is {}.", x);

Rather than first evaluating 9 / 3, the above first evaluates 3 + 6 because it’s located the deepest into the mathematical structure of parenthesis.

Parenthesis can be used inside parenthesis, which can be used inside parenthesis—I’d say you get the idea—precisely like real-world mathematics.

The previous chunk of code will be outputted as:

x is 1.

This, because 3 + 6 is nine and 9 / 9 is—in fact—one.

Variable bindings

You may also use bindings in your calculations:

let x = 12;
let y = x * 5;

println!("y is {}.", y);

This will be outputted into the console:

y is 60

I’m sure you’re already aware of the fact that 12 * 5 is sixty—I’ll keep quiet!

Exercises

  • Make a function called multiply that takes two i32s as arguments and returns the product of the two i32s.
  • Do the same for a function dividing the two i32s. Make sure to watch out for any divisions by zero—use panic! to write out an error if it was to take place.

Moreover

Make sure to learn all the mathematical operators by heart, as well as what you can use them for. It’s bound to make your life a whole lot easier once you know them all, as the world of Rust is sure not to look as strange and foreign upon expanding your knowledge.