Getting Started
📚 Chapter 1
This Chapter is an introduction to Rust and its ecosystem. It covers installing Rust, writing a simple program, and using Cargo, the Rust package manager and build tool.
There are no quizzes for this Chapter.
You can skip this Chapter if you're already familiar with commands like rustc
and cargo
.
Installation
rustup
is a command line tool for managing Rust versions and associated tools.
Installing rustup on Linux or macOS
curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh
Installing C compiler on macOS (if needed)
xcode-select --install
Installing C compiler on Ubuntu (if needed)
sudo apt-get update
sudo apt-get install build-essential
Installing rustup on Windows
Follow the instructions on the Rust website.
Verifying Rust Installation
rustc --version
Updating Rust
rustup update
Uninstalling Rust
rustup self uninstall
Local Documentation
rustup doc
Hello, World!
Creating a Project
mkdir hello_world
cd hello_world
Writing a Rust program
touch main.rs
// Filename: main.rs
fn main() {
println!("Hello, world!");
}
Compiling the program
rustc main.rs
Running the program
./main # or .\main.exe on Windows
Hello, Cargo!
Cargo is the official Rust package manager and build tool. It should have been installed with rustup
.
Verifying Cargo installation
cargo --version
Creating a new project with Cargo
cargo new hello_cargo
cd hello_cargo
Configuring a Cargo project
Cargo uses a Cargo.toml
file to store configuration details.
Cargo.toml
# Filename: Cargo.toml
[package]
name = "hello_cargo"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
Running a Cargo project
cargo run
Running and watching for changes in a Cargo project
cargo install cargo-watch
cargo watch -x run
See more about cargo-watch.
Building a Cargo project
cargo build
cargo build
compiles the project and stores the executable in the target/debug
directory.
Checking compiling errors in a Cargo project
cargo check
cargo check
checks the project for compiling errors without producing an executable. It's faster than cargo build
.
Building a Cargo project for release
cargo build --release
cargo build --release
compiles the project with optimizations and stores the executable in the target/release
directory.
Progress on this Page
There are no quizzes for this chapter.
References
- Read this Chapter on the official Rust book: Getting Started - The Rust Programming Language