- Mastering Rust
- Rahul Sharma Vesa Kaihlavirta
- 278字
- 2021-07-02 13:35:17
Modules, imports, and use statements
Languages often provide a way to split large code bases into multiple files to manage complexity. Java follows the convention of a single public class per .java file, while C++ provides us with header files and include statements. Rust is no different and provides us with modules. Modules are a way to namespace or organize code in a Rust program. To allow flexibility in organizing our code, there are multiple ways to create modules. Modules are a complex topic to understand and to make it brief for this section, we'll highlight only the important aspects about using them. Modules are covered in detail in Chapter 2, Managing Projects with Cargo. The following are the key takeaways about modules in Rust:
- Every Rust program needs to have a root module. In executables, it is usually the main.rs file, and for libraries, it is lib.rs.
- Modules can be declared within other modules or can be organized as files and directories.
- To let the compiler know about our module, we need to declare it using the mod keyword, as in mod my_module;, in our root module.
- To use any of the items within the module, we need to use the use keyword, along with the name of the module. This is known as bringing the item into scope.
- Items defined within modules are private by default, and you need to use the pub keyword to expose them to their consumers.
That was modules in brief. Some of the advanced aspects of modules are also covered in Chapter 7, Advanced Concepts. Next, let's look at the commonly used collection types that are available in the standard library.
- Learning Docker
- 大學計算機基礎實驗教程
- Visual FoxPro 程序設計
- OpenCV 3和Qt5計算機視覺應用開發
- Internet of Things with Intel Galileo
- PLC編程與調試技術(松下系列)
- Android程序設計基礎
- 51單片機C語言開發教程
- Linux C編程:一站式學習
- Spring技術內幕:深入解析Spring架構與設計原理(第2版)
- Serverless Web Applications with React and Firebase
- HTML5 Canvas核心技術:圖形、動畫與游戲開發
- Android應用開發攻略
- Java Web入門很輕松(微課超值版)
- Hands-On GUI Application Development in Go