3

I'm having trouble with importing rand crate from crates.io. After adding the line rand="0.8.3" and then running command cargo build for the project, it keeps displaying the same errors:

error[E0432]: unresolved import `rand`
 --> main.rs:1:5
  |
1 | use rand::Rng;
  |     ^^^^ maybe a missing crate `rand`?

error[E0433]: failed to resolve: use of undeclared crate or module `rand`
 --> main.rs:4:25
  |
4 |     let secret_number = rand::thread_rng().gen_range(1..=11);
  |                         ^^^^ use of undeclared crate or module `rand`

error: aborting due to 2 previous errors

the cargo.toml file

[package]
name = "roller"
version = "0.1.0"
authors = ["User"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rand = "0.8.3"

Basically the simplest reproducible example is this single line of code:

 use rand::Rng;

 fn main(){
    let secret_number = rand::thread_rng().gen_range(1..=11);
    print!("{}",secret_number);
 }

What's wrong with it?


Just in case:
The **cargo.lock**file:
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"

[[package]]
name = "getrandom"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8"
dependencies = [
 "cfg-if",
 "libc",
 "wasi",
]

[[package]]
name = "libc"
version = "0.2.86"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7282d924be3275cec7f6756ff4121987bc6481325397dde6ba3e7802b1a8b1c"

[[package]]
name = "ppv-lite86"
version = "0.2.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857"

[[package]]
name = "rand"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e"
dependencies = [
 "libc",
 "rand_chacha",
 "rand_core",
 "rand_hc",
]

[[package]]
name = "rand_chacha"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d"
dependencies = [
 "ppv-lite86",
 "rand_core",
]

[[package]]
name = "rand_core"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34cf66eb183df1c5876e2dcf6b13d57340741e8dc255b48e40a26de954d06ae7"
dependencies = [
 "getrandom",
]

[[package]]
name = "rand_hc"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73"
dependencies = [
 "rand_core",
]

[[package]]
name = "roller"
version = "0.1.0"
dependencies = [
 "rand",
]

[[package]]
name = "wasi"
version = "0.10.2+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6"
Nantarand
  • 51
  • 1
  • 1
  • 4
  • Can you provide a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example)? The next thing is that for `rand 0.8.3` the function signature for `.gen_range` has changed. It now takes a range, e.g. `.gen_range(0..=42)`. – Jason Feb 25 '21 at 11:09
  • @Nantarand: there is a line of code, yes. But it's too minimal. So minimal that it's not reproducible. One needs to wrap it in `fn main` and add that import on top, I think. – Sergio Tulentsev Feb 25 '21 at 11:49
  • Thank you for pointing the signature change out though I didn't reflect it in the code – Nantarand Feb 25 '21 at 11:49
  • @SergioTulentsev oh I thought that the existence the main function was implicit in this case but I'm adding it – Nantarand Feb 25 '21 at 11:53
  • With the examples you provided I cannot reproduce these warnings in Rust 1.50.0. What compiler version are you using? – Johann150 Feb 25 '21 at 12:10
  • @Johann150 I'm using the same version – Nantarand Feb 25 '21 at 12:19
  • Also why's this peculiarity that the linter will show possible choices when I type 't' after "rand::" but won't show the same after typing 'g' after "thread_rng()." – Nantarand Feb 25 '21 at 12:22
  • You seem to be doing everything right. This is all you need to use a package. I suggest deleting your binary and doing a new `cargo new`. – Hadus Feb 25 '21 at 12:23
  • You might also try to restart whatever editor you are using or even your computer.. – Hadus Feb 25 '21 at 12:26
  • 2
    @Nantarand: well yes, it is normally kind of assumed, but given you seem to have problems with importing a package, it's better to post your main.rs in full. For all we know, you _are_ trying to compile that file with only this line in it. That's an extra clarification from us and waste of time all around. – Sergio Tulentsev Feb 25 '21 at 12:30
  • I'll post the full files in the question's body guys! It seems such a trivial error and a minor inconvenience that's simply wasting time as Sergio said. I'm prob gonna try it with another editor and if not, well, guess won't engage into any randomness-needing project with Rust – Nantarand Feb 25 '21 at 12:33
  • 1
    Which version of the Rust compiler are you using? `rustc --version` or `cargo --version`. Implicit crate imports (as part of the 2018 edition) are only available since 1.31.0. – E_net4 Feb 25 '21 at 14:26
  • Update: It works on IntelliJ's Rust Plugin – Nantarand Mar 01 '21 at 07:59

3 Answers3

5

In file Cargo.toml add lines:

[dependencies]
rand="0.3.14"

And rebuild project!

It worked with these versions:

  • rustc 1.53.0

  • cargo 1.53.0

Took answer from here https://github.com/rust-lang/rust/issues/34616

rlandster
  • 7,294
  • 14
  • 58
  • 96
Kity Sypo
  • 61
  • 1
  • 3
1

got the same error when I run it with vs code(rust-analyzer).

When you press the run button rustic main.rs this command is called in terminal, it will cause error.

Type this in terminal,

cargo build
cargo run

It works well

-1

If you think that you did everything right and get a strange unable to import error like this then you could try cargo clean.

That will allow you to fully re-build your binary/library after that.

Hadus
  • 1,551
  • 11
  • 22