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:
cargo new rusty_bean_cafeCargo replies with something like:
Creating binary (application) `rusty_bean_cafe` packageYou’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:
cd rusty_bean_cafeWhat’s inside the project
Cargo created this layout for you:
rusty_bean_cafe/├── .gitignore├── Cargo.toml└── src/ └── main.rsCargo.tomlis 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.rsis the starting file of your program..rsis the file extension for Rust code..gitignoreis 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:
[package]name = "rusty_bean_cafe"version = "0.1.0"edition = "2024"
[dependencies][package]starts a section describing your project.nameandversionare what you’d expect.editiontells 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.[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:
fn main() { println!("Hello, world!");}fnis short for function, a named block of instructions.mainis its name.mainis special. It’s the entry point: the first thing that runs when your program starts.- The
()aftermainholds the function’s inputs.mainhas none. - Everything between
{and}is the body of the function. println!prints a line of text. The text in double quotes is called a string.- The line ends with a semicolon
;.
Running it with cargo run
In the terminal (inside the rusty_bean_cafe folder), run:
cargo runYou’ll see:
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:
- Compiling: Cargo asked
rustcto turn your code into an executable. - Finished: the build worked.
devmeans a quick development build, not the slower, fully optimized one. - 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:
cargo buildcargo checkcargo buildcompiles your program but doesn’t run it. The executable lands intarget/debug/rusty_bean_cafe(with.exeon Windows). Thetarget/folder is Cargo’s workshop. You never need to edit it.cargo checkchecks 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:
// 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:
Welcome to Rusty Bean Café!Today's menu: Espresso $3.00 Latte $4.50Price of a latte in cents: 4503 coffees ordered so farOpen 7am to 5pmStep by step:
- Each
println!prints its text and then moves to a new line. - Spaces inside the quotes are printed exactly, which is how we lined up the menu.
{}is a placeholder.println!replaces it with the value that comes after the comma, so{}becomes450.- Values can be calculations:
2 + 1is worked out first, so it prints3. - 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:
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 namespacehelp: use `!` to invoke the macro |2 | println!("Welcome to Rusty Bean Café!"); | +How to read this error:
- The first line says what went wrong: there’s no function called
println. --> src/main.rs:2:5says where: filesrc/main.rs, line 2, column 5.- The
^^^^^^^arrows point at the exact problem in your code. - 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
// 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:
Rusty Bean Café: ReceiptLatte: 450 centsCroissant: 325 centsTotal: 775 centsRust 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 namecreates a project withCargo.tomlandsrc/main.rs.- Every program starts running in
fn main(). cargo runcompiles and runs,cargo buildonly compiles, andcargo checkquickly 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