You've already forked comprehensive-rust
mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-12-23 23:12:52 +02:00
These sort of warnings can be distracting when commenting out a few lines of code or demonstrating some other concept. They can be re-enabled for a code block with `warnunused`. I filed https://github.com/rust-lang/mdBook/issues/2527 to get behavior like this upstream.
793 B
793 B
minutes
| minutes |
|---|
| 5 |
Variables
Rust provides type safety via static typing. Variable bindings are made with
let:
fn main() {
let x: i32 = 10;
println!("x: {x}");
// x = 20;
// println!("x: {x}");
}
-
Uncomment the
x = 20to demonstrate that variables are immutable by default. Add themutkeyword to allow changes. -
Warnings are enabled for this slide, such as for unused variables or unnecessary
mut. These are omitted in most slides to avoid distracting warnings. Try removing the mutation but leaving themutkeyword in place. -
The
i32here is the type of the variable. This must be known at compile time, but type inference (covered later) allows the programmer to omit it in many cases.