mirror of
https://github.com/rust-lang/book.git
synced 2026-11-30 23:31:22 -06:00
rebuild pages at 33943ea
This commit is contained in:
+12
-10
@@ -68,16 +68,18 @@
|
||||
|
||||
<div id="content" class="content">
|
||||
<h1>Modules</h1>
|
||||
<p>When we write a program in Rust, your code starts off living in <code>main</code>. 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 <em>module</em> 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:</p>
|
||||
<p>When you write a program in Rust, your code might start off living solely in
|
||||
the <code>main</code> 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.</p>
|
||||
<p>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
|
||||
<em>module</em> 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:</p>
|
||||
<ul>
|
||||
<li><code>mod</code> declares a new module.</li>
|
||||
<li>Everything starts off as private, but the <code>pub</code> keyword makes it public.</li>
|
||||
|
||||
@@ -69,13 +69,14 @@
|
||||
<div id="content" class="content">
|
||||
<h2><code>mod</code> and the filesystem</h2>
|
||||
<p>Every module in Rust starts with the <code>mod</code> 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 <code>rand</code> crate in Chapter 2. So we're not going to use the <code>--bin</code>
|
||||
option like we have before, instead run:</p>
|
||||
<pre><code class="language-bash">$ 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 <code>rand</code> crate
|
||||
in Chapter 2. Imagine that we're creating a library to provide some general
|
||||
networking functionality, and we decide to call our library <code>communicator</code>. So
|
||||
we're not going to use the <code>--bin</code> option like we have before, instead run:</p>
|
||||
<pre><code class="language-bash">$ cargo new communicator
|
||||
$ cd communicator
|
||||
</code></pre>
|
||||
<p>You'll notice that Cargo generated <code>src/lib.rs</code> instead of <code>src/main.rs</code> for
|
||||
us, and inside it we'll find this:</p>
|
||||
@@ -87,13 +88,12 @@ mod tests {
|
||||
}
|
||||
</code></pre>
|
||||
<p>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 <code>#[]</code>
|
||||
stuff and <code>mod tests</code> for a little bit, but leave it at the end of
|
||||
that says "Hello, world!" that we get with the <code>--bin</code> option. Let's ignore the
|
||||
<code>#[]</code> stuff and <code>mod tests</code> for a little bit, but leave it at the end of
|
||||
<code>src/lib.rs</code>.</p>
|
||||
<p>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:</p>
|
||||
<p>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:</p>
|
||||
<p>Filename: src/lib.rs</p>
|
||||
<pre><code class="language-rust">mod network {
|
||||
fn connect() {
|
||||
@@ -135,12 +135,12 @@ within <code>network</code>:</p>
|
||||
<p>In this way, modules form a tree. The contents of <code>src/lib.rs</code> 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:</p>
|
||||
<pre><code class="language-text">modules
|
||||
<pre><code class="language-text">communicator
|
||||
├── network
|
||||
└── client
|
||||
</code></pre>
|
||||
<p>And here's the second:</p>
|
||||
<pre><code class="language-text">modules
|
||||
<pre><code class="language-text">communicator
|
||||
└── network
|
||||
└── client
|
||||
</code></pre>
|
||||
@@ -193,7 +193,7 @@ This file provides the <em>contents</em> of the <code>client</code> module. If w
|
||||
<code>client</code>!</p>
|
||||
<p>Now, everything should compile:</p>
|
||||
<pre><code class="language-bash">$ 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;
|
||||
</code></pre>
|
||||
<p>When we try to <code>cargo build</code>, we'll get an error:</p>
|
||||
<pre><code class="language-bash">$ 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`.
|
||||
</code></pre>
|
||||
<p>This error is actually pretty helpful. It points out something we didn't know
|
||||
that we could do yet:</p>
|
||||
@@ -276,12 +276,12 @@ Then, we try to build:</p>
|
||||
$ 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>
|
||||
$
|
||||
</code></pre>
|
||||
<p>It works! So now our module layout looks like this:</p>
|
||||
<pre><code class="language-text">modules
|
||||
<pre><code class="language-text">communicator
|
||||
├── client
|
||||
└── network
|
||||
└── server
|
||||
|
||||
@@ -68,8 +68,8 @@
|
||||
|
||||
<div id="content" class="content">
|
||||
<h2>Controlling visibility with <code>pub</code></h2>
|
||||
<p>At the end of the last section, we had a project, <code>modules</code>, and when we compiled it, we got some strange warnings:</p>
|
||||
<pre><code class="language-bash"> Compiling modules v0.1.0 (file:///projects/modules)
|
||||
<p>At the end of the last section, we had a project, <code>communicator</code>, and when we compiled it, we got some strange warnings:</p>
|
||||
<pre><code class="language-bash"> 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 <code>src/main.rs</code> file with
|
||||
this code:</p>
|
||||
<p>Filename: src/main.rs</p>
|
||||
<pre><code class="language-rust,ignore">extern crate modules;
|
||||
<pre><code class="language-rust,ignore">extern crate communicator;
|
||||
|
||||
fn main() {
|
||||
modules::client::connect();
|
||||
communicator::client::connect();
|
||||
}
|
||||
</code></pre>
|
||||
<p>We need the <code>extern crate</code> line to bring our <code>modules</code> library crate into
|
||||
<p>We need the <code>extern crate</code> line to bring our <code>communicator</code> library crate into
|
||||
scope, because our package actually now contains <em>two</em> 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 <code>client</code> module; we picked that one since it's the first warning
|
||||
build output above. Invoking <code>cargo build</code> will now give us an error after the
|
||||
warnings:</p>
|
||||
<pre><code class="language-bash">$ 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`.
|
||||
</code></pre>
|
||||
<p>Ah ha! The <code>client</code> 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;
|
||||
</code></pre>
|
||||
<p>The <code>pub</code> goes right before <code>mod</code>. Let's try building again:</p>
|
||||
<pre><code class="language-bash">$ 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();
|
||||
^~~~~~~~~~~~~~~~~~~~~~~~
|
||||
</code></pre>
|
||||
<p>Hooray! We have a different error! Yes, different error messages are a cause
|
||||
@@ -151,7 +151,7 @@ edit <code>src/client.rs</code> to make <code>client::connect()</code> public:</
|
||||
</code></pre>
|
||||
<p>And run <code>cargo build</code> again:</p>
|
||||
<pre><code class="language-bash"> 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;
|
||||
</code></pre>
|
||||
<p>And compile:</p>
|
||||
<pre><code class="language-bash">$ 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;
|
||||
</code></pre>
|
||||
<p>Now if we compile, that warning is gone:</p>
|
||||
<pre><code class="language-bash">$ 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() {
|
||||
|
||||
@@ -186,7 +186,7 @@ mod tests {
|
||||
</code></pre>
|
||||
<p>Run the tests by invoking the <code>cargo test</code> command:</p>
|
||||
<pre><code class="language-bash">$ 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
|
||||
</code></pre>
|
||||
<p>Why doesn't this work? It's not because we don't have <code>modules::</code> in front of
|
||||
the function like we had in <code>src/main.rs</code>: we are definitely within the
|
||||
<code>modules</code> library crate here. The reason is that paths anywhere except in a
|
||||
<code>use</code> statement are relative to the current module. Our <code>tests</code> module doesn't
|
||||
have a <code>client</code> module in its scope!</p>
|
||||
<p>Why doesn't this work? It's not because we don't have <code>communicator::</code> in front
|
||||
of the function like we had in <code>src/main.rs</code>: we are definitely within the
|
||||
<code>communicator</code> library crate here. The reason is that paths anywhere except in
|
||||
a <code>use</code> statement are relative to the current module (In a <code>use</code> statement,
|
||||
they're relative to the crate root by default). Our <code>tests</code> module doesn't have
|
||||
a <code>client</code> module in its scope!</p>
|
||||
<p>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:</p>
|
||||
<pre><code class="language-rust,ignore">::client::connect();
|
||||
@@ -229,8 +230,8 @@ mod tests {
|
||||
<p>If we run <code>cargo test</code> again, the test will pass and the first part of the test
|
||||
result output will be:</p>
|
||||
<pre><code class="language-bash">$ 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
|
||||
|
||||
+54
-51
@@ -4026,16 +4026,18 @@ match expression {
|
||||
<p>In other words, it's the high-level construct we were originally looking for:
|
||||
do something special with only one pattern.</p>
|
||||
<h1>Modules</h1>
|
||||
<p>When we write a program in Rust, your code starts off living in <code>main</code>. 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 <em>module</em> 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:</p>
|
||||
<p>When you write a program in Rust, your code might start off living solely in
|
||||
the <code>main</code> 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.</p>
|
||||
<p>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
|
||||
<em>module</em> 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:</p>
|
||||
<ul>
|
||||
<li><code>mod</code> declares a new module.</li>
|
||||
<li>Everything starts off as private, but the <code>pub</code> keyword makes it public.</li>
|
||||
@@ -4045,13 +4047,14 @@ into scope so that it's easier to refer to them.</li>
|
||||
<p>We'll take a look at each of these parts and see how they fit into the whole.</p>
|
||||
<h2><code>mod</code> and the filesystem</h2>
|
||||
<p>Every module in Rust starts with the <code>mod</code> 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 <code>rand</code> crate in Chapter 2. So we're not going to use the <code>--bin</code>
|
||||
option like we have before, instead run:</p>
|
||||
<pre><code class="language-bash">$ 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 <code>rand</code> crate
|
||||
in Chapter 2. Imagine that we're creating a library to provide some general
|
||||
networking functionality, and we decide to call our library <code>communicator</code>. So
|
||||
we're not going to use the <code>--bin</code> option like we have before, instead run:</p>
|
||||
<pre><code class="language-bash">$ cargo new communicator
|
||||
$ cd communicator
|
||||
</code></pre>
|
||||
<p>You'll notice that Cargo generated <code>src/lib.rs</code> instead of <code>src/main.rs</code> for
|
||||
us, and inside it we'll find this:</p>
|
||||
@@ -4063,13 +4066,12 @@ mod tests {
|
||||
}
|
||||
</code></pre>
|
||||
<p>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 <code>#[]</code>
|
||||
stuff and <code>mod tests</code> for a little bit, but leave it at the end of
|
||||
that says "Hello, world!" that we get with the <code>--bin</code> option. Let's ignore the
|
||||
<code>#[]</code> stuff and <code>mod tests</code> for a little bit, but leave it at the end of
|
||||
<code>src/lib.rs</code>.</p>
|
||||
<p>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:</p>
|
||||
<p>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:</p>
|
||||
<p>Filename: src/lib.rs</p>
|
||||
<pre><code class="language-rust">mod network {
|
||||
fn connect() {
|
||||
@@ -4111,12 +4113,12 @@ within <code>network</code>:</p>
|
||||
<p>In this way, modules form a tree. The contents of <code>src/lib.rs</code> 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:</p>
|
||||
<pre><code class="language-text">modules
|
||||
<pre><code class="language-text">communicator
|
||||
├── network
|
||||
└── client
|
||||
</code></pre>
|
||||
<p>And here's the second:</p>
|
||||
<pre><code class="language-text">modules
|
||||
<pre><code class="language-text">communicator
|
||||
└── network
|
||||
└── client
|
||||
</code></pre>
|
||||
@@ -4169,7 +4171,7 @@ This file provides the <em>contents</em> of the <code>client</code> module. If w
|
||||
<code>client</code>!</p>
|
||||
<p>Now, everything should compile:</p>
|
||||
<pre><code class="language-bash">$ 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;
|
||||
</code></pre>
|
||||
<p>When we try to <code>cargo build</code>, we'll get an error:</p>
|
||||
<pre><code class="language-bash">$ 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`.
|
||||
</code></pre>
|
||||
<p>This error is actually pretty helpful. It points out something we didn't know
|
||||
that we could do yet:</p>
|
||||
@@ -4252,12 +4254,12 @@ Then, we try to build:</p>
|
||||
$ 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>
|
||||
$
|
||||
</code></pre>
|
||||
<p>It works! So now our module layout looks like this:</p>
|
||||
<pre><code class="language-text">modules
|
||||
<pre><code class="language-text">communicator
|
||||
├── client
|
||||
└── network
|
||||
└── server
|
||||
@@ -4296,8 +4298,8 @@ the <code>mod</code> keyword.</p>
|
||||
</ul>
|
||||
<p>Next, we'll talk about the <code>pub</code> keyword, and get rid of those warnings!</p>
|
||||
<h2>Controlling visibility with <code>pub</code></h2>
|
||||
<p>At the end of the last section, we had a project, <code>modules</code>, and when we compiled it, we got some strange warnings:</p>
|
||||
<pre><code class="language-bash"> Compiling modules v0.1.0 (file:///projects/modules)
|
||||
<p>At the end of the last section, we had a project, <code>communicator</code>, and when we compiled it, we got some strange warnings:</p>
|
||||
<pre><code class="language-bash"> 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 <code>src/main.rs</code> file with
|
||||
this code:</p>
|
||||
<p>Filename: src/main.rs</p>
|
||||
<pre><code class="language-rust,ignore">extern crate modules;
|
||||
<pre><code class="language-rust,ignore">extern crate communicator;
|
||||
|
||||
fn main() {
|
||||
modules::client::connect();
|
||||
communicator::client::connect();
|
||||
}
|
||||
</code></pre>
|
||||
<p>We need the <code>extern crate</code> line to bring our <code>modules</code> library crate into
|
||||
<p>We need the <code>extern crate</code> line to bring our <code>communicator</code> library crate into
|
||||
scope, because our package actually now contains <em>two</em> 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 <code>client</code> module; we picked that one since it's the first warning
|
||||
build output above. Invoking <code>cargo build</code> will now give us an error after the
|
||||
warnings:</p>
|
||||
<pre><code class="language-bash">$ 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`.
|
||||
</code></pre>
|
||||
<p>Ah ha! The <code>client</code> 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;
|
||||
</code></pre>
|
||||
<p>The <code>pub</code> goes right before <code>mod</code>. Let's try building again:</p>
|
||||
<pre><code class="language-bash">$ 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();
|
||||
^~~~~~~~~~~~~~~~~~~~~~~~
|
||||
</code></pre>
|
||||
<p>Hooray! We have a different error! Yes, different error messages are a cause
|
||||
@@ -4379,7 +4381,7 @@ edit <code>src/client.rs</code> to make <code>client::connect()</code> public:</
|
||||
</code></pre>
|
||||
<p>And run <code>cargo build</code> again:</p>
|
||||
<pre><code class="language-bash"> 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;
|
||||
</code></pre>
|
||||
<p>And compile:</p>
|
||||
<pre><code class="language-bash">$ 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;
|
||||
</code></pre>
|
||||
<p>Now if we compile, that warning is gone:</p>
|
||||
<pre><code class="language-bash">$ 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 {
|
||||
</code></pre>
|
||||
<p>Run the tests by invoking the <code>cargo test</code> command:</p>
|
||||
<pre><code class="language-bash">$ 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
|
||||
</code></pre>
|
||||
<p>Why doesn't this work? It's not because we don't have <code>modules::</code> in front of
|
||||
the function like we had in <code>src/main.rs</code>: we are definitely within the
|
||||
<code>modules</code> library crate here. The reason is that paths anywhere except in a
|
||||
<code>use</code> statement are relative to the current module. Our <code>tests</code> module doesn't
|
||||
have a <code>client</code> module in its scope!</p>
|
||||
<p>Why doesn't this work? It's not because we don't have <code>communicator::</code> in front
|
||||
of the function like we had in <code>src/main.rs</code>: we are definitely within the
|
||||
<code>communicator</code> library crate here. The reason is that paths anywhere except in
|
||||
a <code>use</code> statement are relative to the current module (In a <code>use</code> statement,
|
||||
they're relative to the crate root by default). Our <code>tests</code> module doesn't have
|
||||
a <code>client</code> module in its scope!</p>
|
||||
<p>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:</p>
|
||||
<pre><code class="language-rust,ignore">::client::connect();
|
||||
@@ -4658,8 +4661,8 @@ mod tests {
|
||||
<p>If we run <code>cargo test</code> again, the test will pass and the first part of the test
|
||||
result output will be:</p>
|
||||
<pre><code class="language-bash">$ 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
|
||||
|
||||
Reference in New Issue
Block a user