In this section, we will explore how to declare and use variables in Rust, and understand the concept of mutability. Rust is a systems programming language that emphasizes safety and performance, and its approach to variables and mutability is a key part of this.
Key Concepts
- Variable Declaration
- Mutability
- Shadowing
- Constants
- Variable Declaration
In Rust, variables are immutable by default. This means that once a value is bound to a variable, it cannot be changed. To declare a variable, you use the let keyword.
Example
Explanation
let x = 5;declares a variablexand binds the value5to it.println!("The value of x is: {}", x);prints the value ofx.
- Mutability
If you need to change the value of a variable, you can make it mutable by using the mut keyword.
Example
fn main() {
let mut y = 10;
println!("The initial value of y is: {}", y);
y = 20;
println!("The new value of y is: {}", y);
}Explanation
let mut y = 10;declares a mutable variableyand binds the value10to it.y = 20;changes the value ofyto20.
- Shadowing
Rust allows you to declare a new variable with the same name as a previous variable. This is called shadowing. The new variable shadows the previous one.
Example
fn main() {
let z = 15;
println!("The value of z is: {}", z);
let z = z + 5;
println!("The shadowed value of z is: {}", z);
}Explanation
let z = 15;declares a variablezand binds the value15to it.let z = z + 5;declares a new variablezthat shadows the previouszand binds the value20to it.
- Constants
Constants are always immutable and must be annotated with a type. They are declared using the const keyword and can be declared in any scope, including the global scope.
Example
Explanation
const MAX_POINTS: u32 = 100_000;declares a constantMAX_POINTSof typeu32and binds the value100,000to it.- Constants are always immutable and their value must be known at compile time.
Practical Exercises
Exercise 1
Declare a mutable variable a with an initial value of 5, change its value to 10, and print both values.
Solution
fn main() {
let mut a = 5;
println!("The initial value of a is: {}", a);
a = 10;
println!("The new value of a is: {}", a);
}Exercise 2
Declare a variable b, shadow it with a new value, and print both values.
Solution
fn main() {
let b = 3;
println!("The value of b is: {}", b);
let b = b * 2;
println!("The shadowed value of b is: {}", b);
}Exercise 3
Declare a constant PI with a value of 3.14 and print it.
Solution
Common Mistakes and Tips
- Forgetting
mutfor mutable variables: If you try to change the value of an immutable variable, Rust will throw a compile-time error. - Shadowing confusion: Remember that shadowing creates a new variable, so the type can change, but it can also lead to confusion if overused.
- Constants must be annotated: Unlike variables, constants require explicit type annotations.
Conclusion
In this section, we covered the basics of variables and mutability in Rust. We learned how to declare variables, make them mutable, use shadowing, and declare constants. Understanding these concepts is crucial for writing safe and efficient Rust code. In the next section, we will dive into Rust's data types.
