遵循Rust官方教程开发猜数字游戏时Cargo run报错求助
猜数字游戏Cargo运行错误解决办法
问题情况
跟着《Rust编程语言》教程开发猜数字游戏时,在VSCodium终端执行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
- OS: 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]段中添加了path字段,这个字段是给虚拟工作区使用的,导致Cargo把你的项目识别成了虚拟清单——既找不到默认的程序入口文件,修改[package]为[lib]或[[bin]]也会触发虚拟清单的限制错误。
修复方案
方案1:遵循Cargo默认约定
- 将你的代码文件重命名为
src/main.rs - 删除Cargo.toml里
[package]段中的path = "src/GuessingGame.rs"这一行 - 重新执行
Cargo run即可正常运行
方案2:自定义二进制入口文件
如果想保留原文件名src/GuessingGame.rs,按以下步骤修改Cargo.toml:
- 删除
[package]段里的path = "src/GuessingGame.rs" - 在文件末尾添加
[[bin]]配置段:
[[bin]] name = "GuessingGame" path = "src/GuessingGame.rs"
修改后的完整Cargo.toml如下:
[package] name = "GuessingGame" version = "0.1.0" edition = "2021" authors = ["my name <example@example.com>"] [dependencies] [[bin]] name = "GuessingGame" path = "src/GuessingGame.rs"
完成修改后执行Cargo run就能正常运行程序了。
内容的提问来源于stack exchange,提问作者ThatVibingCat
相关产品推荐
相关产品推荐

