Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import everything from the crate root?

Tags:

rust

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?

like image 304
Simon Avatar asked Dec 16 '16 18:12

Simon


People also ask

How do you import modules in Rust?

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.

What is crate root in Rust?

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.

What is a library crate in Rust?

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).


1 Answers

As of Rust 1.14, use *; and use ::*; now work as intended (importing everything from the crate root)!


Useful links:

  • PR that introduced the syntax
  • Old issue about the lack of said syntax
  • My old answer in the edit log
like image 165
Lukas Kalbertodt Avatar answered Sep 23 '22 06:09

Lukas Kalbertodt