{"record":{"id":"28fe3618fb48b2ea","repo":"rust-lang/cargo","slug":"a-bin-target-must-be-available-for-cargo-run","errorCode":null,"errorMessage":"a bin target must be available for `cargo run`","messagePattern":"a bin target must be available for `cargo run`","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/ops/cargo_run.rs","lineNumber":43,"sourceCode":"    let packages = options.spec.get_packages(ws)?;\n    let bins: Vec<_> = packages\n        .into_iter()\n        .flat_map(|pkg| {\n            iter::repeat(pkg).zip(pkg.manifest().targets().iter().filter(|target| {\n                !target.is_lib()\n                    && !target.is_custom_build()\n                    && if !options.filter.is_specific() {\n                        target.is_bin()\n                    } 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","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/rust-lang/cargo/blob/eb98b54bc9f3c74519f43d066cb3fd02ebc88df0/src/ops/cargo_run.rs#L25-L61","documentation":"Thrown by `cargo run` when no binary target (`[bin]`) is found in the selected package(s) and no specific filter (like `--bin` or `--example`) is provided. `cargo run` requires at least one executable target to run.","triggerScenarios":"Running `cargo run` on a crate that has only a `[lib]` target and no `[bin]` targets, or where all potential binary targets have been filtered out. The `bins` vector is empty and `!options.filter.is_specific()`.","commonSituations":"A library-only crate with no `main.rs` or `src/bin/`; running `cargo run` in a workspace root; a crate whose binary was removed or renamed.","solutions":["Add a binary target: create `src/main.rs` or add a `[[bin]]` section in Cargo.toml.","If you meant to test, use `cargo test` or `cargo run --example <name>` instead.","Switch to a crate that actually has a binary target."],"exampleFix":"# Cargo.toml — add a binary target\n[[bin]]\nname = \"my_app\"\npath = \"src/main.rs\"\n\n# Create src/main.rs\nfn main() {\n    println!(\"Hello, world!\");\n}","handlingStrategy":"validation","validationCode":"fn workspace_has_binary_target(pkg: &cargo::core::Package) -> bool {\n    pkg.manifest().targets().iter().any(|t| t.is_bin())\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Ensure the crate has at least one `[[bin]]` target or `src/main.rs`.","Use `cargo run --example <name>` to run examples instead.","Check `cargo run` is being invoked in the right package (use `-p <pkg>`)."],"tags":["cargo-run","no-binary","target-selection","missing-target"],"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"}