Skip to content
Sign inGet all access
Chapter 1·12 minFree

Getting Started

Cargo, the edition trap, and let being immutable by default.

Rust is a project, not a file.

cargo new and cargo run

One command makes a buildable project:

terminal
cargo new hello
Output$ cargo new hello Creating binary (application) `hello` package$ find hello -type fhello/.gitignorehello/Cargo.tomlhello/src/main.rs

Three files. The manifest already names an edition:

Cargo.toml
[package]
name = "hello"
version = "0.1.0"
edition = "2024"

[dependencies]

cargo run compiles and runs:

terminal
cargo run
Output$ cargo run Compiling hello v0.1.0 (/tmp/hello) Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.17s Running `target/debug/hello`Hello, world!

fn main and println!

The entry point, and a macro:

fn main() {
    println!("Hello from Rust");
}
OutputHello from Rust

println! is a macro, not a function. Drop the ! and nothing resolves:

fn main() {
    println("no bang");
}
Output$ cargo builderror[E0423]: cannot find function `println` in this scope --> main.rs:2:5 |2 | println("no bang"); | ^^^^^^^ not found in this scope | = note: a macro named `println` exists in another namespacehelp: use `!` to invoke the macro |2 | println!("no bang"); | +

Macros and functions live in separate namespaces, hence “in another namespace”.

Editions

An edition is an opt-in language version. This compiles on 2015:

fn main() {
    let async = 5;
    println!("{async}");
}
Output5

The same file on edition 2024 does not:

fn main() {
    let async = 5;
    println!("{async}");
}
Output$ cargo builderror: expected identifier, found keyword `async` --> main.rs:2:9 |2 | let async = 5; | ^^^^^ expected identifier, found keyword |help: escape `async` to use it as an identifier |2 | let r#async = 5;

async became a keyword, so an old variable name stops being legal.

Editions let Rust change the language without breaking old crates. One compiler builds all four: 2015, 2018, 2021, 2024.

Format strings

{} takes the next argument; a name inside the braces reads that variable:

fn main() {
    let name = "ada";
    let n = 42;
    println!("{}", name);
    println!("{name}");
    println!("{n} and {name}");
}
Outputadaada42 and ada

{:?} is the debug form, showing the value with its quotes:

fn main() {
    let name = "ada";
    println!("{}", name);
    println!("{:?}", name);
}
Outputada"ada"

{:#?} indents it. The bracketed pair is chapter 2:

fn main() {
    let pair = (1, "ada");
    println!("{:?}", pair);
    println!("{:#?}", pair);
}
Output(1, "ada")( 1, "ada",)

{} for people, {:?} for debugging. Chapter 6 opts your own types in.

Width, precision and radix go after a colon:

fn main() {
    println!("{:>8}|", "ada");
    println!("{:08.3}", 3.14159);
    println!("{:x} {:b} {:o}", 255, 5, 8);
}
Output ada|0003.142ff 101 10

let is immutable

let binds once. Assigning again is a compile error, not a convention:

fn main() {
    let x = 5;
    x = 6;
    println!("{x}");
}
Output$ cargo builderror[E0384]: cannot assign twice to immutable variable `x` --> main.rs:3:5 |2 | let x = 5; | - first assignment to `x`3 | x = 6; | ^^^^^ cannot assign twice to immutable variable |help: consider making this binding mutable |2 | let mut x = 5;

The compiler names the fix. mut makes the intent explicit:

fn main() {
    let mut x = 5;
    let first = x;
    x = 6;
    println!("{first} then {x}");
}
Output5 then 6

Unused is a warning

An unused variable warns and the build still succeeds:

fn main() {
    let unused = 5;
    println!("built anyway");
}
Output$ cargo buildwarning: unused variable: `unused` --> main.rs:2:9 |2 | let unused = 5; | ^^^^^^ help: if this is intentional, prefix it with an underscore: `_unused` | = note: `#[warn(unused_variables)]` (part of `#[warn(unused)]`) on by defaultwarning: 1 warning emitted

The binary runs:

fn main() {
    let _unused = 5;
    println!("built anyway");
}
Outputbuilt anyway

cargo fmt and cargo clippy

Formatting is not a discussion. cargo fmt --check prints a diff and fails:

terminal
cargo fmt --check
Output$ cargo fmt --checkDiff in /tmp/hello/src/main.rs:1:-fn main(){-let name="ada";- println!("{}",name);+fn main() {+ let name = "ada";+ println!("{}", name); }

cargo clippy teaches. It reads working code and says what a Rust programmer writes instead:

terminal
cargo clippy
Output$ cargo clippywarning: the loop variable `i` is only used to index `names` --> src/main.rs:3:14 |3 | for i in 0..names.len() { | ^^^^^^^^^^^^^^ = note: `#[warn(clippy::needless_range_loop)]` on by defaulthelp: consider using an iterator |3 - for i in 0..names.len() {3 + for <item> in &names {

That is the loop you arrive with from C, Java or Go. The suggestion is chapter 11’s.

cargo check

cargo check type-checks without building a binary:

terminal
cargo check
Output$ cargo check # warm cache, one editreal 0.09$ cargo build # warm cache, same editreal 0.14

With 175 dependencies and both caches warm: 0.09s against 0.14s. The saving is code generation.

Command Does
cargo check Type-checks only
cargo build Produces target/debug/<name>
cargo run Builds, then runs it
cargo build --release Optimised, and chapter 2 shows why that changes behaviour