Skip to content

Commit e6ab811

Browse files
committed
Tweak and update the transition guide
* Reflect that `rustfix` is now just `cargo fix` and distributed directly with Cargo. * Update that enabling the 2018 edition doesn't enable any warnings, but mention the `rust_2018_idioms` lint which does indeed enable warnings. * Tweak some wording here and there with recent tweaks to the workflow
1 parent c1014af commit e6ab811

File tree

2 files changed

+65
-76
lines changed

2 files changed

+65
-76
lines changed

src/editions/index.md

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -42,27 +42,6 @@ You only need to upgrade if you want to take advantage of such features.
4242
## Trying out the 2018 edition
4343

4444
At the time of writing, there are two editions: 2015 and 2018. 2015 is today's
45-
Rust; Rust 2018 will ship later this year. To give Rust 2018 a try, you can
46-
add this feature to your crate root:
47-
48-
```rust
49-
// Opt in to unstable features expected for Rust 2018
50-
#![feature(rust_2018_preview)]
51-
52-
// Opt in to warnings about new 2018 idioms
53-
#![warn(rust_2018_idioms)]
54-
```
55-
56-
and opt in to the new edition in your `Cargo.toml`:
57-
58-
```toml
59-
cargo-features = ["edition"]
60-
61-
[package]
62-
edition = '2018'
63-
```
64-
65-
Like all `#![feature(..)]` flags, this will only work on nightly Rust.
66-
When nightly compilers are released, more functionality is enabled,
67-
so you may experience new warnings, features, and other things.
68-
This is something to keep in mind and is exactly why nightly is unstable!
45+
Rust; Rust 2018 will ship later this year. To transition to the 2018 edition
46+
from the 2015 edition, you'll want to get started with the [transition
47+
guide](editions/transitioning.html).

src/editions/transitioning.md

Lines changed: 62 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,32 @@
11
# Transitioning your code to a new edition
22

3-
Transitioning between editions is built around lints. Fundamentally, the
4-
process works like this:
5-
3+
New editions might change the way you write Rust -- they add new syntax,
4+
language, and library features but also remove features. For example,
5+
`async`/`await` are keywords in Rust 2018, but not Rust 2015. Despite this
6+
it's our intention that the migration to new editions is as smooth an experience
7+
as possible. It's considered a bug if it's difficult to upgrade your crate to a
8+
new edition. If you have a difficult time then a bug should be filed with Rust
9+
itself.
10+
11+
Transitioning between editions is built around compiler lints. Fundamentally,
12+
the process works like this:
13+
14+
* Turn on lints to indicate where code is incompatible with a new edition
615
* Get your code compiling with no warnings.
7-
* Opt in to the new edition.
8-
* Fix any new warnings that may result.
9-
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
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-
16+
* Opt in to the new edition, the code should compile.
17+
* Optionally, enable lints about *idiomatic* code in the new edition.
3318

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.
19+
Luckily, we've been working on Cargo to help assist with this process,
20+
culminating in a new built-in subcommand `cargo fix`. It can take suggestions
21+
from the compiler and automatically re-write your code to comply with new
22+
features and idioms, drastically reducing the number of warnings you need to fix
23+
manually!
4124

42-
With that out of the way, let's get into it!
25+
> `cargo fix` is still quite young, and very much a work in development. But it
26+
> works for the basics! We're working hard on making it better and more robust,
27+
> but please bear with us for now.
4328
44-
### The preview period
29+
## The preview period
4530

4631
First, editions have a "preview" phase. This lets you try out the new edition
4732
in nightly Rust. During the preview, there's an extra step you need to take
@@ -57,20 +42,29 @@ This will ensure that you're enabling all of the relevant features. Note that
5742
during the time the preview is available, we may continue to add/enable new
5843
features with this flag!
5944

60-
### Running rustfix
45+
## Fix edition compatibility warnings
6146

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:
47+
Next up is to enable compiler warnings about code which is incompatible with the
48+
new 2018 edition. This is where the handy `cargo fix` tool comes into the
49+
picture. To enable the compatibility lints for your project you run:
6550

6651
```shell
67-
$ cargo +nightly fix --prepare-for 2018
52+
$ cargo +nightly fix --prepare-for 2018 --all-targets --all-features
6853
```
6954

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.
55+
This will instruct Cargo to compile all targets in your project (libraries,
56+
binaries, tests, etc.) while enabling all Cargo features and prepare them for
57+
the 2018 edition. Cargo will likely automatically fix a number of files,
58+
informing you as it goes along.
59+
60+
If Cargo can't automatically fix everything it'll print out the remaining
61+
warnings. Continue to run the above command until all warnings have been solved.
62+
63+
You can explore more about the `cargo fix` command with:
64+
65+
```shell
66+
$ cargo +nightly fix --help
67+
```
7468

7569
## Commit to the next edition
7670

@@ -89,15 +83,31 @@ the `[package]` section. As mentioned above, right now this is a nightly-only
8983
feature of Cargo, so you need to enable it for things to work.
9084

9185
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.
86+
build`. If it does not, this is a bug! Please [file an issue][issue].
87+
88+
[issue]: https://github.com/rust-lang/rust/issues/new
9489

95-
## Fix new warnings
90+
## Writing idiomatic code in a new edition
91+
92+
Your crate has now entered the 2018 edition of Rust, congrats! Recall though
93+
that Editions in Rust signify a shift in idioms over time. While much old
94+
code will continue to compile it might be written with different idioms today.
95+
96+
An optional next step you can take is to update your code to be idiomatic within
97+
the new edition. This is done with a different set of "idiom lints". To enable
98+
these lints add this to your `lib.rs` or `main.rs`:
99+
100+
```rust
101+
#![warn(rust_2018_idioms)]
102+
```
96103

97-
To fix up these warnings, we can use `rustfix`:
104+
and then execute:
98105

99106
```shell
100107
$ cargo +nightly fix
101108
```
102109

103-
This will try to fix up all of the new warnings. Congrats! You're done!
110+
As before Cargo will automatically fix as much as it can, but you may also need
111+
to fix some warnings manually. Once all warnings have been solved you're not
112+
only compiling with the 2018 edition but you're also already writing idiomatic
113+
2018 code!

0 commit comments

Comments
 (0)