{"record":{"id":"8ed455a1939b8af1","repo":"rust-lang/cargo","slug":"invalid-package-name-it-is-a-rust-keyword","errorCode":null,"errorMessage":"invalid package name `{}`: it is a Rust keyword{}","messagePattern":"invalid package name `(.+?)`: it is a Rust keyword(.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/ops/cargo_new.rs","lineNumber":159,"sourceCode":"                This can be done by setting the binary filename to `src/bin/{name}.rs` \\\n                or change the name in Cargo.toml with:\\n\\\n                \\n    \\\n                [[bin]]\\n    \\\n                name = \\\"{name}\\\"\\n    \\\n                path = \\\"src/main.rs\\\"\\n\\\n            \",\n                name = name\n            ));\n        }\n        help\n    };\n    PackageName::new(name).map_err(|err| {\n        let help = bin_help();\n        anyhow::anyhow!(\"{err}{help}\")\n    })?;\n\n    if restricted_names::is_keyword(name) {\n        anyhow::bail!(\n            \"invalid package name `{}`: it is a Rust keyword{}\",\n            name,\n            bin_help()\n        );\n    }\n    if restricted_names::is_conflicting_artifact_name(name) {\n        if has_bin {\n            anyhow::bail!(\n                \"invalid package name `{}`: \\\n                it conflicts with cargo's build directory names{}\",\n                name,\n                name_help\n            );\n        } else {\n            shell.warn(format!(\n                \"package `{}` will not support binary \\\n                executables with that name, \\\n                it conflicts with cargo's build directory names\",","sourceCodeStart":141,"sourceCodeEnd":177,"githubUrl":"https://github.com/rust-lang/cargo/blob/98a09e7e7d62850f14e5b6132101fc1edd19a16f/src/ops/cargo_new.rs#L141-L177","documentation":"`restricted_names::is_keyword` returns true for any of Rust's reserved keywords (`fn`, `crate`, `impl`, `match`, `async`, etc.). Cargo forbids these as package names because they collide with the language grammar and would break `use`/`extern crate` references. The bail appends `bin_help()` showing how to keep a binary by that logical name via `[[bin]]`.","triggerScenarios":"`cargo new fn`, `cargo init --name match`, `cargo new --name crate mytool`, or any name matching the keyword list in `restricted_names.rs`.","commonSituations":"Creating a tutorial/example crate named after a concept (`match`, `async`, `trait`). Auto-generating from a directory literally named `test`-adjacent keywords. Tooling that derives crate names from filenames matching keywords.","solutions":["Pick a non-keyword name: suffix/prefix it, e.g. `match-tool`, `my_async`, `learn-trait`.","Keep the binary's logical name but use a valid package name plus `[[bin]] name = \"match\"` in Cargo.toml (as the help text shows), or place it at `src/bin/match.rs`.","Consult the keyword list (https://doc.rust-lang.org/reference/keywords.html) when choosing names."],"exampleFix":"# before\n$ cargo new match   # error: Rust keyword\n\n# after\n$ cargo new match-tool\n# Cargo.toml — keep binary named \"match\" if desired:\n# [[bin]]\n# name = \"match\"\n# path = \"src/main.rs\"","handlingStrategy":"validation","validationCode":"const KEYWORDS: &[&str] = &[\"Self\",\"abstract\",\"as\",\"async\",\"await\",\"become\",\"box\",\"break\",\"const\",\"continue\",\"crate\",\"do\",\"dyn\",\"else\",\"enum\",\"extern\",\"false\",\"final\",\"fn\",\"for\",\"if\",\"impl\",\"in\",\"let\",\"loop\",\"macro\",\"match\",\"mod\",\"move\",\"mut\",\"override\",\"priv\",\"pub\",\"ref\",\"return\",\"self\",\"static\",\"struct\",\"super\",\"trait\",\"true\",\"try\",\"type\",\"typeof\",\"unsafe\",\"unsized\",\"use\",\"virtual\",\"where\",\"while\",\"yield\"];\nfn is_keyword(name: &str) -> bool { KEYWORDS.contains(&name) }","typeGuard":"fn is_non_keyword_name(name: &str) -> bool { !is_keyword(name) }","tryCatchPattern":null,"preventionTips":["Check the chosen name against Rust's keyword list before creating.","Suffix/prefix concept names that collide (e.g. `match-tool`).","In generators, keep a deny-list of keywords and reject or auto-rename.","Cross-reference https://doc.rust-lang.org/reference/keywords.html."],"tags":["package-name","keywords","cargo-new","naming"],"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"}