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

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.
主站蜘蛛池模板: 隆尧县| 陆良县| 金昌市| 凯里市| 专栏| 德保县| 手机| 罗源县| 万州区| 花莲市| 东辽县| 江山市| 平邑县| 湘阴县| 兴文县| 新巴尔虎右旗| 雷波县| 化隆| 张家口市| 湖州市| 昭通市| 赫章县| 宣城市| 多伦县| 敖汉旗| 视频| 广宁县| 嵊州市| 台东县| 连城县| 平山县| 绥江县| 万载县| 桦川县| 陵水| 兰溪市| 汽车| 河源市| 西乌| 遂平县| 庄浪县|