January 2019
Beginner to intermediate
554 pages
13h 31m
English
The simplest way to create a module is by using the mod {} block within an existing module. Consider the following code:
// mod_within.rsmod food { struct Cake; struct Smoothie; struct Pizza;}fn main() { let eatable = Cake;}
We created an inner module named food. To create a module within an existing one, we use the mod keyword, followed by the name of the module, food, followed by a pair of braces. Within braces, we can declare any kind of item or even a nested module. Within our food module, we declared three structs: Cake, Smoothie, and Pizza. In main, we then create a Cake instance from the food module using the path syntax food::Cake. Let's compile this program:
Strange! The compiler does not see any Cake type being defined. ...
Read now
Unlock full access