- 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.
- Python機器學(xué)習(xí):數(shù)據(jù)分析與評分卡建模(微課版)
- FreeSWITCH 1.8
- TypeScript圖形渲染實戰(zhàn):基于WebGL的3D架構(gòu)與實現(xiàn)
- C語言程序設(shè)計案例式教程
- Expert Data Visualization
- R大數(shù)據(jù)分析實用指南
- C# 8.0核心技術(shù)指南(原書第8版)
- 青少年信息學(xué)競賽
- Linux C編程:一站式學(xué)習(xí)
- OpenResty完全開發(fā)指南:構(gòu)建百萬級別并發(fā)的Web應(yīng)用
- ArcGIS for Desktop Cookbook
- Fast Data Processing with Spark(Second Edition)
- Spring技術(shù)內(nèi)幕:深入解析Spring架構(gòu)與設(shè)計原理(第2版)
- Zend Framework 2 Cookbook
- 系統(tǒng)分析師UML用例實戰(zhàn)