Rust quick reference
Rust 1.87 (edition 2024) essentials: variables and types, control flow, functions, ownership and borrowing, structs, enums and traits, Option and Result, collections, strings, input and files — and how a Rust program is compiled and run in Zerethon IDE.
Written for Rust 1.87. Examples that show an output were run in the Zerethon sandbox (Rust 1.87) on 2026-10-24.
Basics
-
Hello, World
The smallest Rust program:
fn main()and theprintln!macro. -
Variables and types
letandlet mut, the number types,Stringand&str, constants, shadowing, and parsing text into numbers. -
Conditions and loops
ifas an expression,match, and the three loops:forover a range or iterator,while, andloopwithbreak value. -
Functions and closures
Define functions with
fn, typed parameters and return values, tuples, and closures. -
Ownership and borrowing
Each value has one owner; pass
&to lend it,&mutto let a function change it, and.clone()to copy it.
Data
-
Vectors, arrays and hash maps
Vec, slices,HashMapwith the entry API, sorting, and iterator chains withfilter,mapandcollect. -
Structs, enums and traits
Define data with
structandenum, add methods inimpl, share behaviour with traits, and deriveDebug. -
Option, Result and the ? operator
Values that may be missing (
Option) and operations that may fail (Result), handled withmatch,if let,unwrap_orand?. -
Strings
Stringand&str, common methods, formatting, characters vs bytes, and splitting text.
Input and files
-
Reading input (stdin)
Read what you type in the IDE's Input tab with
read_lineorlines(). -
Reading and writing files
Read project files with
fs::read_to_string, returnio::Resultfrommain, and write temporary files under/tmp. -
Modules and several files
Split a program into files with
mod, make items visible withpub, and what is available without crates.
In Zerethon IDE
-
Running Rust in Zerethon IDE
What Run compiles, the time limit, input, tests, and what a run cannot do.
-
Common errors
Compiler errors such as
E0382 use of moved value,E0502,E0308 mismatched types, and panics such asindex out of boundsandunwraponNone. -
Official documentation
Where to read more: the Rust book, Rust by Example, the standard library and the error index.