From 68f465bbc33c9b98ded1fb4500732f171c8e2d57 Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Mon, 22 Aug 2016 16:15:00 -0400 Subject: [PATCH] Use network/client/server as mods and connect as functions Instead of foo/bar/tests and it_works. This doesn't make the most sense ever, but it's a little better than metasyntactic variables :) --- src/ch07-02-mod-and-the-filesystem.md | 186 ++++++++++-------- ...ch07-03-controlling-visibility-with-pub.md | 147 +++++++------- 2 files changed, 177 insertions(+), 156 deletions(-) diff --git a/src/ch07-02-mod-and-the-filesystem.md b/src/ch07-02-mod-and-the-filesystem.md index c4d2499b5..fcb29ed69 100644 --- a/src/ch07-02-mod-and-the-filesystem.md +++ b/src/ch07-02-mod-and-the-filesystem.md @@ -25,54 +25,57 @@ 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. We haven't talked -about the `#[]` stuff yet, so let's ignore those for now: +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 `src/lib.rs`. Let's imagine that we're creating a library to provide networking functionality. At the beginning of the file, add: + +Filename: src/lib.rs ```rust -mod tests { - fn it_works() { +mod network { + fn connect() { } } ``` 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 `tests`. In this -case, we have a single function, `it_works`. If we wanted to try and call this -function from outside the `tests` module, we would say `tests::it_works` rather -than `it_works`. +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: +You could have multiple modules, side-by-side. For example, if you wanted a +`client` module: ```rust -mod tests { - fn it_works() { +mod network { + fn connect() { } } -mod foo { - fn it_works() { +mod client { + fn connect() { } } ``` -Now we have a `tests::it_works` function and a `foo::it_works` function. +Now we have a `network::connect` function and a `client::connect` function. -And you can put modules inside of modules: +And you can put modules inside of modules, if you wanted to have `client` to be +within `network`: ```rust -mod tests { - fn it_works() { +mod network { + fn connect() { } - mod foo { - fn it_works() { + mod client { + fn connect() { } } } ``` -This gives us `tests::it_works` and `tests::foo::it_works`. +This gives us `network::connect` and `network::client::connect`. 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 @@ -80,16 +83,16 @@ first example looks like when thought of this way: ```text modules - ├── foo - └── tests + ├── network + └── client ``` And here's the second: ```text modules - └── tests - └── foo + └── network + └── client ``` More complicated projects can have a lot of modules. @@ -100,111 +103,126 @@ 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: +File: src/lib.rs + ```rust -mod foo { - fn it_works() { +mod client { + fn connect() { } } -mod tests { - fn it_works() { +mod network { + fn connect() { } - mod bar { - fn it_works() { + mod server { + fn connect() { } } } ``` -Let's extract the `foo` module into another file. First, we need to change our -code in `src/lib.rs`: +Let's extract the `client` module into another file. First, we need to change +our code in `src/lib.rs`: + +File: src/lib.rs ```rust,ignore -mod foo; +mod client; -mod tests { - fn it_works() { +mod network { + fn connect() { } - mod bar { - fn it_works() { + mod server { + fn connect() { } } } ``` -We still say `mod foo`, 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/foo.rs` and put this in it: +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: + +File: src/client.rs ```rust -fn it_works() { +fn connect() { } ``` Note that we don't need a `mod` declaration in this file. `mod` is for declaring a new module, and we've already declared this module in `src/lib.rs`. -This file provides the _contents_ of the `foo` module. If we put a `mod foo` -here, we'd be giving the `foo` module its own submodule named `foo`! +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: ```bash $ cargo build Compiling modules v0.1.0 (file:///home/steve/tmp/modules) -src/foo.rs:1:1: 2:2 warning: function is never used: `it_works`, + +src/client.rs:1:1: 2:2 warning: function is never used: `connect`, #[warn(dead_code)] on by default -src/foo.rs:1 fn it_works() { - ^ -src/lib.rs:4:5: 5:6 warning: function is never used: `it_works`, +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 it_works() { - ^ -src/lib.rs:8:9: 9:10 warning: function is never used: `it_works`, +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 it_works() { - ^ +src/lib.rs:8 fn connect() { + ^ ``` 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 `tests` module next. Change `src/lib.rs` to look like this: +Let's convert the `network` module next. Change `src/lib.rs` to look like this: + +Filename: src/lib.rs ```rust,ignore -mod foo; +mod client; -mod tests; +mod network; ``` -And then put this in `src/tests.rs` +And then put this in `src/network.rs` + +Filename: src/network.rs ```rust -fn it_works() { +fn connect() { } -mod bar { - fn it_works() { +mod server { + fn connect() { } } ``` And then run `cargo build` again. Success! We have one more module to extract: -`bar`. Unfortunately, our current tactic won't work. Let's try it anyway. Modify -`src/tests.rs` to look like this: +`server`. Unfortunately, our current tactic won't work. Let's try it anyway. Modify `src/network.rs` to look like this: + +Filename: src/network.rs ```rust,ignore -fn it_works() { +fn connect() { } -mod bar; +mod server; ``` -Put this in `src/bar.rs` +Put this in `src/server.rs` + +Filename: src/server.rs ```rust -fn it_works() { +fn connect() { } ``` @@ -213,17 +231,15 @@ When we try to `cargo build`, we'll get an error: ```bash $ cargo build Compiling modules v0.1.0 (file:///home/steve/tmp/modules) -src/tests.rs:4:5: 4:8 error: cannot declare a new module at this location -src/tests.rs:4 mod bar; - ^~~ -src/tests.rs:4:5: 4:8 note: maybe move this module `tests` to its own directory -via `tests/mod.rs` -src/tests.rs:4 mod bar; - ^~~ -src/tests.rs:4:5: 4:8 note: ... or maybe `use` the module `bar` instead of -possibly redeclaring it -src/tests.rs:4 mod bar; - ^~~ +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 `modules`. ``` @@ -231,20 +247,22 @@ error: Could not compile `modules`. This error is actually pretty helpful. It points out something we didn't know that we could do yet: -> note: maybe move this module `tests` to its own directory via `tests/mod.rs` +> note: maybe move this module `network` to its own directory via +`network/mod.rs` -Here's the problem: in our case, we have different names for our modules: `foo` -and `tests::bar`. But what if we had `foo` and `tests::foo`? That's completely -valid, but then which module is `src/foo.rs` for? +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`? That's completely valid, but then which module is +`src/client.rs` for? So instead, we can do what the error suggests. We'll make a new directory, -move `src/bar.rs` into it, and change `src/tests.rs` to `src/tests/mod.rs`. +move `src/server.rs` into it, and change `src/network.rs` to `src/network/mod.rs`. Then, we try to build: ```bash -$ mkdir src/tests -$ mv src/bar.rs src/tests -$ mv src/tests.rs src/tests/mod.rs +$ mkdir src/network +$ mv src/server.rs src/network +$ mv src/network.rs src/network/mod.rs $ cargo build Compiling modules v0.1.0 (file:///home/steve/tmp/modules) diff --git a/src/ch07-03-controlling-visibility-with-pub.md b/src/ch07-03-controlling-visibility-with-pub.md index bd044bec4..d530c9927 100644 --- a/src/ch07-03-controlling-visibility-with-pub.md +++ b/src/ch07-03-controlling-visibility-with-pub.md @@ -5,53 +5,55 @@ looks like this: ```text modules - ├── foo - └── tests - └── bar + ├── client + └── network + └── server ``` When we compiled it, we got some strange warnings: ```bash Compiling modules v0.1.0 (file:///home/steve/tmp/modules) -src/foo.rs:1:1: 2:2 warning: function is never used: `it_works`, +src/client.rs:1:1: 2:2 warning: function is never used: `connect`, #[warn(dead_code)] on by default -src/foo.rs:1 fn it_works() { - ^ -src/tests/mod.rs:1:1: 2:2 warning: function is never used: `it_works`, +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/tests/mod.rs:1 fn it_works() { - ^ -src/tests/bar.rs:1:1: 2:2 warning: function is never used: `it_works`, +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/tests/bar.rs:1 fn it_works() { - ^ +src/network/server.rs:1 fn connect() { + ^ ``` 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? Well, let's try using them. Modify `src/lib.rs` to look like this: +Filename: src/lib.rs + ```rust,ignore fn try_me() { - foo::it_works(); + client::connect(); } -mod foo; +mod client; -mod tests; +mod network; ``` -Here, we make a new `try_me` function, which calls the `it_works` function from -the `foo` module, which is the first warning in our build output above. Invoking -`cargo build` will give us an error: +Here, we make a new `try_me` function, which calls the `connect` function from +the `client` module, which is the first warning in our build output above. +Invoking `cargo build` will give us an error: ```bash $ cargo build Compiling modules v0.1.0 (file:///home/steve/tmp/modules) -src/lib.rs:2:5: 2:18 error: function `it_works` is private -src/lib.rs:2 foo::it_works(); - ^~~~~~~~~~~~~ +src/lib.rs:2:5: 2:20 error: function `connect` is private +src/lib.rs:2 client::connect(); + ^~~~~~~~~~~~~~~ error: aborting due to previous error error: Could not compile `modules`. ``` @@ -63,11 +65,13 @@ all. This is the first time we've run into the concepts of 'public' and 'private' in the context of Rust. There's no keyword to make something private; that's the default state. To make something public, we need to introduce the `pub` keyword. -It goes before the declaration itself. Let's modify `src/foo.rs` to make -`it_works` public: +It goes before the declaration itself. Let's modify `src/client.rs` to make +`connect` public: + +Filename: src/client.rs ```rust -pub fn it_works() { +pub fn connect() { } ``` @@ -76,35 +80,33 @@ The `pub` goes right before `fn`. Let's try building again: ```bash $ cargo build Compiling modules v0.1.0 (file:///home/steve/tmp/modules) -src/lib.rs:1:1: 3:2 warning: function is never used: `try_me`, -#[warn(dead_code)] on by default +src/lib.rs:1:1: 3:2 warning: function is never used: `try_me`, #[warn(dead_code)] on by default src/lib.rs:1 fn try_me() { ^ -src/foo.rs:1:1: 2:2 warning: function is never used: `it_works`, -#[warn(dead_code)] on by default -src/foo.rs:1 pub fn it_works() { - ^ -src/tests/mod.rs:1:1: 2:2 warning: function is never used: `it_works`, -#[warn(dead_code)] on by default -src/tests/mod.rs:1 fn it_works() { - ^ -src/tests/bar.rs:1:1: 2:2 warning: function is never used: `it_works`, -#[warn(dead_code)] on by default -src/tests/bar.rs:1 fn it_works() { - ^ +src/client.rs:1:1: 2:2 warning: function is never used: `connect`, #[warn(dead_code)] on by default +src/client.rs:1 pub 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() { + ^ ``` It worked! But, funny enough, we forgot to make `try_me` public, so we've just added a new warning. Let's make `try_me` public in `src/lib.rs`: +Filename: src/lib.rs + ```rust,ignore pub fn try_me() { - foo::it_works(); + client::connect(); } -mod foo; +mod client; -mod tests; +mod network; ``` And try to build: @@ -112,24 +114,24 @@ And try to build: ```bash $ cargo build Compiling modules v0.1.0 (file:///home/steve/tmp/modules) -src/tests/mod.rs:1:1: 2:2 warning: function is never used: `it_works`, -#[warn(dead_code)] on by default -src/tests/mod.rs:1 fn it_works() { - ^ -src/tests/bar.rs:1:1: 2:2 warning: function is never used: `it_works`, -#[warn(dead_code)] on by default -src/tests/bar.rs:1 fn it_works() { - ^ +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() { + ^ ``` -Success! We now have two warnings. Let's try to get rid of that second one. -Modify `src/tests/mod.rs`: +Success! We now have two warnings. Let's try to get rid of the first one. +Modify `src/network/mod.rs`: + +Filename: src/network/mod.rs ```rust,ignore -pub fn it_works() { +pub fn connect() { } -mod bar; +mod server; ``` And compile: @@ -137,47 +139,48 @@ And compile: ```bash $ cargo build Compiling modules v0.1.0 (file:///home/steve/tmp/modules) -src/tests/mod.rs:1:1: 2:2 warning: function is never used: `it_works`, +src/network/mod.rs:1:1: 2:2 warning: function is never used: `connect`, #[warn(dead_code)] on by default -src/tests/mod.rs:1 pub fn it_works() { - ^ -src/tests/bar.rs:1:1: 2:2 warning: function is never used: `it_works`, +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/tests/bar.rs:1 fn it_works() { - ^ +src/network/server.rs:1 fn connect() { + ^ ``` Hmmm, it says this is still dead, even though it's `pub`. This is because, while the function is public, it's not totally public: the module it's in is not public. Let's change `src/lib.rs`: +Filename: src/lib.rs + ```rust,ignore pub fn try_me() { - foo::it_works(); + client::connect(); } -mod foo; +mod client; -pub mod tests; +pub mod network; ``` -Now, we're declaring the `tests` module public. Lo and behold, that warning +Now, we're declaring the `network` module public. Lo and behold, that warning is gone: ```bash $ cargo build Compiling modules v0.1.0 (file:///home/steve/tmp/modules) -src/tests/bar.rs:1:1: 2:2 warning: function is never used: `it_works`, -#[warn(dead_code)] on by default -src/tests/bar.rs:1 fn it_works() { - ^ +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() { + ^ ``` -Only one last warning! So wait, why did we need to make `tests` public, but not -`foo`? The answer lies in the way that they were used: in the first case, we're -calling `foo::it_works` from `try_me`. But since it's inside a private module, -anyone using our library couldn't call `foo::it_works()`. So why does it need to -be public? Let's take a deeper look at privacy. +Only one last warning! So wait, why did we need to make `network` public, but +not `client`? The answer lies in the way that they were used: in the first +case, we're calling `client::connect` from `try_me`. But since it's inside a +private module, anyone using our library couldn't call `client::connect()`. So +why does it need to be public? Let's take a deeper look at privacy. ## Privacy rules