rustrust-cargovscodium

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


I'm following the instructions in the book "The Rust Programming Language" to build a guessing game, but whenever I try to run my code (via the command Cargo run) in the VSCodium (Open source version of VSCode) terminal, my code refuses to run due to the following error:

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

here's what my Cargo.toml file looks like:

[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]

VERSIONS: VSCodium: 1.73.1 OS: Zorin OS 16.2

i tried changing [package] to [[bin]] and [lib], but it gave me more errors, being: this virtual manifest specifies a [lib] section, which is not allowed and this virtual manifest specifies a [[bin]] section, which is not allowed


Solution

  • To get the configuration you want, you need to specify the package and the target, separately.

    [package]
    name = "GuessingGame"
    version = "0.1.0"
    edition = "2021"
    
    [[bin]]
    name = "GuessingGame"
    path = "src/GuessingGame.rs"
    
    
    [dependencies]
    

    That said — please don't override the path. Rust projects are more readable when they stick to the standard project layout, which is auto-detected by Cargo.

    To do this, make your source file have the name src/main.rs instead of src/GuessingGame.rs, and leave out the [[bin]] section and path entirely from your Cargo.toml. The built executable will still be named GuessingGame automatically, because that's your package name.