2.1 特性

原文链接: https://rust-random.github.io/book/crate-features.html

建议查看 crate 的 Cargo.toml 或 README.md 以了解特性。自 rand v0.9 起,rust-random crate 仅使用显式特性(即所有特性都列在 [features] 下)。

Cargo.toml 的发布版本可在 docs.rs 上查看:

通用特性

以下特性在 rand_core、rand、rand_distr 以及部分 RNG crate 中通用:

  • std:启用依赖 std 库的功能。除 rand_core 外默认启用;对于 no_std 用法,请使用 default-features = false。
  • alloc:启用需要分配器的功能(用于 no_std)。由 std 隐含。
  • serde:通过 serde(1.0 版)启用序列化。

rand_distr 特性

来自 num_traits 和 libm 的浮点函数用于支持 no_std 环境并确保可重现性。如果更倾向于使用 std 的浮点函数(可能提供更好的精度和性能,但可能产生不同的随机值),可以启用 std_math 特性。(请注意,任何其他依赖 num-traits 的 std 特性(默认启用)的 crate 也会产生相同效果。)

最后修改 August 23, 2026: 更新 (499855b16)