- Rust Programming Cookbook
- Claus Matzinger
- 267字
- 2021-06-24 12:27:43
How it works...
In this recipe, we explored the relationship between crates and modules. Rust supports several ways of encapsulating code into units, and the 2018 edition has made it a lot easier to do. Seasoned Rust programmers will miss the extern crate declaration(s) at the top of the files, which is nowadays only necessary in special cases. Instead, the crate's contents can be used right away in a use statement.
In this way, the line between modules and crates is now blurred. However, modules are much simpler to create since they are part of the project and only need to be declared in the root module to be compiled. This declaration is done using the mod statement, which also supports implementation in its body—something that is used a lot in testing. Regardless of the implementation's location, using an external or internal function requires a use statement, often prefixed with crate:: to hint toward its location.
Alternatively to simple files, a module can also be a directory that contains at least a mod.rs file. This way, large code bases can nest and structure their traits and structs accordingly.
A note on function visibility: Rust's default parameter is module visibility. Hence, a function declared and implemented in a module can only be seen from within that module. Contrary to that, the pub modifier exports the function to outside users. The same goes for properties and functions attached to a struct.
We've successfully learned how to split our code with crates and modules. Now, let's move on to the next recipe.
- Mastering OpenLayers 3
- ASP.NET MVC4框架揭秘
- WebAssembly實戰
- 深度學習:算法入門與Keras編程實踐
- C#程序設計基礎:教程、實驗、習題
- SQL基礎教程(第2版)
- Mastering ROS for Robotics Programming
- HTML 5與CSS 3權威指南(第3版·上冊)
- Python圖形化編程(微課版)
- C++反匯編與逆向分析技術揭秘(第2版)
- Python爬蟲、數據分析與可視化:工具詳解與案例實戰
- Frank Kane's Taming Big Data with Apache Spark and Python
- 時空數據建模及其應用
- Modern C++ Programming Cookbook
- JavaScript+jQuery網頁特效設計任務驅動教程