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

example target `{}` is a library and cannot be executed

Error message

example target `{}` is a library and cannot be executed

What it means

When exactly one target matches and it is an example declared as a library (TargetKind::ExampleLib), `cargo run` cannot execute it — only example binaries can be run. The check at cargo_run.rs:52 names the offending target.

Source

Thrown at src/ops/cargo_run.rs:52

                    } 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
                .into_iter()
                .map(|(_pkg, target)| target.name())
                .collect();
            names.sort();
            anyhow::bail!(
                "`cargo run` could not determine which binary to run. \
                 Use the `--bin` option to specify a binary, \
                 or the `default-run` manifest key.\n\
                 available binaries: {}",

View on GitHub (pinned to 0e07a15537)

Solutions

  1. Change the example to a binary by removing `crate-type = ["lib"]` (default is bin for examples).
  2. If you intended a library example, run it via `cargo test --example foo` or `cargo build --example foo` instead of `cargo run`.
  3. Pick a different example target that is a binary.

Example fix

# before
[[example]]
name = "demo"
crate-type = ["lib"]   # makes it a library

$ cargo run --example demo
error: example target `demo` is a library and cannot be executed

# after - remove crate-type (defaults to bin)
[[example]]
name = "demo"
Defensive patterns

Strategy: validation

Validate before calling

# Check the example's crate-type before running it:
name="$1"
ct=$(cargo read-manifest 2>/dev/null \
  | jq -r --arg n "$name" '.targets[]|select(.name==$n)|.crate_types')
[ "$ct" = 'null' ] || case "$ct" in *"lib"*) \
  echo "example $name is a library; cannot execute" >&2; exit 1;; esac
cargo run --example "$name"

Prevention

When it happens

Trigger: `cargo run --example foo` where `foo` is configured as a library example, e.g. `[[example]] name = "foo" crate-type = ["lib"]` or `[[example]] ... [lib]`-style.

Common situations: Examples written as libraries (for sharing/testing code) mistakenly targeted with `cargo run`; example target misconfigured with a lib crate-type.

Related errors


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