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

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.

主站蜘蛛池模板: 孟津县| 东阳市| 乌鲁木齐市| 蒙城县| 黔西县| 新乡市| 乡城县| 习水县| 新干县| 师宗县| 沭阳县| 淅川县| 通海县| 瑞金市| 东乌珠穆沁旗| 华亭县| 志丹县| 兴义市| 茶陵县| 沙洋县| 驻马店市| 宜城市| 枣庄市| 绥芬河市| 庆安县| 巩义市| 苏尼特右旗| 尼勒克县| 西华县| 长白| 汉寿县| 英超| 合川市| 桐柏县| 环江| 开化县| 息烽县| 平泉县| 西峡县| 博湖县| 江华|