``
Install Rust, set up an editor, and run your first program. Step by step.
rustup is the Rust installer and version manager. It installs the compiler (rustc), the build tool (cargo), and the standard library.
Open a terminal and run:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | shFollow the prompts. Choose the default option (press 1 or Enter).
After installation, restart your terminal. Verify:
rustc --version
cargo --versionYou should see version numbers for both. If not, restart your terminal or run source $HOME/.cargo/env.
VS Code with the rust-analyzer extension is the recommended setup.
- Install VS Code
- Open VS Code
- Go to Extensions (Cmd+Shift+X on Mac)
- Search for "rust-analyzer" and install it
rust-analyzer provides:
- Autocompletion
- Inline error messages
- Go to definition
- Rename refactoring
Alternative editors: JetBrains RustRover (dedicated Rust IDE), Neovim with rust-analyzer LSP, Helix (built-in LSP support).
Cargo creates and builds Rust projects. Create a new project:
cargo new hello
cd helloCargo creates this structure:
hello/
Cargo.toml # Project metadata and dependencies
src/
main.rs # Your code goes here
Open src/main.rs. Cargo generated a starter program:
fn main() {
println!("Hello, world!");
}Step by step:
fn main()— every Rust program starts here.mainis the entry point.println!— prints text to the terminal. The!means it is a macro (not a function). You will learn the difference later."Hello, world!"— the string to print.
cargo runOutput:
Compiling hello v0.1.0 (/path/to/hello)
Finished `dev` profile [unoptimized + debuginfo] target(s)
Running `target/debug/hello`
Hello, world!
Step by step:
cargo runcompiles your code (if changed) and runs the binary- First run compiles. Subsequent runs are faster if code has not changed.
- The output "Hello, world!" is from your
println!call.
cargo build --releaseThis creates an optimized binary at target/release/hello. Release builds are slower to compile but significantly faster to run. Use cargo run during development and cargo build --release for production.
| Command | What it does |
|---|---|
cargo new <name> |
Create a new project |
cargo run |
Compile and run (dev mode) |
cargo build |
Compile without running |
cargo build --release |
Compile with optimizations |
cargo check |
Check for errors without building (faster) |
cargo test |
Run tests |
cargo fmt |
Format your code |
cargo clippy |
Run the linter |
Use cargo check during development. It is faster than cargo build because it skips code generation.
- "command not found: cargo" — restart your terminal or run
source $HOME/.cargo/env - "linker 'cc' not found" — install a C compiler. On Linux:
sudo apt install build-essential. On Mac:xcode-select --install. - Compilation errors — copy the error message. Rust errors are detailed and often tell you exactly what to fix.
Now that your environment works, the next file teaches variables and mutability — the first Rust concept that surprises people coming from other languages.