What is the difference between use and external?

I am new to Rust. I think use used to import identifiers into the current scope, and extern used to declare an external module. But this understanding (maybe wrong) does not make any sense to me. Can someone explain why Rust has these two concepts and what are the appropriate cases to use them?

+19
source share
2 answers

extern crate foo indicates what you want to link to the external library, and enters the name of the top-level box into the scope ( use foo ). Starting with Rust 2018, in most cases, you will no longer need to use extern crate , because Cargo informs the compiler about the presence of boxes. (There are one or two exceptions )

use bar is a shorthand for referring to fully qualified characters.

Theoretically, the language does not need use - you can always just fully qualify the names, but entering std::str::String.new(...) will be very tedious! Instead, you can simply type use std::str::String once, and then String will refer to it. (the use statement for String is part of the prelude, so you usually don't see this)

+17
source

The accepted answer was correct at the time of writing. This however is no longer correct. extern crate almost unnecessary with Rust 2018.

Now you only need to add external dependencies to your Cargo.toml.

use works the same as before.

Read more in the official documentation .

Change: the accepted answer has been edited to correctly reflect the changes in Rust 2018.

+5
source

Source: https://habr.com/ru/post/984594/


All Articles