{"record":{"id":"aa8d2f9c3efdf1e2","repo":"rust-lang/cargo","slug":"can-t-specify-both-lib-and-binary-outputs","errorCode":null,"errorMessage":"can't specify both lib and binary outputs","messagePattern":"can't specify both lib and binary outputs","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/ops/cargo_new.rs","lineNumber":80,"sourceCode":"    source_files: Vec<SourceFileInformation>,\n    edition: Option<&'a str>,\n    registry: Option<&'a str>,\n}\n\nimpl NewOptions {\n    pub fn new(\n        version_control: Option<VersionControl>,\n        bin: bool,\n        lib: bool,\n        path: PathBuf,\n        name: Option<String>,\n        edition: Option<String>,\n        registry: Option<String>,\n    ) -> CargoResult<NewOptions> {\n        let auto_detect_kind = !bin && !lib;\n\n        let kind = match (bin, lib) {\n            (true, true) => anyhow::bail!(\"can't specify both lib and binary outputs\"),\n            (false, true) => NewProjectKind::Lib,\n            (_, false) => NewProjectKind::Bin,\n        };\n\n        let opts = NewOptions {\n            version_control,\n            kind,\n            auto_detect_kind,\n            path,\n            name,\n            edition,\n            registry,\n        };\n        Ok(opts)\n    }\n}\n\nfn get_name<'a>(path: &'a Path, opts: &'a NewOptions) -> CargoResult<&'a str> {","sourceCodeStart":62,"sourceCodeEnd":98,"githubUrl":"https://github.com/rust-lang/cargo/blob/98a09e7e7d62850f14e5b6132101fc1edd19a16f/src/ops/cargo_new.rs#L62-L98","documentation":"`cargo new`/`cargo init` accepts `--lib` or `--bin` to force the project kind, but passing both is contradictory — a single new project cannot be declared as both a library and a binary template at creation. The `(bin, lib) == (true, true)` arm of the match bails immediately before touching the filesystem.","triggerScenarios":"Running `cargo new --lib --bin myproject` or `cargo init --lib --bin`. Programmatic callers of `NewOptions::new` passing both `bin: true` and `lib: true`.","commonSituations":"Aliases or shell wrappers that always add `--bin` colliding with an explicit `--lib`. Copy-pasting flags from two different tutorials. Build scripts that combine flags from multiple sources without deduping.","solutions":["Pass only one of `--lib` or `--bin` — for a library use `--lib`, for a binary use `--bin` (or omit both to auto-detect).","For a project that needs both a lib and a bin, create as `--bin` then add a `src/lib.rs` / `[[lib]]` target, or create `--lib` and add a `src/main.rs`.","Fix shell aliases / wrappers that inject both flags."],"exampleFix":"# before\n$ cargo new --lib --bin myproject   # error\n\n# after\n$ cargo new --bin myproject   # then later add src/lib.rs if a lib is also needed\n$ cargo new --lib myproject   # or start as a library","handlingStrategy":"validation","validationCode":"fn resolve_kind(bin: bool, lib: bool) -> Result<Kind, String> {\n    match (bin, lib) {\n        (true, true) => Err(\"pass at most one of --bin/--lib\".into()),\n        (false, true) => Ok(Kind::Lib),\n        _ => Ok(Kind::Bin),\n    }\n}","typeGuard":"fn exactly_one_kind_flag(bin: bool, lib: bool) -> bool { bin ^ lib }","tryCatchPattern":null,"preventionTips":["Validate that `--bin` and `--lib` are not both set before constructing the cargo command.","In shell wrappers, do not unconditionally append `--bin`.","Treat the kind choice as a single enum, not two independent booleans, in programmatic callers.","Remember a project can host both a lib and a bin after creation; the flags only choose the scaffold."],"tags":["cargo-new","cli-usage","project-kind"],"backgroundTag":null,"analyzedSha":"98a09e7e7d62850f14e5b6132101fc1edd19a16f","analyzedAt":"2026-08-11T17:42:36.556Z","contentChangedAt":"2026-08-11T17:42:36.556Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}