9951 explained code solutions for 126 technologies


rustHow do I increment a variable in Rust?


Incrementing a variable in Rust is done using the += operator. This operator adds the right-hand side of the equation to the left-hand side and assigns the result to the left-hand side.

Example

let mut x = 5;
x += 1;

Output example

6

The code above consists of three parts:

  1. let mut x = 5; - This declares a mutable variable x and assigns it the value 5.
  2. x += 1; - This uses the += operator to add 1 to x and assign the result to x.
  3. 6 - This is the output of the code, which is the value of x after the += operator is applied.

Helpful links

Edit this code on GitHub