You can always change the value stored in a variable with the assignment operator(=):
// Initializing with 5:
let mut my_var = 5;
// Assigning the value 10:
my_var = 10;
println!("{}", my_var); // Output: 10
First, this code creates a variable named my_var and initializes it with 5.
Then, the code assigns 10 to my_var. Now, when my_var appears again in the code, the program will treat it as if it is 10.
Assigning the Value of One Variable to Another:
You can also assign the value of some variable to another variable using the assignment operator:
let mut my_var = 5;
let my_num = 10;
my_var = my_num;
println!("{}", my_var); // Output: 10
First, this code creates a variable named my_var and initializes it with 5 and another variable named my_num and initializes it with 10.
Then, the contents of my_num (which is 10) is assigned to the variable my_var. Now, my_var also has the value of 10.
Assignment
Follow the Coding Tutorial and let's reassign some variables.
Hint
Look at the examples above if you get stuck.
Free plan: 30 tutor messages. Premium is unlimited.