From 1601df9a5bdf74b4c5b242c716d9bab47dc4c7a7 Mon Sep 17 00:00:00 2001 From: Steve Klabnik Date: Fri, 22 Jul 2016 17:06:39 -0400 Subject: [PATCH 01/40] Start modules _again_ --- src/SUMMARY.md | 5 +- src/ch07-01-modules.md | 16 ++ src/ch07-02-mod-and-the-filesystem.md | 251 ++++++++++++++++++ ...ch07-03-controlling-visibility-with-pub.md | 193 ++++++++++++++ src/ch07-04-importing-names-with-use.md | 1 + 5 files changed, 465 insertions(+), 1 deletion(-) create mode 100644 src/ch07-01-modules.md create mode 100644 src/ch07-02-mod-and-the-filesystem.md create mode 100644 src/ch07-03-controlling-visibility-with-pub.md create mode 100644 src/ch07-04-importing-names-with-use.md diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 60d306076..7b6d6cd91 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -26,7 +26,10 @@ - [Match](ch06-02-match.md) - [if let](ch06-03-if-let.md) -- [Crates & Modules]() +- [Modules](ch07-01-modules.md) + - [`mod` and the filesystem](ch07-02-mod-and-the-filesystem.md) + - [Controlling visibility with `pub`](ch07-03-controlling-visibility-with-pub.md) + - [Importing names with `use`](ch07-04-importing-names-with-use.md) - [Basic Collections]() - [Vectors]() diff --git a/src/ch07-01-modules.md b/src/ch07-01-modules.md new file mode 100644 index 000000000..4d3e58fb3 --- /dev/null +++ b/src/ch07-01-modules.md @@ -0,0 +1,16 @@ +# Modules + +When a project starts getting large, it’s considered good software engineering +practice to split it up into a bunch of smaller pieces, and then fit them +together. It’s also important to have a well-defined interface, so that some of +your functionality is private, and some is public. To facilitate these kinds of +things, Rust has a module system. It tackles both of these problems. Here's an +overview of how the bits fit together: + +* `mod` declares a new module. +* Everything starts off as private, but the `pub` keyword makes it public. +* The `use` keyword allows you to bring modules into scope so that it's easier + to refer to them. + +We'll take a look at each of these parts and see how they fit into the whole. +Then, we'll take a look at the standard library and its module layout. diff --git a/src/ch07-02-mod-and-the-filesystem.md b/src/ch07-02-mod-and-the-filesystem.md new file mode 100644 index 000000000..34492b21c --- /dev/null +++ b/src/ch07-02-mod-and-the-filesystem.md @@ -0,0 +1,251 @@ +# `mod` and the filesystem + +Every module in Rust starts with the `mod` keyword. Let's give it a try. Let's +make a new project with Cargo: + +```bash +$ cargo new modules +$ cd modules +``` + +If we examine the `src/lib.rs` that Cargo generated for us, we'll find this: + +```rust +#[cfg(test)] +mod tests { + #[test] + fn it_works() { + } +} +``` + +We haven't talked about the `#[]` stuff yet, so let's ignore those for now: + +```rust +mod tests { + fn it_works() { + } +} +``` + +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, +we would say `tests::it_works` rather than `it_works`. + +You could have multiple modules, side-by-side: + +```rust +mod tests { + fn it_works() { + } +} + +mod foo { + fn it_works() { + } +} +``` + +Now we have a `tests::it_works` function and a `foo::it_works` function. + +And you can put modules inside of modules: + +```rust +mod tests { + fn it_works() { + } + + mod foo { + fn it_works() { + } + } +} +``` + +This gives us `tests::it_works` and `tests::foo::it_works`. + +In this way, modules form a tree. The contents of `src/lib.rs` form a sort of +"anonymous module" that's at the root of the tree, with the submodules forming +the leaves. For example, here's what our first example looks like when thought +of this way: + +```text +modules + ├── foo + └── tests +``` + +And here's the second: + +```text +modules + └── tests + └── foo +``` + +More complicated projects can have a lot of modules. + +## Putting modules in another file + +So, 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: + +```rust +mod foo { + fn it_works() { + } +} + +mod tests { + fn it_works() { + } + + mod bar { + fn it_works() { + } + } +} +``` + +Let's extract the `foo` module into another file. First, we need to change our +source code: + +```rust,ignore +mod foo; + +mod tests { + fn it_works() { + } + + mod bar { + fn it_works() { + } + } +} +``` + +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: + +```rust +fn it_works() { +} +``` + +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`. If we put +a `mod foo` here, we'd be giving the `foo` module its own submodule named `foo`! + +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`, +#[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`, +#[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`, +#[warn(dead_code)] on by default +src/lib.rs:8 fn it_works() { + ^ +``` + +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: + +```rust,ignore +mod foo; + +mod tests; +``` + +And then put this in `src/tests.rs` + +```rust +fn it_works() { +} + +mod bar { + fn it_works() { + } +} +``` + +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: + +```rust,ignore +fn it_works() { +} + +mod bar; +``` + +Put this in `src/bar.rs` + +```rust +fn it_works() { +} +``` + +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; + ^~~ +error: aborting due to previous error +error: Could not compile `modules`. +``` + +This error is actually pretty helpful. It points out something we didn't know +yet: + +> note: maybe move this module `tests` to its own directory via `tests/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? + +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`. +Then, we try to build: + +```bash +$ mkdir src/tests +$ mv src/bar.rs src/tests +$ mv src/tests.rs src/tests/mod.rs +$ cargo build + Compiling modules v0.1.0 (file:///home/steve/tmp/modules) + +$ +``` + +It works! In summary, these are the two rules of modules with regards to files: + +* If a module named `foo` has no submodules, it will be named `foo.rs`. +* If a module named `foo` does have submodules, it will be named `foo/mod.rs`. + +Next, we'll talk about the `pub` keyword, and get rid of those warnings! diff --git a/src/ch07-03-controlling-visibility-with-pub.md b/src/ch07-03-controlling-visibility-with-pub.md new file mode 100644 index 000000000..6721f9a8c --- /dev/null +++ b/src/ch07-03-controlling-visibility-with-pub.md @@ -0,0 +1,193 @@ +# Controlling visibility with `pub` + +At the end of the last section, we had a project, `modules`, with a layout that +looks like this: + +```text +modules + ├── foo + └── tests + └── bar +``` + +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`, +#[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`, +#[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() { + ^ +``` + +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: + +```rust,ignore +fn try_me() { + foo::it_works(); +} + +mod foo; + +mod tests; +``` + +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: + +```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(); + ^~~~~~~~~~~~~ +error: aborting due to previous error +error: Could not compile `modules`. +``` + +Ah ha! This is why we're getting the warnings: because our functions were not +public, and none of our public functions called them, they weren't being used at +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: + +```rust +pub fn it_works() { +} +``` + +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 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() { + ^ +``` + +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`: + +```rust,ignore +pub fn try_me() { + foo::it_works(); +} + +mod foo; + +mod tests; +``` + +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() { + ^ +``` + +Success! We now have two warnings. Let's try to get rid of that second one. +Modify `src/tests/mod.rs`: + +```rust,ignore +pub fn it_works() { +} + +mod bar; +``` + +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`, +#[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`, +#[warn(dead_code)] on by default +src/tests/bar.rs:1 fn it_works() { + ^ +``` + +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`: + +```rust,ignore +pub fn try_me() { + foo::it_works(); +} + +mod foo; + +pub mod tests; +``` + +Now, we're declaring the `tests` module public. Lo and behold, our 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() { + ^ +``` + +Only one last error! 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. + +## Privacy rules + +FIXME: these are the rules: + +* If an item is public, then it can be used externally through any of its + parent modules. +* If an item is private, it may be accessed by the current module and its + submodules. + + +but describing them in a human way is very hard. + diff --git a/src/ch07-04-importing-names-with-use.md b/src/ch07-04-importing-names-with-use.md new file mode 100644 index 000000000..089c7a8b9 --- /dev/null +++ b/src/ch07-04-importing-names-with-use.md @@ -0,0 +1 @@ +# Importing names with `use` From e3d1e37c5b9c8ba3485fdca53e756ac714eb4f2e Mon Sep 17 00:00:00 2001 From: Steve Klabnik Date: Tue, 2 Aug 2016 17:37:20 -0400 Subject: [PATCH 02/40] Add use --- src/ch07-04-importing-names-with-use.md | 121 ++++++++++++++++++++++++ 1 file changed, 121 insertions(+) diff --git a/src/ch07-04-importing-names-with-use.md b/src/ch07-04-importing-names-with-use.md index 089c7a8b9..d94f3448e 100644 --- a/src/ch07-04-importing-names-with-use.md +++ b/src/ch07-04-importing-names-with-use.md @@ -1 +1,122 @@ # Importing names with `use` + +We've seen how we can call functions defined within a module by using the +module name as part of the call, like this: + +```rust +pub mod a { + pub mod series { + pub mod of { + pub fn namespaces() {} + } + } +} + +fn main() { + a::series::of::namespaces(); +} +``` + +However, referring to the fully qualified name can get quite lengthy, as we see +in that example. To solve this issue, Rust has a keyword, `use`. It works like +this: + +```rust +pub mod a { + pub mod series { + pub mod of { + pub fn namespaces() {} + } + } +} + +use a::series::of; + +fn main() { + of::namespaces(); +} +``` + +We can `use` a module, and that will bring its name into scope. This allows us +to shorten our function call, only requiring us to type the final module name, +not the entire chain of them. `use` is quite powerful, and can bring all kinds +of things into scope. For example, we could `use` the function itself: + +```rust +pub mod a { + pub mod series { + pub mod of { + pub fn namespaces() {} + } + } +} + +use a::series::of::namespaces; + +fn main() { + namespaces(); +} +``` + +Enums also form this kind of namespace; we can use `use` on those as well: + +```rust +enum TrafficLight { + Red, + Yellow, + Green, +} + +use TrafficLight::Red; +use TrafficLight::Yellow; + +fn main() { + let red = Red; + let yellow = Yellow; + let green = TrafficLight::Green; // because we didn't use TrafficLight::Green +} +``` + +## Glob imports with `*` + +If you'd like to import many names at once, you can use `*` to do so: + +```rust +enum TrafficLight { + Red, + Yellow, + Green, +} + +use TrafficLight::*; + +fn main() { + let red = Red; + let yellow = Yellow; + let green = Green; +} +``` + +The `*` is called a 'glob', and it will import everything that's public inside +of the module. Globs should be used sparingly; they are convenient, but you +might also pull in more things than you expected, causing naming conflicts. + +## Re-exports with `pub use` + +Finally, you can combine the `pub` keyword with `use` to 're-export' something: + +```rust +mod a { + mod namespace { + pub fn function() {} + } +} + +pub use a::namespace::function; +``` + +Here, the `a` and `namespace` modules are not public, so users of our library +couldn't call `a::namespace` themselves. However, since we've `pub use`'d +`function`, it will be public. Users can just call `function` themselves, +directly. This allows us to organize our code internally however we'd like, +while presenting a different external interface. From 816bf59c045912db2a131cde22efc5e765b34822 Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Sat, 6 Aug 2016 15:46:52 -0400 Subject: [PATCH 03/40] Small wording, punctuation, etc edits --- src/ch07-01-modules.md | 4 ++-- src/ch07-02-mod-and-the-filesystem.md | 10 +++++----- src/ch07-03-controlling-visibility-with-pub.md | 6 +++--- src/ch07-04-importing-names-with-use.md | 4 ++-- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/ch07-01-modules.md b/src/ch07-01-modules.md index 4d3e58fb3..613a1df1b 100644 --- a/src/ch07-01-modules.md +++ b/src/ch07-01-modules.md @@ -1,9 +1,9 @@ # Modules When a project starts getting large, it’s considered good software engineering -practice to split it up into a bunch of smaller pieces, and then fit them +practice to split it up into a bunch of smaller pieces and then fit them together. It’s also important to have a well-defined interface, so that some of -your functionality is private, and some is public. To facilitate these kinds of +your functionality is private and some is public. To facilitate these kinds of things, Rust has a module system. It tackles both of these problems. Here's an overview of how the bits fit together: diff --git a/src/ch07-02-mod-and-the-filesystem.md b/src/ch07-02-mod-and-the-filesystem.md index 34492b21c..e8bf8bfad 100644 --- a/src/ch07-02-mod-and-the-filesystem.md +++ b/src/ch07-02-mod-and-the-filesystem.md @@ -1,7 +1,7 @@ # `mod` and the filesystem -Every module in Rust starts with the `mod` keyword. Let's give it a try. Let's -make a new project with Cargo: +Every module in Rust starts with the `mod` keyword. Let's give it a try by +making a new project with Cargo: ```bash $ cargo new modules @@ -89,7 +89,7 @@ More complicated projects can have a lot of modules. ## Putting modules in another file -So, modules form a hierarchical, tree-like structure. So does another thing: +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: @@ -111,7 +111,7 @@ mod tests { ``` Let's extract the `foo` module into another file. First, we need to change our -source code: +code in `src/lib.rs`: ```rust,ignore mod foo; @@ -221,7 +221,7 @@ error: Could not compile `modules`. ``` This error is actually pretty helpful. It points out something we didn't know -yet: +that we could do yet: > note: maybe move this module `tests` to its own directory via `tests/mod.rs` diff --git a/src/ch07-03-controlling-visibility-with-pub.md b/src/ch07-03-controlling-visibility-with-pub.md index 6721f9a8c..bd044bec4 100644 --- a/src/ch07-03-controlling-visibility-with-pub.md +++ b/src/ch07-03-controlling-visibility-with-pub.md @@ -127,7 +127,7 @@ Modify `src/tests/mod.rs`: ```rust,ignore pub fn it_works() { -} +} mod bar; ``` @@ -161,7 +161,7 @@ mod foo; pub mod tests; ``` -Now, we're declaring the `tests` module public. Lo and behold, our warning +Now, we're declaring the `tests` module public. Lo and behold, that warning is gone: ```bash @@ -173,7 +173,7 @@ src/tests/bar.rs:1 fn it_works() { ^ ``` -Only one last error! So wait, why did we need to make `tests` public, but not +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 diff --git a/src/ch07-04-importing-names-with-use.md b/src/ch07-04-importing-names-with-use.md index d94f3448e..db00e318b 100644 --- a/src/ch07-04-importing-names-with-use.md +++ b/src/ch07-04-importing-names-with-use.md @@ -39,7 +39,7 @@ fn main() { We can `use` a module, and that will bring its name into scope. This allows us to shorten our function call, only requiring us to type the final module name, -not the entire chain of them. `use` is quite powerful, and can bring all kinds +not the entire chain of them. `use` is quite powerful and can bring all kinds of things into scope. For example, we could `use` the function itself: ```rust @@ -98,7 +98,7 @@ fn main() { ``` The `*` is called a 'glob', and it will import everything that's public inside -of the module. Globs should be used sparingly; they are convenient, but you +of the module. Globs should be used sparingly: they are convenient, but you might also pull in more things than you expected, causing naming conflicts. ## Re-exports with `pub use` From a5b42c6dc9811137909fd0a361c6b9bb71fe91c4 Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Sat, 6 Aug 2016 15:47:17 -0400 Subject: [PATCH 04/40] Remove a vague part about "things"; next sentence is better --- src/ch07-01-modules.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/ch07-01-modules.md b/src/ch07-01-modules.md index 613a1df1b..ed9e7250a 100644 --- a/src/ch07-01-modules.md +++ b/src/ch07-01-modules.md @@ -3,9 +3,8 @@ When a project starts getting large, it’s considered good software engineering practice to split it up into a bunch of smaller pieces and then fit them together. It’s also important to have a well-defined interface, so that some of -your functionality is private and some is public. To facilitate these kinds of -things, Rust has a module system. It tackles both of these problems. Here's an -overview of how the bits fit together: +your functionality is private and some is public. Rust has a module system that +tackles both of these problems. Here's an overview of how the bits fit together: * `mod` declares a new module. * Everything starts off as private, but the `pub` keyword makes it public. From 926d5bf37f33f1b96a98905ffc062027c5f93c21 Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Mon, 8 Aug 2016 18:05:25 -0400 Subject: [PATCH 05/40] Take aturon's suggestions about what we're bringing into scope --- src/ch07-01-modules.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ch07-01-modules.md b/src/ch07-01-modules.md index ed9e7250a..a196ad726 100644 --- a/src/ch07-01-modules.md +++ b/src/ch07-01-modules.md @@ -8,8 +8,8 @@ tackles both of these problems. Here's an overview of how the bits fit together: * `mod` declares a new module. * Everything starts off as private, but the `pub` keyword makes it public. -* The `use` keyword allows you to bring modules into scope so that it's easier - to refer to them. +* The `use` keyword allows you to bring modules, or definitions inside of them, + into scope so that it's easier to refer to them. We'll take a look at each of these parts and see how they fit into the whole. Then, we'll take a look at the standard library and its module layout. From dd0fdcd2dc5e0382eb756a18d980b6634d8fc1d8 Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Mon, 8 Aug 2016 18:11:48 -0400 Subject: [PATCH 06/40] Explain libraries vs binaries a bit since this is our first lib --- src/ch07-02-mod-and-the-filesystem.md | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/ch07-02-mod-and-the-filesystem.md b/src/ch07-02-mod-and-the-filesystem.md index e8bf8bfad..1f2fe06e6 100644 --- a/src/ch07-02-mod-and-the-filesystem.md +++ b/src/ch07-02-mod-and-the-filesystem.md @@ -1,14 +1,19 @@ # `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: +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: ```bash $ cargo new modules $ cd modules ``` -If we examine the `src/lib.rs` that Cargo generated for us, we'll find this: +You'll notice that Cargo generated `src/lib.rs` instead of `src/main.rs` for +us, and inside it we'll find this: ```rust #[cfg(test)] @@ -19,7 +24,9 @@ mod tests { } ``` -We haven't talked about the `#[]` stuff yet, so let's ignore those for now: +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: ```rust mod tests { From 89541a164904d9f8b1406a9350f8c78672c8a949 Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Mon, 8 Aug 2016 18:13:28 -0400 Subject: [PATCH 07/40] Clarify where we'd have to use the full namespaced fn --- src/ch07-02-mod-and-the-filesystem.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/ch07-02-mod-and-the-filesystem.md b/src/ch07-02-mod-and-the-filesystem.md index 1f2fe06e6..5c237fe2e 100644 --- a/src/ch07-02-mod-and-the-filesystem.md +++ b/src/ch07-02-mod-and-the-filesystem.md @@ -35,11 +35,12 @@ mod tests { } ``` -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, -we would say `tests::it_works` rather than `it_works`. +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`. You could have multiple modules, side-by-side: From 6aba5d834bdcc457636e6509f91ea9018907af05 Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Mon, 8 Aug 2016 18:20:13 -0400 Subject: [PATCH 08/40] Remove the mention of "anonymous module" that confused me --- src/ch07-02-mod-and-the-filesystem.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/ch07-02-mod-and-the-filesystem.md b/src/ch07-02-mod-and-the-filesystem.md index 5c237fe2e..040b5cb18 100644 --- a/src/ch07-02-mod-and-the-filesystem.md +++ b/src/ch07-02-mod-and-the-filesystem.md @@ -74,10 +74,9 @@ mod tests { This gives us `tests::it_works` and `tests::foo::it_works`. -In this way, modules form a tree. The contents of `src/lib.rs` form a sort of -"anonymous module" that's at the root of the tree, with the submodules forming -the leaves. For example, here's what our first example looks like when thought -of this way: +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: ```text modules From a8bf2acd4650c3e11bdb3292b3f0a40fd3f8b2c8 Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Mon, 8 Aug 2016 18:22:30 -0400 Subject: [PATCH 09/40] Take aturon's suggestion about the contents of the foo module --- src/ch07-02-mod-and-the-filesystem.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/ch07-02-mod-and-the-filesystem.md b/src/ch07-02-mod-and-the-filesystem.md index 040b5cb18..c4d2499b5 100644 --- a/src/ch07-02-mod-and-the-filesystem.md +++ b/src/ch07-02-mod-and-the-filesystem.md @@ -143,9 +143,10 @@ fn it_works() { } ``` -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`. If we put -a `mod foo` here, we'd be giving the `foo` module its own submodule named `foo`! +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`! Now, everything should compile: From f94d402f2d9d3fd683de83078d31125bd93265e0 Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Mon, 22 Aug 2016 15:27:06 -0400 Subject: [PATCH 10/40] Add an explicit definition of what a module is Felt like this was missing just now, but not really the first time I read this. Strange. --- src/ch07-01-modules.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/ch07-01-modules.md b/src/ch07-01-modules.md index a196ad726..e60939ad5 100644 --- a/src/ch07-01-modules.md +++ b/src/ch07-01-modules.md @@ -4,7 +4,9 @@ When a project starts getting large, it’s considered good software engineering practice to split it up into a bunch of smaller pieces and then fit them together. It’s also important to have a well-defined interface, so that some of your functionality is private and some is public. Rust has a module system that -tackles both of these problems. Here's an overview of how the bits fit together: +tackles both of these problems. 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: * `mod` declares a new module. * Everything starts off as private, but the `pub` keyword makes it public. From 68f465bbc33c9b98ded1fb4500732f171c8e2d57 Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Mon, 22 Aug 2016 16:15:00 -0400 Subject: [PATCH 11/40] 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 From 84ba655cd9931dc438950f5310a0ae6604f6888e Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Mon, 22 Aug 2016 16:18:50 -0400 Subject: [PATCH 12/40] Make compilation messages use the projects dir --- src/ch07-02-mod-and-the-filesystem.md | 6 +++--- src/ch07-03-controlling-visibility-with-pub.md | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/ch07-02-mod-and-the-filesystem.md b/src/ch07-02-mod-and-the-filesystem.md index fcb29ed69..246bf6587 100644 --- a/src/ch07-02-mod-and-the-filesystem.md +++ b/src/ch07-02-mod-and-the-filesystem.md @@ -162,7 +162,7 @@ Now, everything should compile: ```bash $ cargo build - Compiling modules v0.1.0 (file:///home/steve/tmp/modules) + Compiling modules v0.1.0 (file:///projects/modules) src/client.rs:1:1: 2:2 warning: function is never used: `connect`, #[warn(dead_code)] on by default @@ -230,7 +230,7 @@ When we try to `cargo build`, we'll get an error: ```bash $ cargo build - Compiling modules v0.1.0 (file:///home/steve/tmp/modules) + Compiling modules v0.1.0 (file:///projects/modules) src/network.rs:4:5: 4:11 error: cannot declare a new module at this location src/network.rs:4 mod server; ^~~~~~ @@ -264,7 +264,7 @@ $ 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) + Compiling modules v0.1.0 (file:///projects/modules) $ ``` diff --git a/src/ch07-03-controlling-visibility-with-pub.md b/src/ch07-03-controlling-visibility-with-pub.md index d530c9927..8ddd2e46c 100644 --- a/src/ch07-03-controlling-visibility-with-pub.md +++ b/src/ch07-03-controlling-visibility-with-pub.md @@ -13,7 +13,7 @@ modules When we compiled it, we got some strange warnings: ```bash - Compiling modules v0.1.0 (file:///home/steve/tmp/modules) + Compiling modules v0.1.0 (file:///projects/modules) 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() { @@ -50,7 +50,7 @@ Invoking `cargo build` will give us an error: ```bash $ cargo build - Compiling modules v0.1.0 (file:///home/steve/tmp/modules) + Compiling modules v0.1.0 (file:///projects/modules) src/lib.rs:2:5: 2:20 error: function `connect` is private src/lib.rs:2 client::connect(); ^~~~~~~~~~~~~~~ @@ -79,7 +79,7 @@ The `pub` goes right before `fn`. Let's try building again: ```bash $ cargo build - Compiling modules v0.1.0 (file:///home/steve/tmp/modules) + Compiling modules v0.1.0 (file:///projects/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 fn try_me() { ^ @@ -113,7 +113,7 @@ And try to build: ```bash $ cargo build - Compiling modules v0.1.0 (file:///home/steve/tmp/modules) + Compiling modules v0.1.0 (file:///projects/modules) 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() { ^ @@ -138,7 +138,7 @@ And compile: ```bash $ cargo build - Compiling modules v0.1.0 (file:///home/steve/tmp/modules) + Compiling modules v0.1.0 (file:///projects/modules) 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() { @@ -170,7 +170,7 @@ is gone: ```bash $ cargo build - Compiling modules v0.1.0 (file:///home/steve/tmp/modules) + Compiling modules v0.1.0 (file:///projects/modules) 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() { ^ From 7dc4e78ba7aa0438442bd7de77dca0bf8b17950b Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Mon, 22 Aug 2016 16:44:19 -0400 Subject: [PATCH 13/40] Clear up the module rules --- src/ch07-02-mod-and-the-filesystem.md | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/ch07-02-mod-and-the-filesystem.md b/src/ch07-02-mod-and-the-filesystem.md index 246bf6587..dbbf6e35f 100644 --- a/src/ch07-02-mod-and-the-filesystem.md +++ b/src/ch07-02-mod-and-the-filesystem.md @@ -269,9 +269,23 @@ $ cargo build $ ``` -It works! In summary, these are the two rules of modules with regards to files: +It works! In summary, these are the rules of modules with regards to files: -* If a module named `foo` has no submodules, it will be named `foo.rs`. -* If a module named `foo` does have submodules, it will be named `foo/mod.rs`. +* If a module named `foo` has no submodules, you should put the declarations in + the `foo` module in a file named `foo.rs`. +* If a module named `foo` does have submodules, you should put the declarations + for `foo` in a file named `foo/mod.rs`. +* The first two rules apply recursively, so that if a module named `foo` has a + submodule named `bar` and `bar` does not have submodules, you should have the + following files in your `src` directory: + + ``` + ├── foo + │   ├── bar.rs (contains the declarations in `foo::bar`) + │   └── mod.rs (contains the declarations in `foo`, including `mod bar`) + ``` + +* The modules themselves should be declared in their parent module's file using + the `mod` keyword. Next, we'll talk about the `pub` keyword, and get rid of those warnings! From 14adf56e8aa5b547024f5c10a8bb9246ba1cf4a0 Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Mon, 22 Aug 2016 16:45:49 -0400 Subject: [PATCH 14/40] Oops, hard wrap --- src/ch07-02-mod-and-the-filesystem.md | 14 +++++++++---- ...ch07-03-controlling-visibility-with-pub.md | 21 ++++++++++++------- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/ch07-02-mod-and-the-filesystem.md b/src/ch07-02-mod-and-the-filesystem.md index dbbf6e35f..27d8293f1 100644 --- a/src/ch07-02-mod-and-the-filesystem.md +++ b/src/ch07-02-mod-and-the-filesystem.md @@ -25,7 +25,10 @@ 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 `src/lib.rs`. Let's imagine that we're creating a library to provide networking functionality. At the beginning of the file, add: +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 @@ -206,7 +209,8 @@ mod server { ``` And then run `cargo build` again. Success! We have one more module to extract: -`server`. Unfortunately, our current tactic won't work. Let's try it anyway. Modify `src/network.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 @@ -234,10 +238,12 @@ $ cargo build 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: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: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 diff --git a/src/ch07-03-controlling-visibility-with-pub.md b/src/ch07-03-controlling-visibility-with-pub.md index 8ddd2e46c..270836564 100644 --- a/src/ch07-03-controlling-visibility-with-pub.md +++ b/src/ch07-03-controlling-visibility-with-pub.md @@ -80,16 +80,20 @@ The `pub` goes right before `fn`. Let's try building again: ```bash $ cargo build Compiling modules v0.1.0 (file:///projects/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/client.rs:1:1: 2:2 warning: function is never used: `connect`, #[warn(dead_code)] on by default +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: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:1: 2:2 warning: function is never used: `connect`, +#[warn(dead_code)] on by default src/network/server.rs:1 fn connect() { ^ ``` @@ -114,10 +118,12 @@ And try to build: ```bash $ cargo build Compiling modules v0.1.0 (file:///projects/modules) -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: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:1: 2:2 warning: function is never used: `connect`, +#[warn(dead_code)] on by default src/network/server.rs:1 fn connect() { ^ ``` @@ -171,7 +177,8 @@ is gone: ```bash $ cargo build Compiling modules v0.1.0 (file:///projects/modules) -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:1: 2:2 warning: function is never used: `connect`, +#[warn(dead_code)] on by default src/network/server.rs:1 fn connect() { ^ ``` From e427b5f3a99c3b2d6611eafb59e65d14988d6017 Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Tue, 23 Aug 2016 09:55:26 -0400 Subject: [PATCH 15/40] Make the tree output text so it doesn't get tested as rust --- src/ch07-02-mod-and-the-filesystem.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ch07-02-mod-and-the-filesystem.md b/src/ch07-02-mod-and-the-filesystem.md index 27d8293f1..832b23bf8 100644 --- a/src/ch07-02-mod-and-the-filesystem.md +++ b/src/ch07-02-mod-and-the-filesystem.md @@ -285,7 +285,7 @@ It works! In summary, these are the rules of modules with regards to files: submodule named `bar` and `bar` does not have submodules, you should have the following files in your `src` directory: - ``` + ```text ├── foo │   ├── bar.rs (contains the declarations in `foo::bar`) │   └── mod.rs (contains the declarations in `foo`, including `mod bar`) From 40f08bba94887bfb0a9851b1072a68ae2c3d5fc6 Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Tue, 23 Aug 2016 13:17:36 -0400 Subject: [PATCH 16/40] Encourage curly braces and lists for importing multiple things This is more common than multiple lines or globs, so we should encourage it. --- src/ch07-04-importing-names-with-use.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/ch07-04-importing-names-with-use.md b/src/ch07-04-importing-names-with-use.md index db00e318b..1bb357b54 100644 --- a/src/ch07-04-importing-names-with-use.md +++ b/src/ch07-04-importing-names-with-use.md @@ -58,7 +58,10 @@ fn main() { } ``` -Enums also form this kind of namespace; we can use `use` on those as well: +Enums also form this kind of namespace; we can import an enum's variants with +`use` as well. For any kind of `use` statement, if you are importing multiple +items from one namespace, you can list them using curly braces and commas in +the last position, like so: ```rust enum TrafficLight { @@ -67,8 +70,7 @@ enum TrafficLight { Green, } -use TrafficLight::Red; -use TrafficLight::Yellow; +use TrafficLight::{Red, Yellow}; fn main() { let red = Red; From f0400e75d6e470bfd8c1a456b3a6292b6b06ec89 Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Tue, 23 Aug 2016 13:18:59 -0400 Subject: [PATCH 17/40] Clarify that it's not "many", it's "all" that glob imports --- src/ch07-04-importing-names-with-use.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ch07-04-importing-names-with-use.md b/src/ch07-04-importing-names-with-use.md index 1bb357b54..1e51d6a29 100644 --- a/src/ch07-04-importing-names-with-use.md +++ b/src/ch07-04-importing-names-with-use.md @@ -81,7 +81,7 @@ fn main() { ## Glob imports with `*` -If you'd like to import many names at once, you can use `*` to do so: +If you'd like to import all the items in a namespace at once, you can use `*`: ```rust enum TrafficLight { From 4bab40d45e4c4327d58692a59d591d7004dae75a Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Tue, 23 Aug 2016 13:19:55 -0400 Subject: [PATCH 18/40] Add an example of a naming conflict caused by glob import --- src/ch07-04-importing-names-with-use.md | 46 +++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/src/ch07-04-importing-names-with-use.md b/src/ch07-04-importing-names-with-use.md index 1e51d6a29..8d9cc967c 100644 --- a/src/ch07-04-importing-names-with-use.md +++ b/src/ch07-04-importing-names-with-use.md @@ -99,9 +99,49 @@ fn main() { } ``` -The `*` is called a 'glob', and it will import everything that's public inside -of the module. Globs should be used sparingly: they are convenient, but you -might also pull in more things than you expected, causing naming conflicts. +The `*` is called a 'glob', and it will import everything that's visible inside +of the namespace. Globs should be used sparingly: they are convenient, but you +might also pull in more things than you expected and cause naming conflicts. + +For example, in this program, imagine that `TrafficLight` is actually coming +from an external `traffic_light` crate we're depending on. We could also happen +to define our own enum named `Blue` that looks like: + +```rust +enum Blue { + Cyan, + Navy, + Royal, +} +``` + +If the traffic_light crate released a new major version and we decided to +upgrade, the crate could have added `Blue` to its `TrafficLight` variants: + +```rust +enum TrafficLight { + Red, + Yellow, + Green, + Blue, +} +``` + +Since we have imported all the names in `TrafficLight` using `*`, we would now +have a name conflict in our code and just upgrading would cause our crate to no +longer compile: + +```bash +src/main.rs:10:1: 14:2 error: a type named `Blue` has already been imported in this module [E0255] +src/main.rs:10 enum Blue { + ^ +src/main.rs:8:5: 8:21 note: previous import of `Blue` here +src/main.rs:8 use TrafficLight::*; + ^~~~~~~~~~~~~~~~ +``` + +If, instead, we were explicit about the variants we wanted to import, our code +would not have stopped compiling when we upgraded the `traffic_light` crate. ## Re-exports with `pub use` From 56f69991cfaaa23ca7cf0aa9641ff8c13811a630 Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Tue, 23 Aug 2016 13:37:53 -0400 Subject: [PATCH 19/40] Move `pub use` discussion to the library chapter Maybe we should think about a separate book about creating a library ;) --- src/SUMMARY.md | 1 + src/ch07-04-importing-names-with-use.md | 20 ----------------- src/chYY-YY-public-api.md | 30 +++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 20 deletions(-) create mode 100644 src/chYY-YY-public-api.md diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 7b6d6cd91..e90d3716d 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -86,6 +86,7 @@ - [Creating a Library]() - [Cargo]() - [Crates.io]() + - [Organizing your Public API](chYY-YY-public-api.md) - [Documentation](chYY-YY-documentation.md) - [Workspaces and Multiple Related Crates](chYY-YY-workspaces.md) diff --git a/src/ch07-04-importing-names-with-use.md b/src/ch07-04-importing-names-with-use.md index 8d9cc967c..9b1f91f9c 100644 --- a/src/ch07-04-importing-names-with-use.md +++ b/src/ch07-04-importing-names-with-use.md @@ -142,23 +142,3 @@ src/main.rs:8 use TrafficLight::*; If, instead, we were explicit about the variants we wanted to import, our code would not have stopped compiling when we upgraded the `traffic_light` crate. - -## Re-exports with `pub use` - -Finally, you can combine the `pub` keyword with `use` to 're-export' something: - -```rust -mod a { - mod namespace { - pub fn function() {} - } -} - -pub use a::namespace::function; -``` - -Here, the `a` and `namespace` modules are not public, so users of our library -couldn't call `a::namespace` themselves. However, since we've `pub use`'d -`function`, it will be public. Users can just call `function` themselves, -directly. This allows us to organize our code internally however we'd like, -while presenting a different external interface. diff --git a/src/chYY-YY-public-api.md b/src/chYY-YY-public-api.md new file mode 100644 index 000000000..4b7228b58 --- /dev/null +++ b/src/chYY-YY-public-api.md @@ -0,0 +1,30 @@ +## Organizing your Public API + +Here, we will describe the properties that a good public API in Rust has in +order to be pleasant for others to use. + +### Re-exports with `pub use` + +If your internal code organization doesn't match your desired public API +organization, you can re-export items in the way you want them to be organized. + +To re-export a name, combine the `pub` keyword with `use`: + +```rust +# fn main() {} # this doesn't work if it's inside main +mod a { + pub mod namespace { + pub fn function() {} + } +} + +pub use a::namespace::function; +``` + +Here, the `a` module is not public to users of our library, so neither are its +children, even though `namespace` and `function` are public *within* our +library. So users of our library couldn't call `a::namespace::function()` +themselves. However, since we've re-exported `function()` with `pub use`, +`function()` will be public. Users can just call `function()` themselves, +directly. This allows us to organize our code internally however we'd like, +while presenting a different external interface. From dce64eb42bc607ad4cfd9fa2363dc87d75c24e0b Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Tue, 23 Aug 2016 13:48:35 -0400 Subject: [PATCH 20/40] Remove unfulfilled foreshadowing --- src/ch07-01-modules.md | 1 - 1 file changed, 1 deletion(-) diff --git a/src/ch07-01-modules.md b/src/ch07-01-modules.md index e60939ad5..055da56f8 100644 --- a/src/ch07-01-modules.md +++ b/src/ch07-01-modules.md @@ -14,4 +14,3 @@ their module or not. Here's an overview of how the bits fit together: into scope so that it's easier to refer to them. We'll take a look at each of these parts and see how they fit into the whole. -Then, we'll take a look at the standard library and its module layout. From 96271174644d63cbb26e59ba8e926fbd542dd92a Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Tue, 23 Aug 2016 14:02:42 -0400 Subject: [PATCH 21/40] Use parens with functions in prose --- src/ch07-02-mod-and-the-filesystem.md | 10 +++++----- src/ch07-03-controlling-visibility-with-pub.md | 12 ++++++------ 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/ch07-02-mod-and-the-filesystem.md b/src/ch07-02-mod-and-the-filesystem.md index 832b23bf8..5066cdbeb 100644 --- a/src/ch07-02-mod-and-the-filesystem.md +++ b/src/ch07-02-mod-and-the-filesystem.md @@ -42,9 +42,9 @@ mod network { 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 `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`. +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. For example, if you wanted a `client` module: @@ -61,7 +61,7 @@ mod client { } ``` -Now we have a `network::connect` function and a `client::connect` function. +Now we have a `network::connect()` function and a `client::connect()` function. And you can put modules inside of modules, if you wanted to have `client` to be within `network`: @@ -78,7 +78,7 @@ mod network { } ``` -This gives us `network::connect` and `network::client::connect`. +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 diff --git a/src/ch07-03-controlling-visibility-with-pub.md b/src/ch07-03-controlling-visibility-with-pub.md index 270836564..0acd089a2 100644 --- a/src/ch07-03-controlling-visibility-with-pub.md +++ b/src/ch07-03-controlling-visibility-with-pub.md @@ -44,8 +44,8 @@ mod client; mod network; ``` -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. +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 @@ -66,7 +66,7 @@ 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/client.rs` to make -`connect` public: +`connect()` public: Filename: src/client.rs @@ -98,8 +98,8 @@ 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`: +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 @@ -185,7 +185,7 @@ src/network/server.rs:1 fn connect() { 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 +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. From 31863c35495b37849540b4f713387be868fe1e79 Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Tue, 23 Aug 2016 14:03:03 -0400 Subject: [PATCH 22/40] Set up the example context a bit better --- src/ch07-02-mod-and-the-filesystem.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/ch07-02-mod-and-the-filesystem.md b/src/ch07-02-mod-and-the-filesystem.md index 5066cdbeb..e1a89a212 100644 --- a/src/ch07-02-mod-and-the-filesystem.md +++ b/src/ch07-02-mod-and-the-filesystem.md @@ -27,8 +27,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 -`src/lib.rs`. Let's imagine that we're creating a library to provide networking -functionality. At the beginning of the file, add: +`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: Filename: src/lib.rs From b73f3d1794f03ec14f8b15e971f03c5a610772af Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Tue, 23 Aug 2016 14:03:33 -0400 Subject: [PATCH 23/40] Some small wording edits --- src/ch07-02-mod-and-the-filesystem.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ch07-02-mod-and-the-filesystem.md b/src/ch07-02-mod-and-the-filesystem.md index e1a89a212..984479b9e 100644 --- a/src/ch07-02-mod-and-the-filesystem.md +++ b/src/ch07-02-mod-and-the-filesystem.md @@ -67,7 +67,7 @@ mod client { Now we have a `network::connect()` function and a `client::connect()` function. -And you can put modules inside of modules, if you wanted to have `client` to be +And you can put modules inside of modules. If you wanted to have `client` be within `network`: ```rust @@ -262,7 +262,7 @@ that we could do yet: 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 +`network::client`? That's completely valid, but then which module is the file `src/client.rs` for? So instead, we can do what the error suggests. We'll make a new directory, From 4835474c8294d120978f1301ab2ac2f1dcfe8bf7 Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Tue, 23 Aug 2016 16:04:04 -0400 Subject: [PATCH 24/40] Oops, this comment needs to be a rust comment --- src/chYY-YY-public-api.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/chYY-YY-public-api.md b/src/chYY-YY-public-api.md index 4b7228b58..b5261de47 100644 --- a/src/chYY-YY-public-api.md +++ b/src/chYY-YY-public-api.md @@ -11,7 +11,7 @@ organization, you can re-export items in the way you want them to be organized. To re-export a name, combine the `pub` keyword with `use`: ```rust -# fn main() {} # this doesn't work if it's inside main +# fn main() {} // this ex needs to be outside main mod a { pub mod namespace { pub fn function() {} From 0705ea8eff8e8bdb187d55f2e71cc361b8618f55 Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Tue, 23 Aug 2016 16:04:35 -0400 Subject: [PATCH 25/40] Work through adding `pub` because of warnings in a different way I felt like the previous version was a bit vague and left the impression that dead code warnings should be fixed by adding `pub` everywhere. What I think it was *trying* to say was that because we WANT these functions to be public for external folks to use, THAT'S why we're getting the warnings and why pub is the fix for it. --- ...ch07-03-controlling-visibility-with-pub.md | 240 +++++++++++++----- 1 file changed, 172 insertions(+), 68 deletions(-) diff --git a/src/ch07-03-controlling-visibility-with-pub.md b/src/ch07-03-controlling-visibility-with-pub.md index 0acd089a2..7f4a046a4 100644 --- a/src/ch07-03-controlling-visibility-with-pub.md +++ b/src/ch07-03-controlling-visibility-with-pub.md @@ -30,63 +30,37 @@ 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: +try using them as if we were another project using our library. Create a +`src/main.rs` file with this code: -Filename: src/lib.rs +Filename: src/main.rs ```rust,ignore -fn try_me() { - client::connect(); +extern crate modules; + +fn main() { + modules::client::connect(); } - -mod client; - -mod network; ``` -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: +We need the `extern crate` line to bring our `modules` 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 +functionality is in a library crate, and the executable crate uses that +library. This way, other programs can also use the library crate, and it’s also +a nice separation of concerns. + +Our binary crate right now just calls our library's `connect()` function from +the `client` module; we picked that one since it's the first warning in our +build output above. Invoking `cargo build` will now give us an error: ```bash $ cargo build Compiling modules v0.1.0 (file:///projects/modules) -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`. -``` - -Ah ha! This is why we're getting the warnings: because our functions were not -public, and none of our public functions called them, they weren't being used at -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/client.rs` to make -`connect()` public: - -Filename: src/client.rs - -```rust -pub fn connect() { -} -``` - -The `pub` goes right before `fn`. Let's try building again: - -```bash -$ cargo build - Compiling modules v0.1.0 (file:///projects/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 fn try_me() { - ^ 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/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 @@ -96,28 +70,72 @@ 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() { ^ +src/main.rs:4:5: 4:29 error: module `client` is private +src/main.rs:4 modules::client::connect(); + ^~~~~~~~~~~~~~~~~~~~~~~~ +error: aborting due to previous error +error: Could not compile `modules`. ``` -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`: +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 +keyword to make something private; that's the default state. In this default +state, no one else could possibly use it, so if we don't use it within our +library crate, Rust will warn us that it's unused. Once we tell Rust something +is public, Rust knows that we intend for code external to our crate to use it, +and Rust considers theoretical external usage that is now possible to count as +being used and it will stop warning us. + +To tell Rust we want to make something public, we add the `pub` keyword. This +keyword goes before the declaration of the item we want to make public. Let's +modify `src/lib.rs` to make the `client` module public to fix the error we got: Filename: src/lib.rs ```rust,ignore -pub fn try_me() { - client::connect(); -} - -mod client; +pub mod client; mod network; ``` -And try to build: +The `pub` goes right before `mod`. Let's try building again: ```bash $ cargo build Compiling modules v0.1.0 (file:///projects/modules) +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() { + ^ +src/main.rs:4:5: 4:29 error: function `connect` is private +src/main.rs:4 modules::client::connect(); + ^~~~~~~~~~~~~~~~~~~~~~~~ +``` + +Hooray! We have a different error! Yes, different error messages are a cause +for celebration. The new error says "function `connect` is private", so let's +edit `src/client.rs` to make `client::connect()` public: + +Filename: src/client.rs + +```rust +pub fn connect() { +} +``` + +And run `cargo build` again: + +```bash + cargo build + Compiling modules v0.1.0 (file:///projects/modules) 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() { @@ -128,8 +146,16 @@ src/network/server.rs:1 fn connect() { ^ ``` -Success! We now have two warnings. Let's try to get rid of the first one. -Modify `src/network/mod.rs`: +It compiled! And the warning about `client::connect()` not being used is gone! +Since we also want the other two functions to be part of our crate's public +API, let's mark them as `pub` as well to get rid of the remaining warnings. If +we *didn't* want these functions to be part of our public API and we got these +warnings, they could be alerting us to code we no longer needed, or a bug if we +just removed the place within our library where we called this function +accidentally. + +So to fix the next warning, let's make `network::connect()` public. Modify +`src/network/mod.rs` to be: Filename: src/network/mod.rs @@ -157,22 +183,20 @@ 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`: +is not public. We're working from the interior of the library out, this time, +as opposed to with `client` where we worked from the outside in. Let's change +`src/lib.rs` to add the same fix though: Filename: src/lib.rs ```rust,ignore -pub fn try_me() { - client::connect(); -} - -mod client; +pub mod client; pub mod network; ``` -Now, we're declaring the `network` module public. Lo and behold, that warning -is gone: +Now, we're declaring that the `network` module should be public as well. Lo and +behold, that warning is gone: ```bash $ cargo build @@ -183,11 +207,91 @@ src/network/server.rs:1 fn connect() { ^ ``` -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. +Only one last warning! Try to fix this one on your own! + +## Visibility in tests + +Remember when we created our crate that Cargo made a `tests` module for us? Let's talk about that now. It was in `src/lib.rs`: + +Filename: src/lib.rs + +```rust,ignore +pub mod client; + +pub mod network; + +#[cfg(test)] +mod tests { + #[test] + fn it_works() { + } +} +``` + +We'll explain more about testing in Chapter XX, but we've talked about modules +now, so parts of this should make sense: we have a module named `tests` that +lives next to our other modules and contains one function named `it_works()`. + +Since tests are for exercising the code within our library, let's try to call +our `client::connect()` function from this `it_works()` function, even though +we're not going to be testing any functionality: + +```rust +#[cfg(test)] +mod tests { + #[test] + fn it_works() { + client::connect(); + } +} +``` + +Run the tests by invoking the `cargo test` command: + +```bash +$ cargo test + Compiling modules v0.1.0 (file:///projects/modules) +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 +``` + +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. It's because we have to be explicit about the +names we want to `use` in scope, even with sibling modules in the same library. +We need to bring `client` in scope, and because `client` is a sibling module to +`tests`, we can move up a level using the `super` module to refer to the parent +module of `tests`: + +```rust +#[cfg(test)] +mod tests { + use super::client; + + #[test] + fn it_works() { + client::connect(); + } +} +``` + +If we run `cargo test` again, the test will pass and the first part of the test +result output will be: + +```bash +$ cargo test + Compiling modules v0.1.0 (file:///projects/modules) + Running target/debug/modules-92007ddb5330fa5a + +running 1 test +test tests::it_works ... ok + +test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured +``` ## Privacy rules From d5328011724d49861d81bdbe5aa6ff1340ee57ab Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Thu, 25 Aug 2016 11:03:11 -0400 Subject: [PATCH 26/40] Reword some things on another reading --- src/ch07-02-mod-and-the-filesystem.md | 10 +-- ...ch07-03-controlling-visibility-with-pub.md | 62 ++++++++++--------- 2 files changed, 40 insertions(+), 32 deletions(-) diff --git a/src/ch07-02-mod-and-the-filesystem.md b/src/ch07-02-mod-and-the-filesystem.md index 984479b9e..e302ad1dd 100644 --- a/src/ch07-02-mod-and-the-filesystem.md +++ b/src/ch07-02-mod-and-the-filesystem.md @@ -262,11 +262,13 @@ that we could do yet: 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 the file -`src/client.rs` for? +`network::client`, or `server` and `network::server`? That's completely valid, +but then which module would the files `src/client.rs` and `src/server.rs`, +respectively, be for? -So instead, we can do what the error suggests. We'll make a new directory, -move `src/server.rs` into it, and change `src/network.rs` to `src/network/mod.rs`. +Instead of continuing to follow the same file naming pattern we used +previously, we can do what the error suggests. We'll make a new directory, move +`src/server.rs` into it, and change `src/network.rs` to `src/network/mod.rs`. Then, we try to build: ```bash diff --git a/src/ch07-03-controlling-visibility-with-pub.md b/src/ch07-03-controlling-visibility-with-pub.md index 7f4a046a4..1fef26b97 100644 --- a/src/ch07-03-controlling-visibility-with-pub.md +++ b/src/ch07-03-controlling-visibility-with-pub.md @@ -1,7 +1,7 @@ # Controlling visibility with `pub` -At the end of the last section, we had a project, `modules`, with a layout that -looks like this: +At the end of the last section, we had a project, `modules`, with a module +layout that looks like this: ```text modules @@ -29,9 +29,12 @@ 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 as if we were another project using our library. Create a -`src/main.rs` file with this code: +functions are the public interface that we want our *users* to use? We won't +necessarily be using them within our own crate, but the point of creating them +is that they *will* be used by another project. Let's try using them as if we +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 @@ -84,7 +87,8 @@ state, no one else could possibly use it, so if we don't use it within our library crate, Rust will warn us that it's unused. Once we tell Rust something is public, Rust knows that we intend for code external to our crate to use it, and Rust considers theoretical external usage that is now possible to count as -being used and it will stop warning us. +being used. Thus, when something is marked as public, Rust will stop warning us +that it is unused. To tell Rust we want to make something public, we add the `pub` keyword. This keyword goes before the declaration of the item we want to make public. Let's @@ -147,15 +151,17 @@ src/network/server.rs:1 fn connect() { ``` It compiled! And the warning about `client::connect()` not being used is gone! -Since we also want the other two functions to be part of our crate's public -API, let's mark them as `pub` as well to get rid of the remaining warnings. If -we *didn't* want these functions to be part of our public API and we got these -warnings, they could be alerting us to code we no longer needed, or a bug if we -just removed the place within our library where we called this function -accidentally. -So to fix the next warning, let's make `network::connect()` public. Modify -`src/network/mod.rs` to be: +Making functions public isn't the only way to fix unused code warnings: if +we *didn't* want these functions to be part of our public API and we got these +warnings, the warnings could be alerting us to code we no longer needed and +could safely delete. They could also be alerting us to a bug, if we +had just accidentally removed all places within our library where we called +this function. + +However, we *do* want the other two functions to be part of our crate's public +API, so let's mark them as `pub` as well to get rid of the remaining warnings. +Modify `src/network/mod.rs` to be: Filename: src/network/mod.rs @@ -181,11 +187,11 @@ 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. We're working from the interior of the library out, this time, -as opposed to with `client` where we worked from the outside in. Let's change -`src/lib.rs` to add the same fix though: +Hmmm, it says this is still dead, even though it's `pub`. While the function is +public within the module, the `network` module it's in is not public. We're +working from the interior of the library out, this time, as opposed to with +`client` where we worked from the outside in. Let's change `src/lib.rs` to add +the same fix though, by making `network` public like `client` is: Filename: src/lib.rs @@ -195,8 +201,7 @@ pub mod client; pub mod network; ``` -Now, we're declaring that the `network` module should be public as well. Lo and -behold, that warning is gone: +Now if we compile, that warning is gone: ```bash $ cargo build @@ -228,13 +233,14 @@ mod tests { } ``` -We'll explain more about testing in Chapter XX, but we've talked about modules -now, so parts of this should make sense: we have a module named `tests` that -lives next to our other modules and contains one function named `it_works()`. +We'll explain more about testing in Chapter XX, but parts of this should make +sense now: we have a module named `tests` that lives next to our other modules +and contains one function named `it_works()`. Even though there are special +annotations, the `tests` module is just another module! Since tests are for exercising the code within our library, let's try to call our `client::connect()` function from this `it_works()` function, even though -we're not going to be testing any functionality: +we're not going to be checking any functionality right now: ```rust #[cfg(test)] @@ -263,9 +269,9 @@ 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. It's because we have to be explicit about the names we want to `use` in scope, even with sibling modules in the same library. -We need to bring `client` in scope, and because `client` is a sibling module to -`tests`, we can move up a level using the `super` module to refer to the parent -module of `tests`: +We need to bring `client` in scope. `use` is relative to the current module, +`tests`. We can move up a module level using the `super` module to refer to the +parent module of `tests`, and then we can access the sibling `client` module: ```rust #[cfg(test)] From 7ad920f34a55db8d1a9c27e9dd1ff32f0e736415 Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Thu, 25 Aug 2016 11:07:12 -0400 Subject: [PATCH 27/40] Elide some warnings when we want to concentrate on the errors --- ...ch07-03-controlling-visibility-with-pub.md | 29 +++---------------- 1 file changed, 4 insertions(+), 25 deletions(-) diff --git a/src/ch07-03-controlling-visibility-with-pub.md b/src/ch07-03-controlling-visibility-with-pub.md index 1fef26b97..c06b19d9e 100644 --- a/src/ch07-03-controlling-visibility-with-pub.md +++ b/src/ch07-03-controlling-visibility-with-pub.md @@ -56,23 +56,13 @@ a nice separation of concerns. Our binary crate right now just calls our library's `connect()` function from the `client` module; we picked that one since it's the first warning in our -build output above. Invoking `cargo build` will now give us an error: +build output above. Invoking `cargo build` will now give us an error after the +warnings: ```bash $ cargo build Compiling modules v0.1.0 (file:///projects/modules) -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() { - ^ + src/main.rs:4:5: 4:29 error: module `client` is private src/main.rs:4 modules::client::connect(); ^~~~~~~~~~~~~~~~~~~~~~~~ @@ -107,18 +97,7 @@ The `pub` goes right before `mod`. Let's try building again: ```bash $ cargo build Compiling modules v0.1.0 (file:///projects/modules) -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() { - ^ + src/main.rs:4:5: 4:29 error: function `connect` is private src/main.rs:4 modules::client::connect(); ^~~~~~~~~~~~~~~~~~~~~~~~ From afc4bc25416c0328c52751f48cfe6b31ec333b85 Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Thu, 25 Aug 2016 11:15:03 -0400 Subject: [PATCH 28/40] Move summary of file/module changes to end of part 2 --- src/ch07-02-mod-and-the-filesystem.md | 22 ++++++++++++++++++- ...ch07-03-controlling-visibility-with-pub.md | 12 +--------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/ch07-02-mod-and-the-filesystem.md b/src/ch07-02-mod-and-the-filesystem.md index e302ad1dd..9b3cd5df9 100644 --- a/src/ch07-02-mod-and-the-filesystem.md +++ b/src/ch07-02-mod-and-the-filesystem.md @@ -281,7 +281,27 @@ $ cargo build $ ``` -It works! In summary, these are the rules of modules with regards to files: +It works! So now our module layout looks like this: + +```text +modules + ├── client + └── network + └── server +``` + +And the corresponding file layout looks like this: + +```text +├── src +│   ├── client.rs +│   ├── lib.rs +│   └── network +│   ├── mod.rs +│   └── server.rs +``` + +In summary, these are the rules of modules with regards to files: * If a module named `foo` has no submodules, you should put the declarations in the `foo` module in a file named `foo.rs`. diff --git a/src/ch07-03-controlling-visibility-with-pub.md b/src/ch07-03-controlling-visibility-with-pub.md index c06b19d9e..3de90196c 100644 --- a/src/ch07-03-controlling-visibility-with-pub.md +++ b/src/ch07-03-controlling-visibility-with-pub.md @@ -1,16 +1,6 @@ # Controlling visibility with `pub` -At the end of the last section, we had a project, `modules`, with a module -layout that looks like this: - -```text -modules - ├── client - └── network - └── server -``` - -When we compiled it, we got some strange warnings: +At the end of the last section, we had a project, `modules`, and when we compiled it, we got some strange warnings: ```bash Compiling modules v0.1.0 (file:///projects/modules) From d63d88ddebeea565a144ad2d9623e609cde6190d Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Thu, 25 Aug 2016 12:21:06 -0400 Subject: [PATCH 29/40] Actually, this bit about tests needs to go after intro of `use` --- ...ch07-03-controlling-visibility-with-pub.md | 85 ------------------ src/ch07-04-importing-names-with-use.md | 90 +++++++++++++++++++ 2 files changed, 90 insertions(+), 85 deletions(-) diff --git a/src/ch07-03-controlling-visibility-with-pub.md b/src/ch07-03-controlling-visibility-with-pub.md index 3de90196c..6c68ce918 100644 --- a/src/ch07-03-controlling-visibility-with-pub.md +++ b/src/ch07-03-controlling-visibility-with-pub.md @@ -183,91 +183,6 @@ src/network/server.rs:1 fn connect() { Only one last warning! Try to fix this one on your own! -## Visibility in tests - -Remember when we created our crate that Cargo made a `tests` module for us? Let's talk about that now. It was in `src/lib.rs`: - -Filename: src/lib.rs - -```rust,ignore -pub mod client; - -pub mod network; - -#[cfg(test)] -mod tests { - #[test] - fn it_works() { - } -} -``` - -We'll explain more about testing in Chapter XX, but parts of this should make -sense now: we have a module named `tests` that lives next to our other modules -and contains one function named `it_works()`. Even though there are special -annotations, the `tests` module is just another module! - -Since tests are for exercising the code within our library, let's try to call -our `client::connect()` function from this `it_works()` function, even though -we're not going to be checking any functionality right now: - -```rust -#[cfg(test)] -mod tests { - #[test] - fn it_works() { - client::connect(); - } -} -``` - -Run the tests by invoking the `cargo test` command: - -```bash -$ cargo test - Compiling modules v0.1.0 (file:///projects/modules) -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 -``` - -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. It's because we have to be explicit about the -names we want to `use` in scope, even with sibling modules in the same library. -We need to bring `client` in scope. `use` is relative to the current module, -`tests`. We can move up a module level using the `super` module to refer to the -parent module of `tests`, and then we can access the sibling `client` module: - -```rust -#[cfg(test)] -mod tests { - use super::client; - - #[test] - fn it_works() { - client::connect(); - } -} -``` - -If we run `cargo test` again, the test will pass and the first part of the test -result output will be: - -```bash -$ cargo test - Compiling modules v0.1.0 (file:///projects/modules) - Running target/debug/modules-92007ddb5330fa5a - -running 1 test -test tests::it_works ... ok - -test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured -``` - ## Privacy rules FIXME: these are the rules: diff --git a/src/ch07-04-importing-names-with-use.md b/src/ch07-04-importing-names-with-use.md index 9b1f91f9c..dc97dbf0c 100644 --- a/src/ch07-04-importing-names-with-use.md +++ b/src/ch07-04-importing-names-with-use.md @@ -142,3 +142,93 @@ src/main.rs:8 use TrafficLight::*; If, instead, we were explicit about the variants we wanted to import, our code would not have stopped compiling when we upgraded the `traffic_light` crate. + + +## Using `super` to access a parent module + +Remember when we created our crate that Cargo made a `tests` module for us? Let's talk about that now. It was in `src/lib.rs`: + +Filename: src/lib.rs + +```rust,ignore +pub mod client; + +pub mod network; + +#[cfg(test)] +mod tests { + #[test] + fn it_works() { + } +} +``` + +We'll explain more about testing in Chapter XX, but parts of this should make +sense now: we have a module named `tests` that lives next to our other modules +and contains one function named `it_works()`. Even though there are special +annotations, the `tests` module is just another module! + +Since tests are for exercising the code within our library, let's try to call +our `client::connect()` function from this `it_works()` function, even though +we're not going to be checking any functionality right now: + +```rust +#[cfg(test)] +mod tests { + #[test] + fn it_works() { + client::connect(); + } +} +``` + +Run the tests by invoking the `cargo test` command: + +```bash +$ cargo test + Compiling modules v0.1.0 (file:///projects/modules) +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 +``` + +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. It's because we have to be explicit about the +names we want to `use` in scope, even with sibling modules in the same library. +We need to bring `client` in scope. + +`use` is relative to the current module, `tests`. We can move up a module level +using `super` to refer to the parent module of `tests`, and from there we can +access the sibling `client` module: + +```rust +#[cfg(test)] +mod tests { + use super::client; + + #[test] + fn it_works() { + client::connect(); + } +} +``` + +If we run `cargo test` again, the test will pass and the first part of the test +result output will be: + +```bash +$ cargo test + Compiling modules v0.1.0 (file:///projects/modules) + Running target/debug/modules-92007ddb5330fa5a + +running 1 test +test tests::it_works ... ok + +test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured +``` + +`super` is generally only used with tests because...? From 8c46ff3cd835c352f56cd98096a48a025a2294c0 Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Thu, 25 Aug 2016 13:00:28 -0400 Subject: [PATCH 30/40] This comma looked funny just now --- src/ch07-03-controlling-visibility-with-pub.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ch07-03-controlling-visibility-with-pub.md b/src/ch07-03-controlling-visibility-with-pub.md index 6c68ce918..c8f7bda44 100644 --- a/src/ch07-03-controlling-visibility-with-pub.md +++ b/src/ch07-03-controlling-visibility-with-pub.md @@ -158,7 +158,7 @@ src/network/server.rs:1 fn connect() { Hmmm, it says this is still dead, even though it's `pub`. While the function is public within the module, the `network` module it's in is not public. We're -working from the interior of the library out, this time, as opposed to with +working from the interior of the library out this time, as opposed to with `client` where we worked from the outside in. Let's change `src/lib.rs` to add the same fix though, by making `network` public like `client` is: From 34dbf2e8651db5f94e157c935007b24057d4c281 Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Thu, 25 Aug 2016 13:01:13 -0400 Subject: [PATCH 31/40] Word the privacy rules consistently --- src/ch07-03-controlling-visibility-with-pub.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ch07-03-controlling-visibility-with-pub.md b/src/ch07-03-controlling-visibility-with-pub.md index c8f7bda44..6dc47cd0a 100644 --- a/src/ch07-03-controlling-visibility-with-pub.md +++ b/src/ch07-03-controlling-visibility-with-pub.md @@ -185,12 +185,12 @@ Only one last warning! Try to fix this one on your own! ## Privacy rules -FIXME: these are the rules: +Overall, these are the rules for item visibility: -* If an item is public, then it can be used externally through any of its +1. If an item is public, then it can be accessed through any of its parent modules. -* If an item is private, it may be accessed by the current module and its - submodules. +2. If an item is private, it may be accessed by the current module and its + child modules. but describing them in a human way is very hard. From c9e0a9aafe598008b19e305c2aea219dab16a388 Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Thu, 25 Aug 2016 13:01:22 -0400 Subject: [PATCH 32/40] Add illustrative examples for the privacy rules --- ...ch07-03-controlling-visibility-with-pub.md | 61 ++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/src/ch07-03-controlling-visibility-with-pub.md b/src/ch07-03-controlling-visibility-with-pub.md index 6dc47cd0a..787a91c94 100644 --- a/src/ch07-03-controlling-visibility-with-pub.md +++ b/src/ch07-03-controlling-visibility-with-pub.md @@ -192,6 +192,65 @@ Overall, these are the rules for item visibility: 2. If an item is private, it may be accessed by the current module and its child modules. +Let's look at a few more examples to get some practice. What if we had this +code in a new project's `src/lib.rs`: -but describing them in a human way is very hard. +```rust,ignore +mod outermost { + pub fn middle_function() {} + fn middle_secret_function() {} + + mod inside { + pub fn inner_function() {} + + fn secret_function() {} + } +} + +fn try_me() { + outermost::middle_function(); + outermost::middle_secret_function(); + outermost::inside::inner_function(); + outermost::inside::secret_function(); +} +``` + +Before you try to compile this code, make a guess about which lines in +`try_me()` will have errors. + +Ready? Let's talk through them! + +The `try_me()` function is in the root module of our project. The module named +`outermost` is private, but the second rule says we're allowed to access it +since `outermost` is in our current, root module. + +The function call `outermost::middle_function()` will work. `middle_function()` +is public, and we are accessing it through its parent module, `outermost`, +which we just determined we can access in the previous paragraph. + +`outermost::middle_secret_function()` will cause a compilation error. +`middle_secret_function()` is private, so the second rule applies. Our current +root module is neither the current module of `middle_secret_function()` +(`outermost` is), nor is it a child module of the current module of +`middle_secret_function()`. + +The module named `inside` is private and has no child modules, so it can only +be accessed by its current module, `outermost`. That means the `try_me()` +function is not allowed to call `outermost::inside::inner_function()` or +`outermost::inside::secret_function()`. + +Here are some changes to make to this code. Try each one, make a guess +about what will be allowed or not, compile to see if you're right, and use the +rules to understand why. + +* What if the `inside` module was public? +* What if `outside` was public and `inside` was private? +* What if, in the body of `inner_function()`, we called + `::outermost::middle_secret_function()`? (The two colons at the beginning + mean that we want to refer to the namespaces starting from the root + namespace.) + +Feel free to design more experiments and try them out! + +Next, let's talk about bringing items into a scope with the `use` keyword. From 83637d68c88138a12ab7a360071427ba66852129 Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Thu, 25 Aug 2016 14:31:36 -0700 Subject: [PATCH 33/40] Fix mistakes I made with modules... I've confused myself --- src/ch07-04-importing-names-with-use.md | 38 +++++++++++++++++++------ 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/src/ch07-04-importing-names-with-use.md b/src/ch07-04-importing-names-with-use.md index dc97dbf0c..922c4c487 100644 --- a/src/ch07-04-importing-names-with-use.md +++ b/src/ch07-04-importing-names-with-use.md @@ -143,10 +143,10 @@ src/main.rs:8 use TrafficLight::*; If, instead, we were explicit about the variants we wanted to import, our code would not have stopped compiling when we upgraded the `traffic_light` crate. - ## Using `super` to access a parent module -Remember when we created our crate that Cargo made a `tests` module for us? Let's talk about that now. It was in `src/lib.rs`: +Remember when we created our crate that Cargo made a `tests` module for us? +Let's talk about that now. It was in `src/lib.rs`: Filename: src/lib.rs @@ -197,13 +197,35 @@ 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. It's because we have to be explicit about the -names we want to `use` in scope, even with sibling modules in the same library. -We need to bring `client` in scope. +`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! -`use` is relative to the current module, `tests`. We can move up a module level -using `super` to refer to the parent module of `tests`, and from there we can -access the sibling `client` module: +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: + +```rust,ignore +::client::connect(); +``` + +Or we can use `super` to move up one module in the hierarchy: + +```rust,ignore +super::client::connect(); +``` + +If we were deep in the module hierarchy, starting from the root every time +would get long. Plus, if we rearrange our modules by moving a subtree to +another place, there might be a lot of places the path would need to be updated +if we always used the path from the root. + +It would also be annoying to have to type `super::` all the time in each test, +but we now have a tool for that solution: `use`! `super::` is special and +changes the path we give to `use` so that it is relative to the parent module +instead of to the root module. + +For these reasons, in the `tests` module especially, `use super::something` is +usually the way to go. So now our test looks like this: ```rust #[cfg(test)] From 555023fc28db94c0c6f7d5b3e0987fe348fe82f1 Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Thu, 25 Aug 2016 14:32:04 -0700 Subject: [PATCH 34/40] Give the modules chapter a bit of a summary conclusion --- src/ch07-04-importing-names-with-use.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/ch07-04-importing-names-with-use.md b/src/ch07-04-importing-names-with-use.md index 922c4c487..a10c37f3c 100644 --- a/src/ch07-04-importing-names-with-use.md +++ b/src/ch07-04-importing-names-with-use.md @@ -253,4 +253,6 @@ test tests::it_works ... ok test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured ``` -`super` is generally only used with tests because...? +Now you know techniques for organizing your code! Use these to group related +functionality together, keep files from getting too long, and present a tidy +public API to users of your library. From db52e35bf9ad9e7cdefe1b3329a2ba6121309364 Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Thu, 25 Aug 2016 14:39:09 -0700 Subject: [PATCH 35/40] Zero-index the filenames --- src/SUMMARY.md | 8 ++++---- src/{ch07-01-modules.md => ch07-00-modules.md} | 0 ...he-filesystem.md => ch07-01-mod-and-the-filesystem.md} | 0 ...-pub.md => ch07-02-controlling-visibility-with-pub.md} | 0 ...es-with-use.md => ch07-03-importing-names-with-use.md} | 0 5 files changed, 4 insertions(+), 4 deletions(-) rename src/{ch07-01-modules.md => ch07-00-modules.md} (100%) rename src/{ch07-02-mod-and-the-filesystem.md => ch07-01-mod-and-the-filesystem.md} (100%) rename src/{ch07-03-controlling-visibility-with-pub.md => ch07-02-controlling-visibility-with-pub.md} (100%) rename src/{ch07-04-importing-names-with-use.md => ch07-03-importing-names-with-use.md} (100%) diff --git a/src/SUMMARY.md b/src/SUMMARY.md index e90d3716d..8e4ad4a8b 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -26,10 +26,10 @@ - [Match](ch06-02-match.md) - [if let](ch06-03-if-let.md) -- [Modules](ch07-01-modules.md) - - [`mod` and the filesystem](ch07-02-mod-and-the-filesystem.md) - - [Controlling visibility with `pub`](ch07-03-controlling-visibility-with-pub.md) - - [Importing names with `use`](ch07-04-importing-names-with-use.md) +- [Modules](ch07-00-modules.md) + - [`mod` and the filesystem](ch07-01-mod-and-the-filesystem.md) + - [Controlling visibility with `pub`](ch07-02-controlling-visibility-with-pub.md) + - [Importing names with `use`](ch07-03-importing-names-with-use.md) - [Basic Collections]() - [Vectors]() diff --git a/src/ch07-01-modules.md b/src/ch07-00-modules.md similarity index 100% rename from src/ch07-01-modules.md rename to src/ch07-00-modules.md diff --git a/src/ch07-02-mod-and-the-filesystem.md b/src/ch07-01-mod-and-the-filesystem.md similarity index 100% rename from src/ch07-02-mod-and-the-filesystem.md rename to src/ch07-01-mod-and-the-filesystem.md diff --git a/src/ch07-03-controlling-visibility-with-pub.md b/src/ch07-02-controlling-visibility-with-pub.md similarity index 100% rename from src/ch07-03-controlling-visibility-with-pub.md rename to src/ch07-02-controlling-visibility-with-pub.md diff --git a/src/ch07-04-importing-names-with-use.md b/src/ch07-03-importing-names-with-use.md similarity index 100% rename from src/ch07-04-importing-names-with-use.md rename to src/ch07-03-importing-names-with-use.md From 9090d030504e8b46e24899d665c5d84ad2921340 Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Thu, 25 Aug 2016 14:40:55 -0700 Subject: [PATCH 36/40] Bump subchapter headings in a level --- src/ch07-01-mod-and-the-filesystem.md | 4 ++-- src/ch07-02-controlling-visibility-with-pub.md | 4 ++-- src/ch07-03-importing-names-with-use.md | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/ch07-01-mod-and-the-filesystem.md b/src/ch07-01-mod-and-the-filesystem.md index 9b3cd5df9..f90b23330 100644 --- a/src/ch07-01-mod-and-the-filesystem.md +++ b/src/ch07-01-mod-and-the-filesystem.md @@ -1,4 +1,4 @@ -# `mod` and the filesystem +## `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 @@ -104,7 +104,7 @@ modules More complicated projects can have a lot of modules. -## Putting modules in another file +### Putting modules in another file 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 diff --git a/src/ch07-02-controlling-visibility-with-pub.md b/src/ch07-02-controlling-visibility-with-pub.md index 787a91c94..c5e4b4b5d 100644 --- a/src/ch07-02-controlling-visibility-with-pub.md +++ b/src/ch07-02-controlling-visibility-with-pub.md @@ -1,4 +1,4 @@ -# Controlling visibility with `pub` +## 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: @@ -183,7 +183,7 @@ src/network/server.rs:1 fn connect() { Only one last warning! Try to fix this one on your own! -## Privacy rules +### Privacy rules Overall, these are the rules for item visibility: diff --git a/src/ch07-03-importing-names-with-use.md b/src/ch07-03-importing-names-with-use.md index a10c37f3c..f0321e715 100644 --- a/src/ch07-03-importing-names-with-use.md +++ b/src/ch07-03-importing-names-with-use.md @@ -1,4 +1,4 @@ -# Importing names with `use` +## Importing names with `use` We've seen how we can call functions defined within a module by using the module name as part of the call, like this: @@ -79,7 +79,7 @@ fn main() { } ``` -## Glob imports with `*` +### Glob imports with `*` If you'd like to import all the items in a namespace at once, you can use `*`: @@ -143,7 +143,7 @@ src/main.rs:8 use TrafficLight::*; If, instead, we were explicit about the variants we wanted to import, our code would not have stopped compiling when we upgraded the `traffic_light` crate. -## Using `super` to access a parent module +### Using `super` to access a parent module Remember when we created our crate that Cargo made a `tests` module for us? Let's talk about that now. It was in `src/lib.rs`: From 93f185a1500050d33c1adaa87fa7be38adfcdbe1 Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Fri, 26 Aug 2016 17:43:01 -0700 Subject: [PATCH 37/40] Remove naming conflict example since it should be rarer soon! --- src/ch07-03-importing-names-with-use.md | 40 ------------------------- 1 file changed, 40 deletions(-) diff --git a/src/ch07-03-importing-names-with-use.md b/src/ch07-03-importing-names-with-use.md index f0321e715..634dfbb92 100644 --- a/src/ch07-03-importing-names-with-use.md +++ b/src/ch07-03-importing-names-with-use.md @@ -103,46 +103,6 @@ The `*` is called a 'glob', and it will import everything that's visible inside of the namespace. Globs should be used sparingly: they are convenient, but you might also pull in more things than you expected and cause naming conflicts. -For example, in this program, imagine that `TrafficLight` is actually coming -from an external `traffic_light` crate we're depending on. We could also happen -to define our own enum named `Blue` that looks like: - -```rust -enum Blue { - Cyan, - Navy, - Royal, -} -``` - -If the traffic_light crate released a new major version and we decided to -upgrade, the crate could have added `Blue` to its `TrafficLight` variants: - -```rust -enum TrafficLight { - Red, - Yellow, - Green, - Blue, -} -``` - -Since we have imported all the names in `TrafficLight` using `*`, we would now -have a name conflict in our code and just upgrading would cause our crate to no -longer compile: - -```bash -src/main.rs:10:1: 14:2 error: a type named `Blue` has already been imported in this module [E0255] -src/main.rs:10 enum Blue { - ^ -src/main.rs:8:5: 8:21 note: previous import of `Blue` here -src/main.rs:8 use TrafficLight::*; - ^~~~~~~~~~~~~~~~ -``` - -If, instead, we were explicit about the variants we wanted to import, our code -would not have stopped compiling when we upgraded the `traffic_light` crate. - ### Using `super` to access a parent module Remember when we created our crate that Cargo made a `tests` module for us? From 94088e170fd541b8a859f1b45d33408e97f9d523 Mon Sep 17 00:00:00 2001 From: Steve Klabnik Date: Mon, 29 Aug 2016 12:13:53 -0400 Subject: [PATCH 38/40] re-write intro --- src/ch07-00-modules.md | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/ch07-00-modules.md b/src/ch07-00-modules.md index 055da56f8..9c72a20e3 100644 --- a/src/ch07-00-modules.md +++ b/src/ch07-00-modules.md @@ -1,12 +1,15 @@ # Modules -When a project starts getting large, it’s considered good software engineering -practice to split it up into a bunch of smaller pieces and then fit them -together. It’s also important to have a well-defined interface, so that some of -your functionality is private and some is public. Rust has a module system that -tackles both of these problems. 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 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: * `mod` declares a new module. * Everything starts off as private, but the `pub` keyword makes it public. From b0050958472cd3758b8661c0567c32c5a246e104 Mon Sep 17 00:00:00 2001 From: Steve Klabnik Date: Mon, 29 Aug 2016 12:14:15 -0400 Subject: [PATCH 39/40] these should be single quotes --- src/ch07-01-mod-and-the-filesystem.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ch07-01-mod-and-the-filesystem.md b/src/ch07-01-mod-and-the-filesystem.md index f90b23330..d60a4eee7 100644 --- a/src/ch07-01-mod-and-the-filesystem.md +++ b/src/ch07-01-mod-and-the-filesystem.md @@ -1,7 +1,7 @@ ## `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 +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` From c9e0652355d6779d5d5dfd8539ac56edeecf4a5b Mon Sep 17 00:00:00 2001 From: Steve Klabnik Date: Mon, 29 Aug 2016 12:14:41 -0400 Subject: [PATCH 40/40] these arrows are off-by-one --- src/ch07-01-mod-and-the-filesystem.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ch07-01-mod-and-the-filesystem.md b/src/ch07-01-mod-and-the-filesystem.md index d60a4eee7..aebe9f0b0 100644 --- a/src/ch07-01-mod-and-the-filesystem.md +++ b/src/ch07-01-mod-and-the-filesystem.md @@ -174,15 +174,15 @@ $ cargo build 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() { - ^ + ^ ``` Don't worry about those warnings for now; we'll clear them up in a future