{"id":"54fcd6bf910b3523","repo":"rust-lang/cargo","slug":"example-target-is-a-library-and-cannot-be-exe","errorCode":null,"errorMessage":"example target `{}` is a library and cannot be executed","messagePattern":"example target `(.+?)` is a library and cannot be executed","errorType":"validation","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"src/ops/cargo_run.rs","lineNumber":52,"sourceCode":"                    } else {\n                        options.filter.target_run(target)\n                    }\n            }))\n        })\n        .collect();\n\n    if bins.is_empty() {\n        if !options.filter.is_specific() {\n            anyhow::bail!(\"a bin target must be available for `cargo run`\")\n        } else {\n            // This will be verified in `cargo_compile`.\n        }\n    }\n\n    if bins.len() == 1 {\n        let target = bins[0].1;\n        if let TargetKind::ExampleLib(..) = target.kind() {\n            anyhow::bail!(\n                \"example target `{}` is a library and cannot be executed\",\n                target.name()\n            )\n        }\n    }\n\n    if bins.len() > 1 {\n        if !options.filter.is_specific() {\n            let mut names: Vec<&str> = bins\n                .into_iter()\n                .map(|(_pkg, target)| target.name())\n                .collect();\n            names.sort();\n            anyhow::bail!(\n                \"`cargo run` could not determine which binary to run. \\\n                 Use the `--bin` option to specify a binary, \\\n                 or the `default-run` manifest key.\\n\\\n                 available binaries: {}\",","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/rust-lang/cargo/blob/0e07a155371a6ce88ae53a2c00df940280c09a67/src/ops/cargo_run.rs#L34-L70","documentation":"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.","triggerScenarios":"`cargo run --example foo` where `foo` is configured as a library example, e.g. `[[example]] name = \"foo\" crate-type = [\"lib\"]` or `[[example]] ... [lib]`-style.","commonSituations":"Examples written as libraries (for sharing/testing code) mistakenly targeted with `cargo run`; example target misconfigured with a lib crate-type.","solutions":["Change the example to a binary by removing `crate-type = [\"lib\"]` (default is bin for examples).","If you intended a library example, run it via `cargo test --example foo` or `cargo build --example foo` instead of `cargo run`.","Pick a different example target that is a binary."],"exampleFix":"# before\n[[example]]\nname = \"demo\"\ncrate-type = [\"lib\"]   # makes it a library\n\n$ cargo run --example demo\nerror: example target `demo` is a library and cannot be executed\n\n# after - remove crate-type (defaults to bin)\n[[example]]\nname = \"demo\"","handlingStrategy":"validation","validationCode":"# Check the example's crate-type before running it:\nname=\"$1\"\nct=$(cargo read-manifest 2>/dev/null \\\n  | jq -r --arg n \"$name\" '.targets[]|select(.name==$n)|.crate_types')\n[ \"$ct\" = 'null' ] || case \"$ct\" in *\"lib\"*) \\\n  echo \"example $name is a library; cannot execute\" >&2; exit 1;; esac\ncargo run --example \"$name\"","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep runnable examples as binaries (no crate-type = [\"lib\"]).","Use `cargo test --example` for library-style examples.","Document which examples are runnable vs libraries."],"tags":["run","examples","targets","crate-type"],"analyzedSha":"0e07a155371a6ce88ae53a2c00df940280c09a67","analyzedAt":"2026-08-06T01:46:58.334Z","schemaVersion":2}