rust-lang/cargo · error · anyhow::Error

a bin target must be available for `cargo run`

Error message

a bin target must be available for `cargo run`

What it means

`cargo run` collects candidate binary targets from the selected package(s). If the filter is non-specific (no --bin/--example chosen) and bins is empty, it bails at cargo_run.rs:43 — the package has no [[bin]] target to execute.

Source

Thrown at src/ops/cargo_run.rs:43

    let packages = options.spec.get_packages(ws)?;
    let bins: Vec<_> = packages
        .into_iter()
        .flat_map(|pkg| {
            iter::repeat(pkg).zip(pkg.manifest().targets().iter().filter(|target| {
                !target.is_lib()
                    && !target.is_custom_build()
                    && if !options.filter.is_specific() {
                        target.is_bin()
                    } else {
                        options.filter.target_run(target)
                    }
            }))
        })
        .collect();

    if bins.is_empty() {
        if !options.filter.is_specific() {
            anyhow::bail!("a bin target must be available for `cargo run`")
        } else {
            // This will be verified in `cargo_compile`.
        }
    }

    if bins.len() == 1 {
        let target = bins[0].1;
        if let TargetKind::ExampleLib(..) = target.kind() {
            anyhow::bail!(
                "example target `{}` is a library and cannot be executed",
                target.name()
            )
        }
    }

    if bins.len() > 1 {
        if !options.filter.is_specific() {
            let mut names: Vec<&str> = bins

View on GitHub (pinned to 0e07a15537)

Solutions

  1. Add a [[bin]] target to Cargo.toml pointing at a binary entry point (e.g. src/main.rs or src/bin/foo.rs).
  2. Run from / target the package that actually has the binary: `cargo run -p <pkg>`.
  3. If you only need to test the library, use `cargo test` or `cargo run --example <name>` instead.

Example fix

# before: Cargo.toml has only [lib]
$ cargo run
error: a bin target must be available for `cargo run`

# after: add a binary target
# Cargo.toml
[[bin]]
name = "my-app"
path = "src/main.rs"
Defensive patterns

Strategy: validation

Validate before calling

# Confirm at least one [[bin]] target exists before running:
if ! cargo read-manifest 2>/dev/null | grep -q '"kind":\[.*"bin"'; then
  echo "no bin target; add [[bin]] or use cargo test" >&2
  exit 1
fi
cargo run

Prevention

When it happens

Trigger: Running `cargo run` (no target flags) in a crate that defines only a [lib], or whose [[bin]] targets were removed/renamed.

Common situations: Library-only crates; binary lives in a different workspace member; `[[bin]]` entry points to a missing file.

Related errors


AI-assisted analysis of rust-lang/cargo@0e07a15537 (2026-08-06). Data as JSON: /data/errors/28fe3618fb48b2ea.json. Report an issue: GitHub.