|
| 1 | +--- |
| 2 | +title: "Help test Rust 2018" |
| 3 | +--- |
| 4 | + |
| 5 | +Back in July, we talked about ["Rust 2018"]. In short, we're adding a new |
| 6 | +concept to Rust, "Editions." Editions are a way of showing larger progress |
| 7 | +than our six-week release cycle, and will happen on a roughly three-year |
| 8 | +cycle. Rust 1.0 was "Rust 2015," and Rust 1.31 will be "Rust 2018." |
| 9 | + |
| 10 | +We've been [testing Rust 2018 for a while already], and things are looking |
| 11 | +pretty good! We have just under six weeks until Rust 1.31 ships, and so |
| 12 | +we'd appreciate it if you could give the beta a try. |
| 13 | + |
| 14 | +There's two ways to try out Rust 2018: updating an existing project, and |
| 15 | +starting a new one. For full details, please check out the [Edition Guide], |
| 16 | +but the rest of this post is a quickstart to make it even easier. In |
| 17 | +addition, we have some new lints we'd like you to try; they're described at |
| 18 | +the very end of the post. |
| 19 | + |
| 20 | +If anything goes wrong, or is confusing, please [file an issue] and let us |
| 21 | +know. We want to make sure this is an extra-awesome release! Thank you for |
| 22 | +helping us make Rust even better. <3 |
| 23 | + |
| 24 | +["Rust 2018"]: https://blog.rust-lang.org/2018/07/27/what-is-rust-2018.html |
| 25 | +[testing Rust 2018 for a while already]: https://internals.rust-lang.org/t/rust-2018-release-schedule-and-extended-beta/8076 |
| 26 | +[Edition Guide]: https://rust-lang-nursery.github.io/edition-guide/ |
| 27 | +[file an issue]: https://github.com/rust-lang/rust/issues/new |
| 28 | + |
| 29 | +## Setup: install Rust beta |
| 30 | + |
| 31 | +First things first, you'll need to install the beta release channel of Rust. |
| 32 | +With Rustup, it's as easy as: |
| 33 | + |
| 34 | +```console |
| 35 | +$ rustup install beta |
| 36 | +``` |
| 37 | + |
| 38 | +To use this channel of Rust instead of your default, you can append a `+beta` |
| 39 | +to any `rustc` or cargo commands: |
| 40 | + |
| 41 | +```console |
| 42 | +$ rustc +beta --version |
| 43 | +$ cargo +beta build |
| 44 | +``` |
| 45 | + |
| 46 | +This lets you stick to stable as the default, while using beta for your |
| 47 | +experiments. |
| 48 | + |
| 49 | +## Start a new project |
| 50 | + |
| 51 | +To start a new project with Rust 2018: |
| 52 | + |
| 53 | +```console |
| 54 | +$ cargo +beta new my-sample-project |
| 55 | +``` |
| 56 | + |
| 57 | +Nothing changes! Well, something changed. Check out `Cargo.toml`: |
| 58 | + |
| 59 | +```toml |
| 60 | +[package] |
| 61 | +name = "my-sample-project" |
| 62 | +version = "0.1.0" |
| 63 | +authors = [ "Your Name <[email protected]>"] |
| 64 | +edition = "2018" |
| 65 | + |
| 66 | +[dependencies] |
| 67 | +``` |
| 68 | + |
| 69 | +That new `edition = "2018"` key/value pair means you're working with Rust 2018. |
| 70 | +If it doesn't exist, it's the same as `edition = "2015"`, so all |
| 71 | +existing projects keep working. |
| 72 | + |
| 73 | +## Convert an existing project |
| 74 | + |
| 75 | +You can also convert an existing project to Rust 2018. Remember, none of your |
| 76 | +dependencies need to be updated for this to work; Rust 2018 and 2015 |
| 77 | +interoperate seamlessly! |
| 78 | + |
| 79 | +The first step is to run `cargo fix`: |
| 80 | + |
| 81 | +```console |
| 82 | +$ cargo fix --edition |
| 83 | +``` |
| 84 | + |
| 85 | +This will check your code, and automatically fix any issues that it can. |
| 86 | +`cargo fix` is still pretty new, and so it can't always fix your code |
| 87 | +automatically. If `cargo fix` can't fix something, it will print the warning |
| 88 | +that it cannot fix to the console. If you see one of these warnings, you'll |
| 89 | +have to update your code manually. See the corresponding section of the |
| 90 | +edition guide for help, and if you have problems, please seek help at the |
| 91 | +user's forums. |
| 92 | + |
| 93 | +Keep running `cargo fix --edition` until you have no more warnings. |
| 94 | + |
| 95 | +Congrats! Your code is now valid in both Rust 2015 and Rust 2018! |
| 96 | + |
| 97 | +Once this is done, you can commit to Rust 2018 by updating |
| 98 | +your `Cargo.toml`: |
| 99 | + |
| 100 | +```toml |
| 101 | +[package] |
| 102 | +name = "my-sample-project" |
| 103 | +version = "0.1.0" |
| 104 | +authors = [ "Your Name <[email protected]>"] |
| 105 | +edition = "2018" |
| 106 | + |
| 107 | +[dependencies] |
| 108 | +``` |
| 109 | + |
| 110 | +See that `edition = "2018"`? That's what opts you in to the new features. |
| 111 | +Set it, `cargo +beta build`, and you should be good to go! |
| 112 | + |
| 113 | +## Writing idiomatic code |
| 114 | + |
| 115 | +We have some new lints that suggest using certain idioms in your Rust 2018 |
| 116 | +code. We don't have them turned on by default yet. To see what your code would |
| 117 | +look like, you can use `cargo fix`: |
| 118 | + |
| 119 | +```console |
| 120 | +$ cargo +beta fix --edition-idioms |
| 121 | +``` |
| 122 | + |
| 123 | +If things look great, or things look terrible, please let us know! We hope to make |
| 124 | +these lints warn by default after gaining some experience with them and working |
| 125 | +out the bugs. |
| 126 | + |
| 127 | +> The `--edition-idioms` flag applies only to the "current crate"; if you want |
| 128 | +> to run it against a workspace is necessary to use a workaround with `RUSTFLAGS` |
| 129 | +> in order to execute it in all the workspace members. |
| 130 | +> |
| 131 | +> $ RUSTFLAGS='-Wrust_2018_idioms' cargo fix --all |
0 commit comments