- 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.
推薦閱讀
- 密碼學原理與Java實現
- 深入淺出Electron:原理、工程與實踐
- 你必須知道的204個Visual C++開發問題
- 從學徒到高手:汽車電路識圖、故障檢測與維修技能全圖解
- The DevOps 2.4 Toolkit
- Yocto for Raspberry Pi
- 快速念咒:MySQL入門指南與進階實戰
- 自然語言處理Python進階
- Java EE核心技術與應用
- Learning Apache Karaf
- Julia for Data Science
- Clean Code in C#
- Solr權威指南(下卷)
- Drupal Search Engine Optimization
- Elasticsearch搜索引擎構建入門與實戰