A module as a section of a file

To define a module as a section of a file, we use the mod keyword followed by a name and then a { symbol, then the contents of the module, and then a } symbol to finish it up.

So, if we define a new module containing a couple of functions, it would look something like this:

pub mod module_a {
pub fn a_thing() {
println!("This is a thing");
}

pub fn a_second_thing() {
a_thing();
println!("This is another thing");
}
}

We've created a module named module_a and put the a_thing and a_second_thing functions inside of it. We haven't seen it previously, but the line in a_second_thing that says a_thing(); is an instruction to the computer to run the a_thing function. So, when a_second_thing runs, the first thing it does is run a_thing, and then it prints out its own message afterwards.

The pub keyword means that module_a is part of the public interface of the current module, rather than just being internal data. We'll talk more about that soon.
..................Content has been hidden....................

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