65

I'm trying to use rand::SmallRng. The documentation says

This PRNG is feature-gated: to use, you must enable the crate feature small_rng.

I've been searching and can't figure out how to enable "crate features". The phrase isn't even used anywhere in the Rust docs. This is the best I could come up with:

[features]
default = ["small_rng"]

But I get:

Feature default includes small_rng which is neither a dependency nor another feature

Are the docs wrong, or is there something I'm missing?

2 Answers 2

83

Specify the dependencies in Cargo.toml like so:

[dependencies]
rand = { version = "0.7.2", features = ["small_rng"] }

Alternatively:

[dependencies.rand]
version = "0.7.2"
features = ["small_rng"]

Both work.

Sign up to request clarification or add additional context in comments.

Comments

25

To add a feature you can use. This works even if you have added the crate initially and want to add a feature later

cargo add rand -F small_rng

1 Comment

This works even before adding the crate. The -F is a shorthand for --features.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.