rust-lang/cargo · error
a bin target must be available for `cargo run`
Error message
a bin target must be available for `cargo run`
What it means
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.
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.
Example fix
# Cargo.toml — add a binary target
[[bin]]
name = "my_app"
path = "src/main.rs"
# Create src/main.rs
fn main() {
println!("Hello, world!");
} Defensive patterns
Strategy: validation
Validate before calling
fn workspace_has_binary_target(pkg: &cargo::core::Package) -> bool {
pkg.manifest().targets().iter().any(|t| t.is_bin())
} Prevention
- 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>`).
When it happens
Trigger: 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()`.
Common situations: 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.
Related errors
- `cargo run` can run at most one executable, but multiple…
- `cargo run` could not determine which binary to run. Use…
- `cargo run` does not support glob patterns on target…
- crate types to rustc can only be passed to one target…
- example target ` ` is a library and cannot be executed
AI-assisted analysis of rust-lang/cargo@eb98b54bc9 (2026-08-11).
Data as JSON: /api/errors/28fe3618fb48b2ea.
Report an issue: GitHub.
Appendix: source
Thrown at src/ops/cargo_run.rs:43
let packages = options.spec.get_packages(ws)?;
let bins: Vec<_> = packages
.into_iter()
.flat_map(|pkg| {
iter::repeat(pkg).zip(pkg.manifest().targets().iter().filter(|target| {
!target.is_lib()
&& !target.is_custom_build()
&& if !options.filter.is_specific() {
target.is_bin()
} 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> = binsView on GitHub (pinned to eb98b54bc9)