Set up your Rust environment and write your first program with Cargo.
Rust gives you memory safety without a garbage collector. It achieves this through a compile-time ownership system that catches memory bugs before your code ever runs.
One command gets you everything:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
This installs rustup (the version manager), rustc (the compiler), and cargo (the build system). Verify it worked:
rustc --version
Run rustup update anytime to get the latest stable release.
Create a file called main.rs:
fn main() {
println!("Hello, world!");
}
Compile and run it:
rustc main.rs
./main
Two things to notice here. First, println! has an exclamation mark. That marks it as a macro, not a function. Macros will be covered later. If it were println without the !, you would be calling a function.
Second, Rust compiles ahead of time. The rustc command produces a standalone binary. You can hand that binary to someone who has never heard of Rust, and they can run it. No runtime, no interpreter, no JVM.
You will rarely use rustc directly. Real Rust projects use Cargo. It handles project creation, building, dependency management, testing, documentation, and publishing.
Cargo is opinionated about project structure. Every Rust project looks the same, so you can jump into any codebase and immediately know where things are.
Create a new project:
cargo new hello_cargo
cd hello_cargo
Cargo creates this structure:
hello_cargo/
Cargo.toml
src/
main.rs
.git/
It even initializes a Git repository. The src/main.rs file already contains a hello world program.
Cargo expects your source files to live inside the src/ directory. Configuration lives at the root. This becomes important as projects grow. You might add tests/ for integration tests, benches/ for benchmarks, and examples/ for example programs. Cargo knows about all of these by convention.
If you already have a directory and want to turn it into a Cargo project, use cargo init instead of cargo new. It creates the same structure without making a new folder.
What file extension do Rust source files use?
Build your project:
cargo build
This creates an executable in target/debug/hello_cargo. The debug build compiles fast but runs slower because it skips optimizations.
Run your project in one step:
cargo run
This compiles (if needed) and runs. Cargo is smart about recompilation. If nothing changed, it skips the compile step entirely.
Check that your code compiles without producing a binary:
cargo check
This is faster than cargo build because it skips code generation. Use it while developing to catch errors quickly.
Build for release:
cargo build --release
This creates an optimized binary in target/release/. The compile takes longer, but the resulting binary runs significantly faster. Use this for benchmarks and production.
Here is the typical workflow: run cargo check constantly while writing code. Run cargo run to test behavior. Run cargo build --release when you need to ship or benchmark.
Open Cargo.toml:
[package]
name = "hello_cargo"
version = "0.1.0"
edition = "2021"
[dependencies]
The [package] section defines your project metadata. The [dependencies] section is where you list external libraries. Right now it is empty.
Say you want to use a random number generator. Add the rand crate:
[package]
name = "hello_cargo"
version = "0.1.0"
edition = "2021"
[dependencies]
rand = "0.8"
Run cargo build, and Cargo fetches rand and its dependencies from crates.io, the official Rust package registry.
The version "0.8" means "any version compatible with 0.8.x". Cargo follows semantic versioning rules. Write "0.8.5" to pin an exact version.
After building with dependencies, you will see a Cargo.lock file. This records the exact versions Cargo resolved. Commit this file. It ensures reproducible builds: every developer and CI server uses identical dependency versions.
Write a program that prints the sum of all numbers from 1 to 100 that are divisible by 3 or 5.
Crates.io is where the Rust community publishes libraries. Browse crates, read documentation, and check download stats. When you add a dependency to Cargo.toml, Cargo pulls from crates.io automatically.
Run cargo doc --open to generate and view documentation for your project and all its dependencies. This builds HTML docs locally. The crate documentation you see on docs.rs uses the same system.
You now have Rust installed and understand the workflow. Create projects with cargo new. Build with cargo build. Run with cargo run. Check syntax quickly with cargo check. Add dependencies to Cargo.toml. Lock versions with Cargo.lock.
In the next lesson, we will explore Rust's type system and how variables work. Then we will discover what makes Rust truly unique: a concept called ownership that eliminates entire categories of bugs at compile time.