From e5b3fca64a30f22824fe3f20b457a50dca020e55 Mon Sep 17 00:00:00 2001 From: Speedy Consoles Date: Thu, 20 Dec 2018 16:25:54 +0100 Subject: [PATCH 1/3] Clarify pub(restricted) a bit --- src/visibility-and-privacy.md | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/visibility-and-privacy.md b/src/visibility-and-privacy.md index 17d3cc426..49afc0575 100644 --- a/src/visibility-and-privacy.md +++ b/src/visibility-and-privacy.md @@ -145,14 +145,15 @@ expressions, types, etc. ## `pub(in path)`, `pub(crate)`, `pub(super)`, and `pub(self)` In addition to public and private, Rust allows users to declare an item as -visible within a given scope. The rules for `pub` restrictions are as follows: +visible only within a given scope. The rules for `pub` restrictions are as +follows: - `pub(in path)` makes an item visible within the provided `path`. `path` must be a parent module of the item whose visibility is being declared. - `pub(crate)` makes an item visible within the current crate. - `pub(super)` makes an item visible to the parent module. This is equivalent to `pub(in super)`. - `pub(self)` makes an item visible to the current module. This is equivalent -to `pub(in self)`. +to `pub(in self)` or not using `pub` at all. > **Edition Differences**: Starting with the 2018 edition, paths for > `pub(in path)` must start with `crate`, `self`, or `super`. The 2015 edition @@ -177,7 +178,8 @@ pub mod outer_mod { inner_mod_visible_fn(); } - // This function is visible + // This function is visible only within `inner_mod`, + // which is the same as leaving it private pub(self) fn inner_mod_visible_fn() {} } pub fn foo() { @@ -209,6 +211,11 @@ fn bar() { fn main() { bar() } ``` +Note that this syntax only adds another restriction to the visibility of an +item. It does not guarantee that the item is visible within all parts of the +specified scope. In order to access an item all of its parent items up to the +current scope must still be visible as well. + ## Re-exporting and Visibility Rust allows publicly re-exporting items through a `pub use` directive. Because From b0fc4d91963254f7c475cb15e79f3eafc720109e Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Thu, 20 Dec 2018 20:24:03 +0100 Subject: [PATCH 2/3] Fix grammar in pub(restricted) clarification Co-Authored-By: Speedy-Consoles --- src/visibility-and-privacy.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/visibility-and-privacy.md b/src/visibility-and-privacy.md index 49afc0575..0b5fecec9 100644 --- a/src/visibility-and-privacy.md +++ b/src/visibility-and-privacy.md @@ -179,7 +179,7 @@ pub mod outer_mod { } // This function is visible only within `inner_mod`, - // which is the same as leaving it private + // which is the same as leaving it private. pub(self) fn inner_mod_visible_fn() {} } pub fn foo() { @@ -213,7 +213,7 @@ fn main() { bar() } Note that this syntax only adds another restriction to the visibility of an item. It does not guarantee that the item is visible within all parts of the -specified scope. In order to access an item all of its parent items up to the +specified scope. To access an item, all of its parent items up to the current scope must still be visible as well. ## Re-exporting and Visibility From 495efdb044799956e620227d1b1c103707233983 Mon Sep 17 00:00:00 2001 From: Speedy Consoles Date: Sun, 30 Dec 2018 11:49:51 +0100 Subject: [PATCH 3/3] Indent note for pub(restricted) --- src/visibility-and-privacy.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/visibility-and-privacy.md b/src/visibility-and-privacy.md index 0b5fecec9..70168b7cb 100644 --- a/src/visibility-and-privacy.md +++ b/src/visibility-and-privacy.md @@ -211,10 +211,10 @@ fn bar() { fn main() { bar() } ``` -Note that this syntax only adds another restriction to the visibility of an -item. It does not guarantee that the item is visible within all parts of the -specified scope. To access an item, all of its parent items up to the -current scope must still be visible as well. +> **Note:** This syntax only adds another restriction to the visibility of an +> item. It does not guarantee that the item is visible within all parts of the +> specified scope. To access an item, all of its parent items up to the +> current scope must still be visible as well. ## Re-exporting and Visibility