rust-lang/cargo · error

example target ` ` is a library and cannot be executed

Error message

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

What it means

Thrown by `cargo run` when exactly one target is selected and it is an `ExampleLib` (a library example). Library examples produce `.rlib` files, not executables, so they cannot be run. The check matches `TargetKind::ExampleLib`.

Solutions

  1. Remove the `crate-type` or `proc-macro` setting from the `[example]` declaration so it defaults to a binary.
  2. Use `cargo build --example <name>` if you only need to compile it, not run it.
  3. Pick a different example that is a binary.

Example fix

# Cargo.toml (before)
[[example]]
name = "my_example"
crate-type = ["lib"]

# Cargo.toml (after — default is a binary)
[[example]]
name = "my_example"
Defensive patterns

Strategy: type-guard

Type guard

fn is_executable_example(target: &cargo::core::Target) -> bool {
    // Returns false for ExampleLib targets which cannot be run
    !matches!(target.kind(), cargo::core::TargetKind::ExampleLib(..))
        && target.is_example()
}

Prevention

When it happens

Trigger: Running `cargo run --example <name>` where `<name>` is declared with `crate-type = ["lib"]` or `proc-macro = true` in the example's `[example]` table, making it a library rather than an executable.

Common situations: An example declared as a library (e.g. for documentation or testing purposes); accidentally setting `crate-type` on an example target.

Related errors


AI-assisted analysis of rust-lang/cargo@eb98b54bc9 (2026-08-11). Data as JSON: /api/errors/54fcd6bf910b3523. Report an issue: GitHub.

Appendix: 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 eb98b54bc9)