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 | shInstalling C compiler on macOS (if needed)
xcode-select --installInstalling C compiler on Ubuntu (if needed)
sudo apt-get update
sudo apt-get install build-essentialInstalling rustup on Windows
Follow the instructions on the Rust website.
Verifying Rust Installation
rustc --versionUpdating Rust
rustup updateUninstalling Rust
rustup self uninstallLocal Documentation
rustup docHello, World!
Creating a Project
mkdir hello_world
cd hello_worldWriting a Rust program
touch main.rs// Filename: main.rs
fn main() {
println!("Hello, world!");
}Compiling the program
rustc main.rsRunning the program
./main # or .\main.exe on WindowsHello, Cargo!
Cargo is the official Rust package manager and build tool. It should have been installed with rustup.
Verifying Cargo installation
cargo --versionCreating a new project with Cargo
cargo new hello_cargo
cd hello_cargoConfiguring 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 runRunning and watching for changes in a Cargo project
cargo install cargo-watch
cargo watch -x runSee more about cargo-watch.
Building a Cargo project
cargo buildcargo build compiles the project and stores the executable in the target/debug directory.
Checking compiling errors in a Cargo project
cargo checkcargo 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 --releasecargo 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