我试过了

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};
  |                           ^^^

and 我试过了

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;
  |         ^^^^^^^^^

推荐答案

在遥远的过go ,rand crate 是标准图书馆的一部分,但早已是extracted to a crate箱了.你应该使用这个 crate :

指定一个Cargo.toml:

[package]
name = "stackoverflow"
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}')

为什么从stdlib中删除了这些有用的函数?

Rust的理念是尽可能多地放入 crate ,而不是标准的图书馆.这允许每段代码以不同于标准库的速度增长和演化,还允许代码在不强制永远维护的情况下增长到stop being used.

一个常见的例子是sequence of HTTP libraries in Python.有多个包以不同的方式完成相同的事情,Python维护人员必须保留其中的all个包,以提供向后兼容性.

crate 可以避免这种特殊的结果.如果一个 crate 真的稳定了很长时间,我相信它可以被重新添加到标准库中.

Rust相关问答推荐

为什么我们不能通过指针算法将Rust原始指针指向任意地址?'

捕获FnMut闭包的时间不够长

如果包名称与bin名称相同,并且main.ars位于工作区的同一 crate 中,则无法添加对lib.ars的依赖

我如何使用AWS SDK for Rust获取我承担的角色的凭据?

为什么`str`类型可以是任意大小(未知大小),而`string`类型的大小应该是已知的?

应为关联类型,找到类型参数

有没有一种方法可以创建一个闭包来计算Rust中具有随机系数的n次多项式?

Cargo.toml:如何有条件地启用依赖项功能?

Rust:为什么 &str 不使用 Into

如何在 `connect_activate()` 之外创建一个 `glib::MainContext::channel()` 并将其传入?

UnsafeCell:它如何通知 rustc Select 退出基于别名的优化?

如何在 Rust 中编写一个通用方法,它可以接受任何可以转换为另一个值的值?

Google chrome 和 Apple M1 中的计算着色器

相当于 Rust 中 C++ 的 std::istringstream

返回迭代器的特征

如何连接 Rust 中的相邻切片

Rust 函数指针似乎被borrow 判断器视为有状态的

以下打印数组每个元素的 Rust 代码有什么问题?

带有库+多个二进制文件的Cargo 项目,二进制文件由多个文件组成?

在传输不可复制的值时实现就地枚举修改