创建一个新项目

使用 Cargo 创建的新项目默认配置为使用最新版本

$ cargo new foo Creating binary (application) `foo` package note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.net.cn/cargo/reference/manifest.html $ cat foo/Cargo.toml [package] name = "foo" version = "0.1.0" edition = "2024" [dependencies]

edition = "2024" 设置将您的包配置为使用 Rust 2024 版本构建。无需进一步配置!

您可以使用 cargo new--edition <YEAR> 选项来创建使用特定版本的项目。 例如,创建一个使用 Rust 2018 版本的新项目可以像这样完成

$ cargo new --edition 2018 foo Creating binary (application) `foo` package note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.net.cn/cargo/reference/manifest.html $ cat foo/Cargo.toml [package] name = "foo" version = "0.1.0" edition = "2018" [dependencies]

不用担心意外使用无效的版本年份;cargo new 调用不会接受无效的版本年份值

$ cargo new --edition 2019 foo error: invalid value '2019' for '--edition <YEAR>' [possible values: 2015, 2018, 2021, 2024] tip: a similar value exists: '2021' For more information, try '--help'.

您可以通过简单地编辑 Cargo.toml 文件来更改 edition 键的值。 例如,要使您的包使用 Rust 2015 版本构建,您需要像以下示例中那样设置键

[package] name = "foo" version = "0.1.0" edition = "2015" [dependencies]