-
Notifications
You must be signed in to change notification settings - Fork 137
Document how to transition between editions #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,103 @@ | ||
# Transitioning your code to a new edition | ||
|
||
In general, to update your code to a new edition, the process | ||
will look like this: | ||
Transitioning between editions is built around lints. Fundamentally, the | ||
process works like this: | ||
|
||
* Make sure your build is warning-free. | ||
* Update or add the `edition` key in `Cargo.toml` | ||
* Fix any warnings, possibly by running `rustfix`. | ||
* Get your code compiling with no warnings. | ||
* Opt in to the new edition. | ||
* Fix any new warnings that may result. | ||
|
||
At this time, the `edition` key does not yet work, and `rustfix` is in a | ||
larval state. This section will update as our story gets better here. | ||
Luckily, we've been working on a tool to help assist with this process, | ||
`rustfix`. It can take suggestions from the compiler and automatically | ||
re-write your code to comply with new features and idioms. | ||
|
||
> `rustfix` is still quite young, and very much a work in development. But it works | ||
> for the basics! We're working hard on making it better and more robust, but | ||
> please bear with us for now. | ||
|
||
## Installing rustfix | ||
|
||
You can get `rustfix` from GitHub, and eventually, `crates.io`. Given that you're probably using Cargo, | ||
you'll want to run this: | ||
|
||
```shell | ||
$ cargo install cargo-fix --git https://github.com/rust-lang-nursery/rustfix | ||
``` | ||
|
||
And that's it! | ||
|
||
## Prepare for the next edition | ||
|
||
Before we talk about how to move to the new edition, a reminder: | ||
|
||
|
||
* New editions might change the way you write Rust -- they add syntax, | ||
language, and library features but also remove others. For example, | ||
`async`/`await` is available in Rust 2018, but not Rust 2015. | ||
* It's our intention that the migration to new editions is as smooth an experience | ||
as possible. It's considered a bug if it's difficult to upgrade your crate to | ||
a new edition. If you have a difficult time then a bug should be filed | ||
with either rustfix or Rust itself. | ||
|
||
With that out of the way, let's get into it! | ||
|
||
### The preview period | ||
|
||
First, editions have a "preview" phase. This lets you try out the new edition | ||
in nightly Rust. During the preview, there's an extra step you need to take | ||
to opt in. At the time of writing, Rust 2018 is in its preview phase. | ||
|
||
To do that, add this feature flag to your `lib.rs` or `main.rs`: | ||
|
||
```rust | ||
#![feature(rust_2018_preview)] | ||
``` | ||
|
||
This will ensure that you're enabling all of the relevant features. Note that | ||
during the time the preview is available, we may continue to add/enable new | ||
features with this flag! | ||
|
||
### Running rustfix | ||
|
||
There are some lints that can help you prepare for the next edition, but | ||
they're not currently turned on by default. `rustfix` has your back though! | ||
To turn them on and have `rustfix` fix up your code, run this: | ||
|
||
```shell | ||
$ cargo +nightly fix --prepare-for 2018 | ||
``` | ||
|
||
This would turn on those lints, and fix up the project for the 2018 edition. | ||
If there's something that `rustfix` doesn't know how to fix automatically yet, | ||
the usual compiler warning will be printed; you'll need to fix those | ||
manually. Do so until you get a run with no warnings. | ||
|
||
## Commit to the next edition | ||
|
||
Once you're happy with those changes, it's time to use the new edition. | ||
Add this to your `Cargo.toml`: | ||
|
||
```toml | ||
cargo-features = ["edition"] | ||
|
||
[package] | ||
edition = '2018' | ||
``` | ||
|
||
That `cargo-features` line should go at the very top; and `edition` goes into | ||
the `[package]` section. As mentioned above, right now this is a nightly-only | ||
feature of Cargo, so you need to enable it for things to work. | ||
|
||
At this point, your project should compile with a regular old `cargo +nightly | ||
build`. However, since you've said you're using the new edition, you may get | ||
more warnings! Time to bust out `rustfix` again. | ||
|
||
## Fix new warnings | ||
|
||
To fix up these warnings, we can use `rustfix`: | ||
|
||
```shell | ||
$ cargo +nightly fix | ||
``` | ||
|
||
This will try to fix up all of the new warnings. Congrats! You're done! |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Assumes people use rustup -- fine but should be mentioned