|
1 | 1 | # Transitioning your code to a new edition
|
2 | 2 |
|
3 |
| -In general, to update your code to a new edition, the process |
4 |
| -will look like this: |
| 3 | +Transitioning between editions is built around lints. Fundamentally, the |
| 4 | +process works like this: |
5 | 5 |
|
6 |
| -* Make sure your build is warning-free. |
7 |
| -* Update or add the `edition` key in `Cargo.toml` |
8 |
| -* Fix any warnings, possibly by running `rustfix`. |
| 6 | +* Get your code compiling with no warnings. |
| 7 | +* Opt in to the new edition. |
| 8 | +* Fix any new warnings that may result. |
9 | 9 |
|
10 |
| -At this time, the `edition` key does not yet work, and `rustfix` is in a |
11 |
| -larval state. This section will update as our story gets better here. |
| 10 | +Luckily, we've been working on a tool to help assist with this process, |
| 11 | +`rustfix`. It can take suggestions from the compiler and automatically |
| 12 | +re-write your code to comply with new features and idioms. |
| 13 | + |
| 14 | +> `rustfix` is still quite young, and very much a work in development. But it works |
| 15 | +> for the basics! We're working hard on making it better and more robust, but |
| 16 | +> please bear with us for now. |
| 17 | +
|
| 18 | +## Installing rustfix |
| 19 | + |
| 20 | +You can get `rustfix` from GitHub, and eventually, `crates.io`. Given that you're probably using Cargo, |
| 21 | +you'll want to run this: |
| 22 | + |
| 23 | +```shell |
| 24 | +$ cargo install cargo-fix --git https://github.com/rust-lang-nursery/rustfix |
| 25 | +``` |
| 26 | + |
| 27 | +And that's it! |
| 28 | + |
| 29 | +## Prepare for the next edition |
| 30 | + |
| 31 | +Before we talk about how to move to the new edition, a reminder: |
| 32 | + |
| 33 | + |
| 34 | +* New editions might change the way you write Rust -- they add syntax, |
| 35 | + language, and library features but also remove others. For example, |
| 36 | + `async`/`await` is available in Rust 2018, but not Rust 2015. |
| 37 | +* It's our intention that the migration to new editions is as smooth an experience |
| 38 | + as possible. It's considered a bug if it's difficult to upgrade your crate to |
| 39 | + a new edition. If you have a difficult time then a bug should be filed |
| 40 | + with either rustfix or Rust itself. |
| 41 | + |
| 42 | +With that out of the way, let's get into it! |
| 43 | + |
| 44 | +### The preview period |
| 45 | + |
| 46 | +First, editions have a "preview" phase. This lets you try out the new edition |
| 47 | +in nightly Rust. During the preview, there's an extra step you need to take |
| 48 | +to opt in. At the time of writing, Rust 2018 is in its preview phase. |
| 49 | + |
| 50 | +To do that, add this feature flag to your `lib.rs` or `main.rs`: |
| 51 | + |
| 52 | +```rust |
| 53 | +#![feature(rust_2018_preview)] |
| 54 | +``` |
| 55 | + |
| 56 | +This will ensure that you're enabling all of the relevant features. Note that |
| 57 | +during the time the preview is available, we may continue to add/enable new |
| 58 | +features with this flag! |
| 59 | + |
| 60 | +### Running rustfix |
| 61 | + |
| 62 | +There are some lints that can help you prepare for the next edition, but |
| 63 | +they're not currently turned on by default. `rustfix` has your back though! |
| 64 | +To turn them on and have `rustfix` fix up your code, run this: |
| 65 | + |
| 66 | +```shell |
| 67 | +$ cargo +nightly fix --prepare-for 2018 |
| 68 | +``` |
| 69 | + |
| 70 | +This would turn on those lints, and fix up the project for the 2018 edition. |
| 71 | +If there's something that `rustfix` doesn't know how to fix automatically yet, |
| 72 | +the usual compiler warning will be printed; you'll need to fix those |
| 73 | +manually. Do so until you get a run with no warnings. |
| 74 | + |
| 75 | +## Commit to the next edition |
| 76 | + |
| 77 | +Once you're happy with those changes, it's time to use the new edition. |
| 78 | +Add this to your `Cargo.toml`: |
| 79 | + |
| 80 | +```toml |
| 81 | +cargo-features = ["edition"] |
| 82 | + |
| 83 | +[package] |
| 84 | +edition = '2018' |
| 85 | +``` |
| 86 | + |
| 87 | +That `cargo-features` line should go at the very top; and `edition` goes into |
| 88 | +the `[package]` section. As mentioned above, right now this is a nightly-only |
| 89 | +feature of Cargo, so you need to enable it for things to work. |
| 90 | + |
| 91 | +At this point, your project should compile with a regular old `cargo +nightly |
| 92 | +build`. However, since you've said you're using the new edition, you may get |
| 93 | +more warnings! Time to bust out `rustfix` again. |
| 94 | + |
| 95 | +## Fix new warnings |
| 96 | + |
| 97 | +To fix up these warnings, we can use `rustfix`: |
| 98 | + |
| 99 | +```shell |
| 100 | +$ cargo +nightly fix |
| 101 | +``` |
| 102 | + |
| 103 | +This will try to fix up all of the new warnings. Congrats! You're done! |
0 commit comments