|
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 crates.io. Given that you're probably using Cargo, |
| 21 | +you'll want to run this: |
| 22 | + |
| 23 | +```shell |
| 24 | +$ cargo +nightly install cargo-fix --git https://github.com/killercup/rustfix.git |
| 25 | +``` |
| 26 | + |
| 27 | +And that's it! Note that you do need a nightly Cargo to use `rustfix` at the |
| 28 | +moment; in fact, all of the edition stuff is nightly only for now. Eventually |
| 29 | +this will be stable! Same with installing from `git`, for now, it's needed. |
| 30 | + |
| 31 | +## Prepare for the next edition |
| 32 | + |
| 33 | +There are some lints that can help you prepare for the next edition, but |
| 34 | +they're not currently turned on by default. `rustfix` has your back |
| 35 | +though! To turn them on and have `rustfix` fix up your code, run this: |
| 36 | + |
| 37 | +```shell |
| 38 | +$ cargo +nightly fix --prepare-for 2018 |
| 39 | +``` |
| 40 | + |
| 41 | +This would turn on those lints, and fix up the project for the 2018 edition. |
| 42 | +If there's something that `rustfix` doesn't know how to fix automatically yet, |
| 43 | +the warning will be printed; you'll need to fix those manually. Do so |
| 44 | +until you get a run with no warnings. |
| 45 | + |
| 46 | +## Commit to the next edition |
| 47 | + |
| 48 | +Once you're happy with those changes, it's time to use the new edition. |
| 49 | +Add this to your `Cargo.toml`: |
| 50 | + |
| 51 | +```toml |
| 52 | +cargo-features = ["edition"] |
| 53 | + |
| 54 | +[package] |
| 55 | +edition = '2018' |
| 56 | +``` |
| 57 | + |
| 58 | +That `cargo-features` line should go at the very top; and `edition` goes into |
| 59 | +the `[package]` section. As mentioned above, right now this is a nightly-only |
| 60 | +feature of Cargo, so you need to enable it for things to work. |
| 61 | + |
| 62 | +At this point, your project should compile with a regular old `cargo build`. |
| 63 | +However, since you've said you're using the new edition, you may get more |
| 64 | +warnings! Time to bust out `rustfix` again. |
| 65 | + |
| 66 | +## Fix new warnings |
| 67 | + |
| 68 | +To fix up these warnings, we can use `rustfix`: |
| 69 | + |
| 70 | +```shell |
| 71 | +$ cargo +nightly fix |
| 72 | +``` |
| 73 | + |
| 74 | +This will fix up all of the new warnings. Congrats! You're done! |
0 commit comments