Skip to content

Commit af34bfe

Browse files
authored
Merge pull request #279 from salewski/ads/new-proj-ex-w-edition-opt
"Creating a new project": add example using 'cargo new --edition YEAR'
2 parents dc8e5be + ad31aa6 commit af34bfe

File tree

1 file changed

+38
-10
lines changed

1 file changed

+38
-10
lines changed

src/editions/creating-a-new-project.md

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# Creating a new project
22

3-
When you create a new project with Cargo, it will automatically add
4-
configuration for the latest edition:
3+
A new project created with Cargo is configured to use the latest edition by
4+
default:
55

66
```console
7-
> cargo new foo
7+
$ cargo new foo
88
Created binary (application) `foo` project
9-
> cat foo/Cargo.toml
9+
$ cat foo/Cargo.toml
1010
[package]
1111
name = "foo"
1212
version = "0.1.0"
@@ -15,11 +15,41 @@ edition = "2021"
1515
[dependencies]
1616
```
1717

18-
That `edition = "2021"` setting will configure your package to use Rust 2021.
19-
No more configuration needed!
18+
That `edition = "2021"` setting configures your package to be built using the
19+
Rust 2021 edition. No further configuration needed!
2020

21-
If you'd prefer to use an older edition, you can change the value in that
22-
key, for example:
21+
You can use the `--edition <YEAR>` option of `cargo new` to create the project
22+
using some specific edition. For example, creating a new project to use the
23+
Rust 2018 edition could be done like this:
24+
25+
```console
26+
$ cargo new --edition 2018 foo
27+
Created binary (application) `foo` project
28+
$ cat foo/Cargo.toml
29+
[package]
30+
name = "foo"
31+
version = "0.1.0"
32+
edition = "2018"
33+
34+
[dependencies]
35+
```
36+
37+
Don't worry about accidentally using an invalid year for the edition; the
38+
`cargo new` invocation will not accept an invalid edition year value:
39+
40+
```console
41+
$ cargo new --edition 2019 foo
42+
error: "2019" isn't a valid value for '--edition <YEAR>'
43+
[possible values: 2015, 2018, 2021]
44+
45+
Did you mean "2018"?
46+
47+
For more information try --help
48+
```
49+
50+
You can change the value of the `edition` key by simply editing the
51+
`Cargo.toml` file. For example, to cause your package to be built using the
52+
Rust 2015 edition, you would set the key as in the following example:
2353

2454
```toml
2555
[package]
@@ -29,5 +59,3 @@ edition = "2015"
2959

3060
[dependencies]
3161
```
32-
33-
This will build your package in Rust 2015.

0 commit comments

Comments
 (0)