{"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":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"src/ops/cargo_new.rs","lineNumber":210,"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":192,"sourceCodeEnd":228,"githubUrl":"https://github.com/rust-lang/cargo/blob/0e07a155371a6ce88ae53a2c00df940280c09a67/src/ops/cargo_new.rs#L192-L228","documentation":"check_name rejects a package name that exactly matches a Rust keyword (detected via restricted_names::is_keyword). Keywords like `fn`, `match`, `crate`, `impl`, `type`, `pub` cannot be used as a crate name because they would collide with the language grammar when the crate is referenced as an identifier. The trailing {help} offers a [[bin]] override path when applicable.","triggerScenarios":"`cargo new match`, `cargo new --name crate`, or running `cargo new` inside a directory literally named `fn`, `impl`, `self`, `super`, etc. is_keyword(name) at cargo_new.rs:209 returns true.","commonSituations":"Creating a crate for a tutorial about a language feature and naively naming it after the keyword; directory created by a framework generator that used a keyword; copy of an example whose name happens to be reserved.","solutions":["Pick a non-keyword name, e.g. `match` -> `matcher` or `match-utils`","Use `--name <non-keyword>` to override the directory-derived name","Rename the containing directory before running cargo new"],"exampleFix":"# before\ncargo new match\n# after\ncargo new matcher   # or: cargo new match --name matcher","handlingStrategy":"validation","validationCode":"const KEYWORDS: &[&str] = &[\n    \"as\",\"break\",\"const\",\"continue\",\"crate\",\"dyn\",\"else\",\"enum\",\"extern\",\"false\",\"fn\",\"for\",\n    \"if\",\"impl\",\"in\",\"let\",\"loop\",\"match\",\"mod\",\"move\",\"mut\",\"pub\",\"ref\",\"return\",\"self\",\"Self\",\n    \"static\",\"struct\",\"super\",\"trait\",\"true\",\"type\",\"unsafe\",\"use\",\"where\",\"while\",\"async\",\"await\",\n];\nfn is_rust_keyword(name: &str) -> bool { KEYWORDS.contains(&name) }","typeGuard":"const KEYWORDS = new Set(['fn','match','crate','impl','type','pub','let','if','else','for','while','loop','return','struct','enum','trait','mod','use','move','self','Self','super','static','const','unsafe','as','in','ref','dyn','where','true','false','extern','break','continue','mut','async','await']);\nfunction isNotKeyword(name: string): boolean { return !KEYWORDS.has(name); }","tryCatchPattern":null,"preventionTips":["Cross-check candidate names against the rust-lang keyword list before cargo new","Use a name-linter in project generators","Avoid naming crates after the language feature they exercise"],"tags":["cargo-new","package-name","rust-keyword","naming"],"analyzedSha":"0e07a155371a6ce88ae53a2c00df940280c09a67","analyzedAt":"2026-08-06T01:46:58.334Z","schemaVersion":2}