mirror of
https://github.com/rust-lang-nursery/rust-cookbook
synced 2024-11-22 19:43:06 +00:00
fixed rand crate deprecation warning (#510)
This commit is contained in:
parent
539183c86b
commit
2ee24f4bc7
4 changed files with 5 additions and 5 deletions
|
@ -8,14 +8,13 @@ Randomly generates a string of given length ASCII characters with custom user-de
|
||||||
extern crate rand;
|
extern crate rand;
|
||||||
|
|
||||||
use rand::seq::SliceRandom;
|
use rand::seq::SliceRandom;
|
||||||
use rand::thread_rng;
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
const CHARSET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ\
|
const CHARSET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ\
|
||||||
abcdefghijklmnopqrstuvwxyz\
|
abcdefghijklmnopqrstuvwxyz\
|
||||||
0123456789)(*&^%$#@!~";
|
0123456789)(*&^%$#@!~";
|
||||||
|
|
||||||
let mut rng = thread_rng();
|
let mut rng = rand::thread_rng();
|
||||||
let password: Option<String> = (0..30)
|
let password: Option<String> = (0..30)
|
||||||
.map(|_| Some(*CHARSET.choose(&mut rng)? as char))
|
.map(|_| Some(*CHARSET.choose(&mut rng)? as char))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
By default, random numbers have [uniform distribution].
|
By default, random numbers have [uniform distribution].
|
||||||
To generate numbers with other distributions you instantiate a
|
To generate numbers with other distributions you instantiate a
|
||||||
distribution, then sample from that distribution using
|
distribution, then sample from that distribution using
|
||||||
[`IndependentSample::ind_sample`] with help of a random-number
|
[`Distribution::sample`] with help of a random-number
|
||||||
generator [`rand::Rng`].
|
generator [`rand::Rng`].
|
||||||
|
|
||||||
The [distributions available are documented here][rand-distributions]. An example using the
|
The [distributions available are documented here][rand-distributions]. An example using the
|
||||||
|
|
|
@ -16,13 +16,14 @@ fn main() {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
[`Range`] can obtain values with [uniform distribution].
|
[`Uniform`] can obtain values with [uniform distribution].
|
||||||
This has the same effect, but may be faster when repeatedly generating numbers
|
This has the same effect, but may be faster when repeatedly generating numbers
|
||||||
in the same range.
|
in the same range.
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
extern crate rand;
|
extern crate rand;
|
||||||
|
|
||||||
|
|
||||||
use rand::distributions::{Distribution, Uniform};
|
use rand::distributions::{Distribution, Uniform};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
|
|
||||||
Gets the current working directory by calling [`env::current_dir`],
|
Gets the current working directory by calling [`env::current_dir`],
|
||||||
then for each entries in [`fs::read_dir`], extracts the
|
then for each entries in [`fs::read_dir`], extracts the
|
||||||
[`DirEntry::path`] and gets the metada via [`fs::Metadata`]. The
|
[`DirEntry::path`] and gets the metadata via [`fs::Metadata`]. The
|
||||||
[`Metadata::modified`] returns the [`SystemTime::elapsed`] time since
|
[`Metadata::modified`] returns the [`SystemTime::elapsed`] time since
|
||||||
last modification. [`Duration::as_secs`] converts the time to seconds and
|
last modification. [`Duration::as_secs`] converts the time to seconds and
|
||||||
compared with 24 hours (24 * 60 * 60 seconds). [`Metadata::is_file`] filters
|
compared with 24 hours (24 * 60 * 60 seconds). [`Metadata::is_file`] filters
|
||||||
|
|
Loading…
Reference in a new issue