diff --git a/ch07-01-mod-and-the-filesystem.html b/ch07-01-mod-and-the-filesystem.html
index a7487b584..5f9f1f0e5 100644
--- a/ch07-01-mod-and-the-filesystem.html
+++ b/ch07-01-mod-and-the-filesystem.html
@@ -67,20 +67,21 @@
-
mod and the filesystem
+
mod and the Filesystem
Every module in Rust starts with the mod keyword. In this next example, we'll
start again by making a new project with Cargo. This time, instead of a binary,
we're going to make a library: a project that other people would pull into their
-projects as a dependency. We saw this with the rand crate in Chapter 2.
-
Imagine that we're creating a library to provide some general
-networking functionality, and we decide to call our library communicator. To
-create this library, we won't use the --bin option like we have before. This
-is because by default cargo will create a library:
+projects as a dependency. We saw this with the
rand crate in Chapter 2.
+
Imagine that we're creating a library to provide some general networking
+functionality, and we decide to call our library communicator. To create this
+library, we won't use the --bin option like we have before. This is because
+by default cargo will create a library:
$ cargo new communicator
$ cd communicator
-
You'll notice that Cargo generated src/lib.rs instead of src/main.rs for
-us, and inside it we'll find this:
+
Notice that Cargo generated src/lib.rs instead of src/main.rs for us, and
+inside it we'll find this:
+
Filename: src/lib.rs
#[cfg(test)]
mod tests {
#[test]
@@ -101,14 +102,14 @@ do. To start, add this code at the beginning of the file:
}
}
-
This is our first module declaration. As you can see, you use the mod
-keyword, followed by the name of the module, and then a block of code in curly
-braces. Everything inside this block is inside the namespace network. In this
-case, we have a single function, connect. If we wanted to try and call this
-function from outside the network module, we would say network::connect()
-rather than connect().
-
You could have multiple modules, side-by-side. For example, if you wanted a
+
This is our first module declaration. We use the mod keyword, followed by the
+name of the module, and then a block of code in curly braces. Everything inside
+this block is inside the namespace network. In this case, we have a single
+function, connect. If we wanted to try and call this function from outside
+the network module, we would say network::connect() rather than connect().
+
We could have multiple modules, side-by-side. For example, if we wanted a
client module:
+
Filename: src/lib.rs
mod network {
fn connect() {
}
@@ -120,8 +121,9 @@ mod client {
}
Now we have a network::connect function and a client::connect function.
-
And you can put modules inside of modules. If you wanted to have client be
+
And we can put modules inside of modules. If we wanted to have client be
within network:
+
Filename: src/lib.rs
mod network {
fn connect() {
}
@@ -146,7 +148,7 @@ first example looks like when thought of this way:
└── client
More complicated projects can have a lot of modules.
-
Putting modules in another file
+
Putting Modules in Another File
Modules form a hierarchical, tree-like structure. So does another thing:
file systems! The module system is the way that we split larger Rust projects up
into multiple files. Let's imagine we have a module layout like this:
@@ -182,8 +184,8 @@ mod network {
}
We still say mod client, but instead of curly braces, we have a semicolon.
-This lets Rust know that we have a module, but it's in another file. Which file
-is it in? Open up src/client.rs and put this in it:
+This lets Rust know that we have a module, but it's in another file with that
+module's name. Open up
src/client.rs and put this in it:
File: src/client.rs
fn connect() {
}
@@ -192,7 +194,7 @@ is it in? Open up src/client.rs and put this in it:
declaring a new module, and we've already declared this module in src/lib.rs.
This file provides the contents of the client module. If we put a mod client here, we'd be giving the client module its own submodule named
client!
-Now, everything should compile:
+Now, everything should compile successfully, but with a few warnings:
$ cargo build
Compiling communicator v0.1.0 (file:///projects/communicator)
@@ -216,7 +218,8 @@ warning: function is never used: `connect`, #[warn(dead_code)] on by default
Don't worry about those warnings for now; we'll clear them up in a future
section. They're just warnings, we've built things successfully!
-Let's convert the network module next. Change src/lib.rs to look like this:
+Let's extract the network module into its own file next, using the same
+pattern. Change src/lib.rs to look like this:
Filename: src/lib.rs
mod client;
@@ -233,8 +236,9 @@ mod server {
}
And then run cargo build again. Success! We have one more module to extract:
-server. Unfortunately, our current tactic won't work. Let's try it anyway.
-Modify src/network.rs to look like this:
+server. Unfortunately, our current tactic of extracting a module into a file
+named after that module won't work. Let's try it anyway. Modify
+src/network.rs to look like this:
Filename: src/network.rs
fn connect() {
}
@@ -274,13 +278,14 @@ that we could do yet:
Here's the problem: in our case, we have different names for our modules:
client and network::server. But what if we had client and
-network::client, or server and network::server? That's completely valid,
-but then which module would the files src/client.rs and src/server.rs,
-respectively, be for?
+network::client, or server and network::server? Having two modules at
+different places in the module hierarchy have the same name is completely
+valid, but then which module would the files src/client.rs and
+src/server.rs, respectively, be for?
Instead of continuing to follow the same file naming pattern we used
-previously, we can do what the error suggests. We'll make a new directory, move
-src/server.rs into it, and change src/network.rs to src/network/mod.rs.
-Then, we try to build:
+previously, we can do what the error suggests. We'll make a new directory,
+move src/server.rs into it, and change src/network.rs to
+src/network/mod.rs. Then, when we try to build:
$ mkdir src/network
$ mv src/server.rs src/network
$ mv src/network.rs src/network/mod.rs
diff --git a/ch07-02-controlling-visibility-with-pub.html b/ch07-02-controlling-visibility-with-pub.html
index a32c0ec6c..687e0816d 100644
--- a/ch07-02-controlling-visibility-with-pub.html
+++ b/ch07-02-controlling-visibility-with-pub.html
@@ -67,7 +67,7 @@
-
Controlling visibility with pub
+
Controlling Visibility with pub
At the end of the last section, we had a project, communicator, and when we compiled it, we got some strange warnings:
Compiling communicator v0.1.0 (file:///projects/communicator)
warning: function is never used: `connect`, #[warn(dead_code)] on by default
@@ -90,7 +90,7 @@ warning: function is never used: `connect`, #[warn(dead_code)] on by default
Why does this happen? After all, we're building a library. What if these three
functions are the public interface that we want our users to use? We won't
-necessarily be using them within our own crate, but the point of creating them
+necessarily be using them within our own project, but the point of creating them
is that they will be used by another project. Let's try using them as if we
were another project using our library to see what happens and understand why
we're getting these unused function warnings. Create a src/main.rs file with
@@ -107,8 +107,8 @@ scope, because our package actually now contains two crates. Cargo trea
src/main.rs as the crate root of a binary crate, and we also have our existing
library crate. This pattern is quite common for executable crates: most
functionality is in a library crate, and the executable crate uses that
-library. This way, other programs can also use the library crate, and it’s also
-a nice separation of concerns.
+library. This way, other programs can also use the library crate, and it’s a
+nice separation of concerns.
Our binary crate right now just calls our library's connect function from
the client module; we picked that one since it's the first warning in our
build output above. Invoking cargo build will now give us an error after the
@@ -221,7 +221,7 @@ warning: function is never used: `connect`, #[warn(dead_code)] on by default
| ^
Only one last warning! Try to fix this one on your own!
-
Privacy rules
+
Privacy Rules
Overall, these are the rules for item visibility:
- If an item is public, then it can be accessed through any of its
@@ -231,6 +231,7 @@ child modules.
Let's look at a few more examples to get some practice. What if we had this
code in a new project's src/lib.rs:
+
Filename: src/lib.rs
mod outermost {
pub fn middle_function() {}
@@ -268,7 +269,7 @@ root module is neither the current module of middle_secret_function
be accessed by its current module, outermost. That means the try_me
function is not allowed to call outermost::inside::inner_function() or
outermost::inside::secret_function().
-Here are some changes to make to this code. Try each one, make a guess
+
Here are some changes to try making with this code. Try each one, make a guess
about what will be allowed or not, compile to see if you're right, and use the
rules to understand why.
diff --git a/ch07-03-importing-names-with-use.html b/ch07-03-importing-names-with-use.html
index 2a3040f48..49c65598a 100644
--- a/ch07-03-importing-names-with-use.html
+++ b/ch07-03-importing-names-with-use.html
@@ -67,9 +67,10 @@
-
Importing names with use
+
Importing Names with use
We've seen how we can call functions defined within a module by using the
module name as part of the call, like this:
+
Filename: src/main.rs
pub mod a {
pub mod series {
pub mod of {
@@ -85,6 +86,7 @@ fn main() {
However, referring to the fully qualified name can get quite lengthy, as we see
in that example. To solve this issue, Rust has a keyword, use. It works like
this:
+Filename: src/main.rs
pub mod a {
pub mod series {
pub mod of {
@@ -135,7 +137,7 @@ fn main() {
let green = TrafficLight::Green; // because we didn't use TrafficLight::Green
}
-Glob imports with *
+Glob Imports with *
If you'd like to import all the items in a namespace at once, you can use *:
enum TrafficLight {
Red,
@@ -154,7 +156,7 @@ fn main() {
The * is called a 'glob', and it will import everything that's visible inside
of the namespace. Globs should be used sparingly: they are convenient, but you
might also pull in more things than you expected and cause naming conflicts.
-Using super to access a parent module
+Using super to Access a Parent Module
Remember when we created our crate that Cargo made a tests module for us?
Let's talk about that now. It was in src/lib.rs:
Filename: src/lib.rs
@@ -176,6 +178,7 @@ annotations, the tests module is just another module!
Since tests are for exercising the code within our library, 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 {
#[test]
@@ -199,10 +202,10 @@ warning: function is never used: `connect`, #[warn(dead_code)] on by default
1 | fn connect() {
| ^
-Why doesn't this work? It's not because we don't have communicator:: in front
-of the function like we had in src/main.rs: we are definitely within the
-communicator library crate here. The reason is that paths anywhere except in
-a use statement are relative to the current module (In a use statement,
+
Why doesn't this compile? It's not because we don't have communicator:: in
+front of the function like we had in src/main.rs: we are definitely within
+the communicator library crate here. The reason is that paths anywhere except
+in a use statement are relative to the current module (In a use statement,
they're relative to the crate root by default). Our tests module doesn't have
a client module in its scope!
So how do we get back up one module? We can either use leading colons to say
@@ -222,6 +225,7 @@ changes the path we give to use so that it is relative to the paren
instead of to the root module.
For these reasons, in the tests module especially, use super::something is
usually the way to go. So now our test looks like this:
+Filename: src/lib.rs
#[cfg(test)]
mod tests {
use super::client;
diff --git a/print.html b/print.html
index c46d19b11..c4aec2f4c 100644
--- a/print.html
+++ b/print.html
@@ -4098,20 +4098,21 @@ overview of how the bits fit together:
into scope so that it's easier to refer to them.
We'll take a look at each of these parts and see how they fit into the whole.
-mod and the filesystem
+mod and the Filesystem
Every module in Rust starts with the mod keyword. In this next example, we'll
start again by making a new project with Cargo. This time, instead of a binary,
we're going to make a library: a project that other people would pull into their
-projects as a dependency. We saw this with the rand crate in Chapter 2.
-Imagine that we're creating a library to provide some general
-networking functionality, and we decide to call our library communicator. To
-create this library, we won't use the --bin option like we have before. This
-is because by default cargo will create a library:
+projects as a dependency. We saw this with the rand crate in Chapter 2.
+Imagine that we're creating a library to provide some general networking
+functionality, and we decide to call our library communicator. To create this
+library, we won't use the --bin option like we have before. This is because
+by default cargo will create a library:
$ cargo new communicator
$ cd communicator
-You'll notice that Cargo generated src/lib.rs instead of src/main.rs for
-us, and inside it we'll find this:
+Notice that Cargo generated src/lib.rs instead of src/main.rs for us, and
+inside it we'll find this:
+Filename: src/lib.rs
#[cfg(test)]
mod tests {
#[test]
@@ -4132,14 +4133,14 @@ do. To start, add this code at the beginning of the file:
}
}
-This is our first module declaration. As you can see, you use the mod
-keyword, followed by the name of the module, and then a block of code in curly
-braces. Everything inside this block is inside the namespace network. In this
-case, we have a single function, connect. If we wanted to try and call this
-function from outside the network module, we would say network::connect()
-rather than connect().
-You could have multiple modules, side-by-side. For example, if you wanted a
+
This is our first module declaration. We use the mod keyword, followed by the
+name of the module, and then a block of code in curly braces. Everything inside
+this block is inside the namespace network. In this case, we have a single
+function, connect. If we wanted to try and call this function from outside
+the network module, we would say network::connect() rather than connect().
+We could have multiple modules, side-by-side. For example, if we wanted a
client module:
+Filename: src/lib.rs
mod network {
fn connect() {
}
@@ -4151,8 +4152,9 @@ mod client {
}
Now we have a network::connect function and a client::connect function.
-And you can put modules inside of modules. If you wanted to have client be
+
And we can put modules inside of modules. If we wanted to have client be
within network:
+Filename: src/lib.rs
mod network {
fn connect() {
}
@@ -4177,7 +4179,7 @@ first example looks like when thought of this way:
└── client
More complicated projects can have a lot of modules.
-Putting modules in another file
+Putting Modules in Another File
Modules form a hierarchical, tree-like structure. So does another thing:
file systems! The module system is the way that we split larger Rust projects up
into multiple files. Let's imagine we have a module layout like this:
@@ -4213,8 +4215,8 @@ mod network {
}
We still say mod client, but instead of curly braces, we have a semicolon.
-This lets Rust know that we have a module, but it's in another file. Which file
-is it in? Open up src/client.rs and put this in it:
+This lets Rust know that we have a module, but it's in another file with that
+module's name. Open up src/client.rs and put this in it:
File: src/client.rs
fn connect() {
}
@@ -4223,7 +4225,7 @@ is it in? Open up src/client.rs and put this in it:
declaring a new module, and we've already declared this module in src/lib.rs.
This file provides the contents of the client module. If we put a mod client here, we'd be giving the client module its own submodule named
client!
-Now, everything should compile:
+Now, everything should compile successfully, but with a few warnings:
$ cargo build
Compiling communicator v0.1.0 (file:///projects/communicator)
@@ -4247,7 +4249,8 @@ warning: function is never used: `connect`, #[warn(dead_code)] on by default
Don't worry about those warnings for now; we'll clear them up in a future
section. They're just warnings, we've built things successfully!
-Let's convert the network module next. Change src/lib.rs to look like this:
+Let's extract the network module into its own file next, using the same
+pattern. Change src/lib.rs to look like this:
Filename: src/lib.rs
mod client;
@@ -4264,8 +4267,9 @@ mod server {
}
And then run cargo build again. Success! We have one more module to extract:
-server. Unfortunately, our current tactic won't work. Let's try it anyway.
-Modify src/network.rs to look like this:
+server. Unfortunately, our current tactic of extracting a module into a file
+named after that module won't work. Let's try it anyway. Modify
+src/network.rs to look like this:
Filename: src/network.rs
fn connect() {
}
@@ -4305,13 +4309,14 @@ that we could do yet:
Here's the problem: in our case, we have different names for our modules:
client and network::server. But what if we had client and
-network::client, or server and network::server? That's completely valid,
-but then which module would the files src/client.rs and src/server.rs,
-respectively, be for?
+network::client, or server and network::server? Having two modules at
+different places in the module hierarchy have the same name is completely
+valid, but then which module would the files src/client.rs and
+src/server.rs, respectively, be for?
Instead of continuing to follow the same file naming pattern we used
-previously, we can do what the error suggests. We'll make a new directory, move
-src/server.rs into it, and change src/network.rs to src/network/mod.rs.
-Then, we try to build:
+previously, we can do what the error suggests. We'll make a new directory,
+move src/server.rs into it, and change src/network.rs to
+src/network/mod.rs. Then, when we try to build:
$ mkdir src/network
$ mv src/server.rs src/network
$ mv src/network.rs src/network/mod.rs
@@ -4359,7 +4364,7 @@ the mod keyword.
Next, we'll talk about the pub keyword, and get rid of those warnings!
-Controlling visibility with pub
+Controlling Visibility with pub
At the end of the last section, we had a project, communicator, and when we compiled it, we got some strange warnings:
Compiling communicator v0.1.0 (file:///projects/communicator)
warning: function is never used: `connect`, #[warn(dead_code)] on by default
@@ -4382,7 +4387,7 @@ warning: function is never used: `connect`, #[warn(dead_code)] on by default
Why does this happen? After all, we're building a library. What if these three
functions are the public interface that we want our users to use? We won't
-necessarily be using them within our own crate, but the point of creating them
+necessarily be using them within our own project, but the point of creating them
is that they will be used by another project. Let's try using them as if we
were another project using our library to see what happens and understand why
we're getting these unused function warnings. Create a src/main.rs file with
@@ -4399,8 +4404,8 @@ scope, because our package actually now contains two crates. Cargo trea
src/main.rs as the crate root of a binary crate, and we also have our existing
library crate. This pattern is quite common for executable crates: most
functionality is in a library crate, and the executable crate uses that
-library. This way, other programs can also use the library crate, and it’s also
-a nice separation of concerns.
+library. This way, other programs can also use the library crate, and it’s a
+nice separation of concerns.
Our binary crate right now just calls our library's connect function from
the client module; we picked that one since it's the first warning in our
build output above. Invoking cargo build will now give us an error after the
@@ -4513,7 +4518,7 @@ warning: function is never used: `connect`, #[warn(dead_code)] on by default
| ^
Only one last warning! Try to fix this one on your own!
-Privacy rules
+Privacy Rules
Overall, these are the rules for item visibility:
- If an item is public, then it can be accessed through any of its
@@ -4523,6 +4528,7 @@ child modules.
Let's look at a few more examples to get some practice. What if we had this
code in a new project's src/lib.rs:
+Filename: src/lib.rs
mod outermost {
pub fn middle_function() {}
@@ -4560,7 +4566,7 @@ root module is neither the current module of middle_secret_function
be accessed by its current module, outermost. That means the try_me
function is not allowed to call outermost::inside::inner_function() or
outermost::inside::secret_function().
-Here are some changes to make to this code. Try each one, make a guess
+
Here are some changes to try making with this code. Try each one, make a guess
about what will be allowed or not, compile to see if you're right, and use the
rules to understand why.
@@ -4573,9 +4579,10 @@ namespace.)
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 with use
+Importing Names with use
We've seen how we can call functions defined within a module by using the
module name as part of the call, like this:
+Filename: src/main.rs
pub mod a {
pub mod series {
pub mod of {
@@ -4591,6 +4598,7 @@ fn main() {
However, referring to the fully qualified name can get quite lengthy, as we see
in that example. To solve this issue, Rust has a keyword, use. It works like
this:
+Filename: src/main.rs
pub mod a {
pub mod series {
pub mod of {
@@ -4641,7 +4649,7 @@ fn main() {
let green = TrafficLight::Green; // because we didn't use TrafficLight::Green
}
-Glob imports with *
+Glob Imports with *
If you'd like to import all the items in a namespace at once, you can use *:
enum TrafficLight {
Red,
@@ -4660,7 +4668,7 @@ fn main() {
The * is called a 'glob', and it will import everything that's visible inside
of the namespace. Globs should be used sparingly: they are convenient, but you
might also pull in more things than you expected and cause naming conflicts.
-Using super to access a parent module
+Using super to Access a Parent Module
Remember when we created our crate that Cargo made a tests module for us?
Let's talk about that now. It was in src/lib.rs:
Filename: src/lib.rs
@@ -4682,6 +4690,7 @@ annotations, the tests module is just another module!
Since tests are for exercising the code within our library, 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 {
#[test]
@@ -4705,10 +4714,10 @@ warning: function is never used: `connect`, #[warn(dead_code)] on by default
1 | fn connect() {
| ^
-Why doesn't this work? It's not because we don't have communicator:: in front
-of the function like we had in src/main.rs: we are definitely within the
-communicator library crate here. The reason is that paths anywhere except in
-a use statement are relative to the current module (In a use statement,
+
Why doesn't this compile? It's not because we don't have communicator:: in
+front of the function like we had in src/main.rs: we are definitely within
+the communicator library crate here. The reason is that paths anywhere except
+in a use statement are relative to the current module (In a use statement,
they're relative to the crate root by default). Our tests module doesn't have
a client module in its scope!
So how do we get back up one module? We can either use leading colons to say
@@ -4728,6 +4737,7 @@ changes the path we give to use so that it is relative to the paren
instead of to the root module.
For these reasons, in the tests module especially, use super::something is
usually the way to go. So now our test looks like this:
+Filename: src/lib.rs
#[cfg(test)]
mod tests {
use super::client;