Merge pull request #246 from jonathandturner/master

Some wording and error updates
This commit is contained in:
Carol (Nichols || Goulding)
2016-09-04 12:40:27 -04:00
committed by GitHub
3 changed files with 109 additions and 81 deletions
+44 -34
View File
@@ -1,12 +1,14 @@
## `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. 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:
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:
```bash
$ cargo new communicator
@@ -27,8 +29,8 @@ 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 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`.
`#[]` stuff and `mod tests` for a little bit, but we'll make sure to leave it
in `src/lib.rs` for later.
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
@@ -171,18 +173,23 @@ Now, everything should compile:
$ cargo build
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() {
^
src/lib.rs:4:5: 5:6 warning: function is never used: `connect`,
#[warn(dead_code)] on by default
src/lib.rs:4 fn connect() {
^
src/lib.rs:8:9: 9:10 warning: function is never used: `connect`,
#[warn(dead_code)] on by default
src/lib.rs:8 fn connect() {
^
warning: function is never used: `connect`, #[warn(dead_code)] on by default
--> src/client.rs:1:1
|
1 | fn connect() {
| ^
warning: function is never used: `connect`, #[warn(dead_code)] on by default
--> src/lib.rs:4:5
|
4 | fn connect() {
| ^
warning: function is never used: `connect`, #[warn(dead_code)] on by default
--> src/lib.rs:8:9
|
8 | fn connect() {
| ^
```
Don't worry about those warnings for now; we'll clear them up in a future
@@ -239,19 +246,22 @@ When we try to `cargo build`, we'll get an error:
```bash
$ cargo build
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;
^~~~~~
src/network.rs:4:5: 4:11 note: maybe move this module `network` to its own
directory via `network/mod.rs`
src/network.rs:4 mod server;
^~~~~~
src/network.rs:4:5: 4:11 note: ... or maybe `use` the module `server` instead
of possibly redeclaring it
src/network.rs:4 mod server;
^~~~~~
error: aborting due to previous error
error: Could not compile `communicator`.
error: cannot declare a new module at this location
--> src/network.rs:4:5
|
4 | mod server;
| ^^^^^^
|
note: maybe move this module `network` to its own directory via `network/mod.rs`
--> src/network.rs:4:5
|
4 | mod server;
| ^^^^^^
note: ... or maybe `use` the module `server` instead of possibly redeclaring it
--> src/network.rs:4:5
|
4 | mod server;
| ^^^^^^
```
This error is actually pretty helpful. It points out something we didn't know
+54 -41
View File
@@ -4,18 +4,23 @@ At the end of the last section, we had a project, `communicator`, and when we co
```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() {
^
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() {
^
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() {
^
warning: function is never used: `connect`, #[warn(dead_code)] on by default
--> src/client.rs:1:1
|
1 | fn connect() {
| ^
warning: function is never used: `connect`, #[warn(dead_code)] on by default
--> src/network/mod.rs:1:1
|
1 | fn connect() {
| ^
warning: function is never used: `connect`, #[warn(dead_code)] on by default
--> src/network/server.rs:1:1
|
1 | fn connect() {
| ^
```
Why does this happen? After all, we're building a library. What if these three
@@ -52,12 +57,11 @@ warnings:
```bash
$ cargo build
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 communicator::client::connect();
^~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to previous error
error: Could not compile `communicator`.
error: module `client` is private
--> src/main.rs:4:5
|
4 | communicator::client::connect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
```
Ah ha! The `client` module is private. This is the first time we've run into
@@ -88,9 +92,11 @@ The `pub` goes right before `mod`. Let's try building again:
$ cargo build
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 communicator::client::connect();
^~~~~~~~~~~~~~~~~~~~~~~~
error: function `connect` is private
--> src/main.rs:4:5
|
4 | communicator::client::connect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
```
Hooray! We have a different error! Yes, different error messages are a cause
@@ -109,14 +115,17 @@ And run `cargo build` again:
```bash
cargo build
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() {
^
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() {
^
warning: function is never used: `connect`, #[warn(dead_code)] on by default
--> src/network/mod.rs:1:1
|
1 | fn connect() {
| ^
warning: function is never used: `connect`, #[warn(dead_code)] on by default
--> src/network/server.rs:1:1
|
1 | fn connect() {
| ^
```
It compiled! And the warning about `client::connect()` not being used is gone!
@@ -146,14 +155,17 @@ And compile:
```bash
$ cargo build
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() {
^
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() {
^
warning: function is never used: `connect`, #[warn(dead_code)] on by default
--> src/network/mod.rs:1:1
|
1 | pub fn connect() {
| ^
warning: function is never used: `connect`, #[warn(dead_code)] on by default
--> src/network/server.rs:1:1
|
1 | fn connect() {
| ^
```
Hmmm, it says this is still dead, even though it's `pub`. While the function is
@@ -175,10 +187,11 @@ Now if we compile, that warning is gone:
```bash
$ cargo build
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() {
^
warning: function is never used: `connect`, #[warn(dead_code)] on by default
--> src/network/server.rs:1:1
|
1 | fn connect() {
| ^
```
Only one last warning! Try to fix this one on your own!
+11 -6
View File
@@ -147,12 +147,17 @@ Run the tests by invoking the `cargo test` command:
```bash
$ cargo test
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();
^~~~~~~~~~~~~~~
src/lib.rs:9:9: 9:24 help: run `rustc --explain E0433` to see a detailed
explanation
error[E0433]: failed to resolve. Use of undeclared type or module `client`
--> src/lib.rs:9:9
|
9 | client::connect();
| ^^^^^^^^^^^^^^^ Use of undeclared type or module `client`
warning: function is never used: `connect`, #[warn(dead_code)] on by default
--> src/network/server.rs:1:1
|
1 | fn connect() {
| ^
```
Why doesn't this work? It's not because we don't have `communicator::` in front