我是按照《Rust编程语言》一书中的说明来做的。构建一个猜谜游戏,但每当我试图运行我的代码(通过命令Cargo run
)在VSCodium (VSCode的开源版本)终端,我的代码拒绝运行由于以下错误:
no targets specified in the manifest
either src/lib.rs, src/main.rs, a [lib] section, or [[bin]] section must be present
这是我的货物。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.1OS: Zorin OS 16.2
我试图改变[package]
到[[bin]]
和[lib]
,但它给了我更多的错误,是:this virtual manifest specifies a [lib] section, which is not allowed
和this virtual manifest specifies a [[bin]] section, which is not allowed
要获得您想要的配置,您需要分别指定包和目标。
[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
,并将[[bin]]
部分和path
完全从Cargo.toml
中删除。构建的可执行文件仍将自动命名为GuessingGame
,因为这是您的包名。