dimanche 20 janvier 2019

How do I sample a random String with alphanumeric characters?

I tried to compile the following code:

extern crate rand;
use rand::Rng;

fn main() {
    rand::thread_rng()
        .gen_ascii_chars()
        .take(10)
        .collect::<String>();
}

but cargo build says:

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

error[E0599]: no method named `gen_ascii_chars` found for type `rand::prelude::ThreadRng` in the current scope
 --> src/main.rs:6:10
  |
6 |         .gen_ascii_chars()
  |          ^^^^^^^^^^^^^^^

The Rust compiler asks me to remove the use rand::Rng; clause, at the same time complaining that there is no gen_ascii_chars method. I would expect Rust to just use rand::Rng trait, and to not provide such a contradictory error messages. How can I go further from here?




Aucun commentaire:

Enregistrer un commentaire