官术网_书友最值得收藏!

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.
主站蜘蛛池模板: 崇礼县| 三明市| 曲周县| 连山| 五大连池市| 芒康县| 汶上县| 青海省| 德兴市| 沂南县| 闽清县| 五常市| 滦南县| 刚察县| 务川| 长宁县| 宜春市| 沧源| 剑川县| 德惠市| 旬阳县| 沭阳县| 昌宁县| 海林市| 迭部县| 商南县| 西华县| 红原县| 大港区| 永清县| 积石山| 陇川县| 蓝山县| 增城市| 库尔勒市| 长白| 资中县| 建阳市| 夏河县| 鄂尔多斯市| 黔东|