# Rust - Variables & Mutability **Published by:** [JLaw](https://paragraph.com/@jlaw/) **Published on:** 2023-06-29 **URL:** https://paragraph.com/@jlaw/rust-variables-mutability ## Content Variables & MutabilityIn Rust, variables are immutable by default. By defaulting variables to immutable, Rust steers developers towards safer, clearer code by reducing unintended modifications, enabling compiler optimizations, and promoting functional programming practices. Although variables default to immutable in Rust, they can be altered to become mutable. Being mutable simply means that the value associated with a variable can change, and the opposite is true for immutable. More explicitly, a variable is immutable when the value associated with it cannot be changed. To modify a variable from its default state of immutable, the mut keyword can be added directly in front of the variable name. Here is an example:let mut x = 1; In the example above, the variable x is mutable because the keyword mut is used. This means that its value can be altered in an operation like this:x = 0; // The value of x is now '0' instead of '1'. If the keyword mut was not applied to the variable x in the first code snippet, in the context of a proper program, the second code snippet would cause a compiler error because x would be immutable, and therefore not allowed to have its value modified. The idiomatic approach to immutability in Rust is to limit its scope as much as possible, using mut only when necessary, keeping the portion of code that has access to mutable data as small as possible.ConstantsSimilar to immutable variables, constants are values that are bound to a variable name and are not allowed to change, but there exists a difference between the two. With constants, the mut keyword cannot be used, and they are not just immutable by default, but rather permanently immutable. Constants are declared with the keyword const instead of the keyword let, and the value type has to be annotated. Annotating the value type means explicitly specifying the data type of the constant variable when it is declared. A simple constant variable declaration looks like this:const X: i32 = 1; In the declaration above, : denotes the start of the type annotation, and i32 is the specific type being specified for the variable. Unlike regular mutable and immutable variables, constants must be assigned fixed values that are known at compile time and cannot be assigned values requiring runtime computations or operations. For example:const X: i32 = 1 + 1; // This is valid. const Y: i32 = example(); // This is invalid. Y is invalid because its assigned expression requires the program to be running in order for it to be evaluated. In contrast, X is valid because the compiler can evaluate a limited set of operations at compile time, which allows for X’s expression to be evaluated before the program runs. The naming convention for constant variables is to use all uppercase letters with underscores between words. Having a unique naming convention for constant variables helps distinguish them from other kinds of variables, improving readability and helping prevent errors.ShadowingIn Rust, declaring a new variable with the same name as a previously declared variable is called shadowing. Once an original variable is shadowed by a new variable, subject to the constraints of scope, any reference to the variable name will refer to the new variable instead of the original. This results in the value from the new variable being used whenever the variable name is referenced. Shadowing a variable can be performed by using an existing variable’s name preceded by the let keyword like this:let n = 0; // Original n. println!("{n}"); // This will print '0'. let n = 1; // New n shadowing the original n. println!("{n}"); // This will now print '1'. When shadowing is performed, a new variable is created, allowing for the modification of type and value while still using a previously declared name. In effect, by using the let keyword to shadow variables, transformations can be performed on variables by keeping them immutable. By leveraging shadowing, the need to create and utilize many variable names can be avoided, making code cleaner and more concise. ## Publication Information - [JLaw](https://paragraph.com/@jlaw/): Publication homepage - [All Posts](https://paragraph.com/@jlaw/): More posts from this publication - [RSS Feed](https://api.paragraph.com/blogs/rss/@jlaw): Subscribe to updates