Appendix E · Editions and compatibility.
See how an edition changes source-language rules while crates continue to work together.
The idea behind the syntax.
A Rust edition is a versioned set of source-language rules selected per crate in its manifest. It lets the language evolve without forcing every existing project to rewrite at once. A crate using an older edition can depend on a crate using a newer edition because the compiled interface remains compatible. The edition is therefore not a separate runtime or a different Rust language installed alongside the compiler.
The current reading course uses the 2024 edition. That affects a few syntax and inference rules compared with older editions, so a code example from an older book or repository may need explanation before it is copied into a newer crate. The older PDF reference predates this edition; use it for conceptual breadth while checking edition-sensitive syntax against current documentation.
What Rust is checking.
A compiler has a release version and a crate has an edition. These are separate facts: a recent compiler can compile crates in several editions, but an old compiler cannot understand language features added after its release. cargo fix --edition can automate many migration edits, yet the migration still deserves review and tests because a tool cannot know every intended behavior.
Edition boundaries encourage a useful reading habit. Identify the edition and toolchain assumptions before interpreting unusual syntax, a newly reserved keyword, or a compiler diagnostic. Then follow ownership, types, and control flow as usual; most of Rust's core ideas persist across editions. The examples below show the manifest setting and ordinary code whose meaning depends on those same core rules.
See the rule in context.
Each example isolates one part of the model. Follow the bindings, types, ownership transfers, and possible outcomes line by line.
Rust, rustc & Cargo
Rust compiles source into a native executable. The compiler checks types and borrowing before your program runs; it cannot decide whether your product requirements are correct. rustc is the compiler. Cargo manages packages, builds, tests, and dependencies. Start with a small program you can explain, then use cargo check for fast feedback. A package created with cargo new has a Cargo.toml manifest and a src/main.rs binary entry point. In this course, each foundation example is complete and uses only the standard library; the cookbook introduces ecosystem crates in real projects.
fn main() {
let project = "Build Lab";
println!("Hello, {project}!");
println!("Next: change one thing, then rebuild.");
}Hello, Build Lab! Next: change one thing, then rebuild.
- main is the entry point for this binary.
- println! is a macro; the exclamation mark is part of its name.
- Build the workbook crate with cargo run -p lesson_welcome, or compile this file with rustc --edition 2024 main.rs.
The boundary: A successful compile is evidence of language-rule compliance, not a proof that the program meets its specification.
Cargo manifests & dependencies
Cargo.toml declares package metadata, an edition, and dependencies. Cargo.lock records resolved dependency versions for reproducibility and should be committed for these executable workbook projects. An edition selects language rules and migration boundaries; it is not the compiler version. cargo check type-checks without a final executable, cargo test runs tests, cargo fmt formats, and cargo clippy offers additional diagnostics. Features are additive configuration switches: inspect which optional capabilities a dependency enables instead of adding every feature by default.
fn main() {
let manifest = "[package]\nname = \"first-build\"\nversion = \"0.1.0\"\nedition = \"2024\"\n";
println!(
"{}",
manifest
.lines()
.find(|line| line.starts_with("edition"))
.unwrap()
);
}edition = "2024"
- A manifest is configuration rather than Rust source.
- The workbook generates a proper manifest per lesson.
- Run cargo check, cargo test, and cargo fmt --check from the workspace root as appropriate.
The boundary: A semver requirement such as 1 allows compatible updates. A committed lockfile pins the actual resolved graph until updated.
Declarative macros
Macros operate on token patterns before normal type checking of the expanded code. macro_rules! is useful for small repeated syntactic patterns such as constructing a collection, while derive macros generate trait implementations. A function should remain your first choice when ordinary typed parameters express the problem. Macro expansion errors can point into generated code, so keep macro interfaces narrow. The ! distinguishes a macro invocation such as vec! or println! from a function call.
macro_rules! labels {
($($item:expr),* $(,)?) => { vec![$($item.to_string()),*] };
}
fn main() {
let values = labels!["build", 42, true];
println!("{values:?}");
}["build", "42", "true"]
- The repetition accepts comma-separated expressions.
- Each expression is converted to String.
- The optional final comma is supported explicitly.
The boundary: Avoid macros that evaluate a side-effecting argument multiple times unless that behavior is documented and intended.
Keep this model.
The compiler version determines available features; a crate's edition selects its source rules. Compatibility across editions lets libraries evolve without splitting the ecosystem.