To import symbols from a module you either need to enumerate them or use a wildcard to import everything. That is, I can use either use module::{SomeSymbol, SomeOtherSymbol};
or use module::*;
However, when importing from the top-level module, the crate root, wildcards don't work. I can use either use {SomeSymbol, SomeOtherSymbol};
or use ::{SomeSymbol, SomeOtherSymbol}};
but neither use *;
nor use ::*;
work.
Why doesn't it work and how to import everything from the crate root?
To import the module, we use the keyword mod , followed by the file name without the . rs extension. When a module is imported, it allows us to use any code that's marked as public in the file we're importing the module from. Note that the module must be imported before we use any of its code.
The crate root is a source file that the Rust compiler starts from and makes up the root module of your crate (we'll explain modules in depth in the “Defining Modules to Control Scope and Privacy” section). A package is a bundle of one or more crates that provides a set of functionality. A package contains a Cargo.
Rust programs may contain a binary crate or a library crate. A binary crate is an executable project that has a main() method. A library crate is a group of components that can be reused in other projects. Unlike a binary crate, a library crate does not have an entry point (main() method).
As of Rust 1.14, use *;
and use ::*;
now work as intended (importing everything from the crate root)!
Useful links:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With