Book: ch02: Complete guessing game code doesn't compile

Created on 20 Dec 2018  路  4Comments  路  Source: rust-lang/book

$ cargo build
   Compiling semver-parser v0.7.0
   Compiling libc v0.2.45
   Compiling rand_core v0.3.0
   Compiling rand_hc v0.1.0
   Compiling rand_xorshift v0.1.0
   Compiling rand_isaac v0.1.1
   Compiling semver v0.9.0
   Compiling rustc_version v0.2.3
   Compiling rand_chacha v0.1.0
   Compiling rand_pcg v0.1.1
   Compiling rand v0.6.1
   Compiling guess_game v0.1.0 (/home/juliomon/tmp/rust/guess_game)
error[E0432]: unresolved import `rand`
 --> src/main.rs:3:5
  |
3 | use rand::Rng;
  |     ^^^^ maybe a missing `extern crate rand;`?

warning: unused import: `rand::Rng`
 --> src/main.rs:3:5
  |
3 | use rand::Rng;
  |     ^^^^^^^^^
  |
  = note: #[warn(unused_imports)] on by default

error[E0599]: no method named `gen_range` found for type `rand::rngs::thread::ThreadRng` in the current scope
 --> src/main.rs:8:44
  |
8 |     let secret_number = rand::thread_rng().gen_range(1, 101);
  |                                            ^^^^^^^^^
  |
  = help: items from traits can only be used if the trait is in scope
help: the following trait is implemented but not in scope, perhaps add a `use` for it:
  |
1 | use rand::Rng;
  |

error: aborting due to 2 previous errors

Some errors occurred: E0432, E0599.
For more information about an error, try `rustc --explain E0432`.
error: Could not compile `guess_game`.

All 4 comments

This is a duplicate of https://github.com/rust-lang/book/issues/1663. The fix is to add edition="2018" to your Cargo.toml and use Rust >= 1.31.0. Thanks!

Guessing game doesn't compile for me. The extern crate rand trick doesn't work either. What could I be doing wrong?

use std::io;
use rand::Rng;

fn main() {
    println!("Guess the number!");

    let secret_number = rand::thread_rng().gen_range(1, 101);

    println!("The secret number is: {}", secret_number);

    println!("Please input your guess.");

    let mut guess = String::new();

    io::stdin()
        .read_line(&mut guess)
        .expect("Failed to read line");

    println!("You guessed: {}", guess);
}

Cargo.toml contents below:

[package]
name = "hello-cargo"
version = "0.1.0"
authors = ["rajesh"]
edition = "2018"

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

[dependencies]
rand = "0.6"

Error description as below:

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

error[E0433]: failed to resolve: use of undeclared type or module `rand` --> .\guessinggame2.rs:7:25
  |
7 |     let secret_number = rand::thread_rng().gen_range(1, 101);
  |                         ^^^^ use of undeclared type or module `rand`
error: aborting due to 2 previous errors

rustc --version : rustc 1.44.1 (c7087fe00 2020-06-17)
Running it on Windows 10.

@aiexplorations it looks like your code is in a file named .\guessinggame2.rs that is next to your Cargo.toml file. Try creating a directory named src and putting the contents of guessinggame2.rs in the src directory in a file named main.rs and then use the command cargo run. Cargo looks for main.rs as the root file of a binary crate.

@carols10cents Thank you! That helped. I am now able to compile it.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

istarus picture istarus  路  4Comments

xStrom picture xStrom  路  4Comments

icanrealizeum picture icanrealizeum  路  3Comments

nathany picture nathany  路  3Comments

serialhex picture serialhex  路  4Comments