Rust Get Started
Setting up your Rust development environment
🚀 Getting Started with Rust
Start your Rust journey by installing the toolchain and creating your first project. Rust provides excellent tooling including Cargo package manager, built-in testing framework, and comprehensive documentation to help you build reliable and efficient software.
# Install Rust (Unix/Linux/macOS)
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
# Create a new project
cargo new hello_world
cd hello_world
cargo run
Output:
Hello, world!
Installation Guide
Windows
Download and run rustup-init.exe
# Download from rustup.rs
# Run rustup-init.exe
# Follow the installer prompts
Linux/macOS
Use the rustup installation script
curl --proto '=https' --tlsv1.2 \
-sSf https://sh.rustup.rs | sh
Verify Installation
Check if Rust is installed correctly
rustc --version
cargo --version
Update Rust
Keep your Rust installation current
rustup update
🔹 Creating Your First Project
Use Cargo to create and manage Rust projects:
Steps to create a new project:
- Open your terminal or command prompt
-
Run
cargo new my_project -
Navigate to the project:
cd my_project -
Run the project:
cargo run
# Create a new binary project
cargo new hello_rust
cd hello_rust
# Project structure created:
# hello_rust/
# ├── Cargo.toml
# └── src/
# └── main.rs
Generated main.rs:
fn main() {
println!("Hello, world!");
}
🔹 Understanding Cargo.toml
Cargo.toml is your project's configuration file:
[package]
name = "hello_rust"
version = "0.1.0"
edition = "2021"
# Dependencies go here
[dependencies]
# Example: serde = "1.0"
Cargo.toml sections:
- [package]: Project metadata (name, version, edition)
- [dependencies]: External crates your project uses
- [dev-dependencies]: Dependencies only for tests
- edition: Rust edition (2015, 2018, 2021)
🔹 Essential Cargo Commands
Master these Cargo commands for Rust development:
# Create a new project
cargo new my_project
# Build the project
cargo build
# Build and run
cargo run
# Run tests
cargo test
# Check code without building
cargo check
# Build optimized release version
cargo build --release
# Add a dependency
cargo add serde
Example output of cargo run:
Compiling hello_rust v0.1.0 (/path/to/hello_rust)
Finished dev [unoptimized + debuginfo] target(s) in 0.75s
Running `target/debug/hello_rust`
Hello, world!
🔹 Your Development Environment
Recommended tools for Rust development:
🔸 Code Editors
- VS Code: With rust-analyzer extension
- IntelliJ IDEA: With Rust plugin
- Vim/Neovim: With rust.vim and coc-rust-analyzer
- Emacs: With rust-mode and lsp-mode
🔸 Useful Tools
# Install useful Rust tools
cargo install cargo-watch # Auto-rebuild on file changes
cargo install cargo-edit # Add/remove dependencies easily
cargo install rustfmt # Code formatter
cargo install clippy # Linter for better code
# Usage examples
cargo watch -x run # Auto-run on changes
cargo fmt # Format your code
cargo clippy # Check for improvements