1.3Introduction to values and variables

Last updated June 11, 2026

Programs are instructions, but instructions alone are a vending machine with no snacks. What programs operate on is data: numbers, text, and everything else a machine can represent. A single piece of data is called a value: the number 5, the number -6.7, the character 'H', the text "Hello". (Notice the quoting conventions, they matter: single quotes for one character, double quotes for text, no quotes for numbers.)

Where values live

While a program runs, its values are held in the computer's RAM (random access memory, usually just memory). Picture memory as an enormous row of numbered boxes, each able to hold a small piece of data. When your program needs to remember the number 5, it gets stored into one of those boxes until it's needed.

In assembly-era programming (lesson 0.2), you dealt with those box numbers yourself. In Rust you never do. Instead, you give a value a name, and the compiler handles every detail of where it physically lives.

Variables

A named place that stores a value is called a variable, and you create one with a let statement:

let x = 5;

Read it as: "let x be 5." This defines a variable named x and initializes it with the value 5. From then on, using x anywhere in the function means "the value stored in x":

fn main() {
    let x = 5;
    println!("x is {x}");
}
x is 5

That {x} inside the text is a placeholder that gets filled in with the variable's value when the line prints. It's a preview of lesson 1.5, which covers the printing toolkit properly.

The name x is fine for a math-flavored example and terrible for anything else; real variables get names like score or width. Naming is its own small art, and lesson 1.8 is devoted to it.

Key insight

A variable is a name for a box in memory, not the value itself. The value 5 also exists, fixed, in your source code (let x = 5;), but the variable is the box that the 5 gets copied into when the program runs. The same box can hold a different value later, which is exactly where the next lesson picks up.

Every variable has a type

Each value belongs to a type: 5 is an integer (a whole number), 6.7 is a floating-point number (a number with a fractional part), "Hello" is text. A variable has a type too, fixed at the moment it's defined, which determines what kind of values it can hold. A variable holding an integer will never quietly start holding text.

Here's the part that surprises people coming from some other languages: the Rust compiler knows the type of every variable, always, at compile time, before the program ever runs. Usually you don't have to say the type, because the compiler works it out from the value, a trick called type inference:

let x = 5;        // the compiler infers: x is an integer
let y = 6.7;      // inferred: a floating-point number

You can state the type explicitly, with a colon after the name:

let x: i32 = 5;   // x is an i32: a 32-bit integer

i32 is the name of Rust's default integer type. The whole cast of types, and the rules for when inference needs your help, are chapter 4's subject (lesson 4.9 in particular); for now, write let without types and let the compiler do the bookkeeping. The important idea is that the bookkeeping happens: nothing in Rust has a mystery type, ever.

Quiz time

Question #1

What is data, and what is a value?

Show solution

Data is the information a program operates on: numbers, text, and so on. A value is a single piece of data, like 5 or "Hello".

Question #2

What is a variable?

Show solution

A named place in memory that stores a value. The name lets you use the stored value without ever dealing with memory addresses yourself.

Question #3

What does this statement do, piece by piece: let width = 80;

Show solution

It defines a new variable named width and initializes it with the value 80. let introduces the variable, width is its name, and 80 is the value stored into it. The compiler infers its type (an integer).

Question #4

When does the compiler learn a variable's type: while the program runs, or before?

Show solution

Before, at compile time, for every variable in the program, even when you don't write the type yourself (that's type inference).

Question #5

What's wrong with this statement?

let 5 = x;
Show solution

It's backwards. let puts the name on the left and the value on the right: let x = 5;. As written, it tries to use 5 as a name, names can't be numbers, and the program doesn't compile.

A variable whose value never changed wouldn't be very variable, though. Changing them is where Rust has opinions, and that's the next lesson.