Code runs in Playground but not locally

The following code from this post runs in the Playground...

use rand::{SeedableRng, Rng};
use rand_pcg::Pcg32;

fn main() {
    let mut rng = Pcg32::seed_from_u64(42);
    for _ in 0..10 {
        let x: u32 = rng.gen();
        println!("{}", x);
    }
}

...but has a compilation error (and some warnings) on my machine:

error[E0599]: no function or associated item named `seed_from_u64` found for struct `Lcg64Xsh32` in the current scope
 --> src/main.rs:5:26
  |
5 |     let mut rng = Pcg32::seed_from_u64(42);
  |                          ^^^^^^^^^^^^^ function or associated item not found in `Lcg64Xsh32`
  |
  = 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_core::SeedableRng;
  |

warning: unused import: `SeedableRng`
 --> src/main.rs:1:12
  |
1 | use rand::{SeedableRng, Rng};
  |            ^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: unused import: `Rng`
 --> src/main.rs:1:25
  |
1 | use rand::{SeedableRng, Rng};
  |                         ^^^

Following the compiler's instructions about the error E0599 , I changed

use rand::{SeedableRng, Rng};

to

use rand::Rng;
use rand_core::SeedableRng;

and built again, this time receiving the same type of error, but for a different place in the code:

error[E0599]: no method named `gen` found for struct `Lcg64Xsh32` in the current scope
  --> src/main.rs:8:26
   |
8  |         let x: u32 = rng.gen();
   |                          ^^^ method not found in `Lcg64Xsh32`
   | 
  ::: /home/sjw/.cargo/registry/src/github.com-1ecc6299db9ec823/rand_pcg-0.3.0/src/pcg64.rs:34:1
   |
34 | pub struct Lcg64Xsh32 {
   | ---------------------
   | |
   | doesn't satisfy `Lcg64Xsh32: Rng`
   | doesn't satisfy `Lcg64Xsh32: rand::RngCore`
   |
   = note: the method `gen` exists but the following trait bounds were not satisfied:
           `Lcg64Xsh32: rand::RngCore`
           which is required by `Lcg64Xsh32: Rng`

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

I can see from the compiler output that rand::Rng is unused, and that rand_pcg::Lcg64Xsh32 requires rang::Rng, so I imagine there is maybe something missing to link these two together.

Both the Playground and my local machine are using 1.48.0 from the Stable channel of the 2018 edition.

Thank you for any tips!

What version of the crate rand and rand_pcg you have in local?

I have this in my Cargo.toml [dependencies]:

rand = "0.7.3"
rand_core = "0.6.0"
rand_pcg = "0.3.0"

(When first running the code locally, before modifying it, I did not have the rand_core dependency listed.)

The rand crate has not yet been updated to use the latest versions of rand_core and rand_pcg. Try using the previous version:

rand = "0.7.3"
rand_pcg = "0.2.0"

or use only rand_core and rand_pcg directly, without the rand crate:

rand_core = "0.6.0"
rand_pcg = "0.3.0"

You'll be able to use the latest versions of all three crates together when rand 0.8 is released, soon.

2 Likes

Update: rand 0.8.0 has been released, so you can now use it with the latest version of rand_pcg.

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.