1
1
# Transitioning your code to a new edition
2
2
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
6
15
* 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.
33
18
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!
41
24
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.
43
28
44
- ### The preview period
29
+ ## The preview period
45
30
46
31
First, editions have a "preview" phase. This lets you try out the new edition
47
32
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
57
42
during the time the preview is available, we may continue to add/enable new
58
43
features with this flag!
59
44
60
- ### Running rustfix
45
+ ## Fix edition compatibility warnings
61
46
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 :
65
50
66
51
``` shell
67
- $ cargo +nightly fix --prepare-for 2018
52
+ $ cargo +nightly fix --prepare-for 2018 --all-targets --all-features
68
53
```
69
54
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
+ ```
74
68
75
69
## Commit to the next edition
76
70
@@ -89,15 +83,31 @@ the `[package]` section. As mentioned above, right now this is a nightly-only
89
83
feature of Cargo, so you need to enable it for things to work.
90
84
91
85
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
94
89
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
+ ```
96
103
97
- To fix up these warnings, we can use ` rustfix ` :
104
+ and then execute :
98
105
99
106
``` shell
100
107
$ cargo +nightly fix
101
108
```
102
109
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