Use cargo fix --edition to automatically migrate your codebase to a newer Rust edition by applying compiler suggestions and updating syntax. This command runs in two phases: first, it suggests changes, and second, it applies them to your source files after you confirm.
To migrate a project to the latest edition (e.g., 2021 or 2024), run the following command in your project root:
cargo fix --edition
If you want to migrate to a specific edition, specify it explicitly:
cargo fix --edition 2021
By default, cargo fix will prompt you to review each change before applying it. To apply all changes automatically without interactive prompts (useful for CI/CD pipelines), add the --allow-dirty and --allow-staged flags if you have uncommitted changes, or simply use --allow-dirty to skip the dirty workspace check:
cargo fix --edition --allow-dirty
Note that cargo fix only handles syntactic changes and simple semantic updates that the compiler can safely infer. It will not fix logic errors or complex refactoring needs. After running the command, always run cargo build and cargo test to ensure your application still compiles and behaves as expected. If the migration encounters code that cannot be automatically fixed, the tool will stop and report the specific errors, requiring manual intervention.
For example, if you are upgrading from Rust 2018 to 2021, cargo fix will automatically update use statements, adjust async/await syntax, and fix deprecated macro invocations. However, if you have custom macros or complex generic bounds that changed behavior between editions, you will need to manually review the compiler warnings that appear after the automated pass.
Always ensure your Cargo.toml specifies the target edition before running the fix, or the command will update it for you:
[package]
name = "my-project"
version = "0.1.0"
edition = "2021"
If you are working in a monorepo, run the command from the root workspace directory to migrate all members at once. Remember that edition upgrades can sometimes introduce breaking changes in third-party dependencies, so it is best practice to update your Cargo.lock and dependencies before or immediately after running the fix.