Declaring Variables with let
let is how you tell Rust "I want to remember this value under a name."Basic let Bindings
The let keyword creates a new variable binding, associating a name with a value. Once assigned, that value can be read anywhere later in the same scope.
Example: Basic let Bindings
fn main() {
let language = "Rust";
println!("Learning {}", language);
}
Login to try C/C++/Java/PHP code in the editor
Type Annotations
Although Rust can usually infer a variable's type from its value, you can add an explicit type annotation after a colon. This is required when the type cannot be inferred, and is sometimes used for clarity.
Example: Type Annotations
fn main() {
let count: i32 = 42;
println!("count = {}", count);
}
Login to try C/C++/Java/PHP code in the editor
Immutability by Default
Variables created with let are immutable by default, meaning you cannot assign a new value to them after creation. Attempting to do so is a compile error, which is Rust's way of preventing accidental changes.
Note: Try uncommenting a reassignment line for an immutable variable locally -- the compiler error message is very descriptive.
Example: Immutability by Default
fn main() {
let pi = 3;
println!("pi is approximately {}", pi);
// pi = 4; // this line would fail to compile
}
Login to try C/C++/Java/PHP code in the editor
Scope of Variables
A variable is only valid within the block of code (the curly braces) where it was declared. Once that block ends, the variable goes out of scope and can no longer be accessed.
Example: Scope of Variables
fn main() {
{
let inner = "only visible here";
println!("{}", inner);
}
println!("Outer scope continues");
}
Login to try C/C++/Java/PHP code in the editor
- Forgetting that variables declared with plain
letare immutable by default and cannot be reassigned. - Trying to use a variable before it has been given a value with
let. - Assuming
letalways needs an explicit type, when Rust can usually infer it from the assigned value.
letbinds a name to a value; by default the binding is immutable.- Rust infers types automatically in most cases, but you can annotate them explicitly with
let x: i32 = 5;. - A variable must be initialized before it is used.
- Bindings are scoped to the block
{ }they are declared in.
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: