Absolute Path: The path starting from crate root. For code from an external crate, the path begins with the crate name. For code from the current crate, it starts with the literal crate
Relative Path: Starts from the current module and uses self
, super
, or an identifier in the current module
By default all child modules are private (Parent module cannot see anything in child)
On the other hand the child module can see everything in the parent module
Using super
we can reference the items from parent module of the current module
Even if an Struct is made public the fields in it are private. Each field needs to be explicitly made public (if it needs to be modified)
Enable Structs once an Enum is made public its variants automatically become public
Bringing Modules into Scope
The use
keyword is used to bring modules into scope (Similar to import statements in other languages). Once a module is brought into scope the entire path to items in that module don’t have to be used.
When using a function it is idiomatic in Rust to bring the parent module into scope
On the other hand, when bringing in structs, Enums, and other items with use
, it’s idiomatic to specify the full path
Special words used with use
crate
: Refers to current Rust projectself
: Refers to current modulesuper
: Parent module of the current module we are inside
If eat_at_restaurant
function is moved inside a module the use
statement would stop working
use
only creates the shortcut for the scope in which the use
occurs
When a module is brought into scope using use
keyword, the new name available is private
External code will not be able to use this new namespace
To make the namespace accessible from external code we can re-export the namespace using the pub use
keyword
Bringing Nested Paths into Scope
Using the Glob operation all public items in a namespace can be brought into scope
Modules in Separate Files
A file with the name of the module need to be created
If the module consists on child modules then a folder with the name of the parent module has to be created
In the folder a file with the name of the child module is created
Separating Modules into Different Files - The Rust Programming Language