diff --git a/ch07-00-modules.html b/ch07-00-modules.html
index 6bffffef0..9385fff74 100644
--- a/ch07-00-modules.html
+++ b/ch07-00-modules.html
@@ -73,8 +73,8 @@
into other functions, both for re-use and for better organization. By splitting
your code up into smaller chunks, each chunk is easier to understand on its
own. But what happens if you find yourself with too many functions? Rust has a
-module system that handles the problem of wanting to re-use code while
-keeping your code organized.
+module system that handles the problem of wanting to re-use code while keeping
+your code organized.
In the same way that you extract lines of code into a function, you can extract
functions (and other code like structs and enums too) into different modules. A
module is a namespace that contains definitions of functions or types, and
@@ -82,8 +82,9 @@ you can choose whether those definitions are visible outside their module
(public) or not (private). Here’s an overview of how modules work:
You declare a new module with the keyword mod
-
By default, everything is set as private, but you can use the pub keyword
-to make the module public, and therefore visible outside of the namespace.
+
By default, everything is set as private (including modules). You can use the
+pub keyword to make a module public and therefore visible outside of its
+namespace.
The use keyword allows you to bring modules, or the definitions inside
modules, into scope so that it’s easier to refer to them.
Cargo creates an empty test to help us get our library started, rather
-than the “Hello, world!” binary that we get with the --bin option. We’ll look
-at the #[] and mod tests syntax a little later, but for now just make sure
-to leave it in your src/lib.rs.
+
Cargo creates an empty test to help us get our library started, rather than the
+“Hello, world!” binary that we get with the --bin option. We’ll look at the
+#[] and mod tests syntax a little later, but for now just make sure to
+leave it in your src/lib.rs.
Since we don’t have a src/main.rs, there’s nothing for Cargo to execute with
the cargo run command. Therefore, we will be using the cargo build command
to only compile our library crate’s code.
@@ -105,7 +105,7 @@ you have for your code.
For our communicator networking library, we’re first going to define a module
named network that contains the definition of a function called connect.
Every module definition in Rust starts with the mod keyword. Add this code to
-the beginning of the lib.rs file, above the test code:
+the beginning of the src/lib.rs file, above the test code:
Filename: src/lib.rs
mod network {
fn connect() {
@@ -120,7 +120,7 @@ specify the module and use the namespace syntax ::, like so:
network::connect(), rather than just connect().
We can also have multiple modules, side-by-side, in the same src/lib.rs file.
For example, to have a client module too, that also has a function named
-connect, we can add:
+connect, we can add it as shown in Listing 7-1:
Filename: src/lib.rs
mod network {
@@ -142,13 +142,14 @@ in src/lib.rs
These can have completely different functionality, and the function names do
not conflict with each other since they’re in different modules.
While in this case, we’re building a library, there's nothing special about
-lib.rs. We could also make use of submodules in a main.rs as well. In fact,
-we can also put modules inside of modules. This can be useful as your modules
-grow to keep related functionality organized together and separate
+src/lib.rs. We could also make use of submodules in src/main.rs as well. In
+fact, we can also put modules inside of modules. This can be useful as your
+modules grow to keep related functionality organized together and separate
functionality apart. The choice of how you organize your code depends on how
you think about the relationship between the parts of your code. For instance,
the client code and its connect function might make more sense to users of
-our library if it was inside the network namespace instead, like so:
+our library if it was inside the network namespace instead, like in Listing
+7-2:
Filename: src/lib.rs
mod network {
@@ -194,7 +195,7 @@ structure you would like.
Modules form a hierarchical structure, much like another structure in computing
that you’re used to: file systems! We can use Rust’s module system along with
multiple files to split Rust projects up so that not everything lives in
-src/lib.rs. For this example, we will start with this code in src/lib.rs:
+src/lib.rs. For this example, we will start with the code in Listing 7-3:
Filename: src/lib.rs
mod client {
@@ -213,7 +214,7 @@ mod network {
}
-
Listing 7-3: Three modules, client, network, and network::server all
+
Listing 7-3: Three modules, client, network, and network::server, all
defined in src/lib.rs
@@ -223,12 +224,14 @@ defined in src/lib.rs
└── network
└── server
-
If these modules had many functions, and each function was getting long, we
-would have to scroll through this file to find the code we wanted to work with.
-This would be a good reason to pull each of the client, network, and
-server modules out of src/lib.rs and into their own files. Let’s start by
-extracting the client module into another file. First, replace the client
-module code in src/lib.rs with the following:
+
If these modules had many functions, and those functions were getting long, it
+would be difficult to scroll through this file to find the code we wanted to
+work with. Because the functions are nested inside one or more mod blocks, the
+lines of code inside the functions will start getting long as well. These would
+be good reasons to pull each of the client, network, and server modules
+out of src/lib.rs and into their own files.
+
Let’s start by extracting the client module into another file. First, replace
+the client module code in src/lib.rs with the following:
Filename: src/lib.rs
mod client;
@@ -242,7 +245,6 @@ mod network {
}
}
-
We’re still defining the client module here, but by removing the curly
braces and definitions inside the client module and replacing them with a
semicolon, we’re letting Rust know to look in another location for the code
@@ -308,8 +310,8 @@ mod server {
}
}
-
Notice that we still have a mod declaration within this module file;
-this is because we still want server to be a sub-module of network.
+
Notice that we still have a mod declaration within this module file; this is
+because we still want server to be a sub-module of network.
Now run cargo build again. Success! We have one more module to extract:
server. Because it’s a sub-module—that is, a module within a module—our
current tactic of extracting a module into a file named after that module won’t
@@ -328,7 +330,7 @@ module that we extracted:
fn connect() {
}
-
When we try to cargo build, we’ll get this error:
+
When we try to cargo build, we’ll get the error shown in Listing 7-4:
$ cargo build
Compiling communicator v0.1.0 (file:///projects/communicator)
@@ -359,10 +361,8 @@ pointing to the mod server; line in src/network.rs. So src/lib.rs somehow; let’s keep reading to understand why.
The note in the middle of Listing 7-4 is actually pretty helpful, as it points
out something we haven’t yet talked about doing:
-
-
note: maybe move this module network to its own directory via
-network/mod.rs
-
+
note: maybe move this module `network` to its own directory via `network/mod.rs`
+
Instead of continuing to follow the same file naming pattern we used
previously, we can do what the note suggests:
@@ -394,13 +394,13 @@ same as it did when we had all the code in src/lib.rs in Listing 7-3:
So when we wanted to extract the network::server module, why did we have to
also change the src/network.rs file into the src/network/mod.rs file, and
-also put the code for network::server in the network directory in
+put the code for network::server in the network directory in
src/network/server.rs, instead of just being able to extract the
-network::server into src/server.rs? The reason is that Rust wouldn’t be
-able to tell that server was supposed to be a submodule of network if the
-server.rs file was in the src directory. To make it clearer why Rust can’t
-tell, let’s consider a different example where we have this module hierarchy
-with all the definitions in src/lib.rs:
+network::server module into src/server.rs? The reason is that Rust wouldn’t
+be able to tell that server was supposed to be a submodule of network if
+the server.rs file was in the src directory. To make it clearer why Rust
+can’t tell, let’s consider a different example with the following module
+hierarchy, where all the definitions are in src/lib.rs:
communicator
├── client
└── network
@@ -425,28 +425,20 @@ top-level src/client.rs is unambiguously the code that belongs to the
In summary, these are the rules of modules with regards to files:
-
-
If a module named foo has no submodules, you should put the declarations
-for foo in a file named foo.rs.
-
-
-
If a module named foo does have submodules, you should put the declarations
-for foo in a file named foo/mod.rs.
-
-
-
The first two rules apply recursively, so that if a module named foo has a
-submodule named bar and bar does not have submodules, you should have the
-following files in your src directory:
+
If a module named foo has no submodules, you should put the declarations
+for foo in a file named foo.rs.
+
If a module named foo does have submodules, you should put the declarations
+for foo in a file named foo/mod.rs.
+
+
These rules apply recursively, so that if a module named foo has a submodule
+named bar and bar does not have submodules, you should have the following
+files in your src directory:
├── foo
│ ├── bar.rs (contains the declarations in `foo::bar`)
│ └── mod.rs (contains the declarations in `foo`, including `mod bar`)
-
-
The modules themselves should be declared in their parent module’s file using
the mod keyword.
-
-
Next, we’ll talk about the pub keyword, and get rid of those warnings!
diff --git a/ch07-02-controlling-visibility-with-pub.html b/ch07-02-controlling-visibility-with-pub.html
index 84a10ba6d..bf1fb7df3 100644
--- a/ch07-02-controlling-visibility-with-pub.html
+++ b/ch07-02-controlling-visibility-with-pub.html
@@ -98,8 +98,8 @@ by us within our own project, so it shouldn’t matter that these connect<
functions go unused. The point of creating them is that they will be used by
another project and not our own.
To understand why this program invokes these warnings, let’s try using the
-connect library as if we were another project, calling it externally. We can
-do that by creating a binary crate in the same directory as our library crate,
+connect library as if we were another project, calling it externally. To do
+that, we’ll create a binary crate in the same directory as our library crate,
by making a src/main.rs file containing this code:
Filename: src/main.rs
extern crate communicator;
@@ -115,6 +115,14 @@ the existing library crate whose root file is src/lib.rs. This pattern
quite common for executable projects: most functionality is in a library crate,
and the binary crate uses that library crate. This way, other programs can also
use the library crate, and it’s a nice separation of concerns.
+
From the point of view of a crate outside of the communicator library looking
+in, all of the modules we've been creating are within a module that has the
+same name as the crate, communicator. We call the top-level module of a crate
+the root module.
+
Also note that even if we're using an external crate within a submodule of our
+project, the extern crate should go in our root module (so in src/main.rs
+or src/lib.rs). Then, in our submodules, we can refer to items from external
+crates as if the items are top-level modules.
Our binary crate right now just calls our library’s connect function from the
client module. However, invoking cargo build will now give us an error
after the warnings:
@@ -137,8 +145,7 @@ the function is unused will go away. Marking something public lets Rust know
that we intend for the function to be used by code outside of our program. Rust
considers the theoretical external usage that’s now possible as the function
“being used.” Thus, when something is marked as public, Rust will not require
-that it’s used in our own program and will stop warning that the item is
-unused.
+that it’s used in our own program and will stop warning that the item is unused.
To tell Rust to make something public, we add the pub keyword to the start of
the declaration of the item we want to make public. We’ll focus on fixing the
@@ -160,7 +167,7 @@ error: function `connect` is private
Hooray! We have a different error! Yes, different error messages are a cause
for celebration. The new error says “function connect is private”, so let’s
-edit src/client.rs to make client::connect public too:
+edit src/client.rs to make client::connect public too:
Filename: src/client.rs
pub fn connect() {
}
@@ -212,7 +219,7 @@ warning: function is never used: `connect`, #[warn(dead_code)] on by default
within the module, but the network module that the function resides in is not
public. We’re working from the interior of the library out this time, where
with client::connect we worked from the outside in. We need to change
-src/lib.rs to make network public too:
+src/lib.rs to make network public too:
Filename: src/lib.rs
pub mod client;
@@ -229,8 +236,7 @@ pub mod network;
If an item is public, it can be accessed through any of its
-parent modules.
+
If an item is public, it can be accessed through any of its parent modules.
If an item is private, it may be accessed only by the current module and its
child modules.
@@ -264,8 +270,8 @@ incorrect
Before you try to compile this code, make a guess about which lines in try_me
-function will have errors. Then try compiling to see if you were right, and read
-on for discussion of the errors!
+function will have errors. Then try compiling to see if you were right, and
+read on for discussion of the errors!
The try_me function is in the root module of our project. The module named
outermost is private, but the second privacy rule says the try_me function
@@ -292,9 +298,8 @@ understand why.
What if the inside module was public?
What if outermost was public and inside was private?
What if, in the body of inner_function, you called
-::outermost::middle_secret_function()? (The two colons at the beginning
-mean that we want to refer to the namespaces starting from the root
-namespace.)
+::outermost::middle_secret_function()? (The two colons at the beginning mean
+that we want to refer to the modules starting from the root module.)
Feel free to design more experiments and try them out!
Next, let’s talk about bringing items into a scope with the use keyword.
We’ve covered how to call functions defined within a module using the module
-name as part of the call, as in the call to the namespaces function shown
+name as part of the call, as in the call to the nested_modules function shown
here in Listing 7-6.
Filename: src/main.rs
pub mod a {
pub mod series {
pub mod of {
- pub fn namespaces() {}
+ pub fn nested_modules() {}
}
}
}
fn main() {
- a::series::of::namespaces();
+ a::series::of::nested_modules();
}
@@ -95,12 +95,12 @@ Luckily, Rust has a keyword to make these calls more concise.
Rust’s use keyword works to shorten lengthy function calls by bringing the
modules of the function you want to call into a scope. Here’s an example of
-bringing the a::series::of namespace into a binary crate’s root scope:
+bringing the a::series::of module into a binary crate’s root scope:
Filename: src/main.rs
pub mod a {
pub mod series {
pub mod of {
- pub fn namespaces() {}
+ pub fn nested_modules() {}
}
}
}
@@ -108,37 +108,37 @@ bringing the a::series::of namespace into a binary crate’s root s
use a::series::of;
fn main() {
- of::namespaces();
+ of::nested_modules();
}
-
The line use a::series::of; has made it so that anywhere in this scope that
-we would want to refer to the of namespace, instead of having to say
-a::series::of, we can replace that with of.
+
The line use a::series::of; means that rather than using the full
+a::series::of path wherever we want to refer to the of module, we can use
+of.
The use keyword brings only what we have specified into scope; it does not
bring children of modules into scope. That’s why we still have to say
-of::namespaces when we want to call the namespaces function.
+of::nested_modules when we want to call the nested_modules function.
We could have chosen to bring the function itself into scope, by instead
specifying the function in the use as follows:
pub mod a {
pub mod series {
pub mod of {
- pub fn namespaces() {}
+ pub fn nested_modules() {}
}
}
}
-use a::series::of::namespaces;
+use a::series::of::nested_modules;
fn main() {
- namespaces();
+ nested_modules();
}
-
This allows us to exclude any of the modules and just reference the function at
-the callsite.
-
Since enums also form this kind of namespace, we can import an enum’s variants
-with use as well. For any kind of use statement, if you’re importing
-multiple items from one namespace, you can list them using curly braces and
-commas in the last position, like so:
+
This allows us to exclude all of the modules and reference the function
+directly.
+
Since enums also form a sort of namespace like modules, we can import an enum’s
+variants with use as well. For any kind of use statement, if you’re
+importing multiple items from one namespace, you can list them using curly
+braces and commas in the last position, like so:
Tests are for exercising the code within our library, so let’s try to call
-our client::connect function from this it_works function, even though
-we’re not going to be checking any functionality right now:
+
Tests are for exercising the code within our library, so let’s try to call our
+client::connect function from this it_works function, even though we’re not
+going to be checking any functionality right now:
Filename: src/lib.rs
#[cfg(test)]
mod tests {
@@ -228,7 +228,7 @@ warning: function is never used: `connect`, #[warn(dead_code)] on by default
| ^
The compilation failed, but why? We don’t need to place communicator:: in
-front of the function like we did in src/main.rs because we are definitely
+front of the function like we did in src/main.rs because we are definitely
within the communicator library crate here. The reason is that paths are
always relative to the current module, which here is tests. The only
exception is in a use statement, where paths are relative to the crate root
diff --git a/print.html b/print.html
index cfa7587e5..652c7c803 100644
--- a/print.html
+++ b/print.html
@@ -4659,8 +4659,8 @@ Rust’s modules.
into other functions, both for re-use and for better organization. By splitting
your code up into smaller chunks, each chunk is easier to understand on its
own. But what happens if you find yourself with too many functions? Rust has a
-module system that handles the problem of wanting to re-use code while
-keeping your code organized.
+module system that handles the problem of wanting to re-use code while keeping
+your code organized.
In the same way that you extract lines of code into a function, you can extract
functions (and other code like structs and enums too) into different modules. A
module is a namespace that contains definitions of functions or types, and
@@ -4668,8 +4668,9 @@ you can choose whether those definitions are visible outside their module
(public) or not (private). Here’s an overview of how modules work:
You declare a new module with the keyword mod
-
By default, everything is set as private, but you can use the pub keyword
-to make the module public, and therefore visible outside of the namespace.
+
By default, everything is set as private (including modules). You can use the
+pub keyword to make a module public and therefore visible outside of its
+namespace.
The use keyword allows you to bring modules, or the definitions inside
modules, into scope so that it’s easier to refer to them.
@@ -4698,10 +4699,10 @@ mod tests {
}
}
-
Cargo creates an empty test to help us get our library started, rather
-than the “Hello, world!” binary that we get with the --bin option. We’ll look
-at the #[] and mod tests syntax a little later, but for now just make sure
-to leave it in your src/lib.rs.
+
Cargo creates an empty test to help us get our library started, rather than the
+“Hello, world!” binary that we get with the --bin option. We’ll look at the
+#[] and mod tests syntax a little later, but for now just make sure to
+leave it in your src/lib.rs.
Since we don’t have a src/main.rs, there’s nothing for Cargo to execute with
the cargo run command. Therefore, we will be using the cargo build command
to only compile our library crate’s code.
@@ -4712,7 +4713,7 @@ you have for your code.
For our communicator networking library, we’re first going to define a module
named network that contains the definition of a function called connect.
Every module definition in Rust starts with the mod keyword. Add this code to
-the beginning of the lib.rs file, above the test code:
+the beginning of the src/lib.rs file, above the test code:
Filename: src/lib.rs
mod network {
fn connect() {
@@ -4727,7 +4728,7 @@ specify the module and use the namespace syntax ::, like so:
network::connect(), rather than just connect().
We can also have multiple modules, side-by-side, in the same src/lib.rs file.
For example, to have a client module too, that also has a function named
-connect, we can add:
+connect, we can add it as shown in Listing 7-1:
Filename: src/lib.rs
mod network {
@@ -4749,13 +4750,14 @@ in src/lib.rs
These can have completely different functionality, and the function names do
not conflict with each other since they’re in different modules.
While in this case, we’re building a library, there's nothing special about
-lib.rs. We could also make use of submodules in a main.rs as well. In fact,
-we can also put modules inside of modules. This can be useful as your modules
-grow to keep related functionality organized together and separate
+src/lib.rs. We could also make use of submodules in src/main.rs as well. In
+fact, we can also put modules inside of modules. This can be useful as your
+modules grow to keep related functionality organized together and separate
functionality apart. The choice of how you organize your code depends on how
you think about the relationship between the parts of your code. For instance,
the client code and its connect function might make more sense to users of
-our library if it was inside the network namespace instead, like so:
+our library if it was inside the network namespace instead, like in Listing
+7-2:
Filename: src/lib.rs
mod network {
@@ -4801,7 +4803,7 @@ structure you would like.
Modules form a hierarchical structure, much like another structure in computing
that you’re used to: file systems! We can use Rust’s module system along with
multiple files to split Rust projects up so that not everything lives in
-src/lib.rs. For this example, we will start with this code in src/lib.rs:
+src/lib.rs. For this example, we will start with the code in Listing 7-3:
Filename: src/lib.rs
mod client {
@@ -4820,7 +4822,7 @@ mod network {
}
-
Listing 7-3: Three modules, client, network, and network::server all
+
Listing 7-3: Three modules, client, network, and network::server, all
defined in src/lib.rs
@@ -4830,12 +4832,14 @@ defined in src/lib.rs
└── network
└── server
-
If these modules had many functions, and each function was getting long, we
-would have to scroll through this file to find the code we wanted to work with.
-This would be a good reason to pull each of the client, network, and
-server modules out of src/lib.rs and into their own files. Let’s start by
-extracting the client module into another file. First, replace the client
-module code in src/lib.rs with the following:
+
If these modules had many functions, and those functions were getting long, it
+would be difficult to scroll through this file to find the code we wanted to
+work with. Because the functions are nested inside one or more mod blocks, the
+lines of code inside the functions will start getting long as well. These would
+be good reasons to pull each of the client, network, and server modules
+out of src/lib.rs and into their own files.
+
Let’s start by extracting the client module into another file. First, replace
+the client module code in src/lib.rs with the following:
Filename: src/lib.rs
mod client;
@@ -4849,7 +4853,6 @@ mod network {
}
}
-
We’re still defining the client module here, but by removing the curly
braces and definitions inside the client module and replacing them with a
semicolon, we’re letting Rust know to look in another location for the code
@@ -4915,8 +4918,8 @@ mod server {
}
}
-
Notice that we still have a mod declaration within this module file;
-this is because we still want server to be a sub-module of network.
+
Notice that we still have a mod declaration within this module file; this is
+because we still want server to be a sub-module of network.
Now run cargo build again. Success! We have one more module to extract:
server. Because it’s a sub-module—that is, a module within a module—our
current tactic of extracting a module into a file named after that module won’t
@@ -4935,7 +4938,7 @@ module that we extracted:
fn connect() {
}
-
When we try to cargo build, we’ll get this error:
+
When we try to cargo build, we’ll get the error shown in Listing 7-4:
$ cargo build
Compiling communicator v0.1.0 (file:///projects/communicator)
@@ -4966,10 +4969,8 @@ pointing to the mod server; line in src/network.rs. So src/lib.rs somehow; let’s keep reading to understand why.
The note in the middle of Listing 7-4 is actually pretty helpful, as it points
out something we haven’t yet talked about doing:
-
-
note: maybe move this module network to its own directory via
-network/mod.rs
-
+
note: maybe move this module `network` to its own directory via `network/mod.rs`
+
Instead of continuing to follow the same file naming pattern we used
previously, we can do what the note suggests:
@@ -5001,13 +5002,13 @@ same as it did when we had all the code in src/lib.rs in Listing 7-3:
So when we wanted to extract the network::server module, why did we have to
also change the src/network.rs file into the src/network/mod.rs file, and
-also put the code for network::server in the network directory in
+put the code for network::server in the network directory in
src/network/server.rs, instead of just being able to extract the
-network::server into src/server.rs? The reason is that Rust wouldn’t be
-able to tell that server was supposed to be a submodule of network if the
-server.rs file was in the src directory. To make it clearer why Rust can’t
-tell, let’s consider a different example where we have this module hierarchy
-with all the definitions in src/lib.rs:
+network::server module into src/server.rs? The reason is that Rust wouldn’t
+be able to tell that server was supposed to be a submodule of network if
+the server.rs file was in the src directory. To make it clearer why Rust
+can’t tell, let’s consider a different example with the following module
+hierarchy, where all the definitions are in src/lib.rs:
communicator
├── client
└── network
@@ -5032,28 +5033,20 @@ top-level src/client.rs is unambiguously the code that belongs to the
Rules of Module File Systems
In summary, these are the rules of modules with regards to files:
-
-
If a module named foo has no submodules, you should put the declarations
-for foo in a file named foo.rs.
-
-
-
If a module named foo does have submodules, you should put the declarations
-for foo in a file named foo/mod.rs.
-
-
-
The first two rules apply recursively, so that if a module named foo has a
-submodule named bar and bar does not have submodules, you should have the
-following files in your src directory:
+
If a module named foo has no submodules, you should put the declarations
+for foo in a file named foo.rs.
+
If a module named foo does have submodules, you should put the declarations
+for foo in a file named foo/mod.rs.
+
+
These rules apply recursively, so that if a module named foo has a submodule
+named bar and bar does not have submodules, you should have the following
+files in your src directory:
├── foo
│ ├── bar.rs (contains the declarations in `foo::bar`)
│ └── mod.rs (contains the declarations in `foo`, including `mod bar`)
-
-
The modules themselves should be declared in their parent module’s file using
the mod keyword.
-
-
Next, we’ll talk about the pub keyword, and get rid of those warnings!
Controlling Visibility with pub
We resolved the error messages shown in Listing 7-4 by moving the network and
@@ -5086,8 +5079,8 @@ by us within our own project, so it shouldn’t matter that these connect<
functions go unused. The point of creating them is that they will be used by
another project and not our own.
To understand why this program invokes these warnings, let’s try using the
-connect library as if we were another project, calling it externally. We can
-do that by creating a binary crate in the same directory as our library crate,
+connect library as if we were another project, calling it externally. To do
+that, we’ll create a binary crate in the same directory as our library crate,
by making a src/main.rs file containing this code:
Filename: src/main.rs
extern crate communicator;
@@ -5103,6 +5096,14 @@ the existing library crate whose root file is src/lib.rs. This pattern
quite common for executable projects: most functionality is in a library crate,
and the binary crate uses that library crate. This way, other programs can also
use the library crate, and it’s a nice separation of concerns.
+
From the point of view of a crate outside of the communicator library looking
+in, all of the modules we've been creating are within a module that has the
+same name as the crate, communicator. We call the top-level module of a crate
+the root module.
+
Also note that even if we're using an external crate within a submodule of our
+project, the extern crate should go in our root module (so in src/main.rs
+or src/lib.rs). Then, in our submodules, we can refer to items from external
+crates as if the items are top-level modules.
Our binary crate right now just calls our library’s connect function from the
client module. However, invoking cargo build will now give us an error
after the warnings:
@@ -5125,8 +5126,7 @@ the function is unused will go away. Marking something public lets Rust know
that we intend for the function to be used by code outside of our program. Rust
considers the theoretical external usage that’s now possible as the function
“being used.” Thus, when something is marked as public, Rust will not require
-that it’s used in our own program and will stop warning that the item is
-unused.
+that it’s used in our own program and will stop warning that the item is unused.
Making a Function Public
To tell Rust to make something public, we add the pub keyword to the start of
the declaration of the item we want to make public. We’ll focus on fixing the
@@ -5148,7 +5148,7 @@ error: function `connect` is private
Hooray! We have a different error! Yes, different error messages are a cause
for celebration. The new error says “function connect is private”, so let’s
-edit src/client.rs to make client::connect public too:
+edit src/client.rs to make client::connect public too:
Filename: src/client.rs
pub fn connect() {
}
@@ -5200,7 +5200,7 @@ warning: function is never used: `connect`, #[warn(dead_code)] on by default
within the module, but the network module that the function resides in is not
public. We’re working from the interior of the library out this time, where
with client::connect we worked from the outside in. We need to change
-src/lib.rs to make network public too:
+src/lib.rs to make network public too:
Filename: src/lib.rs
pub mod client;
@@ -5217,8 +5217,7 @@ pub mod network;
Privacy Rules
Overall, these are the rules for item visibility:
-
If an item is public, it can be accessed through any of its
-parent modules.
+
If an item is public, it can be accessed through any of its parent modules.
If an item is private, it may be accessed only by the current module and its
child modules.
@@ -5252,8 +5251,8 @@ incorrect
Before you try to compile this code, make a guess about which lines in try_me
-function will have errors. Then try compiling to see if you were right, and read
-on for discussion of the errors!
+function will have errors. Then try compiling to see if you were right, and
+read on for discussion of the errors!
Looking at the Errors
The try_me function is in the root module of our project. The module named
outermost is private, but the second privacy rule says the try_me function
@@ -5280,28 +5279,27 @@ understand why.
What if the inside module was public?
What if outermost was public and inside was private?
What if, in the body of inner_function, you called
-::outermost::middle_secret_function()? (The two colons at the beginning
-mean that we want to refer to the namespaces starting from the root
-namespace.)
+::outermost::middle_secret_function()? (The two colons at the beginning mean
+that we want to refer to the modules starting from the root module.)
Feel free to design more experiments and try them out!
Next, let’s talk about bringing items into a scope with the use keyword.
Importing Names
We’ve covered how to call functions defined within a module using the module
-name as part of the call, as in the call to the namespaces function shown
+name as part of the call, as in the call to the nested_modules function shown
here in Listing 7-6.
Filename: src/main.rs
pub mod a {
pub mod series {
pub mod of {
- pub fn namespaces() {}
+ pub fn nested_modules() {}
}
}
}
fn main() {
- a::series::of::namespaces();
+ a::series::of::nested_modules();
}
@@ -5314,12 +5312,12 @@ Luckily, Rust has a keyword to make these calls more concise.
Concise Imports with use
Rust’s use keyword works to shorten lengthy function calls by bringing the
modules of the function you want to call into a scope. Here’s an example of
-bringing the a::series::of namespace into a binary crate’s root scope:
+bringing the a::series::of module into a binary crate’s root scope:
Filename: src/main.rs
pub mod a {
pub mod series {
pub mod of {
- pub fn namespaces() {}
+ pub fn nested_modules() {}
}
}
}
@@ -5327,37 +5325,37 @@ bringing the a::series::of namespace into a binary crate’s root s
use a::series::of;
fn main() {
- of::namespaces();
+ of::nested_modules();
}
-
The line use a::series::of; has made it so that anywhere in this scope that
-we would want to refer to the of namespace, instead of having to say
-a::series::of, we can replace that with of.
+
The line use a::series::of; means that rather than using the full
+a::series::of path wherever we want to refer to the of module, we can use
+of.
The use keyword brings only what we have specified into scope; it does not
bring children of modules into scope. That’s why we still have to say
-of::namespaces when we want to call the namespaces function.
+of::nested_modules when we want to call the nested_modules function.
We could have chosen to bring the function itself into scope, by instead
specifying the function in the use as follows:
pub mod a {
pub mod series {
pub mod of {
- pub fn namespaces() {}
+ pub fn nested_modules() {}
}
}
}
-use a::series::of::namespaces;
+use a::series::of::nested_modules;
fn main() {
- namespaces();
+ nested_modules();
}
-
This allows us to exclude any of the modules and just reference the function at
-the callsite.
-
Since enums also form this kind of namespace, we can import an enum’s variants
-with use as well. For any kind of use statement, if you’re importing
-multiple items from one namespace, you can list them using curly braces and
-commas in the last position, like so:
+
This allows us to exclude all of the modules and reference the function
+directly.
+
Since enums also form a sort of namespace like modules, we can import an enum’s
+variants with use as well. For any kind of use statement, if you’re
+importing multiple items from one namespace, you can list them using curly
+braces and commas in the last position, like so:
Tests are for exercising the code within our library, so let’s try to call
-our client::connect function from this it_works function, even though
-we’re not going to be checking any functionality right now:
+
Tests are for exercising the code within our library, so let’s try to call our
+client::connect function from this it_works function, even though we’re not
+going to be checking any functionality right now:
Filename: src/lib.rs
#[cfg(test)]
mod tests {
@@ -5447,7 +5445,7 @@ warning: function is never used: `connect`, #[warn(dead_code)] on by default
| ^
The compilation failed, but why? We don’t need to place communicator:: in
-front of the function like we did in src/main.rs because we are definitely
+front of the function like we did in src/main.rs because we are definitely
within the communicator library crate here. The reason is that paths are
always relative to the current module, which here is tests. The only
exception is in a use statement, where paths are relative to the crate root