- Rust Quick Start Guide
- Daniel Arbuckle
- 218字
- 2021-06-10 19:46:03
A module as a separate file
More often than not, we're going to want to give our modules their own files. It's just nicer to keep things separated and contained as much as possible, because it helps keep the code manageable. Fortunately, this is just as easy. In our .rs file, we can just write something like the following:
pub mod module_b;
That looks a lot like the previous example, except that it doesn't have the module contents right there between { and }. Instead, the Rust compiler goes looking for a file called either module_b.rs or module_b/mod.rs, and uses the whole file as the contents of the module_b module. So, if the file contains a couple of functions similar to the ones we saw previously:
pub fn a_thing() {
println!("This is a module_b thing");
}
pub fn a_second_thing() {
a_thing();
println!("This is another module_b thing");
}
Then module_b will contain two functions named a_thing and a_second_thing. It's not a problem that those functions have the same names as functions in the module_a module from before, because they're in a different module.
- CockroachDB權威指南
- Learning Selenium Testing Tools with Python
- Learning Flask Framework
- 體驗設計原理:行為、情感和細節
- C++面向對象程序設計習題解答與上機指導(第三版)
- 零基礎Java學習筆記
- 創意UI Photoshop玩轉移動UI設計
- 官方 Scratch 3.0 編程趣味卡:讓孩子們愛上編程(全彩)
- SAP Web Dynpro for ABAP開發技術詳解:基礎應用
- Building a Media Center with Raspberry Pi
- Arduino Electronics Blueprints
- React.js實戰
- ServiceDesk Plus 8.x Essentials
- Elixir Cookbook
- Data Analysis with R