Rust Hen House — Rust

Count Mbak Ningrum's eggs with real Cargo: let mut, number types, functions, Vec, String, HashMap. 16 levels, right in the browser.

What you learn in this game

Ms. Ningrum keeps three hundred laying hens at the end of Kweni Lane and has just started learning Rust. She picked Rust because a program that looks wrong will not run: better refused by the compiler than miscounted in front of a buyer. let gives a value a name. In Rust that name stays fixed unless you say otherwise. On every level the program is built with cargo build and run like cargo run, by the real Cargo and rustc 1.99, so its error messages and its printed lines are Rust's own.

Shadowing means declaring a name again with let. The result is a new variable, and the old one is hidden. The new variable may have a different type. That is not the same as let mut: the same variable may change what it holds, but its type stays.

Rust has several sizes of whole number: u8, u16, u32, u64 with no minus sign, and i8, i16, i32, i64 that may be negative. The more bits, the bigger the number that fits. cargo run builds a debug version that checks for overflow. cargo run --release builds a release version that is faster and does not check.

Rust's integers have methods that say what should happen at the limit: checked_sub gives None when the result falls outside the type, saturating_sub stops at the limit, wrapping_sub wraps around. An Option is a value that may be there (Some(value)) or not (None), and match opens it.

as turns one number type into another, for example f64 into u32. It never fails and never complains: a fraction is cut toward zero, and a number too big for the type stops at its limit. Fractions inside a computer are not always exact either: many decimal fractions are stored a hair below or above their value.

A Rust function is written with fn, its parameter types are required, and -> Type names what it returns. Rust blocks have a value: the last line without a semicolon is the value of the block, so no return is needed.