Variables are memory addresses used to store information to be referenced and manipulated in programs. They also provide a way of defining data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves. It is helpful to think of variables as containers that hold information. Their sole purpose is to label and store data in memory. This data can then be used throughout your program in accordance with its scope.
Let us take an example Rust program which prints out GeeksForGeeks with and without storing it in a variable.
Rust
fn main() {
println!( "Welcome to GeeksForGeeks" );
let x = "GeeksForGeeks" ;
println!( "Welcome to {}" , x);
}
Welcome to GeeksForGeeks
Welcome to GeeksForGeeks
|
In the above program, we have printed two output:
- In the first print statement, we have directly printed the output without storing the value in a variable.
- But in the second one, we have used the variable “x” to store the value ‘GeeksForGeeks” and then used the variable instead.
Declaring a variable
In Rust, we can create variables by assigning data to alphanumeric words( excluding keywords) using the let keyword or const keyword.
Syntax: let x = "Value"; or const x = "Value";
Example:
Rust
fn main(){
let x = "Geeksforgeeks" ;
println!( "{}" ,x);
}
|
Output:
Geeksforgeeks
In the above program, we used the variable “x” to store the string “GeeksForGeeks”
- The let keyword (or const keyword) suggests the compiler to consider the letter “x” to be the label of string “GeeksForGeeks” in the memory.
- The mut keyword can be added after let keyword or const keyword to make the variable mutable.
- Rust compiler can detect the data type of the variable, but you can also explicitly define the data type of the variable.
Mutability
Immutability or an immutable object is an object whose state or valve cannot be modified after it is created or assigned. Whereas mutability or a mutable object is an object whose state or valve can be modified after it is created or assigned. By default all rust variables are immutable.
Let us take an example program where we try to change variable value which gives us an error
Rust
fn main() {
let x = 0;
println!( "The value of x is: {}" , x);
x = 1;
println!( "The value of x is: {}" , x);
}
|
Output:

As you can see from the picture the program couldn’t be compiled and gave us an error. As rust compiler detected it was asking to use “mut x” instead of plain x. The mut is a keyword used in rust to say that the object or variable is mutable. Let us add mut and try again.

compiled without errors
In the above rust programs we have used let keyword before the variable, let and const are the same as they are in other programming languages. In Rust, you aren’t allowed to use the mut keyword with constants, Constants aren’t immutable by default.
Example: An example of a constant variable.
Rust
fn main() {
const N: i32 = 1;
println!( "num: {}" , N);
}
|
Output:
num: 1
We can also declare multiple variables in a single line as shown below:
Example:
Rust
fn main() {
let ( first, middle, last ) = ( "geeks" , "for" , "geeks" );
println!( "{} {} {} is amazing" , first, middle, last );
}
|
Output:
geeks for geeks is amazing
Shadowing
Shadowing is an instance where we can make a variable mutable temporarily. Rust supports shadowing of variables. Shadowing also allows changing the data type of variable. In Rust, you can declare a new variable that has the same name as a previously declared variable which shadows the previous variable meaning the second variable’s value is what appears when the variable is used.
Rust
fn main() {
let gfg = 100;
let gfg = gfg -50;
let gfg = gfg * 5;
println!( "The value of gfg is: {}" , gfg);
}
|
Output:
The value of gfg is: 250
Similar Reads
Rust - Slices
Slice is a data type that does not have ownership. Slice references a contiguous memory allocation rather than the whole collection. Slices are also present in Python which is similar to slice here in Rust. Slice is used when you do not want the complete collection, or you want some part of it. Slic
3 min read
Rust - Unrecoverable Errors
Unrecoverable errors are those errors which as the name suggests can not be handled by a programmer. When any unrecoverable error occurs the end result is the program quits (terminates). The complete process is the first panic! macro is fired then the error message is printed along with the location
2 min read
Rust - Traits
A trait tells the Rust compiler about functionality a particular type has and can share with other types. Traits are an abstract definition of shared behavior amongst different types. So, we can say that traits are to Rust what interfaces are to Java or abstract classes are to C++. A trait method is
8 min read
Rust - Build Scripts
In Rust, there is an option for building scripts where Cargo compiles a built script into an executable file, and after running the script, many tasks can be performed. There are scenarios where we need different options for generating a code before it's built. The cargo compiler in this case compil
2 min read
Rust - While Loop
Loops in Rust come into use when we need to repeatedly execute a block of statements. Loops are also useful when we need to iterate over a collection of items. In Rust, we have various kinds of loops, including loops, while loops, and for loops. The while loop is the most common loop in Rust. The lo
3 min read
Rust - Generic Traits
Rust is an extremely fast programming language that supports speed, concurrency, as well as multithreading. In Rust, we have a concept of Generic Traits where Generics are basically an abstraction of various properties, and Traits are basically used for combining generic types for constraining types
2 min read
Rust impl Trait
In Rust, there is a concept of impl trait. Â impl Trait is used to implement functionality for types and is generally used in function argument positions or return positions. It is used in scenarios where our function returns a single type value. In the impl trait, we always specify the impl keyword
2 min read
Rust - Strings
String data type is a very important part of any programming language. Rust handles strings a bit differently from other languages. The String data type in Rust is of two types: String Literal (&str)String Object (String)String LiteralString Literal or &str are called 'string slices', which
2 min read
Rust - Vectors
Vector is a module in Rust that provides the container space to store values. It is a contiguous resizable array type, with heap-allocated contents. It is denoted by Vec<T>. Vectors in Rust have O(1) indexing and push and pop operations in vector also take O(1) complexity. Vectors ensure they
4 min read
Rust - Clone Trait
In Rust, we have a concept of clone trait. Clone traits have the ability to duplicate objects explicitly. We can make anything copy with the Clone Trait. Syntax: //pub refers to public trait pub trait Clone {Â Â fn clone(&self) -> Self; Â Â fn clone_from_trait(&mut self, source: &Self) { ... }} In Rus
2 min read