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:
3
+ New editions might change the way you write Rust -- they add syntax, language,
4
+ and library features but also remove others. For example, ` async ` /` await ` will
5
+ be available in Rust 2018, but not Rust 2015. Despite this it's our intention
6
+ that the migration to new editions is as smooth an experience as possible. It's
7
+ considered a bug if it's difficult to upgrade your crate to a new edition. If
8
+ you have a difficult time then a bug should be filed with Rust itself.
5
9
10
+ Transitioning between editions is built around compiler lints. Fundamentally,
11
+ the process works like this:
12
+
13
+ * Turn on lints to indicate where code is incompatible with a new edition
6
14
* Get your code compiling with no warnings.
7
- * Opt in to the new edition.
8
- * Fix any new warnings that may result .
15
+ * Opt in to the new edition, code should compile .
16
+ * Optionally, enable lints about * idiomatic * code in the new edition .
9
17
10
18
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:
19
+ ` cargo fix ` . It can take suggestions from the compiler and automatically
20
+ re-write your code to comply with new features and idioms, drastically reducing
21
+ the number of warnings you need to fix manually!
32
22
23
+ > ` cargo fix ` is still quite young, and very much a work in development. But it
24
+ > works for the basics! We're working hard on making it better and more robust,
25
+ > but please bear with us for now.
33
26
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
27
+ ## The preview period
45
28
46
29
First, editions have a "preview" phase. This lets you try out the new edition
47
30
in nightly Rust. During the preview, there's an extra step you need to take
@@ -57,20 +40,22 @@ This will ensure that you're enabling all of the relevant features. Note that
57
40
during the time the preview is available, we may continue to add/enable new
58
41
features with this flag!
59
42
60
- ### Running rustfix
43
+ ## Fix edition compatibility warnings
61
44
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 :
45
+ Next up is to enable compiler warnings about code which is incompatible with the
46
+ new 2018 edition. This is where the handy ` cargo fix ` tool comes into the
47
+ picture. To enable the compatibility lints for your project you run :
65
48
66
49
``` shell
67
- $ cargo +nightly fix --prepare-for 2018
50
+ $ cargo +nightly fix --prepare-for 2018 --all-targets
68
51
```
69
52
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.
53
+ This will instruct Cargo to compile all targets in your project (libraries,
54
+ binaries, tests, etc.) and prepare them for the 2018 edition. Cargo will likely
55
+ automatically fix a number of files, informing you as it goes along.
56
+
57
+ If Cargo can't automatically fix everything it'll print out the remaining
58
+ warnings. Continue to run the above command until all warnings have been solved.
74
59
75
60
## Commit to the next edition
76
61
@@ -89,15 +74,31 @@ the `[package]` section. As mentioned above, right now this is a nightly-only
89
74
feature of Cargo, so you need to enable it for things to work.
90
75
91
76
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.
77
+ build`. If it does not, this is a bug! Please [ file an issue] [ issue ] .
78
+
79
+ [ issue ] : https://github.com/rust-lang/rust/issues/new
80
+
81
+ ## Writing idiomatic code in a new edition
94
82
95
- ## Fix new warnings
83
+ Your crate has now entered the 2018 edition of Rust, congrats! Recall though
84
+ that Editions in Rust signify a shift in idioms over time, and while much old
85
+ code will continue to compile it might be written with different idioms today.
86
+
87
+ An optional next step you can take is to update your code to be idiomatic within
88
+ the new edition. This is done with a different set of "idiom lints". To enable
89
+ these lints add this to your ` lib.rs ` or ` main.rs ` :
90
+
91
+ ``` rust
92
+ #![warn(rust_2018_idioms)]
93
+ ```
96
94
97
- To fix up these warnings, we can use ` rustfix ` :
95
+ and then execute :
98
96
99
97
``` shell
100
98
$ cargo +nightly fix
101
99
```
102
100
103
- This will try to fix up all of the new warnings. Congrats! You're done!
101
+ As before Cargo will automatically fix as much as it can, but you may also need
102
+ to fix some warnings manually. Once all warnings have been solved you're not
103
+ only compiling with the 2018 edition but you're also already writing idiomatic
104
+ 2018 code!
0 commit comments