清单中未指定目标 - 必须存在src/lib.rs、src/main.rs、[lib]部分或[[bin]]部分。

3

我正在按照《Rust编程语言》一书中的指示构建一个猜数字游戏,但是每当我尝试在VSCodium(VSCode的开源版本)终端中运行我的代码(通过命令Cargo run),我的代码由于以下错误而拒绝运行:

no targets specified in the manifest
  either src/lib.rs, src/main.rs, a [lib] section, or [[bin]] section must be present

这是我的 Cargo.toml 文件的样子:

[package]
name = "GuessingGame"
path = "src/GuessingGame.rs"
version = "0.1.0"
edition = "2021"
authors = ["my name <example@example.com>"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

版本: VSCodium: 1.73.1 操作系统: Zorin OS 16.2

我尝试将 [package] 更改为 [[bin]][lib],但这给了我更多的错误,分别是: this virtual manifest specifies a [lib] section, which is not allowedthis virtual manifest specifies a [[bin]] section, which is not allowed


你能在终端执行 cargo build 吗?在 src 文件夹里有哪些文件? - Cerberus
cargo build 返回相同的错误,而我在 src 中唯一拥有的文件是 GuessingGame.rs。 - ThatVibingCat
1个回答

6
为了获得所需的配置,你需要分别指定包和目标。
[package]
name = "GuessingGame"
version = "0.1.0"
edition = "2021"

[[bin]]
name = "GuessingGame"
path = "src/GuessingGame.rs"


[dependencies]

话虽如此,请不要覆盖路径。当 Rust 项目遵循标准项目布局时,它们更易读,并且这也可以被 Cargo 自动检测。

为此,将你的源文件命名为 src/main.rs 而不是 src/GuessingGame.rs,并从 Cargo.toml 中完全省略[[bin]]部分和path部分。生成的可执行文件仍然会自动命名为 GuessingGame,因为那是你的包名称。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接