Intro Even though Rust provides its Rust book, I struggled with understanding the keyword. I asked myself, "Am I the only one who has this problem?" A quick Google search confirmed that I was not alone. mut As a result, I decided to write this article to provide a detailed explanation of the keyword. This article is intended for those who come from high-level languages such as Python or JavaScript. mut The Variables To create an unmuttable variable in Rust, simply write . It's straightforward. If you want to create a variable that can be mutated later, just add the keyword after . Rust has a helpful convention that encourages clarity of intentions. let x = 1337 mut let Adding the keyword informs others that this variable will be modified somewhere else in the code. Okay. mut Let's visualize it. Two variables here, and . let mut x = 1337 let y = 42 The References At the moment, everything is straightforward. However, things start to get a little curly when using references. Let's create some. mut let mut x = 1337; let y = 42; let x_ref = &mut x; let y_ref = &y; I created two references (or "borrowed" in terms of Rust). One of them is a mutable reference, and the other is a read-only reference. Let's create a scheme for that again. In the given scheme, I have 4 variables, 2 of which are references. Both reference variables are immutable and do not have the keyword after , which means I cannot change what they point to. However, I can still change the value they reference. mut let *x_ref = 777; If you write this, the Rust compiler will not complain, and the value of (not the ref itself) will change to . However, there's a red square on the scheme indicating that lacks the mutability option. So, why can I change the value it refers to? x 777 x_ref Let’s come back to the scheme for the . let x_ref = &mut x The first white block contains the name: . The second one informs me about the type stored in that variable. In its complete form, without any implicit type annotations, I can write as follows: x_ref let x_ref: &mut i32 = &mut x; I can interpret this as: let's create an named that will hold a to and initialize it immediately with the to the value in the variable. immutable variable x_ref mutable reference i32 mutable reference i32 x This means I can modify the value it points to, but I cannot alter the reference's value (or address). In other words, I can't write something like: let x_ref: &mut i32 = &mut x; let mut z = 0; x_ref = &mut z; // Not allowed! In terms of the schemes, I want to change the direction the arrow is pointing to in the code block above. However, even if the variable is mutable, I can't change the arrow because the problem lies in the immutability of the itself. z x_ref To change the arrow direction, I need to modify the address stored in the variable. However, I cannot do this as the variable is immutable. x_ref Let’s do it! let mut x: i32 = 1337; let mut x_ref: &mut i32 = &mut x; // I've added mut before x_ref let mut z = 0; x_ref = &mut z; // Allowed! There are too many instances of here around , right? Let's describe them. mut x_ref : I am creating a mutable variable named , which means I can change its value later. let mut x_ref x_ref : I am stating that the variable will contain mutable reference(s) to some value of type . &mut i32 i32 : I am borrowing (getting a reference to) the variable . &mut x x Then, I created a variable named and assigned it the value of . Afterward, when I wrote , I indicated that I understand to be a mutable variable that can only hold references to values. z 0 x_ref = &mut z x_ref i32 Since the type of is , I am able to assign its address to the variable. To obtain the address of , I used the syntax. z i32 x_ref z &mut z The scheme. The Mental Trick Take a look at in the statement, it may look a little bit obvious, but… = let mut x_ref = &mut x; … I see it as a divider (especially if you rotate it by 90 degrees) that splits the statement into two sub-statements: left and right. The left side provides information itself, while the right side tells us . about the variable about the value When I use the dereference operator to change the value... * *x_ref = 100; ... I do not change the value of the variable. Instead, I am changing the value that is referencing. x_ref x_ref Unmutable References I used frequently before. What if I omit some of them? mut let i = 1; let j = 2; let mut k = &i; Can I change the value of here? Using the divider technique, it's quite simple to answer. I can change the value of (I see on the left side), but the value (right side) is an immutable reference to (there is no here). i k mut i mut Therefore… let i = 1; let j = 2; let mut k = &i; k = &j; // This is legal. *k = 3; // This is not. The scheme. Conclusion In this article, we've dissected the nuances of the keyword and references. Remember, there's a distinction between a and a holding a reference. Our trick? mut mutable reference mutable variable Using the sign as a mental divider to better understand assignments in Rust. This simple visualization can clear up many confusions. = Happy coding!