mirror of
https://github.com/rust-lang/book.git
synced 2026-11-30 20:34:52 -06:00
Merge pull request #246 from jonathandturner/master
Some wording and error updates
This commit is contained in:
@@ -1,12 +1,14 @@
|
|||||||
## `mod` and the filesystem
|
## `mod` and the filesystem
|
||||||
|
|
||||||
Every module in Rust starts with the `mod` keyword. Let's give it a try by
|
Every module in Rust starts with the `mod` keyword. In this next example, we'll
|
||||||
making a new project with Cargo. This time, instead of a binary, we're going to
|
start again by making a new project with Cargo. This time, instead of a binary,
|
||||||
make a library: a project that other people would pull into their projects as a
|
we're going to make a library: a project that other people would pull into their
|
||||||
dependency to get the functionality we provided, like we used the `rand` crate
|
projects as a dependency. We saw this with the `rand` crate in Chapter 2.
|
||||||
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
|
Imagine that we're creating a library to provide some general
|
||||||
we're not going to use the `--bin` option like we have before, instead run:
|
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
|
```bash
|
||||||
$ cargo new communicator
|
$ 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
|
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
|
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
|
`#[]` stuff and `mod tests` for a little bit, but we'll make sure to leave it
|
||||||
`src/lib.rs`.
|
in `src/lib.rs` for later.
|
||||||
|
|
||||||
We're going to look at different ways we could choose to organize our library's
|
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
|
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
|
$ cargo build
|
||||||
Compiling communicator v0.1.0 (file:///projects/communicator)
|
Compiling communicator v0.1.0 (file:///projects/communicator)
|
||||||
|
|
||||||
src/client.rs:1:1: 2:2 warning: function is never used: `connect`,
|
warning: function is never used: `connect`, #[warn(dead_code)] on by default
|
||||||
#[warn(dead_code)] on by default
|
--> src/client.rs:1:1
|
||||||
src/client.rs:1 fn connect() {
|
|
|
||||||
^
|
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() {
|
warning: function is never used: `connect`, #[warn(dead_code)] on by default
|
||||||
^
|
--> src/lib.rs:4:5
|
||||||
src/lib.rs:8:9: 9:10 warning: function is never used: `connect`,
|
|
|
||||||
#[warn(dead_code)] on by default
|
4 | fn connect() {
|
||||||
src/lib.rs:8 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
|
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
|
```bash
|
||||||
$ cargo build
|
$ cargo build
|
||||||
Compiling communicator v0.1.0 (file:///projects/communicator)
|
Compiling communicator v0.1.0 (file:///projects/communicator)
|
||||||
src/network.rs:4:5: 4:11 error: cannot declare a new module at this location
|
error: cannot declare a new module at this location
|
||||||
src/network.rs:4 mod server;
|
--> src/network.rs:4:5
|
||||||
^~~~~~
|
|
|
||||||
src/network.rs:4:5: 4:11 note: maybe move this module `network` to its own
|
4 | mod server;
|
||||||
directory via `network/mod.rs`
|
| ^^^^^^
|
||||||
src/network.rs:4 mod server;
|
|
|
||||||
^~~~~~
|
note: maybe move this module `network` to its own directory via `network/mod.rs`
|
||||||
src/network.rs:4:5: 4:11 note: ... or maybe `use` the module `server` instead
|
--> src/network.rs:4:5
|
||||||
of possibly redeclaring it
|
|
|
||||||
src/network.rs:4 mod server;
|
4 | mod server;
|
||||||
^~~~~~
|
| ^^^^^^
|
||||||
error: aborting due to previous error
|
note: ... or maybe `use` the module `server` instead of possibly redeclaring it
|
||||||
error: Could not compile `communicator`.
|
--> src/network.rs:4:5
|
||||||
|
|
|
||||||
|
4 | mod server;
|
||||||
|
| ^^^^^^
|
||||||
```
|
```
|
||||||
|
|
||||||
This error is actually pretty helpful. It points out something we didn't know
|
This error is actually pretty helpful. It points out something we didn't know
|
||||||
|
|||||||
@@ -4,18 +4,23 @@ At the end of the last section, we had a project, `communicator`, and when we co
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
Compiling communicator v0.1.0 (file:///projects/communicator)
|
Compiling communicator v0.1.0 (file:///projects/communicator)
|
||||||
src/client.rs:1:1: 2:2 warning: function is never used: `connect`,
|
warning: function is never used: `connect`, #[warn(dead_code)] on by default
|
||||||
#[warn(dead_code)] on by default
|
--> src/client.rs:1:1
|
||||||
src/client.rs:1 fn connect() {
|
|
|
||||||
^
|
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() {
|
warning: function is never used: `connect`, #[warn(dead_code)] on by default
|
||||||
^
|
--> src/network/mod.rs:1:1
|
||||||
src/network/server.rs:1:1: 2:2 warning: function is never used: `connect`,
|
|
|
||||||
#[warn(dead_code)] on by default
|
1 | fn connect() {
|
||||||
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() {
|
||||||
|
| ^
|
||||||
```
|
```
|
||||||
|
|
||||||
Why does this happen? After all, we're building a library. What if these three
|
Why does this happen? After all, we're building a library. What if these three
|
||||||
@@ -52,12 +57,11 @@ warnings:
|
|||||||
```bash
|
```bash
|
||||||
$ cargo build
|
$ cargo build
|
||||||
Compiling communicator v0.1.0 (file:///projects/communicator)
|
Compiling communicator v0.1.0 (file:///projects/communicator)
|
||||||
<warnings>
|
error: module `client` is private
|
||||||
src/main.rs:4:5: 4:29 error: module `client` is private
|
--> src/main.rs:4:5
|
||||||
src/main.rs:4 communicator::client::connect();
|
|
|
||||||
^~~~~~~~~~~~~~~~~~~~~~~~
|
4 | communicator::client::connect();
|
||||||
error: aborting due to previous error
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
error: Could not compile `communicator`.
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Ah ha! The `client` module is private. This is the first time we've run into
|
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
|
$ cargo build
|
||||||
Compiling communicator v0.1.0 (file:///projects/communicator)
|
Compiling communicator v0.1.0 (file:///projects/communicator)
|
||||||
<warnings>
|
<warnings>
|
||||||
src/main.rs:4:5: 4:29 error: function `connect` is private
|
error: function `connect` is private
|
||||||
src/main.rs:4 communicator::client::connect();
|
--> src/main.rs:4:5
|
||||||
^~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
||||||
|
4 | communicator::client::connect();
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
```
|
```
|
||||||
|
|
||||||
Hooray! We have a different error! Yes, different error messages are a cause
|
Hooray! We have a different error! Yes, different error messages are a cause
|
||||||
@@ -109,14 +115,17 @@ And run `cargo build` again:
|
|||||||
```bash
|
```bash
|
||||||
cargo build
|
cargo build
|
||||||
Compiling communicator v0.1.0 (file:///projects/communicator)
|
Compiling communicator v0.1.0 (file:///projects/communicator)
|
||||||
src/network/mod.rs:1:1: 2:2 warning: function is never used: `connect`,
|
warning: function is never used: `connect`, #[warn(dead_code)] on by default
|
||||||
#[warn(dead_code)] on by default
|
--> src/network/mod.rs:1:1
|
||||||
src/network/mod.rs:1 fn connect() {
|
|
|
||||||
^
|
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/server.rs:1:1
|
||||||
|
|
|
||||||
|
1 | fn connect() {
|
||||||
|
| ^
|
||||||
```
|
```
|
||||||
|
|
||||||
It compiled! And the warning about `client::connect()` not being used is gone!
|
It compiled! And the warning about `client::connect()` not being used is gone!
|
||||||
@@ -146,14 +155,17 @@ And compile:
|
|||||||
```bash
|
```bash
|
||||||
$ cargo build
|
$ cargo build
|
||||||
Compiling communicator v0.1.0 (file:///projects/communicator)
|
Compiling communicator v0.1.0 (file:///projects/communicator)
|
||||||
src/network/mod.rs:1:1: 2:2 warning: function is never used: `connect`,
|
warning: function is never used: `connect`, #[warn(dead_code)] on by default
|
||||||
#[warn(dead_code)] on by default
|
--> src/network/mod.rs:1:1
|
||||||
src/network/mod.rs:1 pub fn connect() {
|
|
|
||||||
^
|
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/server.rs:1:1
|
||||||
|
|
|
||||||
|
1 | fn connect() {
|
||||||
|
| ^
|
||||||
```
|
```
|
||||||
|
|
||||||
Hmmm, it says this is still dead, even though it's `pub`. While the function is
|
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
|
```bash
|
||||||
$ cargo build
|
$ cargo build
|
||||||
Compiling communicator v0.1.0 (file:///projects/communicator)
|
Compiling communicator v0.1.0 (file:///projects/communicator)
|
||||||
src/network/server.rs:1:1: 2:2 warning: function is never used: `connect`,
|
warning: function is never used: `connect`, #[warn(dead_code)] on by default
|
||||||
#[warn(dead_code)] on by default
|
--> src/network/server.rs:1:1
|
||||||
src/network/server.rs:1 fn connect() {
|
|
|
||||||
^
|
1 | fn connect() {
|
||||||
|
| ^
|
||||||
```
|
```
|
||||||
|
|
||||||
Only one last warning! Try to fix this one on your own!
|
Only one last warning! Try to fix this one on your own!
|
||||||
|
|||||||
@@ -147,12 +147,17 @@ Run the tests by invoking the `cargo test` command:
|
|||||||
```bash
|
```bash
|
||||||
$ cargo test
|
$ cargo test
|
||||||
Compiling communicator v0.1.0 (file:///projects/communicator)
|
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
|
error[E0433]: failed to resolve. Use of undeclared type or module `client`
|
||||||
`client` [E0433]
|
--> src/lib.rs:9:9
|
||||||
src/lib.rs:9 client::connect();
|
|
|
||||||
^~~~~~~~~~~~~~~
|
9 | client::connect();
|
||||||
src/lib.rs:9:9: 9:24 help: run `rustc --explain E0433` to see a detailed
|
| ^^^^^^^^^^^^^^^ Use of undeclared type or module `client`
|
||||||
explanation
|
|
||||||
|
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
|
Why doesn't this work? It's not because we don't have `communicator::` in front
|
||||||
|
|||||||
Reference in New Issue
Block a user