如何在 Rust 1.0 中获取随机数?
How can I get a random number in Rust 1.0?
我试过了
use std::rand::{task_rng, Rng};
fn main() {
// a number from [-40.0, 13000.0)
let num: f64 = task_rng().gen_range(-40.0, 1.3e4);
println!("{}", num);
}
但这给了
error[E0432]: unresolved import `std::rand::task_rng`
--> rand.rs:1:17
|
1 | use std::rand::{task_rng, Rng};
| ^^^^^^^^ no `task_rng` in `rand`
error[E0432]: unresolved import `std::rand::Rng`
--> rand.rs:1:27
|
1 | use std::rand::{task_rng, Rng};
| ^^^ no `Rng` in `rand`
error[E0603]: module `rand` is private
--> rand.rs:1:17
|
1 | use std::rand::{task_rng, Rng};
| ^^^^^^^^
error[E0603]: module `rand` is private
--> rand.rs:1:27
|
1 | use std::rand::{task_rng, Rng};
| ^^^
我试过了
extern crate rand;
use rand::Rng;
fn main() {
let mut rng = rand::thread_rng();
if rng.gen() {
// random bool
println!("i32: {}, u32: {}", rng.gen::<i32>(), rng.gen::<u32>())
}
let tuple = rand::random::<(f64, char)>();
println!("{:?}", tuple)
}
得到了
error[E0425]: cannot find function `thread_rng` in module `rand`
--> rand.rs:5:29
|
5 | let mut rng = rand::thread_rng();
| ^^^^^^^^^^ not found in `rand`
|
help: possible candidate is found in another module, you can import it into scope
| use std::__rand::thread_rng;
error[E0425]: cannot find function `random` in module `rand`
--> rand.rs:10:27
|
10 | let tuple = rand::random::<(f64, char)>();
| ^^^^^^ not found in `rand`
error: use of unstable library feature 'rand': use `rand` from crates.io (see issue #27703)
--> rand.rs:1:5
|
1 | extern crate rand;
| ^^^^^^^^^^^^^^^^^^
error: use of unstable library feature 'rand': use `rand` from crates.io (see issue #27703)
--> rand.rs:2:9
|
2 | use rand::Rng;
| ^^^^^^^^^
在很久以前,rand
crate 是标准库的一部分,但早已成为 extracted to a crate。这个板条箱应该是您使用的那个:
指定一个Cargo.toml:
[package]
name = "Whosebug"
version = "0.0.1"
authors = ["A. Developer <developer@example.com>"]
[dependencies]
rand = "0.7.0" # Or a newer version
那么您的示例代码就可以工作了:
use rand::Rng; // 0.7.2
fn main() {
let mut rng = rand::thread_rng();
if rng.gen() { // random bool
println!("i32: {}, u32: {}", rng.gen::<i32>(), rng.gen::<u32>())
}
let tuple = rand::random::<(f64, char)>();
println!("{:?}", tuple)
}
输出:
$ cargo run
Running `target/debug/so`
i32: 1819776837, u32: 3293137459
(0.6052759716514547, '\u{69a69}')
$ cargo run
Running `target/debug/so`
(0.23882541338214436, '\u{10deee}')
Why were these useful functions removed from stdlib?
Rust 的理念是将尽可能多的代码放入 crate 而不是标准库中。这允许每段代码以不同于标准库的速度增长和发展,并且还允许代码停止使用而不强制它永远维护。
一个常见的例子是。有多个软件包都以不同的方式做同样的事情,Python 维护者必须保留 所有 以提供向后兼容性。
箱子可以避免这种特殊的结果。如果一个 crate 真的稳定了很长时间,我相信它可以重新添加到标准库中。
我试过了
use std::rand::{task_rng, Rng};
fn main() {
// a number from [-40.0, 13000.0)
let num: f64 = task_rng().gen_range(-40.0, 1.3e4);
println!("{}", num);
}
但这给了
error[E0432]: unresolved import `std::rand::task_rng`
--> rand.rs:1:17
|
1 | use std::rand::{task_rng, Rng};
| ^^^^^^^^ no `task_rng` in `rand`
error[E0432]: unresolved import `std::rand::Rng`
--> rand.rs:1:27
|
1 | use std::rand::{task_rng, Rng};
| ^^^ no `Rng` in `rand`
error[E0603]: module `rand` is private
--> rand.rs:1:17
|
1 | use std::rand::{task_rng, Rng};
| ^^^^^^^^
error[E0603]: module `rand` is private
--> rand.rs:1:27
|
1 | use std::rand::{task_rng, Rng};
| ^^^
我试过了
extern crate rand;
use rand::Rng;
fn main() {
let mut rng = rand::thread_rng();
if rng.gen() {
// random bool
println!("i32: {}, u32: {}", rng.gen::<i32>(), rng.gen::<u32>())
}
let tuple = rand::random::<(f64, char)>();
println!("{:?}", tuple)
}
得到了
error[E0425]: cannot find function `thread_rng` in module `rand`
--> rand.rs:5:29
|
5 | let mut rng = rand::thread_rng();
| ^^^^^^^^^^ not found in `rand`
|
help: possible candidate is found in another module, you can import it into scope
| use std::__rand::thread_rng;
error[E0425]: cannot find function `random` in module `rand`
--> rand.rs:10:27
|
10 | let tuple = rand::random::<(f64, char)>();
| ^^^^^^ not found in `rand`
error: use of unstable library feature 'rand': use `rand` from crates.io (see issue #27703)
--> rand.rs:1:5
|
1 | extern crate rand;
| ^^^^^^^^^^^^^^^^^^
error: use of unstable library feature 'rand': use `rand` from crates.io (see issue #27703)
--> rand.rs:2:9
|
2 | use rand::Rng;
| ^^^^^^^^^
在很久以前,rand
crate 是标准库的一部分,但早已成为 extracted to a crate。这个板条箱应该是您使用的那个:
指定一个Cargo.toml:
[package]
name = "Whosebug"
version = "0.0.1"
authors = ["A. Developer <developer@example.com>"]
[dependencies]
rand = "0.7.0" # Or a newer version
那么您的示例代码就可以工作了:
use rand::Rng; // 0.7.2
fn main() {
let mut rng = rand::thread_rng();
if rng.gen() { // random bool
println!("i32: {}, u32: {}", rng.gen::<i32>(), rng.gen::<u32>())
}
let tuple = rand::random::<(f64, char)>();
println!("{:?}", tuple)
}
输出:
$ cargo run
Running `target/debug/so`
i32: 1819776837, u32: 3293137459
(0.6052759716514547, '\u{69a69}')
$ cargo run
Running `target/debug/so`
(0.23882541338214436, '\u{10deee}')
Why were these useful functions removed from stdlib?
Rust 的理念是将尽可能多的代码放入 crate 而不是标准库中。这允许每段代码以不同于标准库的速度增长和发展,并且还允许代码停止使用而不强制它永远维护。
一个常见的例子是。有多个软件包都以不同的方式做同样的事情,Python 维护者必须保留 所有 以提供向后兼容性。
箱子可以避免这种特殊的结果。如果一个 crate 真的稳定了很长时间,我相信它可以重新添加到标准库中。