Skip to content
Tutorials Center
My learning

Topics

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.

TOML
[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.

Rust Edition 2024: gen is a reserved keyword

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.

Rust Edition 2024: temporaries are dropped earlier

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:

Terminal
# 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, no extern crate lines, use accepts paths from dependencies directly.
  • async, await, dyn and try became keywords; bare trait objects (Box<Trait>) were warned about, and dyn became 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 IntoIterator by value, so array.into_iter() yields items.
  • panic!("{}") and assert! messages always take a format string; the odd single-argument form is gone.
  • TryFrom, TryInto and FromIterator are in the prelude.
  • Or-patterns work in macro pat fragments; the reserved prefixes ident#, ident"..." and ident'...' were set aside for future syntax.
  • Cargo uses feature resolver version 2.

2024

  • Return-position impl Trait captures all in-scope lifetimes (and generics) by default; use use<..> to opt out precisely.
  • if let scrutinee temporaries drop before the else block, and tail-expression temporaries drop before local variables, which avoids many borrow errors and deadlocks.
  • gen is a reserved keyword.
  • extern blocks must be written unsafe extern, and attributes such as no_mangle and export_name must be #[unsafe(no_mangle)].
  • unsafe_op_in_unsafe_fn warns by default: an unsafe operation inside an unsafe fn needs its own unsafe {} block.
  • static mut references are denied by the static_mut_refs lint.
  • std::env::set_var and remove_var are unsafe functions.
  • Box<[T]> iterates by value with into_iter().
  • Future and IntoFuture are 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.
  • rustfmt has a "style edition" that follows the crate edition by default, so formatting can change when you migrate. See rustfmt.toml.
  • Check the compiler's own guide. The edition guide lists every change; this page is a summary, not a complete list.