What is the difference between use and extern crate in Rust? When to use what?
Hello, colleagues! I recently started learning Rust and got confused about two key constructs: `use` and `extern crate`. The documentation says they are both related to importing, but I don't understand when to use which.
Here is my code:
```rust
// This works
extern crate rand;
use rand::Rng;
fn main() {
let secret_number = rand::thread_rng().gen_range(1..101);
println!("Secret number: {}", secret_number);
}
```
But if I write it like this, without `extern crate`:
```rust
use rand::Rng;
fn main() {
let secret_number = rand::thread_rng().gen_range(1..101);
println!("Secret number: {}", secret_number);
}
```
The compiler complains: `can't find crate for rand`. Why is `use` needed if nothing works without `extern crate`? And conversely — if I write only `extern
No answers yet.
Log in to leave an answer
Blogs
SOLID Principles in JavaScript: Theory and Practice for Node.js
Docker and C++: Containerizing Applications from A to Z
Working with Dates and Time in Kotlin: From A to Z — A Complete Guide
Canary Deployments in C#: Safe Releases Without Downtime