rust-random / book

The Rust Rand Book
Other
52 stars 20 forks source link

Fixed compilation on Getting Started page #63

Closed YohDeadfall closed 1 month ago

dhardy commented 1 month ago

Is this enough to actually fix it? According to (a skim of) https://github.com/rust-lang/mdBook/issues/394 we do need the extern crate line, but also -L.

The example works when copy+pasted to the playground (which has the top 1000 crates installed).

YohDeadfall commented 1 month ago

I took that line from another place where an example worked in the book, but here it isn't and fails with that:

   Compiling playground v0.0.1 (/playground)
error[E0433]: failed to resolve: maybe a missing crate `rand`?
 --> src/main.rs:1:5
  |
1 | use rand::prelude::*;
  |     ^^^^ maybe a missing crate `rand`?
  |
  = help: consider adding `extern crate rand` to use the `rand` crate

error[E0599]: no method named `gen_range` found for struct `ThreadRng` in the current scope
   --> src/main.rs:6:41
    |
6   |     println!("Random die roll: {}", rng.gen_range(1..=6));
    |                                         ^^^^^^^^^
    |
   ::: /playground/.cargo/registry/src/index.crates.io-6f17d22bba15001f/rand-0.8.5/src/rng.rs:129:8
    |
129 |     fn gen_range<T, R>(&mut self, range: R) -> T
    |        --------- the method is available for `ThreadRng` here
    |
    = help: items from traits can only be used if the trait is in scope
help: there is a method `gen_ratio` with a similar name, but with different arguments
   --> /playground/.cargo/registry/src/index.crates.io-6f17d22bba15001f/rand-0.8.5/src/rng.rs:299:5
    |
299 |     fn gen_ratio(&mut self, numerator: u32, denominator: u32) -> bool {
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: trait `Rng` which provides `gen_range` is implemented but not in scope; perhaps you want to import it
    |
1   + use rand::Rng;
    |

error[E0599]: no method named `gen` found for struct `ThreadRng` in the current scope
  --> src/main.rs:7:41
   |
7  |     println!("Random UUID: 0x{:X}", rng.gen::<u128>());
   |                                         ^^^ method not found in `ThreadRng`
   |
  ::: /playground/.cargo/registry/src/index.crates.io-6f17d22bba15001f/rand-0.8.5/src/rng.rs:93:8
   |
93 |     fn gen<T>(&mut self) -> T
   |        --- the method is available for `ThreadRng` here
   |
   = help: items from traits can only be used if the trait is in scope
help: trait `Rng` which provides `gen` is implemented but not in scope; perhaps you want to import it
   |
1  + use rand::Rng;
   |

error[E0599]: no method named `gen` found for struct `ThreadRng` in the current scope
  --> src/main.rs:9:12
   |
9  |     if rng.gen() {
   |            ^^^ method not found in `ThreadRng`
   |
  ::: /playground/.cargo/registry/src/index.crates.io-6f17d22bba15001f/rand-0.8.5/src/rng.rs:93:8
   |
93 |     fn gen<T>(&mut self) -> T
   |        --- the method is available for `ThreadRng` here
   |
   = help: items from traits can only be used if the trait is in scope
help: trait `Rng` which provides `gen` is implemented but not in scope; perhaps you want to import it
   |
1  + use rand::Rng;
   |

Some errors have detailed explanations: E0433, E0599.
For more information about an error, try `rustc --explain E0433`.
error: could not compile `playground` (bin "playground") due to 4 previous errors

After adding that # extern crate line it compiles fine:

Random die roll: 4
Random UUID: 0x3CBB0F3727EDEFF3F39D5F1A0B545E20