@@ -263,6 +263,10 @@ the `phrases` crate. We can then use `phrases`’ modules in this one. As we
263263mentioned earlier, you can use double colons to refer to sub-modules and the
264264functions inside of them.
265265
266+ (Note: when importing a crate that has dashes in its name "like-this", which is
267+ not a valid Rust identifier, it will be converted by changing the dashes to
268+ underscores, so you would write ` extern crate like_this; ` .)
269+
266270Also, Cargo assumes that ` src/main.rs ` is the crate root of a binary crate,
267271rather than a library crate. Our package now has two crates: ` src/lib.rs ` and
268272` src/main.rs ` . This pattern is quite common for executable crates: most
@@ -532,3 +536,51 @@ Goodbye in English: Goodbye.
532536Hello in Japanese: こんにちは
533537Goodbye in Japanese: さようなら
534538```
539+
540+ ## Complex imports
541+
542+ Rust offers several advanced options that can add compactness and
543+ convenience to your `extern crate` and `use` statements. Here is an example:
544+
545+ ```rust,ignore
546+ extern crate phrases as sayings;
547+
548+ use sayings::japanese::greetings as ja_greetings;
549+ use sayings::japanese::farewells::*;
550+ use sayings::english::{self, greetings as en_greetings, farewells as en_farewells};
551+
552+ fn main() {
553+ println!("Hello in English; {}", en_greetings::hello());
554+ println!("And in Japanese: {}", ja_greetings::hello());
555+ println!("Goodbye in English: {}", english::farewells::goodbye());
556+ println!("Again: {}", en_farewells::goodbye());
557+ println!("And in Japanese: {}", goodbye());
558+ }
559+ ```
560+
561+ What' s going on here?
562+
563+ First, both ` extern crate` and ` use` allow renaming the thing that is being
564+ imported. So the crate is still called " phrases" , but here we will refer
565+ to it as " sayings" . Similarly, the first ` use` statement pulls in the
566+ ` japanese::farewells` module from the crate, but makes it available as
567+ ` jp_farewells` as opposed to simply ` farewells` . This can help to avoid
568+ ambiguity when importing similarly-named items from different places.
569+
570+ The second ` use` statement uses a star glob to bring in _all_ symbols from the
571+ ` sayings::japanese::farewells` module. As you can see we can later refer to
572+ the Japanese ` goodbye` function with no module qualifiers. This kind of glob
573+ should be used sparingly.
574+
575+ The third ` use` statement bears more explanation. It' s using "brace expansion"
576+ globbing to compress three `use` statements into one (this sort of syntax
577+ may be familiar if you' ve written Linux shell scripts before). The
578+ uncompressed form of this statement would be:
579+ ` ` ` rust,ignore
580+ use sayings::english;
581+ use sayings::english::greetings as en_greetings;
582+ use sayings::english::farewells as en_farewells;
583+ ` ` `
584+ As you can see, the curly brackets compress ` use` statements for several items
585+ under the same path, and in this context ` self` just refers back to that path.
586+ Note: The curly brackets cannot be nested or mixed with star globbing.
0 commit comments