{"id":313,"hash":"cf9249b915ad65f936dc3abe9ae2e96461bfe0db455ad6db8871044fe2ec4ac8","pattern":"Using a crate in a Cargo project errors with &quot;maybe a missing extern crate&quot;","full_message":"I started learning Rust today, but I am stuck at this step. I want use the rand crate in my project, so I updated my Cargo.toml as suggested in the tutorial:\n\n[package]\nname = \"guessing_game\"\nversion = \"0.1.0\"\nauthors = [\"Novice <novice.coder@gmail.com>\"]\n\n[dependencies]\nrand = \"0.3.14\"\n\nImporting it in my code as:\n\nuse rand::Rng;\n\nIt gives this error:\n\nerror[E0432]: unresolved import `rand`\n --> src/main.rs:1:5\n  |\n1 | use rand::Rng;\n  |     ^^^^ maybe a missing `extern crate rand;`?\n\nAm I missing something?\n\nI added edition = \"2018\" as suggested:\n\nCargo.toml:\n\n[package]\nname = \"guessing_game\"\nversion = \"0.1.0\"\nauthors = [\"Novice <novice.coder@gmail.com>\"]\nedition = \"2018\"\n\n[dependencies]\nrand = \"0.3.14\"\n\nCargo build now gives:\n\n$ cargo build --verbose\n   Fresh libc v0.2.45\n   Fresh rand v0.4.3\n   Fresh rand v0.3.22\n Compiling guessing_game v0.1.0 (/home/bappaditya/projects/guessing_game)\n Running `rustc --edition=2018 --crate-name guessing_game src/main.rs --color always --crate-type bin --emit=dep-info,link -C debuginfo=2 -C metadata=4d1c2d587c45b4\nc6 -C extra-filename=-4d1c2d587c45b4c6 --out-dir \n/home/bappaditya/projects/guessing_game/target/debug/deps -C \nincremental=/home/bappaditya/projects/guessing_game/target\n/debug/incremental -L \ndependency=/home/bappaditya/projects/guessing_game/target/debug/deps -- \nextern rand=/home/bappaditya/projects/guessing_game/target/debug/deps/libra\nnd-78fc4b142cc921d4.rlib`\nerror: Edition 2018 is unstable and only available for nightly builds of rustc.\n\nI updated rust using rustup update and then added extern crate rand; to my main.rs. Now it's working as expected. \n\nThe program runs but in my vscode problems tab its still showing the error - \n\nerror[E0432]: unresolved import `rand`\n --> src/main.rs:1:5\n  |\n1 | use rand::Rng;\n  |     ^^^^ maybe a missing `extern crate rand;`?","ecosystem":"cargo","package_name":"rust-cargo","package_version":null,"solution":"The quick fix is to add\n\nedition = \"2021\"\n\nto your Cargo.toml, above the [dependencies] line.\n\nExplanation\nThere are three major editions of Rust: Rust 2015, 2018, and 2021. Rust 2021 is recommended for new code, but since Rust needs to be backward compatible, you have to opt in to use it.\n\nIn Rust 2015, you had to write an extern crate statement before using anything outside of std. That's where the error message comes from. But you don't have to do that in Rust 2018 or 2021 anymore, which is why setting the edition fixes it.\n\nThere are many more changes in Rust 2021; if you're interested, you can read about them in the edition guide.","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/53985641/using-a-crate-in-a-cargo-project-errors-with-maybe-a-missing-extern-crate","votes":26,"created_at":"2026-04-19T04:41:50.438764+00:00","updated_at":"2026-04-19T04:52:30.367802+00:00"}