Setup
Get Rust installed, create a project, and configure your editor in under 10 minutes.
Install Rustup
Rustup manages Rust versions, toolchains, and components.
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
On Windows, download and run rustup-init.exe instead.
After installation, restart your shell or run:
source "$HOME/.cargo/env"
Verify it works:
rustc --version # e.g. rustc 1.87.0
cargo --version # e.g. cargo 1.87.0
rustup --version # e.g. rustup 1.28.0
Update Rust
rustup update # update to latest stable
rustup update nightly # install/update nightly (optional)
Create a Project
cargo new my-project # binary project (default)
cargo new my-lib --lib # library project
cd my-project
Project Structure
my-project/
Cargo.toml # manifest: dependencies, metadata, build config
src/
main.rs # entry point (binary) or lib.rs (library)
Cargo.toml
[package]
name = "my-project"
version = "0.1.0"
edition = "2024" # Rust edition (language version)
[dependencies]
serde = { version = "1", features = ["derive"] }
tokio = { version = "1", features = ["full"] }
src/main.rs
fn main() {
println!("Hello, world!");
}
Build and Run
cargo run # compile and run (debug mode)
cargo run --release # compile with optimizations and run
cargo build # compile only
cargo build --release # optimized build (output in target/release/)
cargo check # type-check without building (fastest feedback)
Tip: Use
cargo checkduring development for the fastest feedback loop. It skips code generation, so it’s much faster thancargo build.
| Command | Speed | Output |
|---|---|---|
cargo check | Fastest | No binary, just type checking |
cargo build | Moderate | Debug binary in target/debug/ |
cargo build --release | Slowest | Optimized binary in target/release/ |
Add Dependencies
cargo add serde --features derive # add with features
cargo add tokio -F full # -F is shorthand for --features
cargo add clap -F derive
cargo remove serde # remove a dependency
Or edit Cargo.toml directly:
[dependencies]
reqwest = { version = "0.12", features = ["json"] }
Then cargo build downloads and compiles everything automatically.
Gotcha:
cargo addrequirescargo-editon older Rust versions, but it’s built into Cargo since Rust 1.62.
Testing
cargo test # run all tests
cargo test test_name # run tests matching a name
cargo test -- --nocapture # show println! output
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
#[test]
fn test_greeting() {
let result = greet("Alice");
assert!(result.contains("Alice"));
}
}
IDE Setup with rust-analyzer
rust-analyzer provides autocomplete, inline type hints, go-to-definition, and error highlighting.
VS Code
Install the “rust-analyzer” extension (id: rust-lang.rust-analyzer). It replaces the old “Rust” extension.
Recommended VS Code settings:
{
"rust-analyzer.check.command": "clippy",
"rust-analyzer.inlayHints.chainingHints.enable": true,
"rust-analyzer.inlayHints.typeHints.enable": true
}
Other Editors
| Editor | Plugin |
|---|---|
| Neovim | Built-in LSP + rust-analyzer binary |
| Zed | Built-in Rust support |
| IntelliJ | ”Rust” plugin (JetBrains) or RustRover IDE |
| Helix | Built-in LSP support |
Useful Cargo Commands
cargo fmt # format code (install: rustup component add rustfmt)
cargo clippy # lint for common mistakes (install: rustup component add clippy)
cargo doc --open # generate and open documentation
cargo bench # run benchmarks
cargo tree # show dependency tree
Tip: Set up
cargo clippyin your CI. It catches subtle bugs and suggests idiomatic improvements that the compiler doesn’t flag.
Cargo Watch (auto-rebuild)
cargo install cargo-watch
cargo watch -x check # re-check on file save
cargo watch -x 'test -- --nocapture' # re-test on file save