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:
[package]
name = "guessing_game"
version = "0.1.0"
authors = ["Novice <[email protected]>"]
[dependencies]
rand = "0.3.14"
Importing it in my code as:
use rand::Rng;
It gives this error:
error[E0432]: unresolved import `rand`
--> src/main.rs:1:5
|
1 | use rand::Rng;
| ^^^^ maybe a missing `extern crate rand;`?
Am I missing something?
I added edition = "2018"
as suggested:
Cargo.toml:
[package]
name = "guessing_game"
version = "0.1.0"
authors = ["Novice <[email protected]>"]
edition = "2018"
[dependencies]
rand = "0.3.14"
Cargo build now gives:
$ cargo build --verbose
Fresh libc v0.2.45
Fresh rand v0.4.3
Fresh rand v0.3.22
Compiling guessing_game v0.1.0 (/home/bappaditya/projects/guessing_game)
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
c6 -C extra-filename=-4d1c2d587c45b4c6 --out-dir
/home/bappaditya/projects/guessing_game/target/debug/deps -C
incremental=/home/bappaditya/projects/guessing_game/target
/debug/incremental -L
dependency=/home/bappaditya/projects/guessing_game/target/debug/deps --
extern rand=/home/bappaditya/projects/guessing_game/target/debug/deps/libra
nd-78fc4b142cc921d4.rlib`
error: Edition 2018 is unstable and only available for nightly builds of rustc.
I updated rust using rustup update
and then added extern crate rand;
to my main.rs. Now it's working as expected.
The program runs but in my vscode problems tab its still showing the error -
error[E0432]: unresolved import `rand`
--> src/main.rs:1:5
|
1 | use rand::Rng;
| ^^^^ maybe a missing `extern crate rand;`?
edition = "2018"
in it. how can you miss it ?extern crate rand;
at the beginning ofmain.rs
, as @LambdaFairy hinted in their answer.