Modules
-When we write a program in Rust, your code starts off living in main. But as
-your code grows, you eventually move functionality out into functions, both for
-re-use, and for nicer organization. By splitting your code up into smaller
-chunks, each chunk is easier to understand on its own. So what happens when we
-start having too many functions? Rust has a module system that tackles both of
-these problems. In the same way that we extract some lines of code into a
-function, we can extract some functions (and other things too) into different
-modules. A module is a namespace that contains definitions of functions or
-types, and those definitions can be visible outside their module or not. Here's
-an overview of how the bits fit together:
When you write a program in Rust, your code might start off living solely in
+the main function. As your code grows, you eventually move functionality out
+into functions, both for re-use and for nicer organization. By splitting your
+code up into smaller chunks, each chunk is easier to understand on its own. So
+what happens when you start having too many functions? Rust has a module system
+that tackles both the problem of wanting to be able to re-use code and the
+problem of 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 +those definitions can be visible outside their module or not. Here's an +overview of how the bits fit together:
moddeclares a new module.- Everything starts off as private, but the
pubkeyword makes it public.
diff --git a/ch07-01-mod-and-the-filesystem.html b/ch07-01-mod-and-the-filesystem.html
index e00bfb76e..a4a52a7e9 100644
--- a/ch07-01-mod-and-the-filesystem.html
+++ b/ch07-01-mod-and-the-filesystem.html
@@ -69,13 +69,14 @@
moddeclares a new module.- Everything starts off as private, but the
pubkeyword makes it public.
@@ -4045,13 +4047,14 @@ into scope so that it's easier to refer to them.
mod and the filesystem
Every module in Rust starts with the mod keyword. Let's give it a try by
-making a new project with Cargo called 'modules'. 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 to get the functionality we provided, like
-we used the rand crate in Chapter 2. So we're not going to use the --bin
-option like we have before, instead run:
$ cargo new modules
-$ cd modules
+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 to get the functionality we provided, like we used 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. So
+we're not going to use the --bin option like we have before, instead run:
+$ 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:
@@ -87,13 +88,12 @@ mod tests {
}
This is an empty test to help us get our library started, instead of the binary
-that says "Hello, world!" that we get with a new binary. Let's ignore the #[]
-stuff and mod tests for a little bit, but leave it at the end of
+that says "Hello, world!" that we get with the --bin option. Let's ignore the
+#[] stuff and mod tests for a little bit, but leave it at the end of
src/lib.rs.
Imagine that we're creating a library to provide some general networking -functionality. We're going to look at different ways we could choose to -organize our code, any of which could make sense depending on exactly what we -were trying to do. To start, add this code at the beginning of the file:
+We're going to look at different ways we could choose to organize our library's +code, any of which could make sense depending on exactly what we were trying to +do. To start, add this code at the beginning of the file:
Filename: src/lib.rs
mod network {
fn connect() {
@@ -135,12 +135,12 @@ within network:
In this way, modules form a tree. The contents of src/lib.rs are at the root
of the project's tree, and the submodules form the leaves. Here's what our
first example looks like when thought of this way:
-modules
+communicator
├── network
└── client
And here's the second:
-modules
+communicator
└── network
└── client
@@ -193,7 +193,7 @@ This file provides the contents of the client module. If w
client!
Now, everything should compile:
$ cargo build
- Compiling modules v0.1.0 (file:///projects/modules)
+ Compiling communicator v0.1.0 (file:///projects/communicator)
src/client.rs:1:1: 2:2 warning: function is never used: `connect`,
#[warn(dead_code)] on by default
@@ -242,7 +242,7 @@ mod server;
When we try to cargo build, we'll get an error:
$ cargo build
- Compiling modules v0.1.0 (file:///projects/modules)
+ Compiling communicator v0.1.0 (file:///projects/communicator)
src/network.rs:4:5: 4:11 error: cannot declare a new module at this location
src/network.rs:4 mod server;
^~~~~~
@@ -255,7 +255,7 @@ of possibly redeclaring it
src/network.rs:4 mod server;
^~~~~~
error: aborting due to previous error
-error: Could not compile `modules`.
+error: Could not compile `communicator`.
This error is actually pretty helpful. It points out something we didn't know
that we could do yet:
@@ -276,12 +276,12 @@ Then, we try to build:
$ mv src/server.rs src/network
$ mv src/network.rs src/network/mod.rs
$ cargo build
- Compiling modules v0.1.0 (file:///projects/modules)
+ Compiling communicator v0.1.0 (file:///projects/communicator)
<warnings>
$
It works! So now our module layout looks like this:
-modules
+communicator
├── client
└── network
└── server
diff --git a/ch07-02-controlling-visibility-with-pub.html b/ch07-02-controlling-visibility-with-pub.html
index 399ef0ca2..3501c5850 100644
--- a/ch07-02-controlling-visibility-with-pub.html
+++ b/ch07-02-controlling-visibility-with-pub.html
@@ -68,8 +68,8 @@
Controlling visibility with pub
-At the end of the last section, we had a project, modules, and when we compiled it, we got some strange warnings:
- Compiling modules v0.1.0 (file:///projects/modules)
+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)
src/client.rs:1:1: 2:2 warning: function is never used: `connect`,
#[warn(dead_code)] on by default
src/client.rs:1 fn connect() {
@@ -91,13 +91,13 @@ 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
this code:
Filename: src/main.rs
-extern crate modules;
+extern crate communicator;
fn main() {
- modules::client::connect();
+ communicator::client::connect();
}
-We need the extern crate line to bring our modules library crate into
+
We need the extern crate line to bring our communicator library crate into
scope, because our package actually now contains two crates. Cargo treats
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
@@ -109,13 +109,13 @@ the client module; we picked that one since it's the first warning
build output above. Invoking cargo build will now give us an error after the
warnings:
$ cargo build
- Compiling modules v0.1.0 (file:///projects/modules)
+ Compiling communicator v0.1.0 (file:///projects/communicator)
<warnings>
src/main.rs:4:5: 4:29 error: module `client` is private
-src/main.rs:4 modules::client::connect();
+src/main.rs:4 communicator::client::connect();
^~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to previous error
-error: Could not compile `modules`.
+error: Could not compile `communicator`.
Ah ha! The client module is private. This is the first time we've run into
the concepts of 'public' and 'private' in the context of Rust. There's no
@@ -136,10 +136,10 @@ mod network;
The pub goes right before mod. Let's try building again:
$ cargo build
- Compiling modules v0.1.0 (file:///projects/modules)
+ Compiling communicator v0.1.0 (file:///projects/communicator)
<warnings>
src/main.rs:4:5: 4:29 error: function `connect` is private
-src/main.rs:4 modules::client::connect();
+src/main.rs:4 communicator::client::connect();
^~~~~~~~~~~~~~~~~~~~~~~~
Hooray! We have a different error! Yes, different error messages are a cause
@@ -151,7 +151,7 @@ edit src/client.rs to make client::connect() public:
And run cargo build again:
cargo build
- Compiling modules v0.1.0 (file:///projects/modules)
+ Compiling communicator v0.1.0 (file:///projects/communicator)
src/network/mod.rs:1:1: 2:2 warning: function is never used: `connect`,
#[warn(dead_code)] on by default
src/network/mod.rs:1 fn connect() {
@@ -179,7 +179,7 @@ mod server;
And compile:
$ cargo build
- Compiling modules v0.1.0 (file:///projects/modules)
+ Compiling communicator v0.1.0 (file:///projects/communicator)
src/network/mod.rs:1:1: 2:2 warning: function is never used: `connect`,
#[warn(dead_code)] on by default
src/network/mod.rs:1 pub fn connect() {
@@ -201,7 +201,7 @@ pub mod network;
Now if we compile, that warning is gone:
$ cargo build
- Compiling modules v0.1.0 (file:///projects/modules)
+ Compiling communicator v0.1.0 (file:///projects/communicator)
src/network/server.rs:1:1: 2:2 warning: function is never used: `connect`,
#[warn(dead_code)] on by default
src/network/server.rs:1 fn connect() {
diff --git a/ch07-03-importing-names-with-use.html b/ch07-03-importing-names-with-use.html
index 974e22c26..365ad44c7 100644
--- a/ch07-03-importing-names-with-use.html
+++ b/ch07-03-importing-names-with-use.html
@@ -186,7 +186,7 @@ mod tests {
Run the tests by invoking the cargo test command:
$ cargo test
- Compiling modules v0.1.0 (file:///projects/modules)
+ Compiling communicator v0.1.0 (file:///projects/communicator)
src/lib.rs:9:9: 9:24 error: failed to resolve. Use of undeclared type or module
`client` [E0433]
src/lib.rs:9 client::connect();
@@ -194,11 +194,12 @@ src/lib.rs:9 client::connect();
src/lib.rs:9:9: 9:24 help: run `rustc --explain E0433` to see a detailed
explanation
-Why doesn't this work? It's not because we don't have modules:: in front of
-the function like we had in src/main.rs: we are definitely within the
-modules library crate here. The reason is that paths anywhere except in a
-use statement are relative to the current module. Our tests module doesn't
-have a client module in its scope!
+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,
+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
that we want to start from the root and list the whole path:
::client::connect();
@@ -229,8 +230,8 @@ mod tests {
If we run cargo test again, the test will pass and the first part of the test
result output will be:
$ cargo test
- Compiling modules v0.1.0 (file:///projects/modules)
- Running target/debug/modules-92007ddb5330fa5a
+ Compiling communicator v0.1.0 (file:///projects/communicator)
+ Running target/debug/communicator-92007ddb5330fa5a
running 1 test
test tests::it_works ... ok
diff --git a/print.html b/print.html
index 60235f1ed..8095fd064 100644
--- a/print.html
+++ b/print.html
@@ -4026,16 +4026,18 @@ match expression {
In other words, it's the high-level construct we were originally looking for:
do something special with only one pattern.
Modules
-When we write a program in Rust, your code starts off living in main. But as
-your code grows, you eventually move functionality out into functions, both for
-re-use, and for nicer organization. By splitting your code up into smaller
-chunks, each chunk is easier to understand on its own. So what happens when we
-start having too many functions? Rust has a module system that tackles both of
-these problems. In the same way that we extract some lines of code into a
-function, we can extract some functions (and other things too) into different
-modules. A module is a namespace that contains definitions of functions or
-types, and those definitions can be visible outside their module or not. Here's
-an overview of how the bits fit together:
+When you write a program in Rust, your code might start off living solely in
+the main function. As your code grows, you eventually move functionality out
+into functions, both for re-use and for nicer organization. By splitting your
+code up into smaller chunks, each chunk is easier to understand on its own. So
+what happens when you start having too many functions? Rust has a module system
+that tackles both the problem of wanting to be able to re-use code and the
+problem of 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
+those definitions can be visible outside their module or not. Here's an
+overview of how the bits fit together:
We'll take a look at each of these parts and see how they fit into the whole.
mod and the filesystem
Every module in Rust starts with the mod keyword. Let's give it a try by
-making a new project with Cargo called 'modules'. 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 to get the functionality we provided, like
-we used the rand crate in Chapter 2. So we're not going to use the --bin
-option like we have before, instead run:
-
$ cargo new modules
-$ cd modules
+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 to get the functionality we provided, like we used 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. So
+we're not going to use the --bin option like we have before, instead run:
+$ 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:
@@ -4063,13 +4066,12 @@ mod tests {
}
This is an empty test to help us get our library started, instead of the binary
-that says "Hello, world!" that we get with a new binary. Let's ignore the #[]
-stuff and mod tests for a little bit, but leave it at the end of
+that says "Hello, world!" that we get with the --bin option. Let's ignore the
+#[] stuff and mod tests for a little bit, but leave it at the end of
src/lib.rs.
-Imagine that we're creating a library to provide some general networking
-functionality. We're going to look at different ways we could choose to
-organize our code, any of which could make sense depending on exactly what we
-were trying to do. To start, add this code at the beginning of the file:
+We're going to look at different ways we could choose to organize our library's
+code, any of which could make sense depending on exactly what we were trying to
+do. To start, add this code at the beginning of the file:
Filename: src/lib.rs
mod network {
fn connect() {
@@ -4111,12 +4113,12 @@ within network:
In this way, modules form a tree. The contents of src/lib.rs are at the root
of the project's tree, and the submodules form the leaves. Here's what our
first example looks like when thought of this way:
-modules
+communicator
├── network
└── client
And here's the second:
-modules
+communicator
└── network
└── client
@@ -4169,7 +4171,7 @@ This file provides the contents of the client module. If w
client!
Now, everything should compile:
$ cargo build
- Compiling modules v0.1.0 (file:///projects/modules)
+ Compiling communicator v0.1.0 (file:///projects/communicator)
src/client.rs:1:1: 2:2 warning: function is never used: `connect`,
#[warn(dead_code)] on by default
@@ -4218,7 +4220,7 @@ mod server;
When we try to cargo build, we'll get an error:
$ cargo build
- Compiling modules v0.1.0 (file:///projects/modules)
+ Compiling communicator v0.1.0 (file:///projects/communicator)
src/network.rs:4:5: 4:11 error: cannot declare a new module at this location
src/network.rs:4 mod server;
^~~~~~
@@ -4231,7 +4233,7 @@ of possibly redeclaring it
src/network.rs:4 mod server;
^~~~~~
error: aborting due to previous error
-error: Could not compile `modules`.
+error: Could not compile `communicator`.
This error is actually pretty helpful. It points out something we didn't know
that we could do yet:
@@ -4252,12 +4254,12 @@ Then, we try to build:
$ mv src/server.rs src/network
$ mv src/network.rs src/network/mod.rs
$ cargo build
- Compiling modules v0.1.0 (file:///projects/modules)
+ Compiling communicator v0.1.0 (file:///projects/communicator)
<warnings>
$
It works! So now our module layout looks like this:
-modules
+communicator
├── client
└── network
└── server
@@ -4296,8 +4298,8 @@ the mod keyword.
Next, we'll talk about the pub keyword, and get rid of those warnings!
Controlling visibility with pub
-At the end of the last section, we had a project, modules, and when we compiled it, we got some strange warnings:
- Compiling modules v0.1.0 (file:///projects/modules)
+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)
src/client.rs:1:1: 2:2 warning: function is never used: `connect`,
#[warn(dead_code)] on by default
src/client.rs:1 fn connect() {
@@ -4319,13 +4321,13 @@ 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
this code:
Filename: src/main.rs
-extern crate modules;
+extern crate communicator;
fn main() {
- modules::client::connect();
+ communicator::client::connect();
}
-We need the extern crate line to bring our modules library crate into
+
We need the extern crate line to bring our communicator library crate into
scope, because our package actually now contains two crates. Cargo treats
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
@@ -4337,13 +4339,13 @@ the client module; we picked that one since it's the first warning
build output above. Invoking cargo build will now give us an error after the
warnings:
$ cargo build
- Compiling modules v0.1.0 (file:///projects/modules)
+ Compiling communicator v0.1.0 (file:///projects/communicator)
<warnings>
src/main.rs:4:5: 4:29 error: module `client` is private
-src/main.rs:4 modules::client::connect();
+src/main.rs:4 communicator::client::connect();
^~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to previous error
-error: Could not compile `modules`.
+error: Could not compile `communicator`.
Ah ha! The client module is private. This is the first time we've run into
the concepts of 'public' and 'private' in the context of Rust. There's no
@@ -4364,10 +4366,10 @@ mod network;
The pub goes right before mod. Let's try building again:
$ cargo build
- Compiling modules v0.1.0 (file:///projects/modules)
+ Compiling communicator v0.1.0 (file:///projects/communicator)
<warnings>
src/main.rs:4:5: 4:29 error: function `connect` is private
-src/main.rs:4 modules::client::connect();
+src/main.rs:4 communicator::client::connect();
^~~~~~~~~~~~~~~~~~~~~~~~
Hooray! We have a different error! Yes, different error messages are a cause
@@ -4379,7 +4381,7 @@ edit src/client.rs to make client::connect() public:
And run cargo build again:
cargo build
- Compiling modules v0.1.0 (file:///projects/modules)
+ Compiling communicator v0.1.0 (file:///projects/communicator)
src/network/mod.rs:1:1: 2:2 warning: function is never used: `connect`,
#[warn(dead_code)] on by default
src/network/mod.rs:1 fn connect() {
@@ -4407,7 +4409,7 @@ mod server;
And compile:
$ cargo build
- Compiling modules v0.1.0 (file:///projects/modules)
+ Compiling communicator v0.1.0 (file:///projects/communicator)
src/network/mod.rs:1:1: 2:2 warning: function is never used: `connect`,
#[warn(dead_code)] on by default
src/network/mod.rs:1 pub fn connect() {
@@ -4429,7 +4431,7 @@ pub mod network;
Now if we compile, that warning is gone:
$ cargo build
- Compiling modules v0.1.0 (file:///projects/modules)
+ Compiling communicator v0.1.0 (file:///projects/communicator)
src/network/server.rs:1:1: 2:2 warning: function is never used: `connect`,
#[warn(dead_code)] on by default
src/network/server.rs:1 fn connect() {
@@ -4615,7 +4617,7 @@ mod tests {
Run the tests by invoking the cargo test command:
$ cargo test
- Compiling modules v0.1.0 (file:///projects/modules)
+ Compiling communicator v0.1.0 (file:///projects/communicator)
src/lib.rs:9:9: 9:24 error: failed to resolve. Use of undeclared type or module
`client` [E0433]
src/lib.rs:9 client::connect();
@@ -4623,11 +4625,12 @@ src/lib.rs:9 client::connect();
src/lib.rs:9:9: 9:24 help: run `rustc --explain E0433` to see a detailed
explanation
-Why doesn't this work? It's not because we don't have modules:: in front of
-the function like we had in src/main.rs: we are definitely within the
-modules library crate here. The reason is that paths anywhere except in a
-use statement are relative to the current module. Our tests module doesn't
-have a client module in its scope!
+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,
+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
that we want to start from the root and list the whole path:
::client::connect();
@@ -4658,8 +4661,8 @@ mod tests {
If we run cargo test again, the test will pass and the first part of the test
result output will be:
$ cargo test
- Compiling modules v0.1.0 (file:///projects/modules)
- Running target/debug/modules-92007ddb5330fa5a
+ Compiling communicator v0.1.0 (file:///projects/communicator)
+ Running target/debug/communicator-92007ddb5330fa5a
running 1 test
test tests::it_works ... ok