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

Public and private module items

In many of the preceding examples, we saw a pub keyword. That keyword makes the item it's attached to public, meaning that it is available to code that is not part of the same module. If we omit the pub keyword on an item, that item is private, meaning that it can only be accessed within the module where it is defined. Private is the default, so we need to explicitly mark those items that we want to have as part of the module's externally accessible interface as public by using the pub keyword.

Making an item private is not a security mechanism. If you're worried that your code will be linked with hostile code that might misuse your code or data, making the code or data private will not protect against such attacks. Rather, the distinction between public and private exists in order to help us make it plain which parts of the code are intended for use outside of the current module, and which are meant to be used only internally. This helps us maintain our software, because we are free to make whatever changes we want to to private items, whereas with public items, we have to be careful that our changes do not break external things we might not even be aware exist.

主站蜘蛛池模板: 洛扎县| 陇南市| 富阳市| 堆龙德庆县| 肇州县| 清苑县| 南江县| 比如县| 天门市| 长治县| 婺源县| 文山县| 乌拉特后旗| 思南县| 盐源县| 涿鹿县| 城市| 青冈县| 屯留县| 辛集市| 湖州市| 江山市| 大连市| 木兰县| 南汇区| 教育| 赤峰市| 焉耆| 靖州| 利辛县| 光泽县| 柘荣县| 铜梁县| 札达县| 白城市| 监利县| 晋宁县| 游戏| 阜新市| 龙口市| 汝南县|