Rust Reference ยท Tools
Rust editions: 2015, 2018, 2021 and 2024
Reference for Rust editions: what an edition is, how to choose one, and the main changes in 2018, 2021 and 2024, with cargo fix --edition migration steps.
A Rust edition is an opt-in set of language changes that would be breaking if they were applied everywhere at once. There have been four: 2015, 2018, 2021 and 2024. Each crate chooses its own edition, and crates of different editions link together without trouble, because editions only change how source code is parsed and checked, never the compiled interface. A 2024 crate can depend on a 2015 crate and the other way around.
New editions arrive roughly every three years. They are never a rewrite: most programs need only a few mechanical edits, and the compiler can make many of them for you.
[package] name = "demo" version = "0.1.0" edition = "2024"
Try it
Our editor compiles with --edition 2024, so you can see the newest rules directly.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
The output shows drop scrutinee before else branch, and drop temporary before drop local. Under edition 2021 both orders would be reversed.
Choosing and migrating an edition
The edition is set per crate: edition = "2024" in Cargo.toml, or rustc --edition 2024 on the command line. New projects from cargo new use the latest edition your toolchain supports; a crate without an edition key is treated as 2015.
To migrate a project, work in this order:
# read-only: run these on your own computer cargo fix --edition # applies the automatic rewrites # edit Cargo.toml: edition = "2024" cargo build && cargo test # fix anything left by hand cargo fmt # style edition may have changed
cargo fix --edition first compiles your code under the old edition with "compatibility" lints turned on, and rewrites what it can (for example r#gen, unsafe extern, explicit lifetime captures). Anything it cannot fix is reported as a warning to handle manually.
What changed
2018
- Paths:
crate::for the current crate, noextern cratelines,useaccepts paths from dependencies directly. async,await,dynandtrybecame keywords; bare trait objects (Box<Trait>) were warned about, anddynbecame required in 2021.- Non-lexical lifetimes, and anonymous trait parameters (
fn f(u8);) were removed.
2021
- Disjoint closure captures: a closure captures only the fields it uses, not the whole struct.
- Arrays implement
IntoIteratorby value, soarray.into_iter()yields items. panic!("{}")andassert!messages always take a format string; the odd single-argument form is gone.TryFrom,TryIntoandFromIteratorare in the prelude.- Or-patterns work in macro
patfragments; the reserved prefixesident#,ident"..."andident'...'were set aside for future syntax. - Cargo uses feature resolver version 2.
2024
- Return-position
impl Traitcaptures all in-scope lifetimes (and generics) by default; useuse<..>to opt out precisely. if letscrutinee temporaries drop before theelseblock, and tail-expression temporaries drop before local variables, which avoids many borrow errors and deadlocks.genis a reserved keyword.externblocks must be writtenunsafe extern, and attributes such asno_mangleandexport_namemust be#[unsafe(no_mangle)].unsafe_op_in_unsafe_fnwarns by default: an unsafe operation inside anunsafe fnneeds its ownunsafe {}block.static mutreferences are denied by thestatic_mut_refslint.std::env::set_varandremove_varareunsafefunctions.Box<[T]>iterates by value withinto_iter().FutureandIntoFutureare in the prelude.- The never type fallback changed, so some inferred types that used to be
()are now!(this can turn a compile error into an edition-dependent warning). - Cargo uses resolver version 3, which prefers dependency versions compatible with your
rust-version.
Usage notes
- Editions are not versions. Every compiler release supports all editions; an old crate keeps compiling on the newest compiler.
- Migration is optional. Staying on 2021 is fine; but new language features that need an edition boundary (like the 2024 drop order) only apply once you move.
- Libraries decide independently. Your edition affects your own code and the macros you define, not the crates you use.
- Macro hygiene crosses editions. A macro defined in a 2015 crate keeps its own edition's keyword rules when used from 2024 code.
rustfmthas a "style edition" that follows the crate edition by default, so formatting can change when you migrate. Seerustfmt.toml.- Check the compiler's own guide. The edition guide lists every change; this page is a summary, not a complete list.