Variables, Mutability, and Shadowing
Understand how to declare variables, manage mutability, and use shadowing for flexible variable redefinition.
Intro to Rust Variables
Welcome! In Rust, variables are fundamental for storing data. They're like labeled boxes where you keep information your program needs.
Rust has a strong focus on safety, and this starts with how it handles variables. You'll learn how to declare them, control their changeability, and even 'redefine' them in clever ways.
Declaring Variables with `let`
To create a variable in Rust, we use the let keyword. This assigns a name to a value. Rust often infers the type, so you don't always need to specify it.
Try running this simple example:
fn main() {
let apples = 5;
println!("I have {} apples.", apples);
}All lessons in this course
- Variables, Mutability, and Shadowing
- Primitive Data Types and Operators
- Functions and Control Flow