Skip to content

Commit 475ddcb

Browse files
committed
re-write the transition section to reflect rc1
1 parent d35c81f commit 475ddcb

File tree

3 files changed

+123
-144
lines changed

3 files changed

+123
-144
lines changed

src/SUMMARY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
- [What are editions?](editions/index.md)
88
- [Creating a new project](editions/creating-a-new-project.md)
9-
- [Transitioning your code to a new edition](editions/transitioning-your-code-to-a-new-edition.md)
9+
- [Transitioning an existing project to a new edition](editions/transitioning-an-existing-project-to-a-new-edition.md)
1010

1111
## Rust 2015
1212

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# Transitioning an existing project to a new edition
2+
3+
New editions might change the way you write Rust – they add new syntax,
4+
language, and library features, and also remove features. For example, `try`,
5+
`async`, and `await` are keywords in Rust 2018, but not Rust 2015. If you
6+
have a project that's using Rust 2015, and you'd like to use Rust 2018 for it
7+
instead, there's a few steps that you need to take.
8+
9+
> It's our intention that the migration to new editions is as smooth an
10+
> experience as possible. If it's difficult for you to upgrade to Rust 2018,
11+
> we consider that a bug. If you run into problems with this process, please
12+
> [file a bug](https://github.com/rust-lang/rust/issues/new). Thank you!
13+
14+
Here's an example. Imagine we have a crate that has this code in
15+
`src/lib.rs`:
16+
17+
```rust
18+
trait Foo {
19+
fn foo(&self, Box<Foo>);
20+
}
21+
```
22+
23+
This code uses an anonymous parameter, that `Box<Foo>`. This is [not
24+
supported in Rust 2018](../rust-2018/trait-system/no-anon-params.html), and
25+
so this would fail to compile. Let's get this code up to date!
26+
27+
## Updating your code to be compatible with the new edition
28+
29+
Your code may or may not use features that are incompatible with the new
30+
edition. In order to help transition to Rust 2018, we've included a new
31+
subcommand with Cargo. To start, let's run it:
32+
33+
```console
34+
> cargo fix --edition
35+
```
36+
37+
This will check your code, and automatically fix any issues that it can.
38+
Let's look at `src/lib.rs` again:
39+
40+
```rust
41+
trait Foo {
42+
fn foo(&self, _: Box<Foo>);
43+
}
44+
```
45+
46+
It's re-written our code to introduce a parameter name for that trait object.
47+
In this case, since it had no name, `cargo fix` will replace it with `_`,
48+
which is conventional for unusued variables.
49+
50+
`cargo fix` is still pretty new, and so it can't always fix your code automatically.
51+
If `cargo fix` can't fix something, it will print the warning that it cannot fix
52+
to the console. If you see one of these warnings, you'll have to update your code
53+
manually. See the corresponding section of this guide for help, and if you have
54+
problems, please seek help at the [user's forums](https://users.rust-lang.org/).
55+
56+
Keep running `cargo fix --edition` until you have no more warnings.
57+
58+
Congrats! Your code is now valid in both Rust 2015 and Rust 2018!
59+
60+
## Enabling the new edition to use new features
61+
62+
In order to use some new features, you must explicitly opt in to the new
63+
edition. Once you're ready to commit, change your `Cargo.toml` to add the new
64+
`edition` key/value pair. For example:
65+
66+
```toml
67+
[package]
68+
name = "foo"
69+
version = "0.1.0"
70+
authors = ["Your Name <you@example.com>"]
71+
edition = "2018"
72+
```
73+
74+
If there's no `edition` key, Cargo will default to Rust 2015. But in this case,
75+
we've chosen `2018`, and so our code is compiling with Rust 2018!
76+
77+
## Writing idiomatic code in a new edition
78+
79+
Editions are not only about new features and removing old ones. In any programming
80+
language, idioms change over time, and Rust is no exception. While old code
81+
will continue to compile, it might be written with different idioms today.
82+
83+
Our sample code contains an outdated idiom. Here it is again:
84+
85+
```rust
86+
trait Foo {
87+
fn foo(&self, _: Box<Foo>);
88+
}
89+
```
90+
91+
In Rust 2018, it's considered idiomatic to use the [`dyn`
92+
keyword](../rust-2018/trait-system/dyn-trait-for-trait-objects.html) for
93+
trait objects.
94+
95+
We can ask `cargo fix` to fix it:
96+
97+
```console
98+
$ cargo fix --edition-idioms
99+
```
100+
> The `--edition-idioms` flag applies only to the "current crate" if you want
101+
> to run it against a workspace is necessary to use a workaround with
102+
> `RUSTFLAGS` in order to execute it in all the workspace members.
103+
>
104+
> ```shell
105+
> $ RUSTFLAGS='-Wrust_2018_idioms' cargo fix --all
106+
> ```
107+
108+
Afterwards, `src/lib.rs` looks like this:
109+
110+
```rust
111+
trait Foo {
112+
fn foo(&self, _: Box<dyn Foo>);
113+
}
114+
```
115+
116+
We're now more idiomatic, and we didn't have to fix our code manually!
117+
118+
As before, `cargo fix` may not be able to automatically update our code.
119+
If `cargo fix` can't fix something, it will print a warning to the console,
120+
and you'll have to fix it manually.
121+
122+
Enjoy the new edition!

src/editions/transitioning-your-code-to-a-new-edition.md

Lines changed: 0 additions & 143 deletions
This file was deleted.

0 commit comments

Comments
 (0)