官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 堆龙德庆县| 莫力| 葫芦岛市| 财经| 垣曲县| 扎鲁特旗| 翼城县| 长泰县| 陆川县| 洛阳市| 广饶县| 栖霞市| 永康市| 准格尔旗| 青浦区| 玉屏| 望谟县| 乌海市| 丰宁| 孝感市| 东阿县| 方正县| 新平| 鲁甸县| 兖州市| 顺平县| 南雄市| 北川| 黔西县| 集贤县| 来宾市| 迭部县| 金塔县| 潢川县| 斗六市| 福安市| 大渡口区| 灵丘县| 怀集县| 武定县| 卢龙县|