I can see a problem with this analogy

There was a reason I chose a car. If we think about it, all of the parts aren't really that discrete; the engine requires fuel, the electrics are needed by the engine, but the engine also generates electricity, and so on. In terms of programming, this will lead to a horrid mess.

How can we keep them apart?

The answer is that we use a scope for each. For example, the top level for this crate would be Car. We then add :: followed by the module name (Car::Engine, Car::Fuel, and so on.). If a module requires access to another module, it can be included using the usual use directive.


The name of the crate is the name used when the library is created using cargo. In this example, the command line to create this crate will be as follows:
cargo new Car
Note that we do not use the --bin flag.

Consider the following example:

// in Car::Engine 
use Fuel; 
use Electrics; 

If we break the modules down further, we extend the scope in the same way as we did previously in order to access them:

// in the main code 
use Car::Interior::Audio; 
use Car::Interior::Windows::HeatedRear; 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset