@@ -532,3 +532,51 @@ Goodbye in English: Goodbye.
532
532
Hello in Japanese: こんにちは
533
533
Goodbye in Japanese: さようなら
534
534
```
535
+
536
+ ## Complex imports
537
+
538
+ Rust offers several advanced options that can add compactness and
539
+ convenience to your `extern crate` and `use` statements. Here is an example:
540
+
541
+ ```rust,ignore
542
+ extern crate phrases as sayings;
543
+
544
+ use sayings::japanese::greetings as ja_greetings;
545
+ use sayings::japanese::farewells::*;
546
+ use sayings::english::{self, greetings as en_greetings, farewells as en_farewells};
547
+
548
+ fn main() {
549
+ println!("Hello in English; {}", en_greetings::hello());
550
+ println!("And in Japanese: {}", ja_greetings::hello());
551
+ println!("Goodbye in English: {}", english::farewells::goodbye());
552
+ println!("Again: {}", en_farewells::goodbye());
553
+ println!("And in Japanese: {}", goodbye());
554
+ }
555
+ ```
556
+
557
+ What' s going on here?
558
+
559
+ First, both ` extern crate` and ` use` allow renaming the thing that is being
560
+ imported. So the crate is still called " phrases" , but here we will refer
561
+ to it as " sayings" . Similarly, the first ` use` statement pulls in the
562
+ ` japanese::farewells` module from the crate, but makes it available as
563
+ ` jp_farewells` as opposed to simply ` farewells` . This can help to avoid
564
+ ambiguity when importing similarly-named items from different places.
565
+
566
+ The second ` use` statement uses a star glob to bring in _all_ symbols from the
567
+ ` sayings::japanese::farewells` module. As you can see we can later refer to
568
+ the Japanese ` goodbye` function with no module qualifiers. This kind of glob
569
+ should be used sparingly.
570
+
571
+ The third ` use` statement bears more explanation. It' s using "brace expansion"
572
+ globbing to compress three `use` statements into one (this sort of syntax
573
+ may be familiar if you' ve written Linux shell scripts before). The
574
+ uncompressed form of this statement would be:
575
+ ` ` ` rust,ignore
576
+ use sayings::english;
577
+ use sayings::english::greetings as en_greetings;
578
+ use sayings::english::farewells as en_farewells;
579
+ ` ` `
580
+ As you can see, the curly brackets compress ` use` statements for several items
581
+ under the same path, and in this context ` self` just refers back to that path.
582
+ Note: The curly brackets cannot be nested or mixed with star globbing.
0 commit comments