Variables
7 min · Rust
Store values in named boxes called variables.
Enemy 1 of 5
A variable is a named place to store a value so you can use it later without retyping it. In Rust you create one with the let keyword: let name = "Ada"; creates a variable called name and stores the text "Ada" inside it. From that point on, writing name anywhere in your code means "the value stored in name."
To print a variable, you can't just drop it into the quotes of println! directly — text inside quotes is treated as literal characters. Instead, put a pair of curly braces {} where you want the value to appear, and list the variable after a comma: println!("{}", name). Rust matches each {} in order to each extra argument you pass, substituting the value in at print time.
One thing that surprises newcomers coming from Python or JavaScript: variables created with let in Rust are immutable by default. Once you write let name = "Ada";, you cannot reassign name to a different value later without adding the mut keyword (let mut name = ...). This is a deliberate design choice — Rust nudges you toward values that don't change unless you explicitly say they should, which helps prevent whole categories of bugs where a variable's value shifts unexpectedly somewhere in a large program.
Loading editor...