Introduction to Rust Development
Rust is a systems programming language focused on safety, performance, and concurrency. Developers in the Rust communitty are oftan affectionately callled Rustaceans. A notable real-world example of Rust's capabilities is the Redox operating system—a Unix-like OS entirely written in Rust—and its adoption by companies like System76 for low-level systems development.
Installing Rust Using rustup
The recommended way to install Rust is via rustup, a toolchain manager that simplifies installation, updating, and managing different versions of Rust.
On Linux and macOS
Run the following command in your terminal:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
This downloads and runs the official installer script.
On Windows
Download the installer directly from:
https://static.rust-lang.org/rustup/dist/x86_64-pc-windows-msvc/rustup-init.exe
Then run rustup-init.exe and follow the prompts.
Updating and Uninstalling Rust
To keep your toolchain up to date:
rustup update
To check the current version of the Rust compiler:
rustc --version
If you ever need to remove Rust from your system:
rustup self uninstall
Setting Up VS Code for Rust
Visual Studio Code offers excellent support for Rust through extensions:
- Rust: Language support including syntax highlighting and IntelliSense.
- Crates: Assists with managing dependencies in
Cargo.toml. - CodeLLDB: Enables debugging Rust binaries using the LLDB backend.
- Better TOML: Provides improved editing experience for TOML configuration files.
Writing Your First Rust Program
Create a new directory and file:
mkdir hello_world && cd hello_world
touch main.rs
Add the following code to main.rs:
fn main() {
println!("Hello, world!");
}
Compile it using the Rust compiler:
rustc main.rs
Run the generated binary:
./main # On Unix-like systems
# or
.\main.exe # On Windows
Using Cargo: Rust’s Build System and Package Manager
Cargo automates building, testing, and packaging Rust projects. Check if it's installed:
cargo --version
Project Configuration – Cargo.toml
Every Cargo project includes a Cargo.toml manifest file. Example structure:
[package]
name = "hello_cargo"
version = "0.1.0"
edition = "2021"
[dependencies]
Creating a New Project
Generate a new binary project:
cargo new hello_cargo
cd hello_cargo
Building and Running
Build the project (generates an executable in target/debug):
cargo build
Build and run in one step:
cargo run
For faster feedback during development without producing an executable:
cargo check
Exploring Documentation Locally
View documentation for your project and its dependencies in the browser:
cargo doc --open
Configuring Cargo to Use Mirror Registries
In regions where access to crates.io is slow, you can configure Cargo to use a mirror registry by creating a .cargo/config.toml file in your project root or home directory.
[source.crates-io]
replace-with = 'tuna'
# Tsinghua University Mirror
[source.tuna]
registry = "https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git"
# University of Science and Technology of China
[source.ustc]
registry = "git://mirrors.ustc.edu.cn/crates.io-index"
# Shanghai Jiao Tong University
[source.sjtu]
registry = "https://mirrors.sjtug.sjtu.edu.cn/git/crates.io-index"
# Rustcc Community Mirror
[source.rustcc]
registry = "git://crates.rustcc.cn/crates.io-index"
This setup improves dependency resolution speed and reliability when fetching packages.