{"record":{"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":"exception","errorClass":null,"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/eb98b54bc9f3c74519f43d066cb3fd02ebc88df0/src/ops/cargo_run.rs#L34-L70","documentation":"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`.","triggerScenarios":"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.","commonSituations":"An example declared as a library (e.g. for documentation or testing purposes); accidentally setting `crate-type` on an example target.","solutions":["Remove the `crate-type` or `proc-macro` setting from the `[example]` declaration so it defaults to a binary.","Use `cargo build --example <name>` if you only need to compile it, not run it.","Pick a different example that is a binary."],"exampleFix":"# Cargo.toml (before)\n[[example]]\nname = \"my_example\"\ncrate-type = [\"lib\"]\n\n# Cargo.toml (after — default is a binary)\n[[example]]\nname = \"my_example\"","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"fn is_executable_example(target: &cargo::core::Target) -> bool {\n    // Returns false for ExampleLib targets which cannot be run\n    !matches!(target.kind(), cargo::core::TargetKind::ExampleLib(..))\n        && target.is_example()\n}","tryCatchPattern":null,"preventionTips":["Do not set `crate-type = [\"lib\"]` on `[example]` targets you intend to run.","Review `[example]` declarations for accidental library settings.","Use `cargo run --example <name>` only on binary examples."],"tags":["cargo-run","example","library","not-executable"],"backgroundTag":null,"analyzedSha":"eb98b54bc9f3c74519f43d066cb3fd02ebc88df0","analyzedAt":"2026-08-11T17:42:36.556Z","contentChangedAt":"2026-08-11T17:42:36.556Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}