< 返回版块

苦瓜小仔 发表于 2021-06-29 19:53

The Rust Rand Book:

Rand 库是一个“古老”的库,Rust 诞生不久它就出现了。

我想大多数人在第一眼看到它的时候都会觉得它的设计过于复杂。 当我翻译完(准确说是翻译一半)这本书之后,依然会觉得 Rand 库相比于标准库没有很好地被设计。

rust-random 里的各个项目 star 数也侧面说明了,它的确存在一些不足。

更新说明】之前的内容全部翻译完毕,之后的内容我觉得没必要翻译。翻译内容仅供学习参考。

评论区

写评论
苦瓜小仔 2021-12-19 00:31

纠正一下:

Standard 依然是均匀分布,

rand_distr 库里的 Normal 才是正态分布。

苦瓜小仔 2021-12-18 22:38

正如 Rand 库自己的介绍

It's also worth pointing out what rand is not:

Small. Most low-level crates are small, but the higher-level rand and rand_distr each contain a lot offunctionality.

Simple (implementation). We have a strong focus on correctness, speed and flexibility, but not simplicity. If youprefer a small-and-simple library, there are alternatives including fastrand and oorandom.

Slow. We take performance seriously, with considerations also for set-up time of new distributions, commonly-usedparameters, and parameters of the current sampler.

Rand 库非常有历史, 库的各个功能拆分地很详细,但是对于使用者来说太麻烦;此外它的许多高级接口都是泛型定义的(当然这有很多好处),初学者很容易望而却步。

我的意思不是它写得不好,而是它不像标准库那样易学易用。熟悉它需要去了解很多细节。比如,我得学习和理解它如何生成正态分布的随机值 let val: f32 = StdRng::from_entropy().sample(Standard); —— 这在其他语言里写相同的功能的代码却很直观。

除了官方推荐的 fastrandoorandom crate,你还可以试试 nanorand

rand 库在 Rust 的地位犹如标准库的存在,它是很优秀的库。

每个库都有每个库被设计出来的目的,如果你追求性能, rand 库是首选。

--
👇
awsa2ron: “当我翻译完(准确说是翻译一半)这本书之后,依然会觉得 Rand 库相比于标准库没有很好地被设计。” 大佬的意思是这个库写的并不好,但是现在也只能凑合用吗?有别的库推荐?

awsa2ron 2021-12-18 19:50

“当我翻译完(准确说是翻译一半)这本书之后,依然会觉得 Rand 库相比于标准库没有很好地被设计。” 大佬的意思是这个库写的并不好,但是现在也只能凑合用吗?有别的库推荐?

xian9Yu 2021-06-30 09:23

大佬666

Mike Tang 2021-06-29 20:38

666

1 共 5 条评论, 1 页