Skip to content

Example code error due to rand crate version difference #1202

@reddevilmidzy

Description

@reddevilmidzy

When loading code from the crate example page using "Load in playground," an error occurs due to a version difference.

use rand::Rng;

fn main() {
    let mut rng = rand::thread_rng();
    println!("{}", rng.gen::<u8>());
}
   Compiling playground v0.0.1 (/playground)
error: expected identifier, found reserved keyword `gen`
 --> src/main.rs:5:24
  |
5 |     println!("{}", rng.gen::<u8>());
  |                        ^^^ expected identifier, found reserved keyword
  |
help: escape `gen` to use it as an identifier
  |
5 |     println!("{}", rng.r#gen::<u8>());
  |                        ++

error[E0425]: cannot find function `thread_rng` in crate `rand`
 --> src/main.rs:4:25
  |
4 |     let mut rng = rand::thread_rng();
  |                         ^^^^^^^^^^ not found in `rand`
  |
help: consider importing one of these functions
  |
1 + use rand_0_8_5::thread_rng;
  |
1 + use rand_0_9_2::thread_rng;
  |
help: if you import `thread_rng`, refer to it directly
  |
4 -     let mut rng = rand::thread_rng();
4 +     let mut rng = thread_rng();
  |

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

For more information about this error, try `rustc --explain E0425`.
warning: `playground` (bin "playground") generated 1 warning
error: could not compile `playground` (bin "playground") due to 2 previous errors; 1 warning emitted

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions