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

  • Rust Quick Start Guide
  • Daniel Arbuckle
  • 179字
  • 2021-06-10 19:46:03

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.
主站蜘蛛池模板: 江源县| 炎陵县| 竹北市| 广水市| 阿鲁科尔沁旗| 鄂伦春自治旗| 共和县| 理塘县| 安陆市| 盐山县| 利津县| 渭南市| 广东省| 乡城县| 简阳市| 怀来县| 新密市| 宜宾市| 汉沽区| 安化县| 沙河市| 长兴县| 博爱县| 灵川县| 宁强县| 塔城市| 乌什县| 四子王旗| 龙江县| 宁安市| 锡林浩特市| 德安县| 开化县| 河东区| 芜湖县| 东至县| 迁西县| 堆龙德庆县| 崇文区| 汉源县| 新绛县|