How to correctly define test-only dependencies in Rust/Cargo?
Hello colleagues! I started learning Rust and encountered a problem when writing tests. I need to use the `rand` crate to generate random data in tests, but I don't want it to be a dependency in the final build.
I added `rand` to the `[dependencies]` section in `Cargo.toml`, but this increases the binary size and compilation time. How do I properly specify dependencies only for tests?
I tried writing this:
```toml
[dev-dependencies]
rand = "0.8"
```
But then in tests I can't import: `use rand::Rng;` — the compiler complains that the crate is not found.
I also tried using `#[cfg(test)]` for conditional compilation, but I don't understand how to apply it correctly to dependencies.
Please advise how to organize test-only dependencies in Rust/Cargo.
No answers yet.
Log in to leave an answer
Blogs
Top 10 Mistakes Beginner Java Developers Make and How to Avoid Them