Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you enable a Rust "crate feature"?

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?

like image 891
brundolf Avatar asked Oct 21 '19 05:10

brundolf


People also ask

What does crate mean in Rust?

A crate is a compilation unit in Rust. Whenever rustc some_file.rs is called, some_file.rs is treated as the crate file. If some_file.rs has mod declarations in it, then the contents of the module files would be inserted in places where mod declarations in the crate file are found, before running the compiler over it.


1 Answers

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.

like image 139
edwardw Avatar answered Sep 22 '22 02:09

edwardw