- 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.
推薦閱讀
- Building a Game with Unity and Blender
- C語言程序設計基礎與實驗指導
- Android Development with Kotlin
- HTML5+CSS3網頁設計
- Web Development with MongoDB and Node(Third Edition)
- Raspberry Pi Home Automation with Arduino(Second Edition)
- Creating Stunning Dashboards with QlikView
- Android玩家必備
- Linux Shell核心編程指南
- Building Serverless Web Applications
- Learning JavaScript Data Structures and Algorithms(Second Edition)
- Kotlin極簡教程
- Python數據可視化之美:專業圖表繪制指南(全彩)
- 創意UI Photoshop玩轉移動UI設計
- 深入實踐DDD:以DSL驅動復雜軟件開發