Install Rust, make your first project with cargo, print values with format strings, and learn to read a Rust compiler error line by line. The compiler often tells you the fix.
Welcome to Rust from Basics to Async. This series is for you if you’ve written a little code in some language and want to learn Rust properly, from your first project to threads and async code. Every part has pictures of what happens in the machine, worked examples, tips and traps, and exercises with answers.
This first part sets up the tools. We install Rust, make a project with Cargo, run it, and print values with format strings. Then we learn the skill you’ll use in every later part: reading a compiler error. Every program here was run on Rust 1.95, and its output is pasted from the run.
Try this first
Read this program and decide what it prints before you scroll down:
fn main() {
let name = "Ferris";
println!("Hello, {nme}!");
}
It doesn’t print anything. It doesn’t compile:
error[E0425]: cannot find value `nme` in this scope
--> src/main.rs:3:23
|
3 | println!("Hello, {nme}!");
| ^^^
|
help: a local variable with a similar name exists
|
3 | println!("Hello, {name}!");
| +
There’s a typo: nme instead of name. Many languages would only find that while the program runs, or print something empty. Rust refuses to build the program at all. Look at the help: part, though. The compiler noticed the variable called name, guessed what you meant, and showed the fixed line with a + under the missing letter.
That’s typical of Rust. Its compiler is strict, but it explains itself. By the end of this part, you’ll be able to read every line of an error like this one.
Installing Rust with rustup
Rust is installed with a small program called rustup. It downloads the compiler and keeps it up to date. On Linux and macOS, run this in a terminal:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
On Windows, download and run rustup-init.exe from rust-lang.org/tools/install, and follow what it says on screen.
rustup installs three tools you’ll meet in this series:
- rustc, the Rust compiler. It turns your code into a program.
- cargo, the build tool. It makes projects, calls
rustcfor you, runs your program and its tests, and downloads libraries. You’ll typecargofar more often thanrustc. - rustup itself, which updates the other two.
rustup updatemoves you to the newest stable Rust.
Open a new terminal and check that both work:
$ rustc --version
rustc 1.95.0 (59807616e 2026-04-14)
$ cargo --version
cargo 1.95.0 (f2d3ce0bd 2026-03-21)
Your version numbers may be newer. Every program in this series works on 1.95 or later. If the terminal says command not found, close it and open a new one. The installer adds Cargo’s folder to your PATH, and an old terminal doesn’t see that change yet.
Your first project: cargo new
Make a project called hello:
$ cargo new hello
Creating binary (application) `hello` package
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
A package is Cargo’s word for a project. “Binary” means it builds a program you can run, as opposed to a library that other code uses. Cargo made a folder with these files in it:
hello/
Cargo.toml
src/
main.rs
.gitignore
If git is installed, cargo new also makes the folder a git repository. That’s why there’s a .gitignore.
Cargo.toml describes the project:
[package]
name = "hello"
version = "0.1.0"
edition = "2024"
[dependencies]
nameis the project’s name, and the name of the program it builds.versionis the project’s own version number. It’s yours to change.editionpicks a set of language rules. Rust makes a few breaking changes every three years or so, and each project says which edition it was written for. This series uses edition 2024, which is whatcargo newpicks today.[dependencies]lists libraries from other people. It’s empty, and in this series it stays empty except in Part 26. Everything else uses only Rust’s standard library.
src/main.rs is the program itself. Cargo already wrote one:
fn main() {
println!("Hello, world!");
}
It prints:
Hello, world!
fn main() declares a function called main. Every Rust program starts running there. println! prints a line of text. The ! means it’s a macro, not a function. A macro is code that the compiler expands into more code before it compiles it. Part 29 shows how macros work. For now, read println! as “print this line”, and remember to type the !.
cargo run
From inside the hello folder, cargo run builds the program and then runs it:
$ cd hello
$ cargo run
Compiling hello v0.1.0 (/home/you/hello)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 1.57s
Running `target/debug/hello`
Hello, world!
The first three lines come from Cargo. Your folder path and the time will be different. The last line is your program.
Run it a second time:
$ cargo run
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.01s
Running `target/debug/hello`
Hello, world!
There’s no Compiling line this time. Cargo saw that src/main.rs hadn’t changed since the last build, so it ran the program it already had. Change one character in main.rs and it compiles again.
When this series shows a program and says “It prints:”, that output is what appears after Cargo’s lines. To try any program yourself, paste it over src/main.rs and type cargo run.
cargo check
cargo check compiles your code far enough to find every error and warning, and then stops. It doesn’t make a program:
$ cargo check
Checking hello v0.1.0 (/home/you/hello)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.03s
That’s faster than a full build, because the slowest step is turning checked code into machine code, and check skips it. On a big project the difference is large. Use cargo check while you’re writing and fixing errors. Use cargo run when you want to see what the program does.
cargo build, and –release
cargo build makes the program without running it:
$ cargo build
Compiling hello v0.1.0 (/home/you/hello)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 1.56s
$ cargo build --release
Compiling hello v0.1.0 (/home/you/hello)
Finished `release` profile [optimized] target(s) in 2.16s
Those two lines name the two profiles, the two ways Cargo can build:
- The dev profile is what
cargo build,cargo runandcargo checkuse. It’s quick to compile. It isn’t optimized, and it keeps extra information for debuggers. - The release profile is what you get with
--release. The compiler spends longer making the program fast. Use it for the program you ship, and for any time you measure speed.
There’s one more difference, and Part 2 covers it: in the dev profile, Rust checks integer arithmetic for overflow and stops the program if it happens. The release profile leaves that check out.
Both programs end up in a folder called target, which Cargo makes next to src:
You write Cargo.toml and src/main.rs. Cargo writes everything under target/: the debug program in target/debug, and the release program in target/release.
In words: you write Cargo.toml and src/main.rs. cargo check reads them and reports problems, but writes no program. cargo build writes the program to target/debug/hello. cargo build --release writes it to target/release/hello. cargo run does what cargo build does, then runs target/debug/hello. cargo run --release builds and runs the release one.
The program in target is an ordinary program. You can run it without Cargo:
$ ./target/release/hello
Hello, world!
On Windows, the file is target\release\hello.exe.
The target folder also holds Cargo’s working files, and it gets big. That’s why the .gitignore that cargo new wrote contains /target: you never commit it. cargo clean deletes it, and the next build makes it again. On the Linux machine used for this series, the debug hello was about 4.3 MB and the release one about 0.5 MB. The debug build is bigger mostly because of the information it keeps for debuggers.
Printing values with format strings
println! takes a format string, the text in quotes, and fills in the parts in curly braces. There are three ways to say what goes in a {}:
fn main() {
let name = "Ana";
let age = 34;
println!("{} is {} years old", name, age);
println!("{name} is {age} years old");
println!("{0} is {1}. Yes, {0}.", name, age);
println!("In ten years, {name} will be {}", age + 10);
println!("Use {{ and }} to print a brace");
}
It prints:
Ana is 34 years old
Ana is 34 years old
Ana is 34. Yes, Ana.
In ten years, Ana will be 44
Use { and } to print a brace
Line by line:
- Each empty
{}takes the next value after the string, in order. {name}prints the variable calledname. This is usually the clearest form, and the one this series uses most.{0}and{1}pick values by position, counting from 0. You can use one more than once.- A name in braces must be a plain variable. For anything you have to work out, like
age + 10, use an empty{}and put the expression after the string. - To print a real brace, double it:
{{prints{and}}prints}.
{:?} prints the debug version
{} uses the type’s Display format: the way the value should look to a person who uses your program. Some types don’t have one. A pair of numbers could be shown as 3, 4 or (3, 4) or x=3 y=4, and Rust doesn’t pick for you.
{:?} uses the Debug format instead: the way the value should look to you, the programmer. Almost every type in the standard library has one:
fn main() {
let point = (3, 4);
let colors = ["red", "green", "blue"];
let name = "Ferris";
println!("{point:?}");
println!("{:?}", colors);
println!("{name} vs {name:?}");
println!("{point:#?}");
}
It prints:
(3, 4)
["red", "green", "blue"]
Ferris vs "Ferris"
(
3,
4,
)
{point:?}is a name and:?together. The part after the colon says how to format.- The Debug version of a string has quotes around it, so you can see where it starts and ends. That helps when a string has spaces at the end.
{:#?}is “pretty” Debug, with one item per line. It’s easier to read when a value is large.
Display and Debug are traits. A trait is a set of abilities that a type can have. Part 14 shows how to give your own types these two. For now: {} needs Display, {:?} needs Debug.
Width and decimal places
A few more options go after the colon. These are the ones you’ll use most:
fn main() {
let shop = "Corner Cafe";
let coffee = 3.5;
let cake = 4.25;
let total = coffee * 2.0 + cake;
println!("{shop}");
println!("{:<8}{:>8.2}", "coffee", coffee * 2.0);
println!("{:<8}{:>8.2}", "cake", cake);
println!("{:<8}{:>8.2}", "total", total);
}
It prints:
Corner Cafe
coffee 7.00
cake 4.25
total 11.25
{:<8} pads the value to 8 characters and lines it up on the left. {:>8.2} pads to 8 and lines up on the right, and .2 prints exactly two decimal places. Together they make a column of prices that lines up.
Reading a compiler error
Here’s a program that tries to print a pair of numbers with {}:
fn main() {
let point = (3, 4);
println!("The point is {}", point);
}
It prints:
error[E0277]: `({integer}, {integer})` doesn't implement `std::fmt::Display`
--> src/main.rs:3:33
|
3 | println!("The point is {}", point);
| -- ^^^^^ `({integer}, {integer})` cannot be formatted with the default formatter
| |
| required by this formatting parameter
|
= help: the trait `std::fmt::Display` is not implemented for `({integer}, {integer})`
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
Every Rust error has the same parts. Take them from the top:
error[E0277]:and a message. This line says what’s wrong.E0277is the error’s code.({integer}, {integer})is how the compiler writes the type ofpoint: a pair of whole numbers whose exact integer type it hasn’t settled yet. Part 2 explains integer types.--> src/main.rs:3:33says where: filesrc/main.rs, line 3, column 33. Most editors jump straight there if you click it.- The code, with line numbers in a gutter on the left. The
|column is just a margin. - The marks under the code.
^^^^^marks the main problem, herepoint.--marks a related place, here the{}that asked for Display. Each mark has a label. A short|line leads from a mark down to its label when the label doesn’t fit beside it. = help:and= note:lines. These are extra hints. The note here is the fix: use{:?}.
Below the error, Cargo prints two more lines:
For more information about this error, try `rustc --explain E0277`.
error: could not compile `hello` (bin "hello") due to 1 previous error
The last line only says the build failed. The real information is always above it, so scroll up to the first error.
Every error code has a longer page in the compiler. rustc --explain prints it, with examples of code that causes the error and code that fixes it. Here’s the start of the page for the typo at the top of this post:
$ rustc --explain E0425
An unresolved name was used.
“Unresolved” means the compiler couldn’t work out what a name refers to. The same pages are online at doc.rust-lang.org/error_codes.
Not every error has a code. Syntax errors, where the code isn’t valid Rust at all, usually don’t. The next example is one.
Worked example: a missing semicolon
In Rust, most lines inside a function end with ;. Here’s one that doesn’t:
fn main() {
let x = 5
let y = 6;
println!("{x} {y}");
}
It prints:
error: expected `;`, found keyword `let`
--> src/main.rs:2:14
|
2 | let x = 5
| ^ help: add `;` here
3 | let y = 6;
| --- unexpected token
Read it in order:
- The message: the compiler expected a
;, and found the wordletinstead. - The place: line 2, column 14. That’s just past the
5, where the;belongs. - The marks:
^sits just after the5, and its label is the fix:help: add ; here.---under theleton line 3 marks where the compiler noticed the problem.
That second mark is worth remembering. The compiler reads on past the end of line 2, because the statement could carry on to the next line. Only when it reaches let does it know something is missing. So when an error points at the start of a line, look at the end of the line before it.
The fix is the one the help gave:
fn main() {
let x = 5;
let y = 6;
println!("{x} {y}");
}
It prints:
5 6
Worked example: an error that looks scary
This program works out the price of two coffees:
fn main() {
let item = "coffee";
let price = 3.5;
let count = 2;
let total = price * count;
println!("{count} x {item} = {total}");
}
It prints:
error[E0277]: cannot multiply `{float}` by `{integer}`
--> src/main.rs:5:23
|
5 | let total = price * count;
| ^ no implementation for `{float} * {integer}`
|
= help: the trait `Mul<{integer}>` is not implemented for `{float}`
= help: the following other types implement trait `Mul<Rhs>`:
`&f128` implements `Mul<f128>`
`&f128` implements `Mul`
...
and 57 others
That’s a lot of text for one *. Don’t read all of it. Read the first line and the line with the ^:
- The first line: you can’t multiply a
{float}by an{integer}.3.5is a float, a number with a decimal point.2is an integer, a whole number. - The
^: it points at the*on line 5.
That’s enough to understand it. Rust never converts between number types on its own, not even from integer to float. Many languages quietly turn 2 into 2.0 here. Rust makes you say what you mean.
The help lines list every type that does support *. The list is correct and doesn’t help here. When an error is long, the top line and the ^ line usually tell you what you need. Read the rest only when those two don’t.
The simplest fix is to make count a float too. Part 2 shows as, the way to convert between number types. We’ll also print the total with two decimal places:
fn main() {
let item = "coffee";
let price = 3.5;
let count = 2.0;
let total = price * count;
println!("{count} x {item} = {total}");
println!("{count} x {item} = {total:.2}");
}
It prints:
2 x coffee = 7
2 x coffee = 7.00
Notice the first line. The Display format of a float drops a .0 at the end, so 2.0 prints as 2 and 7.0 prints as 7. When you want a fixed number of decimal places, say so with :.2.
Tips and traps
- Reading only the last line. “could not compile … due to 1 previous error” tells you nothing about the problem. Scroll up to the first
error[...]line and start there. - Fixing the errors in the wrong order. One mistake can cause several errors further down. Fix the first one, run
cargo checkagain, and see what’s left. Often the rest go away too. - An error that points at the wrong line. For a missing
;or}, the compiler only notices at the next thing it reads. If the marked line looks fine, look at the line above it. - Confusing warnings with errors. A
warning:doesn’t stop the build. Your program still runs. A common one isunused variable, and its help says to start the name with_if you meant it. Read warnings anyway. They often point at a real bug. {name.len()}in a format string. Braces take only a plain variable name. With anything more, you getinvalid format string. Use{}and pass the expression after the string:println!("{}", name.len()).- Too few values for the
{}s.println!("{} and {}", a)fails with “2 positional arguments in format string, but there is 1 argument”. Count the empty braces and the values after the string. - Measuring speed with a debug build.
cargo runbuilds without optimization, so it can be many times slower than the release build. Usecargo run --releasewhenever speed matters.
When the compiler stops you, read the first error from top to bottom; it usually says what’s wrong, where, and how to fix it.
Exercises
Exercise 1. This program doesn’t compile. Fix it so it prints the sizes are [1, 2, 3].
fn main() {
let sizes = [1, 2, 3];
println!("the sizes are {sizes}");
}
It prints:
error[E0277]: `[{integer}; 3]` doesn't implement `std::fmt::Display`
Exercise 2. Without running it, decide what this prints:
let x = 5;
let y = "five";
println!("{{x}} is {x}, {x:?} and {y:?}");
Exercise 3. This program should print Ferris has 6 letters. It doesn’t compile. Read the error, then fix it.
fn main() {
let name = "Ferris";
println!("{name} has {name.len()} letters");
}
It prints:
error: invalid format string: expected `}`, found `.`
Answers
1. An array has no Display format, for the same reason a pair doesn’t. It has a Debug format, so add :?:
fn main() {
let sizes = [1, 2, 3];
println!("the sizes are {sizes:?}");
}
It prints:
the sizes are [1, 2, 3]
2. {{x}} is an escaped brace, then the letter x, then another escaped brace, so it prints {x} as plain text. {x} and {x:?} both print 5, because a number looks the same in both formats. {y:?} prints the string with quotes around it.
fn main() {
let x = 5;
let y = "five";
println!("{{x}} is {x}, {x:?} and {y:?}");
}
It prints:
{x} is 5, 5 and "five"
3. The error says the compiler expected } right after {name, and found . instead. Only a plain name can go in the braces. Use an empty {} and pass name.len() after the string:
fn main() {
let name = "Ferris";
println!("{name} has {} letters", name.len());
}
It prints:
Ferris has 6 letters
len counts bytes, not letters. For “Ferris” those are the same. Part 5 shows a word where they aren’t.
What’s next
We used let in every program here without saying much about it. In Part 2, we’ll look at variables and types: let and mut, the integer and float types, and what happens when a number gets too big for its type.