You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Getting started with Rust is straightforward thanks to rustup, the official Rust toolchain installer. This lesson covers installation, your first program, and an introduction to Cargo.
The recommended way to install Rust is via rustup, which manages Rust versions and associated tools:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
After installation, verify:
rustc --version
cargo --version
| Component | Description |
|---|---|
| rustc | The Rust compiler |
| cargo | Build system and package manager |
| rustfmt | Code formatter |
| clippy | Linter |
| rust-docs | Offline documentation |
| rust-std | Standard library |
# Update Rust to the latest stable version
rustup update
# Install the nightly toolchain
rustup install nightly
# Set the default toolchain
rustup default stable
# Show installed toolchains
rustup show
Rust has three release channels: stable (every 6 weeks), beta, and nightly.
Create a file called main.rs:
fn main() {
println!("Hello, world!");
}
Compile and run it:
rustc main.rs
./main
# Output: Hello, world!
fn main() — the entry point of every Rust programprintln! — a macro (note the !) that prints to standard output;{} for blocks, not indentationTip: The
!inprintln!means it is a macro, not a function. Macros can accept a variable number of arguments and generate code at compile time.
While rustc compiles single files, Cargo is the standard tool for real Rust projects:
# Create a new project
cargo new hello_cargo
cd hello_cargo
# Build the project
cargo build
# Build and run in one step
cargo run
# Check for errors without producing a binary
cargo check
# Build with optimisations for release
cargo build --release
hello_cargo/
├── Cargo.toml # Project manifest (dependencies, metadata)
├── Cargo.lock # Exact dependency versions (auto-generated)
└── src/
└── main.rs # Main source file
The project manifest:
[package]
name = "hello_cargo"
version = "0.1.0"
edition = "2021"
[dependencies]
# Add crates from crates.io here
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.