Skip to content

Commit

Permalink
Merge pull request #1146 from pietroalbini/use-nested-groups-book
Browse files Browse the repository at this point in the history
Add use_nested_groups to the newest features appendix
  • Loading branch information
carols10cents authored Feb 22, 2018
2 parents 28280cf + adba964 commit 8b26794
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions second-edition/dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ proc
pthreads
pushups
QuitMessage
quux
RAII
randcrate
RangeFrom
Expand Down
34 changes: 34 additions & 0 deletions second-edition/src/appendix-06-newest-features.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,37 @@ fn main() {
assert_eq!(result, 20);
}
```


## Nested groups in `use` declarations

If you have a complex module tree with many different submodules and you need
to import a few items from each one, it might be useful to group all the
imports in the same declaration to keep your code clean and avoid repeating the
base modules' name.

The `use` declaration supports nesting to help you in those cases, both with
simple imports and glob ones. For example this snippets imports `bar`, `Foo`,
all the items in `baz` and `Bar`:

```rust
# #![allow(unused_imports, dead_code)]
#
# mod foo {
# pub mod bar {
# pub type Foo = ();
# }
# pub mod baz {
# pub mod quux {
# pub type Bar = ();
# }
# }
# }
#
use foo::{
bar::{self, Foo},
baz::{*, quux::Bar},
};
#
# fn main() {}
```

0 comments on commit 8b26794

Please sign in to comment.