iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

Get Started

Install Rust via rustup, scaffold a project with cargo, build, test, and ship a release binary.

Rust — getting started

EXAMPLE
# ===== 1. Install =====
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
# Windows: download rustup-init.exe from rustup.rs

# Restart shell. Verify:
rustc --version
cargo --version

# ===== 2. Update + components =====
rustup update
rustup component add clippy rustfmt

# ===== 3. Hello, cargo =====
cargo new hello && cd hello
# Generates Cargo.toml + src/main.rs

# src/main.rs (default):
fn main() {
    println!("Hello, world!");
}

cargo run                # debug build + run
cargo build --release    # optimised binary in target/release/
./target/release/hello

# ===== 4. Adding a dependency =====
cargo add serde --features derive
cargo add serde_json
# Or edit Cargo.toml directly.

# src/main.rs
use serde::Serialize;

#[derive(Serialize)]
struct Order { id: u64, total: u64 }

fn main() {
    let o = Order { id: 1, total: 4995 };
    println!("{}", serde_json::to_string(&o).unwrap());
}

# ===== 5. Tests =====
# src/lib.rs
pub fn add(a: i32, b: i32) -> i32 { a + b }

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn it_adds() { assert_eq!(add(2, 3), 5); }
}

cargo test

# ===== 6. Format + lint =====
cargo fmt
cargo clippy --all-targets -- -D warnings

# ===== 7. A tiny HTTP server (axum) =====
cargo add axum tokio --features tokio/full
cargo add serde_json

// src/main.rs
use axum::{routing::get, Json, Router};
use serde_json::json;

#[tokio::main]
async fn main() {
    let app = Router::new().route("/healthz", get(|| async { Json(json!({"ok": true})) }));
    let listener = tokio::net::TcpListener::bind("0.0.0.0:8080").await.unwrap();
    axum::serve(listener, app).await.unwrap();
}

cargo run --release

# ===== Patterns to internalise =====
# - cargo new + cargo add + cargo run + cargo test = your daily loop
# - cargo fmt on save; cargo clippy in CI with -D warnings
# - Release builds for any benchmark; debug builds lie about performance
# - One repo, one Cargo workspace; sub-crates under members:

# ===== Pitfalls =====
# - Confusing debug vs release perf (debug is 10-100x slower)
# - Skipping clippy; misses many idiomatic improvements
# - Using stable types from nightly crates -> friction later
# - cargo.lock not committed for binaries (commit it for binaries; library crates can skip)

Why it matters

rustup + cargo is the whole front door. cargo new, cargo run, cargo test, cargo fmt, cargo clippy. Once these are reflex, building anything in Rust is the same handful of commands plus your code.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
# Install rustup, then:
rustup default stable
cargo --version
cargo new hello && cd hello && cargo run
Try it Yourself »

Discussion

Loading…