3 / 35

Rust Basics· 10 min read· Lesson 3 of 35

Hello World with Cargo

Part 1 · Rust. Create your first Rust project with Cargo, explore its files, run it, and print the Rusty Bean Café welcome sign with println!.

It’s opening day at Rusty Bean Café, and every café needs a welcome sign. In this lesson you’ll create your first real Rust project with Cargo, look at the files it makes, run your program, and learn how to print text and leave notes in your code.

Creating a project with cargo new

Open a terminal and move to a folder where you’d like to keep your code (for example your home folder or a code folder). Then run:

Terminal window
cargo new rusty_bean_cafe

Cargo replies with something like:

Output
Creating binary (application) `rusty_bean_cafe` package

You’ve created a package: a folder holding everything for one Rust program. “Binary (application)” means it’s a program you can run, as opposed to a library, which is code meant to be used by other programs.

Note

Rust project names use lowercase words joined by underscores, like rusty_bean_cafe. This style is called snake_case, and you’ll see it everywhere in Rust. Package names may also use hyphens, like rusty-bean; Cargo turns the hyphens into underscores when you refer to the package in code (rusty_bean).

Now move into the new folder:

Terminal window
cd rusty_bean_cafe

What’s inside the project

Cargo created this layout for you:

Output
rusty_bean_cafe/
├── .gitignore
├── Cargo.toml
└── src/
└── main.rs
  • Cargo.toml is the project’s settings file. It’s like the café’s sign-in sheet: the project’s name, version, and the list of outside ingredients (libraries) it uses.
  • src/ is where your source code (the code you write) lives.
  • src/main.rs is the starting file of your program. .rs is the file extension for Rust code.
  • .gitignore is for Git, a tool that tracks changes to your code. Cargo also sets up a Git repository for you. You can ignore both for now.

Let’s peek at Cargo.toml:

Cargo.toml
[package]
name = "rusty_bean_cafe"
version = "0.1.0"
edition = "2024"
[dependencies]
  1. [package] starts a section describing your project.
  2. name and version are what you’d expect.
  3. edition tells the compiler which edition (a set of language rules) of Rust to use. Cargo picks the latest for new projects. You may see a different year in older projects, and that’s fine.
  4. [dependencies] is empty for now. Later, this is where we’ll list libraries, including Laravel Rust.

Your first program: main.rs

Open the project folder in VS Code (File → Open Folder…) and look at src/main.rs:

src/main.rs
fn main() {
println!("Hello, world!");
}
  1. fn is short for function, a named block of instructions. main is its name.
  2. main is special. It’s the entry point: the first thing that runs when your program starts.
  3. The () after main holds the function’s inputs. main has none.
  4. Everything between { and } is the body of the function.
  5. println! prints a line of text. The text in double quotes is called a string.
  6. The line ends with a semicolon ;.

Running it with cargo run

In the terminal (inside the rusty_bean_cafe folder), run:

Terminal window
cargo run

You’ll see:

Output
Compiling rusty_bean_cafe v0.1.0 (/path/to/rusty_bean_cafe)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.82s
Running `target/debug/rusty_bean_cafe`
Hello, world!

Three things happened:

  1. Compiling: Cargo asked rustc to turn your code into an executable.
  2. Finished: the build worked. dev means a quick development build, not the slower, fully optimized one.
  3. Running: Cargo ran the program, which printed Hello, world!.

Congratulations, you’re officially a Rust programmer! 🎉

cargo build and cargo check

cargo run is the command you’ll use most. Two cousins are worth knowing:

Terminal window
cargo build
cargo check
  • cargo build compiles your program but doesn’t run it. The executable lands in target/debug/rusty_bean_cafe (with .exe on Windows). The target/ folder is Cargo’s workshop. You never need to edit it.
  • cargo check checks that your code would compile, without producing an executable. It’s faster than a full build, which makes it great for asking “do I have any errors?” while you work.

When you’re ready to ship a program, cargo build --release makes an optimized version in target/release/. It takes longer to compile but runs faster.

Printing with println!

Let’s turn “Hello, world!” into a proper café welcome sign. Replace the contents of src/main.rs with:

src/main.rs
// The café's very first program.
fn main() {
println!("Welcome to Rusty Bean Café!");
println!("Today's menu:");
println!(" Espresso $3.00");
println!(" Latte $4.50"); // our best seller
println!("Price of a latte in cents: {}", 450);
println!("{} coffees ordered so far", 2 + 1);
/* The doors open at 7 a.m.
and close at 5 p.m. */
println!("Open {} to {}", "7am", "5pm");
}

Run it with cargo run:

Output
Welcome to Rusty Bean Café!
Today's menu:
Espresso $3.00
Latte $4.50
Price of a latte in cents: 450
3 coffees ordered so far
Open 7am to 5pm

Step by step:

  1. Each println! prints its text and then moves to a new line.
  2. Spaces inside the quotes are printed exactly, which is how we lined up the menu.
  3. {} is a placeholder. println! replaces it with the value that comes after the comma, so {} becomes 450.
  4. Values can be calculations: 2 + 1 is worked out first, so it prints 3.
  5. You can use several placeholders. They’re filled in order: the first {} gets "7am", the second gets "5pm".

Why the exclamation mark?

The ! means println! is a macro, not a normal function. A macro is code that writes more code for you when your program compiles. That’s how println! can accept any number of placeholders. For now, remember: println! always needs its !.

Warning

Forgetting the ! is one of the most common beginner mistakes. If you write println("Welcome to Rusty Bean Café!");, the compiler responds:

Output
error[E0423]: cannot find function `println` in this scope
--> src/main.rs:2:5
|
2 | println("Welcome to Rusty Bean Café!");
| ^^^^^^^ not found in this scope
|
= note: a macro named `println` exists in another namespace
help: use `!` to invoke the macro
|
2 | println!("Welcome to Rusty Bean Café!");
| +

How to read this error:

  1. The first line says what went wrong: there’s no function called println.
  2. --> src/main.rs:2:5 says where: file src/main.rs, line 2, column 5.
  3. The ^^^^^^^ arrows point at the exact problem in your code.
  4. The help: section shows the fix, with a + marking the character to add.

Always read Rust errors from top to bottom. The answer is very often in the help: line.

Comments

Comments are notes for humans. The compiler ignores them completely. Our program used both kinds:

  • // starts a line comment. Everything after it on that line is ignored. It can sit on its own line or at the end of a line of code.
  • /* ... */ is a block comment that can span several lines.

Most Rust code uses // comments. Use them to explain why the code does something, not to repeat what the code already says clearly. A good comment is like a sticky note on the espresso machine: “Descale every Friday!”

Tip

Comments are handy for temporarily switching off a line of code while you experiment. Put // in front of a println! and it won’t run. In VS Code, Ctrl + / (or Cmd + / on macOS) toggles this for you.

Try it yourself

Change your program so it prints a small receipt for one customer. It should show the café name, two items with their prices in cents using {} placeholders, and the total worked out by Rust (use +). Add one comment explaining what the program does.

Show solution
src/main.rs
// Prints a receipt for one customer's order.
fn main() {
println!("Rusty Bean Café: Receipt");
println!("Latte: {} cents", 450);
println!("Croissant: {} cents", 325);
println!("Total: {} cents", 450 + 325);
}

Output:

Output
Rusty Bean Café: Receipt
Latte: 450 cents
Croissant: 325 cents
Total: 775 cents

Rust does the arithmetic 450 + 325 before printing, so the total is always right. In the next lesson we’ll give these numbers names, so we don’t have to type 450 twice.

Recap

  • cargo new name creates a project with Cargo.toml and src/main.rs.
  • Every program starts running in fn main().
  • cargo run compiles and runs, cargo build only compiles, and cargo check quickly checks for errors.
  • println! prints a line. {} placeholders get filled with the values after the text, in order.
  • The ! marks a macro. Comments start with // (or /* ... */) and are ignored by the compiler.

What’s next

Typing 450 everywhere gets old fast. In Variables and Mutability you’ll learn to give values names and decide which ones are allowed to change.

0 of 35 lessons complete

Search lessons

Type to search all lessons.