Introduction
A .rs file holds source code in Rust,
a compiled systems programming language. The Rust compiler, rustc, usually
run through the Cargo build tool, turns a crate (a tree of .rs files) into a
program or library.
A crate’s root file is normally src/main.rs for a program or
src/lib.rs for a library, with a Cargo.toml file in the
folder above that names the crate and its dependencies.
The pages listed below describe the Rust language as a source file uses it: its
syntax, its items, types, expressions, patterns, macros and attributes, and the rules
of ownership, traits, generics and unsafe code. They follow the language chapters of
The Rust Reference for Rust 1.99, the
current stable release in November 2026, and name the Reference chapter for each
topic. They are not a reference to the standard library, to Cargo or to the compiler;
the Not described
section lists what they leave out.
Structure
A Rust source file is plain UTF-8 text. It holds items: functions, structs, enums,
traits, impl blocks and modules. A declaration such as
mod parser; loads a module from parser.rs or
parser/mod.rs, so the files of a crate form a tree that starts at its root
file. Comments starting /// and //! are documentation, which
rustdoc turns into web pages. The
File structure page covers this in full.
use std::env;
/// Prints a greeting for each name given on the command line.
fn main() {
for name in env::args().skip(1) {
println!("Hello, {name}");
}
}
Language reference
The reference is split into these pages.
- File structure – crates, source files, UTF-8, the shebang, inner attributes, editions, how
modmaps to files,build.rsand the Cargo layout - Lexical structure – comments and doc comments, keywords by edition, raw identifiers, every kind of literal, lifetimes and punctuation
- Items – modules, visibility,
use, functions, type aliases, constants, statics and extern blocks - Types – primitive, sequence and pointer types, function types, the never type, trait objects,
impl Traitand layout - Ownership and borrowing – moves,
Copy, references, lifetimes, elision, variance and drop order - Structs, enums and unions – the forms of each, struct expressions, discriminants and union rules
- Traits –
implblocks, associated items, supertraits, coherence and orphan rules, auto traits and dyn compatibility - Generics – parameters,
whereclauses, bounds, const generics and higher-ranked bounds - Expressions – the precedence table, blocks as expressions, operators, casts, closures, ranges, the try operator and constant expressions
- Statements and control flow –
let,let else,if, let chains,match, loops, labels and break with a value - Patterns – every form of pattern, binding modes and refutability
- Macros –
macro_rulessyntax, hygiene, procedural macros and the built-in macros - Attributes – the built-in attributes,
cfgandderive - Error handling – panics,
Result,Optionand theErrortrait - Concurrency and async –
SendandSync, threads, async and await,FutureandPin - Unsafe Rust – unsafe blocks and functions, raw pointers, FFI and the list of undefined behaviour
- Standard library –
core,allocandstd, the prelude and the main modules - Editions and versions – Rust 1.0, the 2015, 2018, 2021 and 2024 editions and what each changed
Not described
Known, not yet covered
- Reference: the pages follow the language chapters of The Rust Reference. They do not describe type inference or trait solving (the types and traits pages cover coercions and coherence), nor the format string syntax of
println!,format!andwrite!(width, precision,{:?}, named arguments), which is in the standard library documentation. - Reference: the standard library page names the modules and the main traits. It does not describe the types, functions and methods inside them (
Vec,HashMap,RcandArc,std::io,std::fmtand the rest); the standard library documentation does. Inline assembly (asm!) is only named, not described. - Reference: the contents of
Cargo.toml([dependencies], features, profiles, workspaces),Cargo.lock,.cargo/config.toml,rustfmt.tomland the Clippy configuration are not described, only the package layout is; the Cargo Book describes them. The compiler flags, the crate file types (.rlib,.rmeta) and nightly-only features are not described either. - Viewer: the contextual words are read from where they stand:
.awaitafter a dot is a keyword, andunionis a keyword only in front of a type name and its body (union U { a: u32 }), so a variable namedunionstays plain. The newer wordgenis plain. - Viewer: only the built-in types and common standard names (
Vec,Option,Box,HashMap) are coloured as types. A struct, enum, trait or generic parameter you declare (S,T) is plain, a path such asstd::iois plain, and a call is coloured as a function by name only. - Viewer: the text of a macro (
macro_rules!bodies, the arguments ofprintln!) is coloured as ordinary code, and the{x:?}parts of a format string are part of the string colour. The file is not parsed, borrow-checked or compiled: there is no outline or list of problems, and only the first 1 MiB is read and coloured.
Links
Software
- Rust – the official toolchain, with the
rustccompiler and Cargo (Windows, macOS, Linux) - rust-analyzer – the language server most editors use for Rust
- Visual Studio Code – free code editor, with rust-analyzer as an extension (Windows, macOS, Linux)
- RustRover – JetBrains’ development environment for Rust (Windows, macOS, Linux)
- Vim – text editor with Rust syntax support (Windows, macOS, Linux)
Developers
- syn – parses Rust source into a syntax tree (Rust)
- tree-sitter-rust – Rust grammar for the Tree-sitter parser used by editors
Reference
- The Rust Reference – the official description of the language
- The Rust Programming Language – the official book
- The Rust Edition Guide – what each edition changed