In Programming, a variable refers to a storage container used to store values. Simply put, a variable is a named storage location where the program can store and retrieve data. Although the fundamental concept of a variable is universal across programming languages, Rust treats variables a little differently.
In Rust, a variable is associated with a specific type. The compiler will use the data type to determine the layout of the variable’s memory and the space to allocate to the variable.
In this run, we will explore how to work with variables in Rust and how to use them to store various types of information.
Rust Variable Naming
As we mentioned, a variable is a named storage location within a program. This means we have to create an identifier that other parts of the program can reference.
Although naming something as a variable can seem very trivial, there are certain rules to which we adhere.
These include:
- A variable name cannot start with a digit or special characters apart from an underscore.
- A name can be composed of lowercase or uppercase alphanumeric characters or an underscore.
- Variable names are case-sensitive in Rust. Hence, the variables age and Age are not the same.
- Although not a requirement, the Rust naming rules define a local variable that should use a snake case for joining names. For example, total_points instead of TotalPoints.
The above are some naming rules to adhere to when naming a variable. You can check the Rust naming rules in the resource below:
https://rust-lang.github.io/api-guidelines/naming.html
Rust Variable Declaration
We can declare a variable using the let or const keyword. After the let keyword, we set the name of the variable and its data type.
The syntax is as shown:
An example program with variable declaration is as shown:
let url:&str;
}
The above declares a variable called url of type &str.
Type annotation in Rust is optional. This means that the compiler can infer the type from the value assigned to the variable.
An example of variable declaration without type annotation is shown below:
let url = "https://linuxhint.com";
}
The const keyword in Rust allows you to define constants variables. Unlike the let keyword, you must perform type annotation when using the const keyword.
An example is as shown:
const URL: &str = "https://linuxhint.com";
}
We cannot change the value of a constant value later in the program after declaration. A constant will also live for the lifetime of the program and has no fixed memory address.
NOTE: Constants are fundamentally immutable, unlike does not support the keyword mut. Check variable mutability sections to learn more.
Second: The value of a constant must be, well, constant. Don’t set the value of a const variable to an expression to be evaluated at runtime.
Third: Although they do follow the rules of variable naming. Constants use SCREAMING_SNAKE_CASE for their names.
Rust Variable Mutability
In Rust, variables are immutable objects by default. This means that we cannot modify their values after declaration.
Take the example below:
let url = "https://linuxhint.com";
url = "https://google.com";
}
If we compile the code above, we should get an error as shown below:
The above output shows that the compiler does not allow the reassignment of variables by default.
However, Rust allows you to create mutable variables. You can use the mut keyword during function declaration to make it mutable.
Consider the example below:
let mut url = "https://linuxhint.com";
url = "https://google.com";
}
The program above creates a mutable variable called url. We can now reassign its value later in the program.
Rust Variable Shadowing
Rust offers a feature known as shadowing; well, most Rustaceans refer it to that. It is a feature in which you can declare a variable with the same name as a variable that has already been declared and set a new value or type for it.
I like to think of it as a way of introducing temporary mutability to a variable.
Take the example program below:
leturl = "https://linuxhint.com";
{
// the second variable shadows the first
leturl = "https://google.com";
println!("{}", url);
}
println!("{}", url);
}
In the program above, we declare an immutable variable called url in the global scope. We then create an inner scope that redeclares the url variable and assigns it a new value. The url variable in the inner scope shadows the global variable in this case.
If we run the program above, we should get an output as:
Keep in mind that variable shadowing differs from variable mutability. This is because,
- we are creating a new variable when using the let keyword.
- This functionality is volatile, and the function remains immutable but with a new value.
Closing
We have explored how variables work in the Rust programming language. Keep practicing or check the documentation to learn more.