From 5bf8642da8fc66d6fa1f4140e23b2072f075c87c Mon Sep 17 00:00:00 2001 From: Chris Krycho Date: Tue, 3 Dec 2024 11:41:50 -0700 Subject: [PATCH 1/2] infra: update dprint-plugin-markdown for inline code fix Correctly applies line wrapping to inline code blocks. See [the issue][129] and [fix PR][fix] for more details. [issue]: https://github.com/dprint/dprint-plugin-markdown/issues/129 [fix]: https://github.com/dprint/dprint-plugin-markdown/pull/130 From 38b2b8537f30ff85a78868f22088517cfac5e49c Mon Sep 17 00:00:00 2001 From: Chris Krycho Date: Tue, 3 Dec 2024 11:34:58 -0700 Subject: [PATCH 2/2] infra: dprint, part 2 - Reformat everything now that the bug in dprint for wrapping lines with inline code is fixed. - Additionally, apply the formatting rules I *should have* applied the first time, so the repo has the same style it has historically used. --- .github/ISSUE_TEMPLATE/new_translation.md | 3 +- ADMIN_TASKS.md | 133 ++++---- CONTRIBUTING.md | 36 +-- README.md | 40 +-- TODO.md | 30 +- dprint.jsonc | 2 + packages/mdbook-trpl/README.md | 8 +- .../src/bin/README - mdbook-trpl-note.md | 20 +- packages/trpl/CHANGELOG.md | 3 +- src/SUMMARY.md | 245 ++++++++------- src/appendix-01-keywords.md | 130 ++++---- src/appendix-02-operators.md | 4 +- src/appendix-03-derivable-traits.md | 82 ++--- src/appendix-04-useful-development-tools.md | 11 +- src/appendix-05-editions.md | 34 +- src/appendix-06-translation.md | 44 +-- src/appendix-07-nightly-rust.md | 112 +++---- src/ch00-00-introduction.md | 95 +++--- src/ch01-00-getting-started.md | 6 +- src/ch01-01-installation.md | 22 +- src/ch01-02-hello-world.md | 60 ++-- src/ch01-03-hello-cargo.md | 92 +++--- src/ch02-00-guessing-game-tutorial.md | 294 +++++++++--------- src/ch03-00-common-programming-concepts.md | 4 +- src/ch03-01-variables-and-mutability.md | 116 +++---- src/ch03-02-data-types.md | 119 +++---- src/ch03-03-how-functions-work.md | 88 +++--- src/ch03-05-control-flow.md | 129 ++++---- src/ch04-01-what-is-ownership.md | 242 +++++++------- src/ch04-02-references-and-borrowing.md | 106 +++---- src/ch04-03-slices.md | 102 +++--- src/ch05-00-structs.md | 12 +- src/ch05-01-defining-structs.md | 94 +++--- src/ch05-02-example-structs.md | 119 +++---- src/ch05-03-method-syntax.md | 86 ++--- src/ch06-01-defining-an-enum.md | 142 +++++---- src/ch06-02-match.md | 125 ++++---- src/ch06-03-if-let.md | 34 +- ...ojects-with-packages-crates-and-modules.md | 34 +- src/ch07-01-packages-and-crates.md | 41 +-- ...ng-modules-to-control-scope-and-privacy.md | 80 ++--- ...referring-to-an-item-in-the-module-tree.md | 156 +++++----- ...g-paths-into-scope-with-the-use-keyword.md | 83 ++--- ...separating-modules-into-different-files.md | 63 ++-- src/ch08-00-common-collections.md | 12 +- src/ch08-01-vectors.md | 110 +++---- src/ch08-02-strings.md | 139 ++++----- src/ch08-03-hash-maps.md | 101 +++--- src/ch09-00-error-handling.md | 4 +- ...ch09-01-unrecoverable-errors-with-panic.md | 87 +++--- src/ch09-02-recoverable-errors-with-result.md | 225 +++++++------- src/ch09-03-to-panic-or-not-to-panic.md | 133 ++++---- src/ch10-00-generics.md | 54 ++-- src/ch10-01-syntax.md | 145 +++++---- src/ch10-02-traits.md | 133 ++++---- src/ch10-03-lifetime-syntax.md | 274 ++++++++-------- src/ch11-00-testing.md | 26 +- src/ch11-01-writing-tests.md | 258 +++++++-------- src/ch11-02-running-tests.md | 70 ++--- src/ch11-03-test-organization.md | 75 ++--- src/ch12-00-an-io-project.md | 24 +- ...h12-01-accepting-command-line-arguments.md | 79 ++--- src/ch12-02-reading-a-file.md | 23 +- ...improving-error-handling-and-modularity.md | 242 +++++++------- ...2-04-testing-the-librarys-functionality.md | 46 +-- ...2-05-working-with-environment-variables.md | 58 ++-- ...-06-writing-to-stderr-instead-of-stdout.md | 8 +- src/ch13-00-functional-features.md | 18 +- src/ch13-01-closures.md | 176 +++++------ src/ch13-02-iterators.md | 92 +++--- src/ch13-03-improving-our-io-project.md | 47 ++- src/ch13-04-performance.md | 52 ++-- src/ch14-00-more-about-cargo.md | 15 +- src/ch14-01-release-profiles.md | 26 +- src/ch14-02-publishing-to-crates-io.md | 171 +++++----- src/ch14-03-cargo-workspaces.md | 99 +++--- src/ch14-04-installing-binaries.md | 13 +- src/ch15-00-smart-pointers.md | 58 ++-- src/ch15-01-box.md | 127 ++++---- src/ch15-02-deref.md | 139 ++++----- src/ch15-03-drop.md | 78 ++--- src/ch15-04-rc.md | 70 ++--- src/ch15-05-interior-mutability.md | 200 ++++++------ src/ch15-06-reference-cycles.md | 163 +++++----- src/ch16-00-concurrency.md | 51 ++- src/ch16-01-threads.md | 67 ++-- src/ch16-02-message-passing.md | 84 ++--- src/ch16-03-shared-state.md | 104 ++++--- ...04-extensible-concurrency-sync-and-send.md | 47 +-- src/ch17-00-async-await.md | 13 +- src/ch17-01-futures-and-syntax.md | 134 ++++---- src/ch17-02-concurrency-with-async.md | 68 ++-- src/ch17-03-more-futures.md | 126 ++++---- src/ch17-04-streams.md | 63 ++-- src/ch17-05-traits-for-async.md | 76 ++--- src/ch17-06-futures-tasks-threads.md | 20 +- src/ch18-01-what-is-oo.md | 114 +++---- src/ch18-02-trait-objects.md | 159 +++++----- src/ch18-03-oo-design-patterns.md | 231 +++++++------- src/ch19-00-patterns.md | 24 +- src/ch19-01-all-the-places-for-patterns.md | 106 ++++--- src/ch19-02-refutability.md | 55 ++-- src/ch19-03-pattern-syntax.md | 206 ++++++------ src/ch20-00-advanced-features.md | 10 +- src/ch20-01-unsafe-rust.md | 183 +++++------ src/ch20-03-advanced-traits.md | 162 +++++----- src/ch20-04-advanced-types.md | 105 +++---- ...ch20-05-advanced-functions-and-closures.md | 33 +- src/ch20-06-macros.md | 206 ++++++------ src/ch21-01-single-threaded.md | 204 ++++++------ src/ch21-02-multithreaded.md | 240 +++++++------- src/ch21-03-graceful-shutdown-and-cleanup.md | 75 +++-- src/foreword.md | 24 +- src/title-page.md | 7 +- style-guide.md | 43 +-- 115 files changed, 5223 insertions(+), 5168 deletions(-) diff --git a/.github/ISSUE_TEMPLATE/new_translation.md b/.github/ISSUE_TEMPLATE/new_translation.md index c7bdc06edd..5a4b5821b9 100644 --- a/.github/ISSUE_TEMPLATE/new_translation.md +++ b/.github/ISSUE_TEMPLATE/new_translation.md @@ -3,5 +3,4 @@ name: New translation about: Let us know of a new language translation you're working on --- -Language your translation is for: -URL to the repo where you're working: +Language your translation is for: URL to the repo where you're working: diff --git a/ADMIN_TASKS.md b/ADMIN_TASKS.md index b91b2ef96c..043347ee85 100644 --- a/ADMIN_TASKS.md +++ b/ADMIN_TASKS.md @@ -5,17 +5,17 @@ occasional maintenance tasks. ## Update the `rustc` version -- Delete your `target` directory, you're about to recompile everything anyway -- Change the version number in `.github/workflows/main.yml` -- Change the version number in `rust-toolchain`, which should change the - version you're using locally with `rustup` -- Change the version number in `src/title-page.md` -- Run `./tools/update-rustc.sh` (see its commented code for details on what it +* Delete your `target` directory, you're about to recompile everything anyway +* Change the version number in `.github/workflows/main.yml` +* Change the version number in `rust-toolchain`, which should change the version + you're using locally with `rustup` +* Change the version number in `src/title-page.md` +* Run `./tools/update-rustc.sh` (see its commented code for details on what it does) -- Inspect the changes (by looking at the files changed according to git) and +* Inspect the changes (by looking at the files changed according to git) and their effects (by looking at the files in `tmp/book-before` and `tmp/book-after`) and commit them if they look good -- Grep for `manual-regeneration` and follow the instructions in those places to +* Grep for `manual-regeneration` and follow the instructions in those places to update output that cannot be generated by a script ## Update the `edition` in all listings @@ -30,18 +30,19 @@ Open `book.toml` and `nostarch/book.toml` and set the `edition` value in the ## Release a new version of the listings -We now make `.tar` files of complete projects containing every listing -available [as GitHub Releases](https://github.com/rust-lang/book/releases). To -create a new release artifact, for example if there have been code changes due -to edits or due to updating Rust and `rustfmt`, do the following: +We now make `.tar` files of complete projects containing every listing available +[as GitHub Releases](https://github.com/rust-lang/book/releases). To create a +new release artifact, for example if there have been code changes due to edits +or due to updating Rust and `rustfmt`, do the following: -- Create a git tag for the release and push it to GitHub, or create a new tag - by going to the GitHub UI, [drafting a new release](https://github.com/rust-lang/book/releases/new), and entering a new - tag instead of selecting an existing tag -- Run `cargo run --bin release_listings`, which will generate +* Create a git tag for the release and push it to GitHub, or create a new tag by + going to the GitHub UI, + [drafting a new release](https://github.com/rust-lang/book/releases/new), and + entering a new tag instead of selecting an existing tag +* Run `cargo run --bin release_listings`, which will generate `tmp/listings.tar.gz` -- Upload `tmp/listings.tar.gz` in the GitHub UI for the draft release -- Publish the release +* Upload `tmp/listings.tar.gz` in the GitHub UI for the draft release +* Publish the release ## Add a new listing @@ -50,78 +51,79 @@ output when the compiler is updated, and produce release artifacts containing full projects for the listings, any listing beyond the most trivial should be extracted into a file. To do that: -- Find where the new listing should go in the `listings` directory. - - There is one subdirectory for each chapter - - Numbered listings should use `listing-[chapter num]-[listing num]` for - their directory names. - - Listings without a number should start with `no-listing-` followed by a +* Find where the new listing should go in the `listings` directory. + * There is one subdirectory for each chapter + * Numbered listings should use `listing-[chapter num]-[listing num]` for their + directory names. + * Listings without a number should start with `no-listing-` followed by a number that indicates its position in the chapter relative to the other listings without numbers in the chapter, then a short description that someone could read to find the code they're looking for. - - Listings used only for displaying the output of the code (for example, when - we say "if we had written x instead of y, we would get this compiler - error:" but we don't actually show code x) should be named with - `output-only-` followed by a number that indicates its position in the - chapter relative to the other listings used only for output, then a short - description that authors or contributors could read to find the code - they're looking for. - - **Remember to adjust surrounding listing numbers as appropriate!** -- Create a full Cargo project in that directory, either by using `cargo new` or + * Listings used only for displaying the output of the code (for example, when + we say "if we had written x instead of y, we would get this compiler error:" + but we don't actually show code x) should be named with `output-only-` + followed by a number that indicates its position in the chapter relative to + the other listings used only for output, then a short description that + authors or contributors could read to find the code they're looking for. + * **Remember to adjust surrounding listing numbers as appropriate!** +* Create a full Cargo project in that directory, either by using `cargo new` or copying another listing as a starting point. -- Add the code and any surrounding code needed to create a full working example. -- If you only want to show part of the code in the file, use anchor comments - (`// ANCHOR: some_tag` and `// ANCHOR_END: some_tag`) to mark the parts of - the file you want to show. -- For Rust code, use the `{{#rustdoc_include [filename:some_tag]}}` directive +* Add the code and any surrounding code needed to create a full working example. +* If you only want to show part of the code in the file, use anchor comments + (`// ANCHOR: some_tag` and `// ANCHOR_END: some_tag`) to mark the parts of the + file you want to show. +* For Rust code, use the `{{#rustdoc_include [filename:some_tag]}}` directive within the code blocks in the text. The `rustdoc_include` directive gives the code that doesn't get displayed to `rustdoc` for `mdbook test` purposes. -- For anything else, use the `{{#include [filename:some_tag]}}` directive. -- If you want to display the output of a command in the text as well, create an +* For anything else, use the `{{#include [filename:some_tag]}}` directive. +* If you want to display the output of a command in the text as well, create an `output.txt` file in the listing's directory as follows: - - Run the command, like `cargo run` or `cargo test`, and copy all of the + * Run the command, like `cargo run` or `cargo test`, and copy all of the output. - - Create a new `output.txt` file with the first line `$ [the command you + * Create a new `output.txt` file with the first line + `$ [the command you ran]`. - - Paste the output you just copied. - - Run `./tools/update-rustc.sh`, which should perform some normalization on + * Paste the output you just copied. + * Run `./tools/update-rustc.sh`, which should perform some normalization on the compiler output. - - Include the output in the text with the `{{#include [filename]}}` directive. - - Add and commit output.txt. -- If you want to display output but for some reason it can't be generated by a + * Include the output in the text with the `{{#include [filename]}}` directive. + * Add and commit output.txt. +* If you want to display output but for some reason it can't be generated by a script (say, because of user input or external events like making a web request), keep the output inline but make a comment that contains `manual-regeneration` and instructions for manually updating the inline output. -- If you don't want this example to even be attempted to be formatted by +* If you don't want this example to even be attempted to be formatted by `rustfmt` (for example because the example doesn't parse on purpose), add a - `rustfmt-ignore` file in the listing's directory and the reason it's not - being formatted as the contents of that file (in case it's a rustfmt bug that - might get fixed someday). + `rustfmt-ignore` file in the listing's directory and the reason it's not being + formatted as the contents of that file (in case it's a rustfmt bug that might + get fixed someday). ## See the effect of some change on the rendered book To check, say, updating `mdbook` or changing the way files get included: -- Generate a built book before the change you want to test by running `mdbook +* Generate a built book before the change you want to test by running + `mdbook build -d tmp/book-before` -- Apply the changes you want to test and run `mdbook build -d tmp/book-after` -- Run `./tools/megadiff.sh` -- Files remaining in `tmp/book-before` and `tmp/book-after` have differences - you can manually inspect with your favorite diff viewing mechanism +* Apply the changes you want to test and run `mdbook build -d tmp/book-after` +* Run `./tools/megadiff.sh` +* Files remaining in `tmp/book-before` and `tmp/book-after` have differences you + can manually inspect with your favorite diff viewing mechanism ## Produce new markdown files for No Starch -- Run `./tools/nostarch.sh` -- Spot check the files that script created in the `nostarch` directory -- Check them into git if you're starting a round of edits +* Run `./tools/nostarch.sh` +* Spot check the files that script created in the `nostarch` directory +* Check them into git if you're starting a round of edits ## Produce markdown from docx for diffing -- Save the docx file to `tmp/chapterXX.docx`. -- In Word, go to the review tab, choose "Accept all changes and stop tracking" -- Save the docx again and close Word -- Run `./tools/doc-to-md.sh` -- This should write `nostarch/chapterXX.md`. Adjust the XSL in +* Save the docx file to `tmp/chapterXX.docx`. +* In Word, go to the review tab, choose "Accept all changes and stop tracking" +* Save the docx again and close Word +* Run `./tools/doc-to-md.sh` +* This should write `nostarch/chapterXX.md`. Adjust the XSL in `tools/doc-to-md.xsl` and run `./tools/doc-to-md.sh` again if needed. ## Generate Graphviz dot @@ -143,7 +145,8 @@ values that don't cut off the image. We sometimes publish to GitHub Pages for in-progress previews. The recommended flow for publishing is: -- Install the `ghp-import` tool by running `pip install ghp-import` (or `pipx install ghp-import`, using [pipx][pipx]). -- In the root, run `tools/generate-preview.sh` +* Install the `ghp-import` tool by running `pip install ghp-import` (or + `pipx install ghp-import`, using [pipx][pipx]). +* In the root, run `tools/generate-preview.sh` [pipx]: https://pipx.pypa.io/stable/#install-pipx diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 68a2dc0c28..6f00b28298 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -56,25 +56,25 @@ reporting a new issue or opening a new PR. ## Licensing -This repository is under the same license as Rust itself, MIT/Apache2. You -can find the full text of each license in the `LICENSE-*` files in this -repository. +This repository is under the same license as Rust itself, MIT/Apache2. You can +find the full text of each license in the `LICENSE-*` files in this repository. ## Code of Conduct -The Rust project has [a code of conduct](http://rust-lang.org/policies/code-of-conduct) -that governs all sub-projects, including this one. Please respect it! +The Rust project has +[a code of conduct](http://rust-lang.org/policies/code-of-conduct) that governs +all sub-projects, including this one. Please respect it! ## Expectations -Because the book is [printed][nostarch], and because we want -to keep the online version of the book close to the print version when -possible, it may take longer than you're used to for us to address your issue -or pull request. +Because the book is [printed][nostarch], and because we want to keep the online +version of the book close to the print version when possible, it may take longer +than you're used to for us to address your issue or pull request. [nostarch]: https://nostarch.com/rust-programming-language-2nd-edition -So far, we've been doing a larger revision to coincide with [Rust Editions](https://doc.rust-lang.org/edition-guide/). Between those larger +So far, we've been doing a larger revision to coincide with +[Rust Editions](https://doc.rust-lang.org/edition-guide/). Between those larger revisions, we will only be correcting errors. If your issue or pull request isn't strictly fixing an error, it might sit until the next time that we're working on a large revision: expect on the order of months or years. Thank you @@ -82,20 +82,20 @@ for your patience! ## Help wanted -If you're looking for ways to help that don't involve large amounts of -reading or writing, check out the [open issues with the E-help-wanted -label][help-wanted]. These might be small fixes to the text, Rust code, -frontend code, or shell scripts that would help us be more efficient or -enhance the book in some way! +If you're looking for ways to help that don't involve large amounts of reading +or writing, check out the +[open issues with the E-help-wanted label][help-wanted]. These might be small +fixes to the text, Rust code, frontend code, or shell scripts that would help us +be more efficient or enhance the book in some way! [help-wanted]: https://github.com/rust-lang/book/issues?q=is%3Aopen+is%3Aissue+label%3AE-help-wanted ## Translations We'd love help translating the book! See the [Translations] label to join in -efforts that are currently in progress. Open a new issue to start working on -a new language! We're waiting on [mdbook support] for multiple languages -before we merge any in, but feel free to start! +efforts that are currently in progress. Open a new issue to start working on a +new language! We're waiting on [mdbook support] for multiple languages before we +merge any in, but feel free to start! [Translations]: https://github.com/rust-lang/book/issues?q=is%3Aopen+is%3Aissue+label%3ATranslations [mdbook support]: https://github.com/rust-lang/mdBook/issues/5 diff --git a/README.md b/README.md index a632402385..fa5632d8b2 100644 --- a/README.md +++ b/README.md @@ -9,15 +9,16 @@ This repository contains the source of "The Rust Programming Language" book. [nostarch]: https://nostarch.com/rust-programming-language-2nd-edition You can also read the book for free online. Please see the book as shipped with -the latest [stable], [beta], or [nightly] Rust releases. Be aware that issues -in those versions may have been fixed in this repository already, as those -releases are updated less frequently. +the latest [stable], [beta], or [nightly] Rust releases. Be aware that issues in +those versions may have been fixed in this repository already, as those releases +are updated less frequently. [stable]: https://doc.rust-lang.org/stable/book/ [beta]: https://doc.rust-lang.org/beta/book/ [nightly]: https://doc.rust-lang.org/nightly/book/ -See the [releases] to download just the code of all the code listings that appear in the book. +See the [releases] to download just the code of all the code listings that +appear in the book. [releases]: https://github.com/rust-lang/book/releases @@ -50,8 +51,8 @@ To build the book, type: $ mdbook build ``` -The output will be in the `book` subdirectory. To check it out, open it in -your web browser. +The output will be in the `book` subdirectory. To check it out, open it in your +web browser. _Firefox:_ @@ -85,12 +86,12 @@ kinds of contributions we're looking for. [contrib]: https://github.com/rust-lang/book/blob/main/CONTRIBUTING.md -Because the book is [printed][nostarch], and because we want -to keep the online version of the book close to the print version when -possible, it may take longer than you're used to for us to address your issue -or pull request. +Because the book is [printed][nostarch], and because we want to keep the online +version of the book close to the print version when possible, it may take longer +than you're used to for us to address your issue or pull request. -So far, we've been doing a larger revision to coincide with [Rust Editions](https://doc.rust-lang.org/edition-guide/). Between those larger +So far, we've been doing a larger revision to coincide with +[Rust Editions](https://doc.rust-lang.org/edition-guide/). Between those larger revisions, we will only be correcting errors. If your issue or pull request isn't strictly fixing an error, it might sit until the next time that we're working on a large revision: expect on the order of months or years. Thank you @@ -99,18 +100,17 @@ for your patience! ### Translations We'd love help translating the book! See the [Translations] label to join in -efforts that are currently in progress. Open a new issue to start working on -a new language! We're waiting on [mdbook support] for multiple languages -before we merge any in, but feel free to start! +efforts that are currently in progress. Open a new issue to start working on a +new language! We're waiting on [mdbook support] for multiple languages before we +merge any in, but feel free to start! [Translations]: https://github.com/rust-lang/book/issues?q=is%3Aopen+is%3Aissue+label%3ATranslations [mdbook support]: https://github.com/rust-lang/mdBook/issues/5 ## Spellchecking -To scan source files for spelling errors, you can use the `spellcheck.sh` -script available in the `ci` directory. It needs a dictionary of valid words, -which is provided in `ci/dictionary.txt`. If the script produces a false -positive (say, you used the word `BTreeMap` which the script considers invalid), -you need to add this word to `ci/dictionary.txt` (keep the sorted order for -consistency). +To scan source files for spelling errors, you can use the `spellcheck.sh` script +available in the `ci` directory. It needs a dictionary of valid words, which is +provided in `ci/dictionary.txt`. If the script produces a false positive (say, +you used the word `BTreeMap` which the script considers invalid), you need to +add this word to `ci/dictionary.txt` (keep the sorted order for consistency). diff --git a/TODO.md b/TODO.md index b61f853145..240f5ce4c3 100644 --- a/TODO.md +++ b/TODO.md @@ -1,17 +1,17 @@ # In each chapter -- [ ] Manual regeneration -- [ ] Check for upstream changes from last snapshot -- [ ] Propagate updated output to docx -- [ ] Extract docx and check diff -- [ ] Answer all comments -- [ ] Check cross references -- [ ] Check indentation of --snip-- -- [ ] Numbered lines, Gray out unchanged lines -- [ ] Check line wrapping -- [ ] Check for unneeded command/compiling/running output -- [ ] Check println style and error messages -- [ ] Add alt text to images -- [ ] Index tags - - [ ] search for "convention" conventions:naming:of blah - - [ ] check for double spaces, spaces at the end of paragraphs +* [ ] Manual regeneration +* [ ] Check for upstream changes from last snapshot +* [ ] Propagate updated output to docx +* [ ] Extract docx and check diff +* [ ] Answer all comments +* [ ] Check cross references +* [ ] Check indentation of --snip-- +* [ ] Numbered lines, Gray out unchanged lines +* [ ] Check line wrapping +* [ ] Check for unneeded command/compiling/running output +* [ ] Check println style and error messages +* [ ] Add alt text to images +* [ ] Index tags + * [ ] search for "convention" conventions:naming:of blah + * [ ] check for double spaces, spaces at the end of paragraphs diff --git a/dprint.jsonc b/dprint.jsonc index bf34e5b4de..9281563529 100644 --- a/dprint.jsonc +++ b/dprint.jsonc @@ -4,6 +4,8 @@ "json": { }, "markdown": { + "textWrap": "always", + "unorderedListKind": "asterisks", }, "malva": { }, diff --git a/packages/mdbook-trpl/README.md b/packages/mdbook-trpl/README.md index 15aacced0d..fb7c440323 100644 --- a/packages/mdbook-trpl/README.md +++ b/packages/mdbook-trpl/README.md @@ -1,12 +1,12 @@ # mdbook_trpl -A shared package for [mdbook][mdbook] [preprocessors][pre] used in [_The Rust -Programming Language_][trpl]. +A shared package for [mdbook][mdbook] [preprocessors][pre] used in +[_The Rust Programming Language_][trpl]. Supplies the following preprocessor binaries: -- [mdbook-trpl-note](./src/bin/note) -- [mdbook-trpl-listing](./src/bin/listing) +* [mdbook-trpl-note](./src/bin/note) +* [mdbook-trpl-listing](./src/bin/listing) [mdbook]: https://crates.io/crates/mdbook [pre]: https://rust-lang.github.io/mdBook/format/configuration/preprocessors.html diff --git a/packages/mdbook-trpl/src/bin/README - mdbook-trpl-note.md b/packages/mdbook-trpl/src/bin/README - mdbook-trpl-note.md index 3e54ae7fd1..1e02f0e864 100644 --- a/packages/mdbook-trpl/src/bin/README - mdbook-trpl-note.md +++ b/packages/mdbook-trpl/src/bin/README - mdbook-trpl-note.md @@ -1,6 +1,8 @@ # mdbook-trpl-note -This is a _very_ simple [preprocessor][pre] for [mdBook][mdbook], focused specifically on the content of _The Rust Programming Language_ book. This preprocessor takes Markdown like this— +This is a _very_ simple [preprocessor][pre] for [mdBook][mdbook], focused +specifically on the content of _The Rust Programming Language_ book. This +preprocessor takes Markdown like this— ```markdown > Note: This is some material we want to provide more emphasis for, because it @@ -34,15 +36,21 @@ Here is all the important things to know about that particular subject. ``` -This allows using the relatively standard Markdown convention of (incorrectly!) using blockquotes for “callouts” or “notes” like this, while still producing semantic HTML which conveys the actual intent. +This allows using the relatively standard Markdown convention of (incorrectly!) +using blockquotes for “callouts” or “notes” like this, while still producing +semantic HTML which conveys the actual intent. > [!NOTE] -> This is _not_ a full “admonition” preprocessor, and it is not remotely compliant with [the GitHub “alert” syntax][alerts]. It exists almost entirely for the sake of providing better semantic HTML for _The Rust Programming Language_ book with a minimum of disruption to existing workflows! +> This is _not_ a full “admonition” preprocessor, and it is not remotely +> compliant with [the GitHub “alert” syntax][alerts]. It exists almost entirely +> for the sake of providing better semantic HTML for _The Rust Programming +> Language_ book with a minimum of disruption to existing workflows! > -> You are probably better off using one of the other existing alert/admonition preprocessors: +> You are probably better off using one of the other existing alert/admonition +> preprocessors: > -> - [mdbook-alerts][mdbook-alerts] -> - [mdbook-admonish][mdbook-admonish] +> * [mdbook-alerts][mdbook-alerts] +> * [mdbook-admonish][mdbook-admonish] [pre]: https://rust-lang.github.io/mdBook/format/configuration/preprocessors.html [mdbook]: https://github.com/rust-lang/mdBook diff --git a/packages/trpl/CHANGELOG.md b/packages/trpl/CHANGELOG.md index 5555b3ee64..82276d6636 100644 --- a/packages/trpl/CHANGELOG.md +++ b/packages/trpl/CHANGELOG.md @@ -2,4 +2,5 @@ ## 0.1.0 -Initial release! Adds support code for the first draft of the new async chapter of the book. +Initial release! Adds support code for the first draft of the new async chapter +of the book. diff --git a/src/SUMMARY.md b/src/SUMMARY.md index a157cd05af..32cce341a5 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -1,143 +1,142 @@ # The Rust Programming Language -[The Rust Programming Language](title-page.md) -[Foreword](foreword.md) +[The Rust Programming Language](title-page.md) [Foreword](foreword.md) [Introduction](ch00-00-introduction.md) ## Getting started -- [Getting Started](ch01-00-getting-started.md) - - [Installation](ch01-01-installation.md) - - [Hello, World!](ch01-02-hello-world.md) - - [Hello, Cargo!](ch01-03-hello-cargo.md) +* [Getting Started](ch01-00-getting-started.md) + * [Installation](ch01-01-installation.md) + * [Hello, World!](ch01-02-hello-world.md) + * [Hello, Cargo!](ch01-03-hello-cargo.md) -- [Programming a Guessing Game](ch02-00-guessing-game-tutorial.md) +* [Programming a Guessing Game](ch02-00-guessing-game-tutorial.md) -- [Common Programming Concepts](ch03-00-common-programming-concepts.md) - - [Variables and Mutability](ch03-01-variables-and-mutability.md) - - [Data Types](ch03-02-data-types.md) - - [Functions](ch03-03-how-functions-work.md) - - [Comments](ch03-04-comments.md) - - [Control Flow](ch03-05-control-flow.md) +* [Common Programming Concepts](ch03-00-common-programming-concepts.md) + * [Variables and Mutability](ch03-01-variables-and-mutability.md) + * [Data Types](ch03-02-data-types.md) + * [Functions](ch03-03-how-functions-work.md) + * [Comments](ch03-04-comments.md) + * [Control Flow](ch03-05-control-flow.md) -- [Understanding Ownership](ch04-00-understanding-ownership.md) - - [What is Ownership?](ch04-01-what-is-ownership.md) - - [References and Borrowing](ch04-02-references-and-borrowing.md) - - [The Slice Type](ch04-03-slices.md) +* [Understanding Ownership](ch04-00-understanding-ownership.md) + * [What is Ownership?](ch04-01-what-is-ownership.md) + * [References and Borrowing](ch04-02-references-and-borrowing.md) + * [The Slice Type](ch04-03-slices.md) -- [Using Structs to Structure Related Data](ch05-00-structs.md) - - [Defining and Instantiating Structs](ch05-01-defining-structs.md) - - [An Example Program Using Structs](ch05-02-example-structs.md) - - [Method Syntax](ch05-03-method-syntax.md) +* [Using Structs to Structure Related Data](ch05-00-structs.md) + * [Defining and Instantiating Structs](ch05-01-defining-structs.md) + * [An Example Program Using Structs](ch05-02-example-structs.md) + * [Method Syntax](ch05-03-method-syntax.md) -- [Enums and Pattern Matching](ch06-00-enums.md) - - [Defining an Enum](ch06-01-defining-an-enum.md) - - [The `match` Control Flow Construct](ch06-02-match.md) - - [Concise Control Flow with `if let`](ch06-03-if-let.md) +* [Enums and Pattern Matching](ch06-00-enums.md) + * [Defining an Enum](ch06-01-defining-an-enum.md) + * [The `match` Control Flow Construct](ch06-02-match.md) + * [Concise Control Flow with `if let`](ch06-03-if-let.md) ## Basic Rust Literacy -- [Managing Growing Projects with Packages, Crates, and Modules](ch07-00-managing-growing-projects-with-packages-crates-and-modules.md) - - [Packages and Crates](ch07-01-packages-and-crates.md) - - [Defining Modules to Control Scope and Privacy](ch07-02-defining-modules-to-control-scope-and-privacy.md) - - [Paths for Referring to an Item in the Module Tree](ch07-03-paths-for-referring-to-an-item-in-the-module-tree.md) - - [Bringing Paths Into Scope with the `use` Keyword](ch07-04-bringing-paths-into-scope-with-the-use-keyword.md) - - [Separating Modules into Different Files](ch07-05-separating-modules-into-different-files.md) - -- [Common Collections](ch08-00-common-collections.md) - - [Storing Lists of Values with Vectors](ch08-01-vectors.md) - - [Storing UTF-8 Encoded Text with Strings](ch08-02-strings.md) - - [Storing Keys with Associated Values in Hash Maps](ch08-03-hash-maps.md) - -- [Error Handling](ch09-00-error-handling.md) - - [Unrecoverable Errors with `panic!`](ch09-01-unrecoverable-errors-with-panic.md) - - [Recoverable Errors with `Result`](ch09-02-recoverable-errors-with-result.md) - - [To `panic!` or Not to `panic!`](ch09-03-to-panic-or-not-to-panic.md) - -- [Generic Types, Traits, and Lifetimes](ch10-00-generics.md) - - [Generic Data Types](ch10-01-syntax.md) - - [Traits: Defining Shared Behavior](ch10-02-traits.md) - - [Validating References with Lifetimes](ch10-03-lifetime-syntax.md) - -- [Writing Automated Tests](ch11-00-testing.md) - - [How to Write Tests](ch11-01-writing-tests.md) - - [Controlling How Tests Are Run](ch11-02-running-tests.md) - - [Test Organization](ch11-03-test-organization.md) - -- [An I/O Project: Building a Command Line Program](ch12-00-an-io-project.md) - - [Accepting Command Line Arguments](ch12-01-accepting-command-line-arguments.md) - - [Reading a File](ch12-02-reading-a-file.md) - - [Refactoring to Improve Modularity and Error Handling](ch12-03-improving-error-handling-and-modularity.md) - - [Developing the Library’s Functionality with Test Driven Development](ch12-04-testing-the-librarys-functionality.md) - - [Working with Environment Variables](ch12-05-working-with-environment-variables.md) - - [Writing Error Messages to Standard Error Instead of Standard Output](ch12-06-writing-to-stderr-instead-of-stdout.md) +* [Managing Growing Projects with Packages, Crates, and Modules](ch07-00-managing-growing-projects-with-packages-crates-and-modules.md) + * [Packages and Crates](ch07-01-packages-and-crates.md) + * [Defining Modules to Control Scope and Privacy](ch07-02-defining-modules-to-control-scope-and-privacy.md) + * [Paths for Referring to an Item in the Module Tree](ch07-03-paths-for-referring-to-an-item-in-the-module-tree.md) + * [Bringing Paths Into Scope with the `use` Keyword](ch07-04-bringing-paths-into-scope-with-the-use-keyword.md) + * [Separating Modules into Different Files](ch07-05-separating-modules-into-different-files.md) + +* [Common Collections](ch08-00-common-collections.md) + * [Storing Lists of Values with Vectors](ch08-01-vectors.md) + * [Storing UTF-8 Encoded Text with Strings](ch08-02-strings.md) + * [Storing Keys with Associated Values in Hash Maps](ch08-03-hash-maps.md) + +* [Error Handling](ch09-00-error-handling.md) + * [Unrecoverable Errors with `panic!`](ch09-01-unrecoverable-errors-with-panic.md) + * [Recoverable Errors with `Result`](ch09-02-recoverable-errors-with-result.md) + * [To `panic!` or Not to `panic!`](ch09-03-to-panic-or-not-to-panic.md) + +* [Generic Types, Traits, and Lifetimes](ch10-00-generics.md) + * [Generic Data Types](ch10-01-syntax.md) + * [Traits: Defining Shared Behavior](ch10-02-traits.md) + * [Validating References with Lifetimes](ch10-03-lifetime-syntax.md) + +* [Writing Automated Tests](ch11-00-testing.md) + * [How to Write Tests](ch11-01-writing-tests.md) + * [Controlling How Tests Are Run](ch11-02-running-tests.md) + * [Test Organization](ch11-03-test-organization.md) + +* [An I/O Project: Building a Command Line Program](ch12-00-an-io-project.md) + * [Accepting Command Line Arguments](ch12-01-accepting-command-line-arguments.md) + * [Reading a File](ch12-02-reading-a-file.md) + * [Refactoring to Improve Modularity and Error Handling](ch12-03-improving-error-handling-and-modularity.md) + * [Developing the Library’s Functionality with Test Driven Development](ch12-04-testing-the-librarys-functionality.md) + * [Working with Environment Variables](ch12-05-working-with-environment-variables.md) + * [Writing Error Messages to Standard Error Instead of Standard Output](ch12-06-writing-to-stderr-instead-of-stdout.md) ## Thinking in Rust -- [Functional Language Features: Iterators and Closures](ch13-00-functional-features.md) - - [Closures: Anonymous Functions that Capture Their Environment](ch13-01-closures.md) - - [Processing a Series of Items with Iterators](ch13-02-iterators.md) - - [Improving Our I/O Project](ch13-03-improving-our-io-project.md) - - [Comparing Performance: Loops vs. Iterators](ch13-04-performance.md) - -- [More about Cargo and Crates.io](ch14-00-more-about-cargo.md) - - [Customizing Builds with Release Profiles](ch14-01-release-profiles.md) - - [Publishing a Crate to Crates.io](ch14-02-publishing-to-crates-io.md) - - [Cargo Workspaces](ch14-03-cargo-workspaces.md) - - [Installing Binaries from Crates.io with `cargo install`](ch14-04-installing-binaries.md) - - [Extending Cargo with Custom Commands](ch14-05-extending-cargo.md) - -- [Smart Pointers](ch15-00-smart-pointers.md) - - [Using `Box` to Point to Data on the Heap](ch15-01-box.md) - - [Treating Smart Pointers Like Regular References with the `Deref` Trait](ch15-02-deref.md) - - [Running Code on Cleanup with the `Drop` Trait](ch15-03-drop.md) - - [`Rc`, the Reference Counted Smart Pointer](ch15-04-rc.md) - - [`RefCell` and the Interior Mutability Pattern](ch15-05-interior-mutability.md) - - [Reference Cycles Can Leak Memory](ch15-06-reference-cycles.md) - -- [Fearless Concurrency](ch16-00-concurrency.md) - - [Using Threads to Run Code Simultaneously](ch16-01-threads.md) - - [Using Message Passing to Transfer Data Between Threads](ch16-02-message-passing.md) - - [Shared-State Concurrency](ch16-03-shared-state.md) - - [Extensible Concurrency with the `Sync` and `Send` Traits](ch16-04-extensible-concurrency-sync-and-send.md) - -- [Async and Await](ch17-00-async-await.md) - - [Futures and the Async Syntax](ch17-01-futures-and-syntax.md) - - [Concurrency With Async](ch17-02-concurrency-with-async.md) - - [Working With Any Number of Futures](ch17-03-more-futures.md) - - [Streams](ch17-04-streams.md) - - [Digging Into the Traits for Async](ch17-05-traits-for-async.md) - - [Futures, Tasks, and Threads](ch17-06-futures-tasks-threads.md) - -- [Object Oriented Programming Features of Rust](ch18-00-oop.md) - - [Characteristics of Object-Oriented Languages](ch18-01-what-is-oo.md) - - [Using Trait Objects That Allow for Values of Different Types](ch18-02-trait-objects.md) - - [Implementing an Object-Oriented Design Pattern](ch18-03-oo-design-patterns.md) +* [Functional Language Features: Iterators and Closures](ch13-00-functional-features.md) + * [Closures: Anonymous Functions that Capture Their Environment](ch13-01-closures.md) + * [Processing a Series of Items with Iterators](ch13-02-iterators.md) + * [Improving Our I/O Project](ch13-03-improving-our-io-project.md) + * [Comparing Performance: Loops vs. Iterators](ch13-04-performance.md) + +* [More about Cargo and Crates.io](ch14-00-more-about-cargo.md) + * [Customizing Builds with Release Profiles](ch14-01-release-profiles.md) + * [Publishing a Crate to Crates.io](ch14-02-publishing-to-crates-io.md) + * [Cargo Workspaces](ch14-03-cargo-workspaces.md) + * [Installing Binaries from Crates.io with `cargo install`](ch14-04-installing-binaries.md) + * [Extending Cargo with Custom Commands](ch14-05-extending-cargo.md) + +* [Smart Pointers](ch15-00-smart-pointers.md) + * [Using `Box` to Point to Data on the Heap](ch15-01-box.md) + * [Treating Smart Pointers Like Regular References with the `Deref` Trait](ch15-02-deref.md) + * [Running Code on Cleanup with the `Drop` Trait](ch15-03-drop.md) + * [`Rc`, the Reference Counted Smart Pointer](ch15-04-rc.md) + * [`RefCell` and the Interior Mutability Pattern](ch15-05-interior-mutability.md) + * [Reference Cycles Can Leak Memory](ch15-06-reference-cycles.md) + +* [Fearless Concurrency](ch16-00-concurrency.md) + * [Using Threads to Run Code Simultaneously](ch16-01-threads.md) + * [Using Message Passing to Transfer Data Between Threads](ch16-02-message-passing.md) + * [Shared-State Concurrency](ch16-03-shared-state.md) + * [Extensible Concurrency with the `Sync` and `Send` Traits](ch16-04-extensible-concurrency-sync-and-send.md) + +* [Async and Await](ch17-00-async-await.md) + * [Futures and the Async Syntax](ch17-01-futures-and-syntax.md) + * [Concurrency With Async](ch17-02-concurrency-with-async.md) + * [Working With Any Number of Futures](ch17-03-more-futures.md) + * [Streams](ch17-04-streams.md) + * [Digging Into the Traits for Async](ch17-05-traits-for-async.md) + * [Futures, Tasks, and Threads](ch17-06-futures-tasks-threads.md) + +* [Object Oriented Programming Features of Rust](ch18-00-oop.md) + * [Characteristics of Object-Oriented Languages](ch18-01-what-is-oo.md) + * [Using Trait Objects That Allow for Values of Different Types](ch18-02-trait-objects.md) + * [Implementing an Object-Oriented Design Pattern](ch18-03-oo-design-patterns.md) ## Advanced Topics -- [Patterns and Matching](ch19-00-patterns.md) - - [All the Places Patterns Can Be Used](ch19-01-all-the-places-for-patterns.md) - - [Refutability: Whether a Pattern Might Fail to Match](ch19-02-refutability.md) - - [Pattern Syntax](ch19-03-pattern-syntax.md) - -- [Advanced Features](ch20-00-advanced-features.md) - - [Unsafe Rust](ch20-01-unsafe-rust.md) - - [Advanced Traits](ch20-03-advanced-traits.md) - - [Advanced Types](ch20-04-advanced-types.md) - - [Advanced Functions and Closures](ch20-05-advanced-functions-and-closures.md) - - [Macros](ch20-06-macros.md) - -- [Final Project: Building a Multithreaded Web Server](ch21-00-final-project-a-web-server.md) - - [Building a Single-Threaded Web Server](ch21-01-single-threaded.md) - - [Turning Our Single-Threaded Server into a Multithreaded Server](ch21-02-multithreaded.md) - - [Graceful Shutdown and Cleanup](ch21-03-graceful-shutdown-and-cleanup.md) - -- [Appendix](appendix-00.md) - - [A - Keywords](appendix-01-keywords.md) - - [B - Operators and Symbols](appendix-02-operators.md) - - [C - Derivable Traits](appendix-03-derivable-traits.md) - - [D - Useful Development Tools](appendix-04-useful-development-tools.md) - - [E - Editions](appendix-05-editions.md) - - [F - Translations of the Book](appendix-06-translation.md) - - [G - How Rust is Made and “Nightly Rust”](appendix-07-nightly-rust.md) +* [Patterns and Matching](ch19-00-patterns.md) + * [All the Places Patterns Can Be Used](ch19-01-all-the-places-for-patterns.md) + * [Refutability: Whether a Pattern Might Fail to Match](ch19-02-refutability.md) + * [Pattern Syntax](ch19-03-pattern-syntax.md) + +* [Advanced Features](ch20-00-advanced-features.md) + * [Unsafe Rust](ch20-01-unsafe-rust.md) + * [Advanced Traits](ch20-03-advanced-traits.md) + * [Advanced Types](ch20-04-advanced-types.md) + * [Advanced Functions and Closures](ch20-05-advanced-functions-and-closures.md) + * [Macros](ch20-06-macros.md) + +* [Final Project: Building a Multithreaded Web Server](ch21-00-final-project-a-web-server.md) + * [Building a Single-Threaded Web Server](ch21-01-single-threaded.md) + * [Turning Our Single-Threaded Server into a Multithreaded Server](ch21-02-multithreaded.md) + * [Graceful Shutdown and Cleanup](ch21-03-graceful-shutdown-and-cleanup.md) + +* [Appendix](appendix-00.md) + * [A - Keywords](appendix-01-keywords.md) + * [B - Operators and Symbols](appendix-02-operators.md) + * [C - Derivable Traits](appendix-03-derivable-traits.md) + * [D - Useful Development Tools](appendix-04-useful-development-tools.md) + * [E - Editions](appendix-05-editions.md) + * [F - Translations of the Book](appendix-06-translation.md) + * [G - How Rust is Made and “Nightly Rust”](appendix-07-nightly-rust.md) diff --git a/src/appendix-01-keywords.md b/src/appendix-01-keywords.md index 1df1691174..d6df85fc59 100644 --- a/src/appendix-01-keywords.md +++ b/src/appendix-01-keywords.md @@ -1,11 +1,11 @@ ## Appendix A: Keywords -The following list contains keywords that are reserved for current or future -use by the Rust language. As such, they cannot be used as identifiers (except -as raw identifiers as we’ll discuss in the “[Raw -Identifiers][raw-identifiers]” section). Identifiers are names -of functions, variables, parameters, struct fields, modules, crates, constants, -macros, static values, attributes, types, traits, or lifetimes. +The following list contains keywords that are reserved for current or future use +by the Rust language. As such, they cannot be used as identifiers (except as raw +identifiers as we’ll discuss in the +“[Raw Identifiers][raw-identifiers]” section). Identifiers are +names of functions, variables, parameters, struct fields, modules, crates, +constants, macros, static values, attributes, types, traits, or lifetimes. [raw-identifiers]: #raw-identifiers @@ -14,48 +14,48 @@ macros, static values, attributes, types, traits, or lifetimes. The following is a list of keywords currently in use, with their functionality described. -- `as` - perform primitive casting, disambiguate the specific trait containing +* `as` - perform primitive casting, disambiguate the specific trait containing an item, or rename items in `use` statements -- `async` - return a `Future` instead of blocking the current thread -- `await` - suspend execution until the result of a `Future` is ready -- `break` - exit a loop immediately -- `const` - define constant items or constant raw pointers -- `continue` - continue to the next loop iteration -- `crate` - in a module path, refers to the crate root -- `dyn` - dynamic dispatch to a trait object -- `else` - fallback for `if` and `if let` control flow constructs -- `enum` - define an enumeration -- `extern` - link an external function or variable -- `false` - Boolean false literal -- `fn` - define a function or the function pointer type -- `for` - loop over items from an iterator, implement a trait, or specify a +* `async` - return a `Future` instead of blocking the current thread +* `await` - suspend execution until the result of a `Future` is ready +* `break` - exit a loop immediately +* `const` - define constant items or constant raw pointers +* `continue` - continue to the next loop iteration +* `crate` - in a module path, refers to the crate root +* `dyn` - dynamic dispatch to a trait object +* `else` - fallback for `if` and `if let` control flow constructs +* `enum` - define an enumeration +* `extern` - link an external function or variable +* `false` - Boolean false literal +* `fn` - define a function or the function pointer type +* `for` - loop over items from an iterator, implement a trait, or specify a higher-ranked lifetime -- `if` - branch based on the result of a conditional expression -- `impl` - implement inherent or trait functionality -- `in` - part of `for` loop syntax -- `let` - bind a variable -- `loop` - loop unconditionally -- `match` - match a value to patterns -- `mod` - define a module -- `move` - make a closure take ownership of all its captures -- `mut` - denote mutability in references, raw pointers, or pattern bindings -- `pub` - denote public visibility in struct fields, `impl` blocks, or modules -- `ref` - bind by reference -- `return` - return from function -- `Self` - a type alias for the type we are defining or implementing -- `self` - method subject or current module -- `static` - global variable or lifetime lasting the entire program execution -- `struct` - define a structure -- `super` - parent module of the current module -- `trait` - define a trait -- `true` - Boolean true literal -- `type` - define a type alias or associated type -- `union` - define a [union][union]; is only a keyword when used +* `if` - branch based on the result of a conditional expression +* `impl` - implement inherent or trait functionality +* `in` - part of `for` loop syntax +* `let` - bind a variable +* `loop` - loop unconditionally +* `match` - match a value to patterns +* `mod` - define a module +* `move` - make a closure take ownership of all its captures +* `mut` - denote mutability in references, raw pointers, or pattern bindings +* `pub` - denote public visibility in struct fields, `impl` blocks, or modules +* `ref` - bind by reference +* `return` - return from function +* `Self` - a type alias for the type we are defining or implementing +* `self` - method subject or current module +* `static` - global variable or lifetime lasting the entire program execution +* `struct` - define a structure +* `super` - parent module of the current module +* `trait` - define a trait +* `true` - Boolean true literal +* `type` - define a type alias or associated type +* `union` - define a [union][union]; is only a keyword when used in a union declaration -- `unsafe` - denote unsafe code, functions, traits, or implementations -- `use` - bring symbols into scope -- `where` - denote clauses that constrain a type -- `while` - loop conditionally based on the result of an expression +* `unsafe` - denote unsafe code, functions, traits, or implementations +* `use` - bring symbols into scope +* `where` - denote clauses that constrain a type +* `while` - loop conditionally based on the result of an expression [union]: ../reference/items/unions.html @@ -64,19 +64,19 @@ described. The following keywords do not yet have any functionality but are reserved by Rust for potential future use. -- `abstract` -- `become` -- `box` -- `do` -- `final` -- `macro` -- `override` -- `priv` -- `try` -- `typeof` -- `unsized` -- `virtual` -- `yield` +* `abstract` +* `become` +* `box` +* `do` +* `final` +* `macro` +* `override` +* `priv` +* `try` +* `typeof` +* `unsized` +* `virtual` +* `yield` ### Raw Identifiers @@ -124,14 +124,14 @@ This code will compile without any errors. Note the `r#` prefix on the function name in its definition as well as where the function is called in `main`. Raw identifiers allow you to use any word you choose as an identifier, even if -that word happens to be a reserved keyword. This gives us more freedom to -choose identifier names, as well as lets us integrate with programs written in -a language where these words aren’t keywords. In addition, raw identifiers -allow you to use libraries written in a different Rust edition than your crate -uses. For example, `try` isn’t a keyword in the 2015 edition but is in the 2018 +that word happens to be a reserved keyword. This gives us more freedom to choose +identifier names, as well as lets us integrate with programs written in a +language where these words aren’t keywords. In addition, raw identifiers allow +you to use libraries written in a different Rust edition than your crate uses. +For example, `try` isn’t a keyword in the 2015 edition but is in the 2018 edition. If you depend on a library that’s written using the 2015 edition and has a `try` function, you’ll need to use the raw identifier syntax, `r#try` in -this case, to call that function from your 2018 edition code. See [Appendix -E][appendix-e] for more information on editions. +this case, to call that function from your 2018 edition code. See +[Appendix E][appendix-e] for more information on editions. [appendix-e]: appendix-05-editions.html diff --git a/src/appendix-02-operators.md b/src/appendix-02-operators.md index 6c8b8d89db..4477d599ac 100644 --- a/src/appendix-02-operators.md +++ b/src/appendix-02-operators.md @@ -141,8 +141,8 @@ parameters with trait bounds. | `T: ?Sized` | Allow generic type parameter to be a dynamically sized type | | `'a + trait`, `trait + trait` | Compound type constraint | -Table B-6 shows symbols that appear in the context of calling or defining -macros and specifying attributes on an item. +Table B-6 shows symbols that appear in the context of calling or defining macros +and specifying attributes on an item. Table B-6: Macros and Attributes diff --git a/src/appendix-03-derivable-traits.md b/src/appendix-03-derivable-traits.md index 2365ade130..eafd36a978 100644 --- a/src/appendix-03-derivable-traits.md +++ b/src/appendix-03-derivable-traits.md @@ -1,18 +1,18 @@ ## Appendix C: Derivable Traits -In various places in the book, we’ve discussed the `derive` attribute, which -you can apply to a struct or enum definition. The `derive` attribute generates -code that will implement a trait with its own default implementation on the -type you’ve annotated with the `derive` syntax. +In various places in the book, we’ve discussed the `derive` attribute, which you +can apply to a struct or enum definition. The `derive` attribute generates code +that will implement a trait with its own default implementation on the type +you’ve annotated with the `derive` syntax. In this appendix, we provide a reference of all the traits in the standard library that you can use with `derive`. Each section covers: -- What operators and methods deriving this trait will enable -- What the implementation of the trait provided by `derive` does -- What implementing the trait signifies about the type -- The conditions in which you’re allowed or not allowed to implement the trait -- Examples of operations that require the trait +* What operators and methods deriving this trait will enable +* What the implementation of the trait provided by `derive` does +* What implementing the trait signifies about the type +* The conditions in which you’re allowed or not allowed to implement the trait +* Examples of operations that require the trait If you want different behavior from that provided by the `derive` attribute, consult the [standard library documentation](../std/index.html) @@ -31,15 +31,15 @@ would be most relevant to them? The Rust compiler doesn’t have this insight, s it can’t provide appropriate default behavior for you. The list of derivable traits provided in this appendix is not comprehensive: -libraries can implement `derive` for their own traits, making the list of -traits you can use `derive` with truly open-ended. Implementing `derive` -involves using a procedural macro, which is covered in the -[“Macros”][macros] section of Chapter 20. +libraries can implement `derive` for their own traits, making the list of traits +you can use `derive` with truly open-ended. Implementing `derive` involves using +a procedural macro, which is covered in the [“Macros”][macros] +section of Chapter 20. ### `Debug` for Programmer Output -The `Debug` trait enables debug formatting in format strings, which you -indicate by adding `:?` within `{}` placeholders. +The `Debug` trait enables debug formatting in format strings, which you indicate +by adding `:?` within `{}` placeholders. The `Debug` trait allows you to print instances of a type for debugging purposes, so you and other programmers using your type can inspect an instance @@ -56,12 +56,11 @@ equality and enables use of the `==` and `!=` operators. Deriving `PartialEq` implements the `eq` method. When `PartialEq` is derived on structs, two instances are equal only if _all_ fields are equal, and the -instances are not equal if any fields are not equal. When derived on enums, -each variant is equal to itself and not equal to the other variants. +instances are not equal if any fields are not equal. When derived on enums, each +variant is equal to itself and not equal to the other variants. -The `PartialEq` trait is required, for example, with the use of the -`assert_eq!` macro, which needs to be able to compare two instances of a type -for equality. +The `PartialEq` trait is required, for example, with the use of the `assert_eq!` +macro, which needs to be able to compare two instances of a type for equality. The `Eq` trait has no methods. Its purpose is to signal that for every value of the annotated type, the value is equal to itself. The `Eq` trait can only be @@ -92,9 +91,9 @@ value in each field in the order in which the fields appear in the struct definition. When derived on enums, variants of the enum declared earlier in the enum definition are considered less than the variants listed later. -The `PartialOrd` trait is required, for example, for the `gen_range` method -from the `rand` crate that generates a random value in the range specified by a -range expression. +The `PartialOrd` trait is required, for example, for the `gen_range` method from +the `rand` crate that generates a random value in the range specified by a range +expression. The `Ord` trait allows you to know that for any two values of the annotated type, a valid ordering will exist. The `Ord` trait implements the `cmp` method, @@ -111,7 +110,8 @@ a data structure that stores data based on the sort order of the values. The `Clone` trait allows you to explicitly create a deep copy of a value, and the duplication process might involve running arbitrary code and copying heap -data. See the [“Ways Variables and Data Interact: +data. See the +[“Ways Variables and Data Interact: Clone”][ways-variables-and-data-interact-clone] section in Chapter 4 for more information on `Clone`. @@ -121,17 +121,17 @@ fields or values in the type must also implement `Clone` to derive `Clone`. An example of when `Clone` is required is when calling the `to_vec` method on a slice. The slice doesn’t own the type instances it contains, but the vector -returned from `to_vec` will need to own its instances, so `to_vec` calls -`clone` on each item. Thus, the type stored in the slice must implement `Clone`. +returned from `to_vec` will need to own its instances, so `to_vec` calls `clone` +on each item. Thus, the type stored in the slice must implement `Clone`. The `Copy` trait allows you to duplicate a value by only copying bits stored on -the stack; no arbitrary code is necessary. See the [“Stack-Only Data: -Copy”][stack-only-data-copy] section in Chapter 4 for more -information on `Copy`. +the stack; no arbitrary code is necessary. See the +[“Stack-Only Data: Copy”][stack-only-data-copy] section in +Chapter 4 for more information on `Copy`. The `Copy` trait doesn’t define any methods to prevent programmers from -overloading those methods and violating the assumption that no arbitrary code -is being run. That way, all programmers can assume that copying a value will be +overloading those methods and violating the assumption that no arbitrary code is +being run. That way, all programmers can assume that copying a value will be very fast. You can derive `Copy` on any type whose parts all implement `Copy`. A type that @@ -140,8 +140,8 @@ implements `Copy` must also implement `Clone`, because a type that implements `Copy`. The `Copy` trait is rarely required; types that implement `Copy` have -optimizations available, meaning you don’t have to call `clone`, which makes -the code more concise. +optimizations available, meaning you don’t have to call `clone`, which makes the +code more concise. Everything possible with `Copy` you can also accomplish with `Clone`, but the code might be slower or have to use `clone` in places. @@ -154,23 +154,23 @@ map that instance to a value of fixed size using a hash function. Deriving method combines the result of calling `hash` on each of the parts of the type, meaning all fields or values must also implement `Hash` to derive `Hash`. -An example of when `Hash` is required is in storing keys in a `HashMap` -to store data efficiently. +An example of when `Hash` is required is in storing keys in a `HashMap` to +store data efficiently. ### `Default` for Default Values The `Default` trait allows you to create a default value for a type. Deriving `Default` implements the `default` function. The derived implementation of the `default` function calls the `default` function on each part of the type, -meaning all fields or values in the type must also implement `Default` to -derive `Default`. +meaning all fields or values in the type must also implement `Default` to derive +`Default`. The `Default::default` function is commonly used in combination with the struct -update syntax discussed in the [“Creating Instances From Other Instances With -Struct Update +update syntax discussed in the +[“Creating Instances From Other Instances With Struct Update Syntax”][creating-instances-from-other-instances-with-struct-update-syntax] -section in Chapter 5. You can customize a few fields of a struct and then -set and use a default value for the rest of the fields by using +section in Chapter 5. You can customize a few fields of a struct and then set +and use a default value for the rest of the fields by using `..Default::default()`. The `Default` trait is required when you use the method `unwrap_or_default` on diff --git a/src/appendix-04-useful-development-tools.md b/src/appendix-04-useful-development-tools.md index 75e0c871b5..ba68722096 100644 --- a/src/appendix-04-useful-development-tools.md +++ b/src/appendix-04-useful-development-tools.md @@ -7,8 +7,8 @@ warning fixes, a linter, and integrating with IDEs. ### Automatic Formatting with `rustfmt` The `rustfmt` tool reformats your code according to the community code style. -Many collaborative projects use `rustfmt` to prevent arguments about which -style to use when writing Rust: everyone formats their code using the tool. +Many collaborative projects use `rustfmt` to prevent arguments about which style +to use when writing Rust: everyone formats their code using the tool. To install `rustfmt`, enter the following: @@ -67,7 +67,8 @@ warning: unused variable: `i` The warning suggests that we use `_i` as a name instead: the underscore indicates that we intend for this variable to be unused. We can automatically -apply that suggestion using the `rustfix` tool by running the command `cargo +apply that suggestion using the `rustfix` tool by running the command +`cargo fix`: ```console @@ -143,8 +144,8 @@ error: approximate value of `f{32, 64}::consts::PI` found This error lets you know that Rust already has a more precise `PI` constant defined, and that your program would be more correct if you used the constant -instead. You would then change your code to use the `PI` constant. The -following code doesn’t result in any errors or warnings from Clippy: +instead. You would then change your code to use the `PI` constant. The following +code doesn’t result in any errors or warnings from Clippy: Filename: src/main.rs diff --git a/src/appendix-05-editions.md b/src/appendix-05-editions.md index ea9758ffa7..41b7f02cc0 100644 --- a/src/appendix-05-editions.md +++ b/src/appendix-05-editions.md @@ -17,11 +17,11 @@ six-week release process. Editions serve different purposes for different people: -- For active Rust users, a new edition brings together incremental changes into +* For active Rust users, a new edition brings together incremental changes into an easy-to-understand package. -- For non-users, a new edition signals that some major advancements have - landed, which might make Rust worth another look. -- For those developing Rust, a new edition provides a rallying point for the +* For non-users, a new edition signals that some major advancements have landed, + which might make Rust worth another look. +* For those developing Rust, a new edition provides a rallying point for the project as a whole. At the time of this writing, three Rust editions are available: Rust 2015, Rust @@ -33,17 +33,17 @@ value for backward compatibility reasons. Each project can opt in to an edition other than the default 2015 edition. Editions can contain incompatible changes, such as including a new keyword that -conflicts with identifiers in code. However, unless you opt in to those -changes, your code will continue to compile even as you upgrade the Rust -compiler version you use. +conflicts with identifiers in code. However, unless you opt in to those changes, +your code will continue to compile even as you upgrade the Rust compiler version +you use. All Rust compiler versions support any edition that existed prior to that -compiler’s release, and they can link crates of any supported editions -together. Edition changes only affect the way the compiler initially parses -code. Therefore, if you’re using Rust 2015 and one of your dependencies uses -Rust 2018, your project will compile and be able to use that dependency. The -opposite situation, where your project uses Rust 2018 and a dependency uses -Rust 2015, works as well. +compiler’s release, and they can link crates of any supported editions together. +Edition changes only affect the way the compiler initially parses code. +Therefore, if you’re using Rust 2015 and one of your dependencies uses Rust +2018, your project will compile and be able to use that dependency. The opposite +situation, where your project uses Rust 2018 and a dependency uses Rust 2015, +works as well. To be clear: most features will be available on all editions. Developers using any Rust edition will continue to see improvements as new stable releases are @@ -51,6 +51,8 @@ made. However, in some cases, mainly when new keywords are added, some new features might only be available in later editions. You will need to switch editions if you want to take advantage of such features. -For more details, the [_Edition Guide_](https://doc.rust-lang.org/stable/edition-guide/) is a complete book -about editions that enumerates the differences between editions and explains -how to automatically upgrade your code to a new edition via `cargo fix`. +For more details, the +[_Edition Guide_](https://doc.rust-lang.org/stable/edition-guide/) is a complete +book about editions that enumerates the differences between editions and +explains how to automatically upgrade your code to a new edition via +`cargo fix`. diff --git a/src/appendix-06-translation.md b/src/appendix-06-translation.md index 0a8077b9db..351034cc9b 100644 --- a/src/appendix-06-translation.md +++ b/src/appendix-06-translation.md @@ -5,24 +5,26 @@ For resources in languages other than English. Most are still in progress; see [label]: https://github.com/rust-lang/book/issues?q=is%3Aopen+is%3Aissue+label%3ATranslations -- [Português](https://github.com/rust-br/rust-book-pt-br) (BR) -- [Português](https://github.com/nunojesus/rust-book-pt-pt) (PT) -- [简体中文](https://github.com/KaiserY/trpl-zh-cn) -- [正體中文](https://github.com/rust-tw/book-tw) -- [Українська](https://rust-lang-ua.github.io/rustbook_ukrainian) -- [Español](https://github.com/thecodix/book), [alternate](https://github.com/ManRR/rust-book-es), [Español por RustLangES](https://github.com/RustLangES/rust-book-es) -- [Русский](https://github.com/rust-lang-ru/book) -- [한국어](https://github.com/rinthel/rust-lang-book-ko) -- [日本語](https://github.com/rust-lang-ja/book-ja) -- [Français](https://github.com/Jimskapt/rust-book-fr) -- [Polski](https://github.com/paytchoo/book-pl) -- [Cebuano](https://github.com/agentzero1/book) -- [Tagalog](https://github.com/josephace135/book) -- [Esperanto](https://github.com/psychoslave/Rust-libro) -- [ελληνική](https://github.com/TChatzigiannakis/rust-book-greek) -- [Svenska](https://github.com/sebras/book) -- [Farsi](https://github.com/RustFarsi/book) -- [Deutsch](https://github.com/rust-lang-de/rustbook-de) -- [हिंदी](https://github.com/venkatarun95/rust-book-hindi) -- [ไทย](https://github.com/rust-lang-th/book-th) -- [Danske](https://github.com/DanKHansen/book-dk) +* [Português](https://github.com/rust-br/rust-book-pt-br) (BR) +* [Português](https://github.com/nunojesus/rust-book-pt-pt) (PT) +* [简体中文](https://github.com/KaiserY/trpl-zh-cn) +* [正體中文](https://github.com/rust-tw/book-tw) +* [Українська](https://rust-lang-ua.github.io/rustbook_ukrainian) +* [Español](https://github.com/thecodix/book), + [alternate](https://github.com/ManRR/rust-book-es), + [Español por RustLangES](https://github.com/RustLangES/rust-book-es) +* [Русский](https://github.com/rust-lang-ru/book) +* [한국어](https://github.com/rinthel/rust-lang-book-ko) +* [日本語](https://github.com/rust-lang-ja/book-ja) +* [Français](https://github.com/Jimskapt/rust-book-fr) +* [Polski](https://github.com/paytchoo/book-pl) +* [Cebuano](https://github.com/agentzero1/book) +* [Tagalog](https://github.com/josephace135/book) +* [Esperanto](https://github.com/psychoslave/Rust-libro) +* [ελληνική](https://github.com/TChatzigiannakis/rust-book-greek) +* [Svenska](https://github.com/sebras/book) +* [Farsi](https://github.com/RustFarsi/book) +* [Deutsch](https://github.com/rust-lang-de/rustbook-de) +* [हिंदी](https://github.com/venkatarun95/rust-book-hindi) +* [ไทย](https://github.com/rust-lang-th/book-th) +* [Danske](https://github.com/DanKHansen/book-dk) diff --git a/src/appendix-07-nightly-rust.md b/src/appendix-07-nightly-rust.md index 5246d72825..21a8ad19e3 100644 --- a/src/appendix-07-nightly-rust.md +++ b/src/appendix-07-nightly-rust.md @@ -5,16 +5,16 @@ developer. ### Stability Without Stagnation -As a language, Rust cares a _lot_ about the stability of your code. We want -Rust to be a rock-solid foundation you can build on, and if things were -constantly changing, that would be impossible. At the same time, if we can’t -experiment with new features, we may not find out important flaws until after -their release, when we can no longer change things. +As a language, Rust cares a _lot_ about the stability of your code. We want Rust +to be a rock-solid foundation you can build on, and if things were constantly +changing, that would be impossible. At the same time, if we can’t experiment +with new features, we may not find out important flaws until after their +release, when we can no longer change things. -Our solution to this problem is what we call “stability without stagnation”, -and our guiding principle is this: you should never have to fear upgrading to a -new version of stable Rust. Each upgrade should be painless, but should also -bring you new features, fewer bugs, and faster compile times. +Our solution to this problem is what we call “stability without stagnation”, and +our guiding principle is this: you should never have to fear upgrading to a new +version of stable Rust. Each upgrade should be painless, but should also bring +you new features, fewer bugs, and faster compile times. ### Choo, Choo! Release Channels and Riding the Trains @@ -23,20 +23,20 @@ done on the `master` branch of the Rust repository. Releases follow a software release train model, which has been used by Cisco IOS and other software projects. There are three _release channels_ for Rust: -- Nightly -- Beta -- Stable +* Nightly +* Beta +* Stable -Most Rust developers primarily use the stable channel, but those who want to -try out experimental new features may use nightly or beta. +Most Rust developers primarily use the stable channel, but those who want to try +out experimental new features may use nightly or beta. -Here’s an example of how the development and release process works: let’s -assume that the Rust team is working on the release of Rust 1.5. That release -happened in December of 2015, but it will provide us with realistic version -numbers. A new feature is added to Rust: a new commit lands on the `master` -branch. Each night, a new nightly version of Rust is produced. Every day is a -release day, and these releases are created by our release infrastructure -automatically. So as time passes, our releases look like this, once a night: +Here’s an example of how the development and release process works: let’s assume +that the Rust team is working on the release of Rust 1.5. That release happened +in December of 2015, but it will provide us with realistic version numbers. A +new feature is added to Rust: a new commit lands on the `master` branch. Each +night, a new nightly version of Rust is produced. Every day is a release day, +and these releases are created by our release infrastructure automatically. So +as time passes, our releases look like this, once a night: ```text nightly: * - - * - - * @@ -63,9 +63,9 @@ beta: * ``` Let’s say a regression is found. Good thing we had some time to test the beta -release before the regression snuck into a stable release! The fix is applied -to `master`, so that nightly is fixed, and then the fix is backported to the -`beta` branch, and a new release of beta is produced: +release before the regression snuck into a stable release! The fix is applied to +`master`, so that nightly is fixed, and then the fix is backported to the `beta` +branch, and a new release of beta is produced: ```text nightly: * - - * - - * - - * - - * - - * @@ -109,9 +109,9 @@ to worry: another one is happening in a short time! This helps reduce pressure to sneak possibly unpolished features in close to the release deadline. Thanks to this process, you can always check out the next build of Rust and -verify for yourself that it’s easy to upgrade to: if a beta release doesn’t -work as expected, you can report it to the team and get it fixed before the -next stable release happens! Breakage in a beta release is relatively rare, but +verify for yourself that it’s easy to upgrade to: if a beta release doesn’t work +as expected, you can report it to the team and get it fixed before the next +stable release happens! Breakage in a beta release is relatively rare, but `rustc` is still a piece of software, and bugs do exist. ### Maintenance time @@ -133,8 +133,8 @@ appropriate flag to opt in. If you’re using a beta or stable release of Rust, you can’t use any feature flags. This is the key that allows us to get practical use with new features before we declare them stable forever. Those who wish to opt into the bleeding -edge can do so, and those who want a rock-solid experience can stick with -stable and know that their code won’t break. Stability without stagnation. +edge can do so, and those who want a rock-solid experience can stick with stable +and know that their code won’t break. Stability without stagnation. This book only contains information about stable features, as in-progress features are still changing, and surely they’ll be different between when this @@ -151,9 +151,9 @@ install nightly, for example: $ rustup toolchain install nightly ``` -You can see all of the _toolchains_ (releases of Rust and associated -components) you have installed with `rustup` as well. Here’s an example on one -of your authors’ Windows computer: +You can see all of the _toolchains_ (releases of Rust and associated components) +you have installed with `rustup` as well. Here’s an example on one of your +authors’ Windows computer: ```powershell > rustup toolchain list @@ -163,10 +163,10 @@ nightly-x86_64-pc-windows-msvc ``` As you can see, the stable toolchain is the default. Most Rust users use stable -most of the time. You might want to use stable most of the time, but use -nightly on a specific project, because you care about a cutting-edge feature. -To do so, you can use `rustup override` in that project’s directory to set the -nightly toolchain as the one `rustup` should use when you’re in that directory: +most of the time. You might want to use stable most of the time, but use nightly +on a specific project, because you care about a cutting-edge feature. To do so, +you can use `rustup override` in that project’s directory to set the nightly +toolchain as the one `rustup` should use when you’re in that directory: ```console $ cd ~/projects/needs-nightly @@ -175,32 +175,32 @@ $ rustup override set nightly Now, every time you call `rustc` or `cargo` inside of _~/projects/needs-nightly_, `rustup` will make sure that you are using nightly -Rust, rather than your default of stable Rust. This comes in handy when you -have a lot of Rust projects! +Rust, rather than your default of stable Rust. This comes in handy when you have +a lot of Rust projects! ### The RFC Process and Teams -So how do you learn about these new features? Rust’s development model follows -a _Request For Comments (RFC) process_. If you’d like an improvement in Rust, -you can write up a proposal, called an RFC. +So how do you learn about these new features? Rust’s development model follows a +_Request For Comments (RFC) process_. If you’d like an improvement in Rust, you +can write up a proposal, called an RFC. Anyone can write RFCs to improve Rust, and the proposals are reviewed and -discussed by the Rust team, which is comprised of many topic subteams. There’s -a full list of the teams [on Rust’s website](https://www.rust-lang.org/governance), which includes teams for -each area of the project: language design, compiler implementation, -infrastructure, documentation, and more. The appropriate team reads the -proposal and the comments, writes some comments of their own, and eventually, -there’s consensus to accept or reject the feature. +discussed by the Rust team, which is comprised of many topic subteams. There’s a +full list of the teams +[on Rust’s website](https://www.rust-lang.org/governance), which includes teams +for each area of the project: language design, compiler implementation, +infrastructure, documentation, and more. The appropriate team reads the proposal +and the comments, writes some comments of their own, and eventually, there’s +consensus to accept or reject the feature. If the feature is accepted, an issue is opened on the Rust repository, and someone can implement it. The person who implements it very well may not be the person who proposed the feature in the first place! When the implementation is -ready, it lands on the `master` branch behind a feature gate, as we discussed -in the [“Unstable Features”](#unstable-features) section. - -After some time, once Rust developers who use nightly releases have been able -to try out the new feature, team members will discuss the feature, how it’s -worked out on nightly, and decide if it should make it into stable Rust or not. -If the decision is to move forward, the feature gate is removed, and the -feature is now considered stable! It rides the trains into a new stable release -of Rust. +ready, it lands on the `master` branch behind a feature gate, as we discussed in +the [“Unstable Features”](#unstable-features) section. + +After some time, once Rust developers who use nightly releases have been able to +try out the new feature, team members will discuss the feature, how it’s worked +out on nightly, and decide if it should make it into stable Rust or not. If the +decision is to move forward, the feature gate is removed, and the feature is now +considered stable! It rides the trains into a new stable release of Rust. diff --git a/src/ch00-00-introduction.md b/src/ch00-00-introduction.md index c2c5fa6ec2..168c9251c5 100644 --- a/src/ch00-00-introduction.md +++ b/src/ch00-00-introduction.md @@ -1,14 +1,14 @@ # Introduction -> Note: This edition of the book is the same as [The Rust Programming -> Language][nsprust] available in print and ebook format from [No Starch -> Press][nsp]. +> Note: This edition of the book is the same as +> [The Rust Programming Language][nsprust] available in print and ebook format +> from [No Starch Press][nsp]. [nsprust]: https://nostarch.com/rust-programming-language-2nd-edition [nsp]: https://nostarch.com/ -Welcome to _The Rust Programming Language_, an introductory book about Rust. -The Rust programming language helps you write faster, more reliable software. +Welcome to _The Rust Programming Language_, an introductory book about Rust. The +Rust programming language helps you write faster, more reliable software. High-level ergonomics and low-level control are often at odds in programming language design; Rust challenges that conflict. Through balancing powerful technical capacity and a great developer experience, Rust gives you the option @@ -26,20 +26,20 @@ Rust is proving to be a productive tool for collaborating among large teams of developers with varying levels of systems programming knowledge. Low-level code is prone to various subtle bugs, which in most other languages can be caught only through extensive testing and careful code review by experienced -developers. In Rust, the compiler plays a gatekeeper role by refusing to -compile code with these elusive bugs, including concurrency bugs. By working -alongside the compiler, the team can spend their time focusing on the program’s -logic rather than chasing down bugs. +developers. In Rust, the compiler plays a gatekeeper role by refusing to compile +code with these elusive bugs, including concurrency bugs. By working alongside +the compiler, the team can spend their time focusing on the program’s logic +rather than chasing down bugs. Rust also brings contemporary developer tools to the systems programming world: -- Cargo, the included dependency manager and build tool, makes adding, +* Cargo, the included dependency manager and build tool, makes adding, compiling, and managing dependencies painless and consistent across the Rust ecosystem. -- The Rustfmt formatting tool ensures a consistent coding style across +* The Rustfmt formatting tool ensures a consistent coding style across developers. -- The rust-analyzer powers Integrated Development Environment (IDE) - integration for code completion and inline error messages. +* The rust-analyzer powers Integrated Development Environment (IDE) integration + for code completion and inline error messages. By using these and other tools in the Rust ecosystem, developers can be productive while writing systems-level code. @@ -48,9 +48,9 @@ productive while writing systems-level code. Rust is for students and those who are interested in learning about systems concepts. Using Rust, many people have learned about topics like operating -systems development. The community is very welcoming and happy to answer -student questions. Through efforts such as this book, the Rust teams want to -make systems concepts more accessible to more people, especially those new to +systems development. The community is very welcoming and happy to answer student +questions. Through efforts such as this book, the Rust teams want to make +systems concepts more accessible to more people, especially those new to programming. ### Companies @@ -96,9 +96,9 @@ reading a book that specifically provides an introduction to programming. ## How to Use This Book In general, this book assumes that you’re reading it in sequence from front to -back. Later chapters build on concepts in earlier chapters, and earlier -chapters might not delve into details on a particular topic but will revisit -the topic in a later chapter. +back. Later chapters build on concepts in earlier chapters, and earlier chapters +might not delve into details on a particular topic but will revisit the topic in +a later chapter. You’ll find two kinds of chapters in this book: concept chapters and project chapters. In concept chapters, you’ll learn about an aspect of Rust. In project @@ -107,33 +107,33 @@ far. Chapters 2, 12, and 21 are project chapters; the rest are concept chapters. Chapter 1 explains how to install Rust, how to write a “Hello, world!” program, and how to use Cargo, Rust’s package manager and build tool. Chapter 2 is a -hands-on introduction to writing a program in Rust, having you build up a -number guessing game. Here we cover concepts at a high level, and later -chapters will provide additional detail. If you want to get your hands dirty -right away, Chapter 2 is the place for that. Chapter 3 covers Rust features -that are similar to those of other programming languages, and in Chapter 4 -you’ll learn about Rust’s ownership system. If you’re a particularly meticulous -learner who prefers to learn every detail before moving on to the next, you -might want to skip Chapter 2 and go straight to Chapter 3, returning to Chapter -2 when you’d like to work on a project applying the details you’ve learned. +hands-on introduction to writing a program in Rust, having you build up a number +guessing game. Here we cover concepts at a high level, and later chapters will +provide additional detail. If you want to get your hands dirty right away, +Chapter 2 is the place for that. Chapter 3 covers Rust features that are similar +to those of other programming languages, and in Chapter 4 you’ll learn about +Rust’s ownership system. If you’re a particularly meticulous learner who prefers +to learn every detail before moving on to the next, you might want to skip +Chapter 2 and go straight to Chapter 3, returning to Chapter 2 when you’d like +to work on a project applying the details you’ve learned. Chapter 5 discusses structs and methods, and Chapter 6 covers enums, `match` expressions, and the `if let` control flow construct. You’ll use structs and enums to make custom types in Rust. In Chapter 7, you’ll learn about Rust’s module system and about privacy rules -for organizing your code and its public Application Programming Interface -(API). Chapter 8 discusses some common collection data structures that the -standard library provides, such as vectors, strings, and hash maps. Chapter 9 -explores Rust’s error-handling philosophy and techniques. +for organizing your code and its public Application Programming Interface (API). +Chapter 8 discusses some common collection data structures that the standard +library provides, such as vectors, strings, and hash maps. Chapter 9 explores +Rust’s error-handling philosophy and techniques. Chapter 10 digs into generics, traits, and lifetimes, which give you the power to define code that applies to multiple types. Chapter 11 is all about testing, which even with Rust’s safety guarantees is necessary to ensure your program’s logic is correct. In Chapter 12, we’ll build our own implementation of a subset -of functionality from the `grep` command line tool that searches for text -within files. For this, we’ll use many of the concepts we discussed in the -previous chapters. +of functionality from the `grep` command line tool that searches for text within +files. For this, we’ll use many of the concepts we discussed in the previous +chapters. Chapter 13 explores closures and iterators: features of Rust that come from functional programming languages. In Chapter 14, we’ll examine Cargo in more @@ -157,27 +157,26 @@ more about lifetimes, traits, types, functions, and closures. In Chapter 21, we’ll complete a project in which we’ll implement a low-level multithreaded web server! -Finally, some appendices contain useful information about the language in a -more reference-like format. Appendix A covers Rust’s keywords, Appendix B -covers Rust’s operators and symbols, Appendix C covers derivable traits -provided by the standard library, Appendix D covers some useful development -tools, and Appendix E explains Rust editions. In Appendix F, you can find -translations of the book, and in Appendix G we’ll cover how Rust is made and -what nightly Rust is. +Finally, some appendices contain useful information about the language in a more +reference-like format. Appendix A covers Rust’s keywords, Appendix B covers +Rust’s operators and symbols, Appendix C covers derivable traits provided by the +standard library, Appendix D covers some useful development tools, and Appendix +E explains Rust editions. In Appendix F, you can find translations of the book, +and in Appendix G we’ll cover how Rust is made and what nightly Rust is. There is no wrong way to read this book: if you want to skip ahead, go for it! -You might have to jump back to earlier chapters if you experience any -confusion. But do whatever works for you. +You might have to jump back to earlier chapters if you experience any confusion. +But do whatever works for you. An important part of the process of learning Rust is learning how to read the error messages the compiler displays: these will guide you toward working code. As such, we’ll provide many examples that don’t compile along with the error -message the compiler will show you in each situation. Know that if you enter -and run a random example, it may not compile! Make sure you read the -surrounding text to see whether the example you’re trying to run is meant to -error. Ferris will also help you distinguish code that isn’t meant to work: +message the compiler will show you in each situation. Know that if you enter and +run a random example, it may not compile! Make sure you read the surrounding +text to see whether the example you’re trying to run is meant to error. Ferris +will also help you distinguish code that isn’t meant to work: | Ferris | Meaning | | ---------------------------------------------------------------------------------------------------------------- | ------------------------------------------------ | diff --git a/src/ch01-00-getting-started.md b/src/ch01-00-getting-started.md index ccb10e884e..ff5e324f7a 100644 --- a/src/ch01-00-getting-started.md +++ b/src/ch01-00-getting-started.md @@ -3,6 +3,6 @@ Let’s start your Rust journey! There’s a lot to learn, but every journey starts somewhere. In this chapter, we’ll discuss: -- Installing Rust on Linux, macOS, and Windows -- Writing a program that prints `Hello, world!` -- Using `cargo`, Rust’s package manager and build system +* Installing Rust on Linux, macOS, and Windows +* Writing a program that prints `Hello, world!` +* Using `cargo`, Rust’s package manager and build system diff --git a/src/ch01-01-installation.md b/src/ch01-01-installation.md index 55636c09b3..e16107844c 100644 --- a/src/ch01-01-installation.md +++ b/src/ch01-01-installation.md @@ -31,9 +31,9 @@ If you’re using Linux or macOS, open a terminal and enter the following comman $ curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh ``` -The command downloads a script and starts the installation of the `rustup` -tool, which installs the latest stable version of Rust. You might be prompted -for your password. If the install is successful, the following line will appear: +The command downloads a script and starts the installation of the `rustup` tool, +which installs the latest stable version of Rust. You might be prompted for your +password. If the install is successful, the following line will appear: ```text Rust is installed now. Great! @@ -104,9 +104,9 @@ In Linux and macOS, use: $ echo $PATH ``` -If that’s all correct and Rust still isn’t working, there are a number of -places you can get help. Find out how to get in touch with other Rustaceans (a -silly nickname we call ourselves) on [the community page][community]. +If that’s all correct and Rust still isn’t working, there are a number of places +you can get help. Find out how to get in touch with other Rustaceans (a silly +nickname we call ourselves) on [the community page][community]. ### Updating and Uninstalling @@ -126,9 +126,9 @@ $ rustup self uninstall ### Local Documentation -The installation of Rust also includes a local copy of the documentation so -that you can read it offline. Run `rustup doc` to open the local documentation -in your browser. +The installation of Rust also includes a local copy of the documentation so that +you can read it offline. Run `rustup doc` to open the local documentation in +your browser. Any time a type or function is provided by the standard library and you’re not sure what it does or how to use it, use the application programming interface @@ -139,8 +139,8 @@ sure what it does or how to use it, use the application programming interface This book makes no assumptions about what tools you use to author Rust code. Just about any text editor will get the job done! However, many text editors and integrated development environments (IDEs) have built-in support for Rust. You -can always find a fairly current list of many editors and IDEs on [the tools -page][tools] on the Rust website. +can always find a fairly current list of many editors and IDEs on +[the tools page][tools] on the Rust website. [otherinstall]: https://forge.rust-lang.org/infra/other-installation-methods.html [install]: https://www.rust-lang.org/tools/install diff --git a/src/ch01-02-hello-world.md b/src/ch01-02-hello-world.md index 600401d4d8..64d612c5f4 100644 --- a/src/ch01-02-hello-world.md +++ b/src/ch01-02-hello-world.md @@ -1,22 +1,22 @@ ## Hello, World! -Now that you’ve installed Rust, it’s time to write your first Rust program. -It’s traditional when learning a new language to write a little program that -prints the text `Hello, world!` to the screen, so we’ll do the same here! - -> Note: This book assumes basic familiarity with the command line. Rust makes -> no specific demands about your editing or tooling or where your code lives, so -> if you prefer to use an integrated development environment (IDE) instead of -> the command line, feel free to use your favorite IDE. Many IDEs now have some +Now that you’ve installed Rust, it’s time to write your first Rust program. It’s +traditional when learning a new language to write a little program that prints +the text `Hello, world!` to the screen, so we’ll do the same here! + +> Note: This book assumes basic familiarity with the command line. Rust makes no +> specific demands about your editing or tooling or where your code lives, so if +> you prefer to use an integrated development environment (IDE) instead of the +> command line, feel free to use your favorite IDE. Many IDEs now have some > degree of Rust support; check the IDE’s documentation for details. The Rust > team has been focusing on enabling great IDE support via `rust-analyzer`. See > [Appendix D][devtools] for more details. ### Creating a Project Directory -You’ll start by making a directory to store your Rust code. It doesn’t matter -to Rust where your code lives, but for the exercises and projects in this book, -we suggest making a _projects_ directory in your home directory and keeping all +You’ll start by making a directory to store your Rust code. It doesn’t matter to +Rust where your code lives, but for the exercises and projects in this book, we +suggest making a _projects_ directory in your home directory and keeping all your projects there. Open a terminal and enter the following commands to make a _projects_ directory @@ -107,10 +107,9 @@ line as the function declaration, adding one space in between. > Note: If you want to stick to a standard style across Rust projects, you can > use an automatic formatter tool called `rustfmt` to format your code in a -> particular style (more on `rustfmt` in -> [Appendix D][devtools]). The Rust team has included this tool -> with the standard Rust distribution, as `rustc` is, so it should already be -> installed on your computer! +> particular style (more on `rustfmt` in [Appendix D][devtools]). +> The Rust team has included this tool with the standard Rust distribution, as +> `rustc` is, so it should already be installed on your computer! The body of the `main` function holds the following code: @@ -125,9 +124,9 @@ First, Rust style is to indent with four spaces, not a tab. Second, `println!` calls a Rust macro. If it had called a function instead, it would be entered as `println` (without the `!`). We’ll discuss Rust macros in -more detail in Chapter 20. For now, you just need to know that using a `!` -means that you’re calling a macro instead of a normal function and that macros -don’t always follow the same rules as functions. +more detail in Chapter 20. For now, you just need to know that using a `!` means +that you’re calling a macro instead of a normal function and that macros don’t +always follow the same rules as functions. Third, you see the `"Hello, world!"` string. We pass this string as an argument to `println!`, and the string is printed to the screen. @@ -160,8 +159,8 @@ $ ls main main.rs ``` -On Linux and macOS, you’ll see two files. With PowerShell on Windows, you’ll -see the same three files that you would see using CMD. With CMD on Windows, you +On Linux and macOS, you’ll see two files. With PowerShell on Windows, you’ll see +the same three files that you would see using CMD. With CMD on Windows, you would enter the following: ```cmd @@ -173,24 +172,25 @@ main.rs This shows the source code file with the _.rs_ extension, the executable file (_main.exe_ on Windows, but _main_ on all other platforms), and, when using -Windows, a file containing debugging information with the _.pdb_ extension. -From here, you run the _main_ or _main.exe_ file, like this: +Windows, a file containing debugging information with the _.pdb_ extension. From +here, you run the _main_ or _main.exe_ file, like this: ```console $ ./main # or .\main.exe on Windows ``` -If your _main.rs_ is your “Hello, world!” program, this line prints `Hello, +If your _main.rs_ is your “Hello, world!” program, this line prints +`Hello, world!` to your terminal. If you’re more familiar with a dynamic language, such as Ruby, Python, or -JavaScript, you might not be used to compiling and running a program as -separate steps. Rust is an _ahead-of-time compiled_ language, meaning you can -compile a program and give the executable to someone else, and they can run it -even without having Rust installed. If you give someone a _.rb_, _.py_, or -_.js_ file, they need to have a Ruby, Python, or JavaScript implementation -installed (respectively). But in those languages, you only need one command to -compile and run your program. Everything is a trade-off in language design. +JavaScript, you might not be used to compiling and running a program as separate +steps. Rust is an _ahead-of-time compiled_ language, meaning you can compile a +program and give the executable to someone else, and they can run it even +without having Rust installed. If you give someone a _.rb_, _.py_, or _.js_ +file, they need to have a Ruby, Python, or JavaScript implementation installed +(respectively). But in those languages, you only need one command to compile and +run your program. Everything is a trade-off in language design. Just compiling with `rustc` is fine for simple programs, but as your project grows, you’ll want to manage all the options and make it easy to share your diff --git a/src/ch01-03-hello-cargo.md b/src/ch01-03-hello-cargo.md index 048ee864c5..c3d1b5fb56 100644 --- a/src/ch01-03-hello-cargo.md +++ b/src/ch01-03-hello-cargo.md @@ -1,20 +1,20 @@ ## Hello, Cargo! Cargo is Rust’s build system and package manager. Most Rustaceans use this tool -to manage their Rust projects because Cargo handles a lot of tasks for you, -such as building your code, downloading the libraries your code depends on, and +to manage their Rust projects because Cargo handles a lot of tasks for you, such +as building your code, downloading the libraries your code depends on, and building those libraries. (We call the libraries that your code needs _dependencies_.) The simplest Rust programs, like the one we’ve written so far, don’t have any dependencies. If we had built the “Hello, world!” project with Cargo, it would only use the part of Cargo that handles building your code. As you write more -complex Rust programs, you’ll add dependencies, and if you start a project -using Cargo, adding dependencies will be much easier to do. +complex Rust programs, you’ll add dependencies, and if you start a project using +Cargo, adding dependencies will be much easier to do. Because the vast majority of Rust projects use Cargo, the rest of this book -assumes that you’re using Cargo too. Cargo comes installed with Rust if you -used the official installers discussed in the +assumes that you’re using Cargo too. Cargo comes installed with Rust if you used +the official installers discussed in the [“Installation”][installation] section. If you installed Rust through some other means, check whether Cargo is installed by entering the following in your terminal: @@ -23,16 +23,17 @@ following in your terminal: $ cargo --version ``` -If you see a version number, you have it! If you see an error, such as `command +If you see a version number, you have it! If you see an error, such as +`command not found`, look at the documentation for your method of installation to determine how to install Cargo separately. ### Creating a Project with Cargo Let’s create a new project using Cargo and look at how it differs from our -original “Hello, world!” project. Navigate back to your _projects_ directory -(or wherever you decided to store your code). Then, on any operating system, -run the following: +original “Hello, world!” project. Navigate back to your _projects_ directory (or +wherever you decided to store your code). Then, on any operating system, run the +following: ```console $ cargo new hello_cargo @@ -47,8 +48,8 @@ Go into the _hello_cargo_ directory and list the files. You’ll see that Cargo has generated two files and one directory for us: a _Cargo.toml_ file and a _src_ directory with a _main.rs_ file inside. -It has also initialized a new Git repository along with a _.gitignore_ file. -Git files won’t be generated if you run `cargo new` within an existing Git +It has also initialized a new Git repository along with a _.gitignore_ file. Git +files won’t be generated if you run `cargo new` within an existing Git repository; you can override this behavior by using `cargo new --vcs=git`. > Note: Git is a common version control system. You can change `cargo new` to @@ -107,8 +108,8 @@ and we have a _Cargo.toml_ configuration file in the top directory. Cargo expects your source files to live inside the _src_ directory. The top-level project directory is just for README files, license information, configuration files, and anything else not related to your code. Using Cargo -helps you organize your projects. There’s a place for everything, and -everything is in its place. +helps you organize your projects. There’s a place for everything, and everything +is in its place. If you started a project that doesn’t use Cargo, as we did with the “Hello, world!” project, you can convert it to a project that does use Cargo. Move the @@ -138,9 +139,10 @@ $ ./target/debug/hello_cargo # or .\target\debug\hello_cargo.exe on Windows Hello, world! ``` -If all goes well, `Hello, world!` should print to the terminal. Running `cargo -build` for the first time also causes Cargo to create a new file at the top -level: _Cargo.lock_. This file keeps track of the exact versions of +If all goes well, `Hello, world!` should print to the terminal. Running +`cargo +build` for the first time also causes Cargo to create a new file at the +top level: _Cargo.lock_. This file keeps track of the exact versions of dependencies in your project. This project doesn’t have dependencies, so the file is a bit sparse. You won’t ever need to change this file manually; Cargo manages its contents for you. @@ -157,7 +159,8 @@ Hello, world! ``` Using `cargo run` is more convenient than having to remember to run `cargo -build` and then use the whole path to the binary, so most developers use `cargo +build` +and then use the whole path to the binary, so most developers use `cargo run`. Notice that this time we didn’t see output indicating that Cargo was compiling @@ -187,18 +190,18 @@ Why would you not want an executable? Often, `cargo check` is much faster than `cargo build` because it skips the step of producing an executable. If you’re continually checking your work while writing the code, using `cargo check` will speed up the process of letting you know if your project is still compiling! As -such, many Rustaceans run `cargo check` periodically as they write their -program to make sure it compiles. Then they run `cargo build` when they’re -ready to use the executable. +such, many Rustaceans run `cargo check` periodically as they write their program +to make sure it compiles. Then they run `cargo build` when they’re ready to use +the executable. Let’s recap what we’ve learned so far about Cargo: -- We can create a project using `cargo new`. -- We can build a project using `cargo build`. -- We can build and run a project in one step using `cargo run`. -- We can build a project without producing a binary to check for errors using +* We can create a project using `cargo new`. +* We can build a project using `cargo build`. +* We can build and run a project in one step using `cargo run`. +* We can build a project without producing a binary to check for errors using `cargo check`. -- Instead of saving the result of the build in the same directory as our code, +* Instead of saving the result of the build in the same directory as our code, Cargo stores it in the _target/debug_ directory. An additional advantage of using Cargo is that the commands are the same no @@ -207,16 +210,17 @@ longer provide specific instructions for Linux and macOS versus Windows. ### Building for Release -When your project is finally ready for release, you can use `cargo build ---release` to compile it with optimizations. This command will create an -executable in _target/release_ instead of _target/debug_. The optimizations -make your Rust code run faster, but turning them on lengthens the time it takes -for your program to compile. This is why there are two different profiles: one -for development, when you want to rebuild quickly and often, and another for -building the final program you’ll give to a user that won’t be rebuilt -repeatedly and that will run as fast as possible. If you’re benchmarking your -code’s running time, be sure to run `cargo build --release` and benchmark with -the executable in _target/release_. +When your project is finally ready for release, you can use +`cargo build +--release` to compile it with optimizations. This command will +create an executable in _target/release_ instead of _target/debug_. The +optimizations make your Rust code run faster, but turning them on lengthens the +time it takes for your program to compile. This is why there are two different +profiles: one for development, when you want to rebuild quickly and often, and +another for building the final program you’ll give to a user that won’t be +rebuilt repeatedly and that will run as fast as possible. If you’re benchmarking +your code’s running time, be sure to run `cargo build --release` and benchmark +with the executable in _target/release_. ### Cargo as Convention @@ -243,16 +247,16 @@ For more information about Cargo, check out [its documentation][cargo]. You’re already off to a great start on your Rust journey! In this chapter, you’ve learned how to: -- Install the latest stable version of Rust using `rustup` -- Update to a newer Rust version -- Open locally installed documentation -- Write and run a “Hello, world!” program using `rustc` directly -- Create and run a new project using the conventions of Cargo +* Install the latest stable version of Rust using `rustup` +* Update to a newer Rust version +* Open locally installed documentation +* Write and run a “Hello, world!” program using `rustc` directly +* Create and run a new project using the conventions of Cargo This is a great time to build a more substantial program to get used to reading -and writing Rust code. So, in Chapter 2, we’ll build a guessing game program. -If you would rather start by learning how common programming concepts work in -Rust, see Chapter 3 and then return to Chapter 2. +and writing Rust code. So, in Chapter 2, we’ll build a guessing game program. If +you would rather start by learning how common programming concepts work in Rust, +see Chapter 3 and then return to Chapter 2. [installation]: ch01-01-installation.html#installation [toml]: https://toml.io diff --git a/src/ch02-00-guessing-game-tutorial.md b/src/ch02-00-guessing-game-tutorial.md index e2e7054d83..1e4183f842 100644 --- a/src/ch02-00-guessing-game-tutorial.md +++ b/src/ch02-00-guessing-game-tutorial.md @@ -53,16 +53,16 @@ you. Check out the _src/main.rs_ file: {{#rustdoc_include ../listings/ch02-guessing-game-tutorial/no-listing-01-cargo-new/src/main.rs}} ``` -Now let’s compile this “Hello, world!” program and run it in the same step -using the `cargo run` command: +Now let’s compile this “Hello, world!” program and run it in the same step using +the `cargo run` command: ```console {{#include ../listings/ch02-guessing-game-tutorial/no-listing-01-cargo-new/output.txt}} ``` The `run` command comes in handy when you need to rapidly iterate on a project, -as we’ll do in this game, quickly testing each iteration before moving on to -the next one. +as we’ll do in this game, quickly testing each iteration before moving on to the +next one. Reopen the _src/main.rs_ file. You’ll be writing all the code in this file. @@ -82,8 +82,8 @@ _src/main.rs_. This code contains a lot of information, so let’s go over it line by line. To -obtain user input and then print the result as output, we need to bring the -`io` input/output library into scope. The `io` library comes from the standard +obtain user input and then print the result as output, we need to bring the `io` +input/output library into scope. The `io` library comes from the standard library, known as `std`: ```rust,ignore @@ -94,10 +94,10 @@ By default, Rust has a set of items defined in the standard library that it brings into the scope of every program. This set is called the _prelude_, and you can see everything in it [in the standard library documentation][prelude]. -If a type you want to use isn’t in the prelude, you have to bring that type -into scope explicitly with a `use` statement. Using the `std::io` library -provides you with a number of useful features, including the ability to accept -user input. +If a type you want to use isn’t in the prelude, you have to bring that type into +scope explicitly with a `use` statement. Using the `std::io` library provides +you with a number of useful features, including the ability to accept user +input. As you saw in Chapter 1, the `main` function is the entry point into the program: @@ -136,10 +136,9 @@ let apples = 5; This line creates a new variable named `apples` and binds it to the value 5. In Rust, variables are immutable by default, meaning once we give the variable a -value, the value won’t change. We’ll be discussing this concept in detail in -the [“Variables and Mutability”][variables-and-mutability] -section in Chapter 3. To make a variable mutable, we add `mut` before the -variable name: +value, the value won’t change. We’ll be discussing this concept in detail in the +[“Variables and Mutability”][variables-and-mutability] section in +Chapter 3. To make a variable mutable, we add `mut` before the variable name: ```rust,ignore let apples = 5; // immutable @@ -160,19 +159,18 @@ library that is a growable, UTF-8 encoded bit of text. The `::` syntax in the `::new` line indicates that `new` is an associated function of the `String` type. An _associated function_ is a function that’s -implemented on a type, in this case `String`. This `new` function creates a -new, empty string. You’ll find a `new` function on many types because it’s a -common name for a function that makes a new value of some kind. +implemented on a type, in this case `String`. This `new` function creates a new, +empty string. You’ll find a `new` function on many types because it’s a common +name for a function that makes a new value of some kind. In full, the `let mut guess = String::new();` line has created a mutable variable that is currently bound to a new, empty instance of a `String`. Whew! ### Receiving User Input -Recall that we included the input/output functionality from the standard -library with `use std::io;` on the first line of the program. Now we’ll call -the `stdin` function from the `io` module, which will allow us to handle user -input: +Recall that we included the input/output functionality from the standard library +with `use std::io;` on the first line of the program. Now we’ll call the `stdin` +function from the `io` module, which will allow us to handle user input: ```rust,ignore {{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-01/src/main.rs:read}} @@ -185,23 +183,22 @@ the program, we could still use the function by writing this function call as handle to the standard input for your terminal. Next, the line `.read_line(&mut guess)` calls the [`read_line`][read_line] method on the standard input handle to get input from the user. -We’re also passing `&mut guess` as the argument to `read_line` to tell it what -string to store the user input in. The full job of `read_line` is to take -whatever the user types into standard input and append that into a string -(without overwriting its contents), so we therefore pass that string as an -argument. The string argument needs to be mutable so the method can change the -string’s content. +ignore --> method on the standard input handle to get input from the user. We’re +also passing `&mut guess` as the argument to `read_line` to tell it what string +to store the user input in. The full job of `read_line` is to take whatever the +user types into standard input and append that into a string (without +overwriting its contents), so we therefore pass that string as an argument. The +string argument needs to be mutable so the method can change the string’s +content. The `&` indicates that this argument is a _reference_, which gives you a way to -let multiple parts of your code access one piece of data without needing to -copy that data into memory multiple times. References are a complex feature, -and one of Rust’s major advantages is how safe and easy it is to use -references. You don’t need to know a lot of those details to finish this -program. For now, all you need to know is that, like variables, references are -immutable by default. Hence, you need to write `&mut guess` rather than -`&guess` to make it mutable. (Chapter 4 will explain references more -thoroughly.) +let multiple parts of your code access one piece of data without needing to copy +that data into memory multiple times. References are a complex feature, and one +of Rust’s major advantages is how safe and easy it is to use references. You +don’t need to know a lot of those details to finish this program. For now, all +you need to know is that, like variables, references are immutable by default. +Hence, you need to write `&mut guess` rather than `&guess` to make it mutable. +(Chapter 4 will explain references more thoroughly.) @@ -225,8 +222,8 @@ io::stdin().read_line(&mut guess).expect("Failed to read line"); However, one long line is difficult to read, so it’s best to divide it. It’s often wise to introduce a newline and other whitespace to help break up long -lines when you call a method with the `.method_name()` syntax. Now let’s -discuss what this line does. +lines when you call a method with the `.method_name()` syntax. Now let’s discuss +what this line does. As mentioned earlier, `read_line` puts whatever the user enters into the string we pass to it, but it also returns a `Result` value. [`Result`][result] will cover enums in more detail. The purpose of these `Result` types is to encode error-handling information. -`Result`’s variants are `Ok` and `Err`. The `Ok` variant indicates the -operation was successful, and inside `Ok` is the successfully generated value. -The `Err` variant means the operation failed, and `Err` contains information -about how or why the operation failed. +`Result`’s variants are `Ok` and `Err`. The `Ok` variant indicates the operation +was successful, and inside `Ok` is the successfully generated value. The `Err` +variant means the operation failed, and `Err` contains information about how or +why the operation failed. Values of the `Result` type, like values of any type, have methods defined on them. An instance of `Result` has an [`expect` method][expect] -that you can call. If this instance of `Result` is an `Err` value, `expect` -will cause the program to crash and display the message that you passed as an +that you can call. If this instance of `Result` is an `Err` value, `expect` will +cause the program to crash and display the message that you passed as an argument to `expect`. If the `read_line` method returns an `Err`, it would -likely be the result of an error coming from the underlying operating system. -If this instance of `Result` is an `Ok` value, `expect` will take the return -value that `Ok` is holding and return just that value to you so you can use it. -In this case, that value is the number of bytes in the user’s input. +likely be the result of an error coming from the underlying operating system. If +this instance of `Result` is an `Ok` value, `expect` will take the return value +that `Ok` is holding and return just that value to you so you can use it. In +this case, that value is the number of bytes in the user’s input. If you don’t call `expect`, the program will compile, but you’ll get a warning: @@ -263,8 +260,8 @@ indicating that the program hasn’t handled a possible error. The right way to suppress the warning is to actually write error-handling code, but in our case we just want to crash this program when a problem occurs, so we -can use `expect`. You’ll learn about recovering from errors in [Chapter -9][recover]. +can use `expect`. You’ll learn about recovering from errors in +[Chapter 9][recover]. ### Printing Values with `println!` Placeholders @@ -356,11 +353,11 @@ In the _Cargo.toml_ file, everything that follows a header is part of that section that continues until another section starts. In `[dependencies]` you tell Cargo which external crates your project depends on and which versions of those crates you require. In this case, we specify the `rand` crate with the -semantic version specifier `0.8.5`. Cargo understands [Semantic -Versioning][semver] (sometimes called _SemVer_), which is a -standard for writing version numbers. The specifier `0.8.5` is actually -shorthand for `^0.8.5`, which means any version that is at least 0.8.5 but -below 0.9.0. +semantic version specifier `0.8.5`. Cargo understands +[Semantic Versioning][semver] (sometimes called _SemVer_), which +is a standard for writing version numbers. The specifier `0.8.5` is actually +shorthand for `^0.8.5`, which means any version that is at least 0.8.5 but below +0.9.0. Cargo considers these versions to have public APIs compatible with version 0.8.5, and this specification ensures you’ll get the latest patch release that @@ -413,19 +410,19 @@ system), and the lines may be in a different order. When we include an external dependency, Cargo fetches the latest versions of everything that dependency needs from the _registry_, which is a copy of data -from [Crates.io][cratesio]. Crates.io is where people in the Rust ecosystem -post their open source Rust projects for others to use. +from [Crates.io][cratesio]. Crates.io is where people in the Rust ecosystem post +their open source Rust projects for others to use. After updating the registry, Cargo checks the `[dependencies]` section and downloads any crates listed that aren’t already downloaded. In this case, although we only listed `rand` as a dependency, Cargo also grabbed other crates -that `rand` depends on to work. After downloading the crates, Rust compiles -them and then compiles the project with the dependencies available. +that `rand` depends on to work. After downloading the crates, Rust compiles them +and then compiles the project with the dependencies available. -If you immediately run `cargo build` again without making any changes, you -won’t get any output aside from the `Finished` line. Cargo knows it has already -downloaded and compiled the dependencies, and you haven’t changed anything -about them in your _Cargo.toml_ file. Cargo also knows that you haven’t changed +If you immediately run `cargo build` again without making any changes, you won’t +get any output aside from the `Finished` line. Cargo knows it has already +downloaded and compiled the dependencies, and you haven’t changed anything about +them in your _Cargo.toml_ file. Cargo also knows that you haven’t changed anything about your code, so it doesn’t recompile that either. With nothing to do, it simply exits. @@ -452,11 +449,10 @@ reuse what it has already downloaded and compiled for those. Cargo has a mechanism that ensures you can rebuild the same artifact every time you or anyone else builds your code: Cargo will use only the versions of the dependencies you specified until you indicate otherwise. For example, say that -next week version 0.8.6 of the `rand` crate comes out, and that version -contains an important bug fix, but it also contains a regression that will -break your code. To handle this, Rust creates the _Cargo.lock_ file the first -time you run `cargo build`, so we now have this in the _guessing_game_ -directory. +next week version 0.8.6 of the `rand` crate comes out, and that version contains +an important bug fix, but it also contains a regression that will break your +code. To handle this, Rust creates the _Cargo.lock_ file the first time you run +`cargo build`, so we now have this in the _guessing_game_ directory. When you build a project for the first time, Cargo figures out all the versions of the dependencies that fit the criteria and then writes them to the @@ -470,13 +466,12 @@ checked into source control with the rest of the code in your project. #### Updating a Crate to Get a New Version -When you _do_ want to update a crate, Cargo provides the command `update`, -which will ignore the _Cargo.lock_ file and figure out all the latest versions -that fit your specifications in _Cargo.toml_. Cargo will then write those -versions to the _Cargo.lock_ file. In this case, Cargo will only look for -versions greater than 0.8.5 and less than 0.9.0. If the `rand` crate has -released the two new versions 0.8.6 and 0.9.0, you would see the following if -you ran `cargo update`: +When you _do_ want to update a crate, Cargo provides the command `update`, which +will ignore the _Cargo.lock_ file and figure out all the latest versions that +fit your specifications in _Cargo.toml_. Cargo will then write those versions to +the _Cargo.lock_ file. In this case, Cargo will only look for versions greater +than 0.8.5 and less than 0.9.0. If the `rand` crate has released the two new +versions 0.8.6 and 0.9.0, you would see the following if you ran `cargo update`: and [its -ecosystem][doccratesio], which we’ll discuss in Chapter 14, but -for now, that’s all you need to know. Cargo makes it very easy to reuse +There’s a lot more to say about [Cargo][doccargo] and +[its ecosystem][doccratesio], which we’ll discuss in Chapter 14, +but for now, that’s all you need to know. Cargo makes it very easy to reuse libraries, so Rustaceans are able to write smaller projects that are assembled from a number of packages. @@ -528,28 +523,29 @@ random number generators implement, and this trait must be in scope for us to use those methods. Chapter 10 will cover traits in detail. Next, we’re adding two lines in the middle. In the first line, we call the -`rand::thread_rng` function that gives us the particular random number -generator we’re going to use: one that is local to the current thread of -execution and is seeded by the operating system. Then we call the `gen_range` -method on the random number generator. This method is defined by the `Rng` -trait that we brought into scope with the `use rand::Rng;` statement. The -`gen_range` method takes a range expression as an argument and generates a -random number in the range. The kind of range expression we’re using here takes -the form `start..=end` and is inclusive on the lower and upper bounds, so we -need to specify `1..=100` to request a number between 1 and 100. +`rand::thread_rng` function that gives us the particular random number generator +we’re going to use: one that is local to the current thread of execution and is +seeded by the operating system. Then we call the `gen_range` method on the +random number generator. This method is defined by the `Rng` trait that we +brought into scope with the `use rand::Rng;` statement. The `gen_range` method +takes a range expression as an argument and generates a random number in the +range. The kind of range expression we’re using here takes the form +`start..=end` and is inclusive on the lower and upper bounds, so we need to +specify `1..=100` to request a number between 1 and 100. > Note: You won’t just know which traits to use and which methods and functions > to call from a crate, so each crate has documentation with instructions for -> using it. Another neat feature of Cargo is that running the `cargo doc -> --open` command will build documentation provided by all your dependencies -> locally and open it in your browser. If you’re interested in other -> functionality in the `rand` crate, for example, run `cargo doc --open` and -> click `rand` in the sidebar on the left. +> using it. Another neat feature of Cargo is that running the +> `cargo doc +> --open` command will build documentation provided by all your +> dependencies locally and open it in your browser. If you’re interested in +> other functionality in the `rand` crate, for example, run `cargo doc --open` +> and click `rand` in the sidebar on the left. The second new line prints the secret number. This is useful while we’re -developing the program to be able to test it, but we’ll delete it from the -final version. It’s not much of a game if the program prints the answer as soon -as it starts! +developing the program to be able to test it, but we’ll delete it from the final +version. It’s not much of a game if the program prints the answer as soon as it +starts! Try running the program a few times: @@ -604,22 +600,22 @@ First we add another `use` statement, bringing a type called is another enum and has the variants `Less`, `Greater`, and `Equal`. These are the three outcomes that are possible when you compare two values. -Then we add five new lines at the bottom that use the `Ordering` type. The -`cmp` method compares two values and can be called on anything that can be -compared. It takes a reference to whatever you want to compare with: here it’s -comparing `guess` to `secret_number`. Then it returns a variant of the -`Ordering` enum we brought into scope with the `use` statement. We use a +Then we add five new lines at the bottom that use the `Ordering` type. The `cmp` +method compares two values and can be called on anything that can be compared. +It takes a reference to whatever you want to compare with: here it’s comparing +`guess` to `secret_number`. Then it returns a variant of the `Ordering` enum we +brought into scope with the `use` statement. We use a [`match`][match] expression to decide what to do next based on which variant of `Ordering` was returned from the call to `cmp` with the values in `guess` and `secret_number`. A `match` expression is made up of _arms_. An arm consists of a _pattern_ to match against, and the code that should be run if the value given to `match` -fits that arm’s pattern. Rust takes the value given to `match` and looks -through each arm’s pattern in turn. Patterns and the `match` construct are -powerful Rust features: they let you express a variety of situations your code -might encounter and they make sure you handle them all. These features will be -covered in detail in Chapter 6 and Chapter 19, respectively. +fits that arm’s pattern. Rust takes the value given to `match` and looks through +each arm’s pattern in turn. Patterns and the `match` construct are powerful Rust +features: they let you express a variety of situations your code might encounter +and they make sure you handle them all. These features will be covered in detail +in Chapter 6 and Chapter 19, respectively. Let’s walk through an example with the `match` expression we use here. Say that the user has guessed 50 and the randomly generated secret number this time is @@ -631,8 +627,8 @@ the `Ordering::Greater` value and starts checking each arm’s pattern. It looks at the first arm’s pattern, `Ordering::Less`, and sees that the value `Ordering::Greater` does not match `Ordering::Less`, so it ignores the code in that arm and moves to the next arm. The next arm’s pattern is -`Ordering::Greater`, which _does_ match `Ordering::Greater`! The associated -code in that arm will execute and print `Too big!` to the screen. The `match` +`Ordering::Greater`, which _does_ match `Ordering::Greater`! The associated code +in that arm will execute and print `Too big!` to the screen. The `match` expression ends after the first successful match, so it won’t look at the last arm in this scenario. @@ -649,8 +645,8 @@ anchor or snip comments The core of the error states that there are _mismatched types_. Rust has a strong, static type system. However, it also has type inference. When we wrote -`let mut guess = String::new()`, Rust was able to infer that `guess` should be -a `String` and didn’t make us write the type. The `secret_number`, on the other +`let mut guess = String::new()`, Rust was able to infer that `guess` should be a +`String` and didn’t make us write the type. The `secret_number`, on the other hand, is a number type. A few of Rust’s number types can have a value between 1 and 100: `i32`, a 32-bit number; `u32`, an unsigned 32-bit number; `i64`, a 64-bit number; as well as others. Unless otherwise specified, Rust defaults to @@ -674,8 +670,8 @@ The line is: let guess: u32 = guess.trim().parse().expect("Please type a number!"); ``` -We create a variable named `guess`. But wait, doesn’t the program already have -a variable named `guess`? It does, but helpfully Rust allows us to shadow the +We create a variable named `guess`. But wait, doesn’t the program already have a +variable named `guess`? It does, but helpfully Rust allows us to shadow the previous value of `guess` with a new one. _Shadowing_ lets us reuse the `guess` variable name rather than forcing us to create two unique variables, such as `guess_str` and `guess`, for example. We’ll cover this in more detail in @@ -699,8 +695,8 @@ another type. Here, we use it to convert from a string to a number. We need to tell Rust the exact number type we want by using `let guess: u32`. The colon (`:`) after `guess` tells Rust we’ll annotate the variable’s type. Rust has a few built-in number types; the `u32` seen here is an unsigned, 32-bit integer. -It’s a good default choice for a small positive number. You’ll learn about -other number types in [Chapter 3][integers]. +It’s a good default choice for a small positive number. You’ll learn about other +number types in [Chapter 3][integers]. Additionally, the `u32` annotation in this example program and the comparison with `secret_number` means Rust will infer that `secret_number` should be a @@ -711,7 +707,8 @@ The `parse` method will only work on characters that can logically be converted into numbers and so can easily cause errors. If, for example, the string contained `A👍%`, there would be no way to convert that to a number. Because it might fail, the `parse` method returns a `Result` type, much as the `read_line` -method does (discussed earlier in [“Handling Potential Failure with +method does (discussed earlier in +[“Handling Potential Failure with `Result`”](#handling-potential-failure-with-result)). We’ll treat this `Result` the same way by using the `expect` method again. If `parse` returns an `Err` `Result` variant because it couldn’t create a number from the @@ -752,8 +749,8 @@ Let’s change that by adding a loop! ## Allowing Multiple Guesses with Looping -The `loop` keyword creates an infinite loop. We’ll add a loop to give users -more chances at guessing the number: +The `loop` keyword creates an infinite loop. We’ll add a loop to give users more +chances at guessing the number: Filename: src/main.rs @@ -761,14 +758,15 @@ more chances at guessing the number: {{#rustdoc_include ../listings/ch02-guessing-game-tutorial/no-listing-04-looping/src/main.rs:here}} ``` -As you can see, we’ve moved everything from the guess input prompt onward into -a loop. Be sure to indent the lines inside the loop another four spaces each -and run the program again. The program will now ask for another guess forever, -which actually introduces a new problem. It doesn’t seem like the user can quit! +As you can see, we’ve moved everything from the guess input prompt onward into a +loop. Be sure to indent the lines inside the loop another four spaces each and +run the program again. The program will now ask for another guess forever, which +actually introduces a new problem. It doesn’t seem like the user can quit! The user could always interrupt the program by using the keyboard shortcut ctrl-c. But there’s another way to escape this insatiable -monster, as mentioned in the `parse` discussion in [“Comparing the Guess to the +monster, as mentioned in the `parse` discussion in +[“Comparing the Guess to the Secret Number”](#comparing-the-guess-to-the-secret-number): if the user enters a non-number answer, the program will crash. We can take advantage of that to allow the user to quit, as shown here: @@ -828,10 +826,10 @@ exiting the program, because the loop is the last part of `main`. ### Handling Invalid Input -To further refine the game’s behavior, rather than crashing the program when -the user inputs a non-number, let’s make the game ignore a non-number so the -user can continue guessing. We can do that by altering the line where `guess` -is converted from a `String` to a `u32`, as shown in Listing 2-5. +To further refine the game’s behavior, rather than crashing the program when the +user inputs a non-number, let’s make the game ignore a non-number so the user +can continue guessing. We can do that by altering the line where `guess` is +converted from a `String` to a `u32`, as shown in Listing 2-5. @@ -841,27 +839,27 @@ is converted from a `String` to a `u32`, as shown in Listing 2-5. -We switch from an `expect` call to a `match` expression to move from crashing -on an error to handling the error. Remember that `parse` returns a `Result` -type and `Result` is an enum that has the variants `Ok` and `Err`. We’re using -a `match` expression here, as we did with the `Ordering` result of the `cmp` +We switch from an `expect` call to a `match` expression to move from crashing on +an error to handling the error. Remember that `parse` returns a `Result` type +and `Result` is an enum that has the variants `Ok` and `Err`. We’re using a +`match` expression here, as we did with the `Ordering` result of the `cmp` method. -If `parse` is able to successfully turn the string into a number, it will -return an `Ok` value that contains the resultant number. That `Ok` value will -match the first arm’s pattern, and the `match` expression will just return the -`num` value that `parse` produced and put inside the `Ok` value. That number -will end up right where we want it in the new `guess` variable we’re creating. +If `parse` is able to successfully turn the string into a number, it will return +an `Ok` value that contains the resultant number. That `Ok` value will match the +first arm’s pattern, and the `match` expression will just return the `num` value +that `parse` produced and put inside the `Ok` value. That number will end up +right where we want it in the new `guess` variable we’re creating. If `parse` is _not_ able to turn the string into a number, it will return an -`Err` value that contains more information about the error. The `Err` value -does not match the `Ok(num)` pattern in the first `match` arm, but it does -match the `Err(_)` pattern in the second arm. The underscore, `_`, is a -catchall value; in this example, we’re saying we want to match all `Err` -values, no matter what information they have inside them. So the program will -execute the second arm’s code, `continue`, which tells the program to go to the -next iteration of the `loop` and ask for another guess. So, effectively, the -program ignores all errors that `parse` might encounter! +`Err` value that contains more information about the error. The `Err` value does +not match the `Ok(num)` pattern in the first `match` arm, but it does match the +`Err(_)` pattern in the second arm. The underscore, `_`, is a catchall value; in +this example, we’re saying we want to match all `Err` values, no matter what +information they have inside them. So the program will execute the second arm’s +code, `continue`, which tells the program to go to the next iteration of the +`loop` and ask for another guess. So, effectively, the program ignores all +errors that `parse` might encounter! Now everything in the program should work as expected. Let’s try it: @@ -915,12 +913,12 @@ At this point, you’ve successfully built the guessing game. Congratulations! ## Summary This project was a hands-on way to introduce you to many new Rust concepts: -`let`, `match`, functions, the use of external crates, and more. In the next -few chapters, you’ll learn about these concepts in more detail. Chapter 3 -covers concepts that most programming languages have, such as variables, data -types, and functions, and shows how to use them in Rust. Chapter 4 explores -ownership, a feature that makes Rust different from other languages. Chapter 5 -discusses structs and method syntax, and Chapter 6 explains how enums work. +`let`, `match`, functions, the use of external crates, and more. In the next few +chapters, you’ll learn about these concepts in more detail. Chapter 3 covers +concepts that most programming languages have, such as variables, data types, +and functions, and shows how to use them in Rust. Chapter 4 explores ownership, +a feature that makes Rust different from other languages. Chapter 5 discusses +structs and method syntax, and Chapter 6 explains how enums work. [prelude]: ../std/prelude/index.html [variables-and-mutability]: ch03-01-variables-and-mutability.html#variables-and-mutability diff --git a/src/ch03-00-common-programming-concepts.md b/src/ch03-00-common-programming-concepts.md index 21ca8c58da..e8b95625e6 100644 --- a/src/ch03-00-common-programming-concepts.md +++ b/src/ch03-00-common-programming-concepts.md @@ -3,8 +3,8 @@ This chapter covers concepts that appear in almost every programming language and how they work in Rust. Many programming languages have much in common at their core. None of the concepts presented in this chapter are unique to Rust, -but we’ll discuss them in the context of Rust and explain the conventions -around using these concepts. +but we’ll discuss them in the context of Rust and explain the conventions around +using these concepts. Specifically, you’ll learn about variables, basic types, functions, comments, and control flow. These foundations will be in every Rust program, and learning diff --git a/src/ch03-01-variables-and-mutability.md b/src/ch03-01-variables-and-mutability.md index e5d599c030..13d8cff640 100644 --- a/src/ch03-01-variables-and-mutability.md +++ b/src/ch03-01-variables-and-mutability.md @@ -1,19 +1,19 @@ ## Variables and Mutability -As mentioned in the [“Storing Values with -Variables”][storing-values-with-variables] section, by default, -variables are immutable. This is one of many nudges Rust gives you to write -your code in a way that takes advantage of the safety and easy concurrency that -Rust offers. However, you still have the option to make your variables mutable. -Let’s explore how and why Rust encourages you to favor immutability and why -sometimes you might want to opt out. +As mentioned in the +[“Storing Values with Variables”][storing-values-with-variables] +section, by default, variables are immutable. This is one of many nudges Rust +gives you to write your code in a way that takes advantage of the safety and +easy concurrency that Rust offers. However, you still have the option to make +your variables mutable. Let’s explore how and why Rust encourages you to favor +immutability and why sometimes you might want to opt out. When a variable is immutable, once a value is bound to a name, you can’t change that value. To illustrate this, generate a new project called _variables_ in your _projects_ directory by using `cargo new variables`. -Then, in your new _variables_ directory, open _src/main.rs_ and replace its -code with the following code, which won’t compile just yet: +Then, in your new _variables_ directory, open _src/main.rs_ and replace its code +with the following code, which won’t compile just yet: Filename: src/main.rs @@ -29,29 +29,31 @@ regarding an immutability error, as shown in this output: ``` This example shows how the compiler helps you find errors in your programs. -Compiler errors can be frustrating, but really they only mean your program -isn’t safely doing what you want it to do yet; they do _not_ mean that you’re -not a good programmer! Experienced Rustaceans still get compiler errors. - -You received the error message `` cannot assign twice to immutable variable `x` `` because you tried to assign a second value to the immutable `x` variable. - -It’s important that we get compile-time errors when we attempt to change a -value that’s designated as immutable because this very situation can lead to -bugs. If one part of our code operates on the assumption that a value will -never change and another part of our code changes that value, it’s possible -that the first part of the code won’t do what it was designed to do. The cause -of this kind of bug can be difficult to track down after the fact, especially -when the second piece of code changes the value only _sometimes_. The Rust -compiler guarantees that when you state that a value won’t change, it really -won’t change, so you don’t have to keep track of it yourself. Your code is thus -easier to reason through. +Compiler errors can be frustrating, but really they only mean your program isn’t +safely doing what you want it to do yet; they do _not_ mean that you’re not a +good programmer! Experienced Rustaceans still get compiler errors. + +You received the error message +`` cannot assign twice to immutable variable `x` `` because you tried to assign +a second value to the immutable `x` variable. + +It’s important that we get compile-time errors when we attempt to change a value +that’s designated as immutable because this very situation can lead to bugs. If +one part of our code operates on the assumption that a value will never change +and another part of our code changes that value, it’s possible that the first +part of the code won’t do what it was designed to do. The cause of this kind of +bug can be difficult to track down after the fact, especially when the second +piece of code changes the value only _sometimes_. The Rust compiler guarantees +that when you state that a value won’t change, it really won’t change, so you +don’t have to keep track of it yourself. Your code is thus easier to reason +through. But mutability can be very useful, and can make code more convenient to write. -Although variables are immutable by default, you can make them mutable by -adding `mut` in front of the variable name as you did in [Chapter -2][storing-values-with-variables]. Adding `mut` also conveys -intent to future readers of the code by indicating that other parts of the code -will be changing this variable’s value. +Although variables are immutable by default, you can make them mutable by adding +`mut` in front of the variable name as you did in +[Chapter 2][storing-values-with-variables]. Adding `mut` also +conveys intent to future readers of the code by indicating that other parts of +the code will be changing this variable’s value. For example, let’s change _src/main.rs_ to the following: @@ -74,8 +76,8 @@ depends on what you think is clearest in that particular situation. ### Constants Like immutable variables, _constants_ are values that are bound to a name and -are not allowed to change, but there are a few differences between constants -and variables. +are not allowed to change, but there are a few differences between constants and +variables. First, you aren’t allowed to use `mut` with constants. Constants aren’t just immutable by default—they’re always immutable. You declare constants using the @@ -102,10 +104,10 @@ of minutes in an hour) by 3 (the number of hours we want to count in this program). Rust’s naming convention for constants is to use all uppercase with underscores between words. The compiler is able to evaluate a limited set of operations at compile time, which lets us choose to write out this value in a -way that’s easier to understand and verify, rather than setting this constant -to the value 10,800. See the [Rust Reference’s section on constant -evaluation][const-eval] for more information on what operations can be used -when declaring constants. +way that’s easier to understand and verify, rather than setting this constant to +the value 10,800. See the +[Rust Reference’s section on constant evaluation][const-eval] for more +information on what operations can be used when declaring constants. Constants are valid for the entire time a program runs, within the scope in which they were declared. This property makes constants useful for values in @@ -120,15 +122,15 @@ hardcoded value needed to be updated in the future. ### Shadowing -As you saw in the guessing game tutorial in [Chapter -2][comparing-the-guess-to-the-secret-number], you can declare a -new variable with the same name as a previous variable. Rustaceans say that the -first variable is _shadowed_ by the second, which means that the second -variable is what the compiler will see when you use the name of the variable. -In effect, the second variable overshadows the first, taking any uses of the -variable name to itself until either it itself is shadowed or the scope ends. -We can shadow a variable by using the same variable’s name and repeating the -use of the `let` keyword as follows: +As you saw in the guessing game tutorial in +[Chapter 2][comparing-the-guess-to-the-secret-number], you can +declare a new variable with the same name as a previous variable. Rustaceans say +that the first variable is _shadowed_ by the second, which means that the second +variable is what the compiler will see when you use the name of the variable. In +effect, the second variable overshadows the first, taking any uses of the +variable name to itself until either it itself is shadowed or the scope ends. We +can shadow a variable by using the same variable’s name and repeating the use of +the `let` keyword as follows: Filename: src/main.rs @@ -139,10 +141,10 @@ use of the `let` keyword as follows: This program first binds `x` to a value of `5`. Then it creates a new variable `x` by repeating `let x =`, taking the original value and adding `1` so the value of `x` is then `6`. Then, within an inner scope created with the curly -brackets, the third `let` statement also shadows `x` and creates a new -variable, multiplying the previous value by `2` to give `x` a value of `12`. -When that scope is over, the inner shadowing ends and `x` returns to being `6`. -When we run this program, it will output the following: +brackets, the third `let` statement also shadows `x` and creates a new variable, +multiplying the previous value by `2` to give `x` a value of `12`. When that +scope is over, the inner shadowing ends and `x` returns to being `6`. When we +run this program, it will output the following: ```console {{#include ../listings/ch03-common-programming-concepts/no-listing-03-shadowing/output.txt}} @@ -150,9 +152,9 @@ When we run this program, it will output the following: Shadowing is different from marking a variable as `mut` because we’ll get a compile-time error if we accidentally try to reassign to this variable without -using the `let` keyword. By using `let`, we can perform a few transformations -on a value but have the variable be immutable after those transformations have -been completed. +using the `let` keyword. By using `let`, we can perform a few transformations on +a value but have the variable be immutable after those transformations have been +completed. The other difference between `mut` and shadowing is that because we’re effectively creating a new variable when we use the `let` keyword again, we can @@ -164,11 +166,11 @@ inputting space characters, and then we want to store that input as a number: {{#rustdoc_include ../listings/ch03-common-programming-concepts/no-listing-04-shadowing-can-change-types/src/main.rs:here}} ``` -The first `spaces` variable is a string type and the second `spaces` variable -is a number type. Shadowing thus spares us from having to come up with -different names, such as `spaces_str` and `spaces_num`; instead, we can reuse -the simpler `spaces` name. However, if we try to use `mut` for this, as shown -here, we’ll get a compile-time error: +The first `spaces` variable is a string type and the second `spaces` variable is +a number type. Shadowing thus spares us from having to come up with different +names, such as `spaces_str` and `spaces_num`; instead, we can reuse the simpler +`spaces` name. However, if we try to use `mut` for this, as shown here, we’ll +get a compile-time error: ```rust,ignore,does_not_compile {{#rustdoc_include ../listings/ch03-common-programming-concepts/no-listing-05-mut-cant-change-types/src/main.rs:here}} diff --git a/src/ch03-02-data-types.md b/src/ch03-02-data-types.md index e6d88fbb1d..f85030626f 100644 --- a/src/ch03-02-data-types.md +++ b/src/ch03-02-data-types.md @@ -8,7 +8,8 @@ Keep in mind that Rust is a _statically typed_ language, which means that it must know the types of all variables at compile time. The compiler can usually infer what type we want to use based on the value and how we use it. In cases when many types are possible, such as when we converted a `String` to a numeric -type using `parse` in the [“Comparing the Guess to the Secret +type using `parse` in the +[“Comparing the Guess to the Secret Number”][comparing-the-guess-to-the-secret-number] section in Chapter 2, we must add a type annotation, like this: @@ -52,32 +53,32 @@ the type of an integer value. | 128-bit | `i128` | `u128` | | arch | `isize` | `usize` | -Each variant can be either signed or unsigned and has an explicit size. -_Signed_ and _unsigned_ refer to whether it’s possible for the number to be -negative—in other words, whether the number needs to have a sign with it -(signed) or whether it will only ever be positive and can therefore be -represented without a sign (unsigned). It’s like writing numbers on paper: when -the sign matters, a number is shown with a plus sign or a minus sign; however, -when it’s safe to assume the number is positive, it’s shown with no sign. -Signed numbers are stored using [two’s complement][twos-complement] representation. Each signed variant can store numbers from -(2n - 1) to 2n - 1 - 1 inclusive, where _n_ is the number of bits that variant uses. So an `i8` can store numbers from -(27) to 27 - 1, which equals --128 to 127. Unsigned variants can store numbers from 0 to 2n - 1, -so a `u8` can store numbers from 0 to 28 - 1, which equals 0 to 255. +-128 to 127. Unsigned variants can store numbers from 0 to 2n - 1, so +a `u8` can store numbers from 0 to 28 - 1, which equals 0 to 255. Additionally, the `isize` and `usize` types depend on the architecture of the -computer your program is running on, which is denoted in the table as “arch”: -64 bits if you’re on a 64-bit architecture and 32 bits if you’re on a 32-bit +computer your program is running on, which is denoted in the table as “arch”: 64 +bits if you’re on a 64-bit architecture and 32 bits if you’re on a 32-bit architecture. -You can write integer literals in any of the forms shown in Table 3-2. Note -that number literals that can be multiple numeric types allow a type suffix, -such as `57u8`, to designate the type. Number literals can also use `_` as a -visual separator to make the number easier to read, such as `1_000`, which will -have the same value as if you had specified `1000`. +You can write integer literals in any of the forms shown in Table 3-2. Note that +number literals that can be multiple numeric types allow a type suffix, such as +`57u8`, to designate the type. Number literals can also use `_` as a visual +separator to make the number easier to read, such as `1_000`, which will have +the same value as if you had specified `1000`. Table 3-2: Integer Literals in Rust @@ -90,9 +91,9 @@ have the same value as if you had specified `1000`. | Byte (`u8` only) | `b'A'` | So how do you know which type of integer to use? If you’re unsure, Rust’s -defaults are generally good places to start: integer types default to `i32`. -The primary situation in which you’d use `isize` or `usize` is when indexing -some sort of collection. +defaults are generally good places to start: integer types default to `i32`. The +primary situation in which you’d use `isize` or `usize` is when indexing some +sort of collection. > ##### Integer Overflow > @@ -102,7 +103,8 @@ some sort of collection. > When you’re compiling in debug mode, Rust includes checks for integer overflow > that cause your program to _panic_ at runtime if this behavior occurs. Rust > uses the term _panicking_ when a program exits with an error; we’ll discuss -> panics in more depth in the [“Unrecoverable Errors with +> panics in more depth in the +> [“Unrecoverable Errors with > `panic!`”][unrecoverable-errors-with-panic] section in Chapter > 9. > @@ -118,11 +120,11 @@ some sort of collection. > To explicitly handle the possibility of overflow, you can use these families > of methods provided by the standard library for primitive numeric types: > -> - Wrap in all modes with the `wrapping_*` methods, such as `wrapping_add`. -> - Return the `None` value if there is overflow with the `checked_*` methods. -> - Return the value and a boolean indicating whether there was overflow with +> * Wrap in all modes with the `wrapping_*` methods, such as `wrapping_add`. +> * Return the `None` value if there is overflow with the `checked_*` methods. +> * Return the value and a boolean indicating whether there was overflow with > the `overflowing_*` methods. -> - Saturate at the value’s minimum or maximum values with the `saturating_*` +> * Saturate at the value’s minimum or maximum values with the `saturating_*` > methods. #### Floating-Point Types @@ -157,9 +159,9 @@ how you’d use each numeric operation in a `let` statement: ``` Each expression in these statements uses a mathematical operator and evaluates -to a single value, which is then bound to a variable. [Appendix -B][appendix_b] contains a list of all operators that Rust -provides. +to a single value, which is then bound to a variable. +[Appendix B][appendix_b] contains a list of all operators that +Rust provides. #### The Boolean Type @@ -174,8 +176,8 @@ Rust is specified using `bool`. For example: ``` The main way to use Boolean values is through conditionals, such as an `if` -expression. We’ll cover how `if` expressions work in Rust in the [“Control -Flow”][control-flow] section. +expression. We’ll cover how `if` expressions work in Rust in the +[“Control Flow”][control-flow] section. #### The Character Type @@ -192,17 +194,18 @@ Note that we specify `char` literals with single quotes, as opposed to string literals, which use double quotes. Rust’s `char` type is four bytes in size and represents a Unicode Scalar Value, which means it can represent a lot more than just ASCII. Accented letters; Chinese, Japanese, and Korean characters; emoji; -and zero-width spaces are all valid `char` values in Rust. Unicode Scalar -Values range from `U+0000` to `U+D7FF` and `U+E000` to `U+10FFFF` inclusive. -However, a “character” isn’t really a concept in Unicode, so your human -intuition for what a “character” is may not match up with what a `char` is in -Rust. We’ll discuss this topic in detail in [“Storing UTF-8 Encoded Text with -Strings”][strings] in Chapter 8. +and zero-width spaces are all valid `char` values in Rust. Unicode Scalar Values +range from `U+0000` to `U+D7FF` and `U+E000` to `U+10FFFF` inclusive. However, a +“character” isn’t really a concept in Unicode, so your human intuition for what +a “character” is may not match up with what a `char` is in Rust. We’ll discuss +this topic in detail in +[“Storing UTF-8 Encoded Text with Strings”][strings] in +Chapter 8. ### Compound Types -_Compound types_ can group multiple values into one type. Rust has two -primitive compound types: tuples and arrays. +_Compound types_ can group multiple values into one type. Rust has two primitive +compound types: tuples and arrays. #### The Tuple Type @@ -222,8 +225,8 @@ type annotations in this example: ``` The variable `tup` binds to the entire tuple because a tuple is considered a -single compound element. To get the individual values out of a tuple, we can -use pattern matching to destructure a tuple value, like this: +single compound element. To get the individual values out of a tuple, we can use +pattern matching to destructure a tuple value, like this: Filename: src/main.rs @@ -234,8 +237,8 @@ use pattern matching to destructure a tuple value, like this: This program first creates a tuple and binds it to the variable `tup`. It then uses a pattern with `let` to take `tup` and turn it into three separate variables, `x`, `y`, and `z`. This is called _destructuring_ because it breaks -the single tuple into three parts. Finally, the program prints the value of -`y`, which is `6.4`. +the single tuple into three parts. Finally, the program prints the value of `y`, +which is `6.4`. We can also access a tuple element directly by using a period (`.`) followed by the index of the value we want to access. For example: @@ -257,9 +260,9 @@ return any other value. #### The Array Type -Another way to have a collection of multiple values is with an _array_. Unlike -a tuple, every element of an array must have the same type. Unlike arrays in -some other languages, arrays in Rust have a fixed length. +Another way to have a collection of multiple values is with an _array_. Unlike a +tuple, every element of an array must have the same type. Unlike arrays in some +other languages, arrays in Rust have a fixed length. We write the values in an array as a comma-separated list inside square brackets: @@ -289,8 +292,8 @@ let months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; ``` -You write an array’s type using square brackets with the type of each element, -a semicolon, and then the number of elements in the array, like so: +You write an array’s type using square brackets with the type of each element, a +semicolon, and then the number of elements in the array, like so: ```rust let a: [i32; 5] = [1, 2, 3, 4, 5]; @@ -314,8 +317,8 @@ more concise way. ##### Accessing Array Elements An array is a single chunk of memory of a known, fixed size that can be -allocated on the stack. You can access elements of an array using indexing, -like this: +allocated on the stack. You can access elements of an array using indexing, like +this: Filename: src/main.rs @@ -356,14 +359,14 @@ index out of bounds: the len is 5 but the index is 10 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace ``` -The program resulted in a _runtime_ error at the point of using an invalid -value in the indexing operation. The program exited with an error message and -didn’t execute the final `println!` statement. When you attempt to access an -element using indexing, Rust will check that the index you’ve specified is less -than the array length. If the index is greater than or equal to the length, -Rust will panic. This check has to happen at runtime, especially in this case, -because the compiler can’t possibly know what value a user will enter when they -run the code later. +The program resulted in a _runtime_ error at the point of using an invalid value +in the indexing operation. The program exited with an error message and didn’t +execute the final `println!` statement. When you attempt to access an element +using indexing, Rust will check that the index you’ve specified is less than the +array length. If the index is greater than or equal to the length, Rust will +panic. This check has to happen at runtime, especially in this case, because the +compiler can’t possibly know what value a user will enter when they run the code +later. This is an example of Rust’s memory safety principles in action. In many low-level languages, this kind of check is not done, and when you provide an diff --git a/src/ch03-03-how-functions-work.md b/src/ch03-03-how-functions-work.md index 8442825ab1..fecf49e55f 100644 --- a/src/ch03-03-how-functions-work.md +++ b/src/ch03-03-how-functions-work.md @@ -6,8 +6,8 @@ point of many programs. You’ve also seen the `fn` keyword, which allows you to declare new functions. Rust code uses _snake case_ as the conventional style for function and variable -names, in which all letters are lowercase and underscores separate words. -Here’s a program that contains an example function definition: +names, in which all letters are lowercase and underscores separate words. Here’s +a program that contains an example function definition: Filename: src/main.rs @@ -16,19 +16,19 @@ Here’s a program that contains an example function definition: ``` We define a function in Rust by entering `fn` followed by a function name and a -set of parentheses. The curly brackets tell the compiler where the function -body begins and ends. +set of parentheses. The curly brackets tell the compiler where the function body +begins and ends. -We can call any function we’ve defined by entering its name followed by a set -of parentheses. Because `another_function` is defined in the program, it can be +We can call any function we’ve defined by entering its name followed by a set of +parentheses. Because `another_function` is defined in the program, it can be called from inside the `main` function. Note that we defined `another_function` _after_ the `main` function in the source code; we could have defined it before as well. Rust doesn’t care where you define your functions, only that they’re defined somewhere in a scope that can be seen by the caller. -Let’s start a new binary project named _functions_ to explore functions -further. Place the `another_function` example in _src/main.rs_ and run it. You -should see the following output: +Let’s start a new binary project named _functions_ to explore functions further. +Place the `another_function` example in _src/main.rs_ and run it. You should see +the following output: ```console {{#include ../listings/ch03-common-programming-concepts/no-listing-16-functions/output.txt}} @@ -44,8 +44,8 @@ We can define functions to have _parameters_, which are special variables that are part of a function’s signature. When a function has parameters, you can provide it with concrete values for those parameters. Technically, the concrete values are called _arguments_, but in casual conversation, people tend to use -the words _parameter_ and _argument_ interchangeably for either the variables -in a function’s definition or the concrete values passed in when you call a +the words _parameter_ and _argument_ interchangeably for either the variables in +a function’s definition or the concrete values passed in when you call a function. In this version of `another_function` we add a parameter: @@ -64,11 +64,11 @@ Try running this program; you should get the following output: The declaration of `another_function` has one parameter named `x`. The type of `x` is specified as `i32`. When we pass `5` in to `another_function`, the -`println!` macro puts `5` where the pair of curly brackets containing `x` was -in the format string. +`println!` macro puts `5` where the pair of curly brackets containing `x` was in +the format string. -In function signatures, you _must_ declare the type of each parameter. This is -a deliberate decision in Rust’s design: requiring type annotations in function +In function signatures, you _must_ declare the type of each parameter. This is a +deliberate decision in Rust’s design: requiring type annotations in function definitions means the compiler almost never needs you to use them elsewhere in the code to figure out what type you mean. The compiler is also able to give more helpful error messages if it knows what types the function expects. @@ -88,7 +88,8 @@ named `unit_label` and is type `char`. The function then prints text containing both the `value` and the `unit_label`. Let’s try running this code. Replace the program currently in your _functions_ -project’s _src/main.rs_ file with the preceding example and run it using `cargo +project’s _src/main.rs_ file with the preceding example and run it using +`cargo run`: ```console @@ -102,15 +103,15 @@ the value for `unit_label`, the program output contains those values. Function bodies are made up of a series of statements optionally ending in an expression. So far, the functions we’ve covered haven’t included an ending -expression, but you have seen an expression as part of a statement. Because -Rust is an expression-based language, this is an important distinction to -understand. Other languages don’t have the same distinctions, so let’s look at -what statements and expressions are and how their differences affect the bodies -of functions. +expression, but you have seen an expression as part of a statement. Because Rust +is an expression-based language, this is an important distinction to understand. +Other languages don’t have the same distinctions, so let’s look at what +statements and expressions are and how their differences affect the bodies of +functions. -- **Statements** are instructions that perform some action and do not return - a value. -- **Expressions** evaluate to a resultant value. Let’s look at some examples. +* **Statements** are instructions that perform some action and do not return a + value. +* **Expressions** evaluate to a resultant value. Let’s look at some examples. We’ve actually already used statements and expressions. Creating a variable and assigning a value to it with the `let` keyword is a statement. In Listing 3-1, @@ -153,9 +154,9 @@ Expressions evaluate to a value and make up most of the rest of the code that you’ll write in Rust. Consider a math operation, such as `5 + 6`, which is an expression that evaluates to the value `11`. Expressions can be part of statements: in Listing 3-1, the `6` in the statement `let y = 6;` is an -expression that evaluates to the value `6`. Calling a function is an -expression. Calling a macro is an expression. A new scope block created with -curly brackets is an expression, for example: +expression that evaluates to the value `6`. Calling a function is an expression. +Calling a macro is an expression. A new scope block created with curly brackets +is an expression, for example: Filename: src/main.rs @@ -172,13 +173,12 @@ This expression: } ``` -is a block that, in this case, evaluates to `4`. That value gets bound to `y` -as part of the `let` statement. Note that the `x + 1` line doesn’t have a -semicolon at the end, which is unlike most of the lines you’ve seen so far. -Expressions do not include ending semicolons. If you add a semicolon to the end -of an expression, you turn it into a statement, and it will then not return a -value. Keep this in mind as you explore function return values and expressions -next. +is a block that, in this case, evaluates to `4`. That value gets bound to `y` as +part of the `let` statement. Note that the `x + 1` line doesn’t have a semicolon +at the end, which is unlike most of the lines you’ve seen so far. Expressions do +not include ending semicolons. If you add a semicolon to the end of an +expression, you turn it into a statement, and it will then not return a value. +Keep this in mind as you explore function return values and expressions next. ### Functions with Return Values @@ -187,8 +187,8 @@ values, but we must declare their type after an arrow (`->`). In Rust, the return value of the function is synonymous with the value of the final expression in the block of the body of a function. You can return early from a function by using the `return` keyword and specifying a value, but most -functions return the last expression implicitly. Here’s an example of a -function that returns a value: +functions return the last expression implicitly. Here’s an example of a function +that returns a value: Filename: src/main.rs @@ -215,9 +215,9 @@ that line is the same as the following: let x = 5; ``` -Second, the `five` function has no parameters and defines the type of the -return value, but the body of the function is a lonely `5` with no semicolon -because it’s an expression whose value we want to return. +Second, the `five` function has no parameters and defines the type of the return +value, but the body of the function is a lonely `5` with no semicolon because +it’s an expression whose value we want to return. Let’s look at another example: @@ -227,9 +227,9 @@ Let’s look at another example: {{#rustdoc_include ../listings/ch03-common-programming-concepts/no-listing-22-function-parameter-and-return/src/main.rs}} ``` -Running this code will print `The value of x is: 6`. But if we place a -semicolon at the end of the line containing `x + 1`, changing it from an -expression to a statement, we’ll get an error: +Running this code will print `The value of x is: 6`. But if we place a semicolon +at the end of the line containing `x + 1`, changing it from an expression to a +statement, we’ll get an error: Filename: src/main.rs @@ -245,8 +245,8 @@ Compiling this code produces an error, as follows: The main error message, `mismatched types`, reveals the core issue with this code. The definition of the function `plus_one` says that it will return an -`i32`, but statements don’t evaluate to a value, which is expressed by `()`, -the unit type. Therefore, nothing is returned, which contradicts the function +`i32`, but statements don’t evaluate to a value, which is expressed by `()`, the +unit type. Therefore, nothing is returned, which contradicts the function definition and results in an error. In this output, Rust provides a message to possibly help rectify this issue: it suggests removing the semicolon, which would fix the error. diff --git a/src/ch03-05-control-flow.md b/src/ch03-05-control-flow.md index ac89726fde..8dbc3f7186 100644 --- a/src/ch03-05-control-flow.md +++ b/src/ch03-05-control-flow.md @@ -8,8 +8,8 @@ the flow of execution of Rust code are `if` expressions and loops. ### `if` Expressions An `if` expression allows you to branch your code depending on conditions. You -provide a condition and then state, “If this condition is met, run this block -of code. If the condition is not met, do not run this block of code.” +provide a condition and then state, “If this condition is met, run this block of +code. If the condition is not met, do not run this block of code.” Create a new project called _branches_ in your _projects_ directory to explore the `if` expression. In the _src/main.rs_ file, input the following: @@ -21,19 +21,20 @@ the `if` expression. In the _src/main.rs_ file, input the following: ``` All `if` expressions start with the keyword `if`, followed by a condition. In -this case, the condition checks whether or not the variable `number` has a -value less than 5. We place the block of code to execute if the condition is -`true` immediately after the condition inside curly brackets. Blocks of code -associated with the conditions in `if` expressions are sometimes called _arms_, -just like the arms in `match` expressions that we discussed in the [“Comparing +this case, the condition checks whether or not the variable `number` has a value +less than 5. We place the block of code to execute if the condition is `true` +immediately after the condition inside curly brackets. Blocks of code associated +with the conditions in `if` expressions are sometimes called _arms_, just like +the arms in `match` expressions that we discussed in the +[“Comparing the Guess to the Secret Number”][comparing-the-guess-to-the-secret-number] section of Chapter 2. -Optionally, we can also include an `else` expression, which we chose to do -here, to give the program an alternative block of code to execute should the -condition evaluate to `false`. If you don’t provide an `else` expression and -the condition is `false`, the program will just skip the `if` block and move on -to the next bit of code. +Optionally, we can also include an `else` expression, which we chose to do here, +to give the program an alternative block of code to execute should the condition +evaluate to `false`. If you don’t provide an `else` expression and the condition +is `false`, the program will just skip the `if` block and move on to the next +bit of code. Try running this code; you should see the following output: @@ -74,8 +75,8 @@ error: The error indicates that Rust expected a `bool` but got an integer. Unlike languages such as Ruby and JavaScript, Rust will not automatically try to convert non-Boolean types to a Boolean. You must be explicit and always provide -`if` with a Boolean as its condition. If we want the `if` code block to run -only when a number is not equal to `0`, for example, we can change the `if` +`if` with a Boolean as its condition. If we want the `if` code block to run only +when a number is not equal to `0`, for example, we can change the `if` expression to the following: Filename: src/main.rs @@ -136,12 +137,12 @@ expression. Run this code to see what happens: ``` Remember that blocks of code evaluate to the last expression in them, and -numbers by themselves are also expressions. In this case, the value of the -whole `if` expression depends on which block of code executes. This means the -values that have the potential to be results from each arm of the `if` must be -the same type; in Listing 3-2, the results of both the `if` arm and the `else` -arm were `i32` integers. If the types are mismatched, as in the following -example, we’ll get an error: +numbers by themselves are also expressions. In this case, the value of the whole +`if` expression depends on which block of code executes. This means the values +that have the potential to be results from each arm of the `if` must be the same +type; in Listing 3-2, the results of both the `if` arm and the `else` arm were +`i32` integers. If the types are mismatched, as in the following example, we’ll +get an error: Filename: src/main.rs @@ -150,8 +151,8 @@ example, we’ll get an error: ``` When we try to compile this code, we’ll get an error. The `if` and `else` arms -have value types that are incompatible, and Rust indicates exactly where to -find the problem in the program: +have value types that are incompatible, and Rust indicates exactly where to find +the problem in the program: ```console {{#include ../listings/ch03-common-programming-concepts/no-listing-31-arms-must-return-same-type/output.txt}} @@ -163,15 +164,15 @@ have a single type, and Rust needs to know at compile time what type the `number` variable is, definitively. Knowing the type of `number` lets the compiler verify the type is valid everywhere we use `number`. Rust wouldn’t be able to do that if the type of `number` was only determined at runtime; the -compiler would be more complex and would make fewer guarantees about the code -if it had to keep track of multiple hypothetical types for any variable. +compiler would be more complex and would make fewer guarantees about the code if +it had to keep track of multiple hypothetical types for any variable. ### Repetition with Loops -It’s often useful to execute a block of code more than once. For this task, -Rust provides several _loops_, which will run through the code inside the loop -body to the end and then start immediately back at the beginning. To experiment -with loops, let’s make a new project called _loops_. +It’s often useful to execute a block of code more than once. For this task, Rust +provides several _loops_, which will run through the code inside the loop body +to the end and then start immediately back at the beginning. To experiment with +loops, let’s make a new project called _loops_. Rust has three kinds of loops: `loop`, `while`, and `for`. Let’s try each one. @@ -216,16 +217,16 @@ The symbol `^C` represents where you pressed ctrl-c. You may or may not see the word `again!` printed after the `^C`, depending on where the code was in the loop when it received the interrupt signal. -Fortunately, Rust also provides a way to break out of a loop using code. You -can place the `break` keyword within the loop to tell the program when to stop +Fortunately, Rust also provides a way to break out of a loop using code. You can +place the `break` keyword within the loop to tell the program when to stop executing the loop. Recall that we did this in the guessing game in the [“Quitting After a Correct Guess”][quitting-after-a-correct-guess] section of Chapter 2 to exit the program when the user won the game by guessing the correct number. We also used `continue` in the guessing game, which in a loop tells the program -to skip over any remaining code in this iteration of the loop and go to the -next iteration. +to skip over any remaining code in this iteration of the loop and go to the next +iteration. #### Returning Values from Loops @@ -233,20 +234,20 @@ One of the uses of a `loop` is to retry an operation you know might fail, such as checking whether a thread has completed its job. You might also need to pass the result of that operation out of the loop to the rest of your code. To do this, you can add the value you want returned after the `break` expression you -use to stop the loop; that value will be returned out of the loop so you can -use it, as shown here: +use to stop the loop; that value will be returned out of the loop so you can use +it, as shown here: ```rust {{#rustdoc_include ../listings/ch03-common-programming-concepts/no-listing-33-return-value-from-loop/src/main.rs}} ``` -Before the loop, we declare a variable named `counter` and initialize it to -`0`. Then we declare a variable named `result` to hold the value returned from -the loop. On every iteration of the loop, we add `1` to the `counter` variable, -and then check whether the `counter` is equal to `10`. When it is, we use the -`break` keyword with the value `counter * 2`. After the loop, we use a -semicolon to end the statement that assigns the value to `result`. Finally, we -print the value in `result`, which in this case is `20`. +Before the loop, we declare a variable named `counter` and initialize it to `0`. +Then we declare a variable named `result` to hold the value returned from the +loop. On every iteration of the loop, we add `1` to the `counter` variable, and +then check whether the `counter` is equal to `10`. When it is, we use the +`break` keyword with the value `counter * 2`. After the loop, we use a semicolon +to end the statement that assigns the value to `result`. Finally, we print the +value in `result`, which in this case is `20`. You can also `return` from inside a loop. While `break` only exits the current loop, `return` always exits the current function. @@ -254,10 +255,10 @@ loop, `return` always exits the current function. #### Loop Labels to Disambiguate Between Multiple Loops If you have loops within loops, `break` and `continue` apply to the innermost -loop at that point. You can optionally specify a _loop label_ on a loop that -you can then use with `break` or `continue` to specify that those keywords -apply to the labeled loop instead of the innermost loop. Loop labels must begin -with a single quote. Here’s an example with two nested loops: +loop at that point. You can optionally specify a _loop label_ on a loop that you +can then use with `break` or `continue` to specify that those keywords apply to +the labeled loop instead of the innermost loop. Loop labels must begin with a +single quote. Here’s an example with two nested loops: ```rust {{#rustdoc_include ../listings/ch03-common-programming-concepts/no-listing-32-5-loop-labels/src/main.rs}} @@ -266,7 +267,8 @@ with a single quote. Here’s an example with two nested loops: The outer loop has the label `'counting_up`, and it will count up from 0 to 2. The inner loop without a label counts down from 10 to 9. The first `break` that doesn’t specify a label will exit the inner loop only. The `break -'counting_up;` statement will exit the outer loop. This code prints: +'counting_up;` +statement will exit the outer loop. This code prints: ```console {{#rustdoc_include ../listings/ch03-common-programming-concepts/no-listing-32-5-loop-labels/output.txt}} @@ -292,8 +294,8 @@ time, and then, after the loop, print a message and exit. This construct eliminates a lot of nesting that would be necessary if you used -`loop`, `if`, `else`, and `break`, and it’s clearer. While a condition -evaluates to `true`, the code runs; otherwise, it exits the loop. +`loop`, `if`, `else`, and `break`, and it’s clearer. While a condition evaluates +to `true`, the code runs; otherwise, it exits the loop. #### Looping Through a Collection with `for` @@ -310,9 +312,9 @@ element in the array `a`. Here, the code counts up through the elements in the array. It starts at index -`0`, and then loops until it reaches the final index in the array (that is, -when `index < 5` is no longer `true`). Running this code will print every -element in the array: +`0`, and then loops until it reaches the final index in the array (that is, when +`index < 5` is no longer `true`). Running this code will print every element in +the array: ```console {{#include ../listings/ch03-common-programming-concepts/listing-03-04/output.txt}} @@ -346,16 +348,15 @@ chance of bugs that might result from going beyond the end of the array or not going far enough and missing some items. Using the `for` loop, you wouldn’t need to remember to change any other code if -you changed the number of values in the array, as you would with the method -used in Listing 3-4. +you changed the number of values in the array, as you would with the method used +in Listing 3-4. The safety and conciseness of `for` loops make them the most commonly used loop construct in Rust. Even in situations in which you want to run some code a -certain number of times, as in the countdown example that used a `while` loop -in Listing 3-3, most Rustaceans would use a `for` loop. The way to do that -would be to use a `Range`, provided by the standard library, which generates -all numbers in sequence starting from one number and ending before another -number. +certain number of times, as in the countdown example that used a `while` loop in +Listing 3-3, most Rustaceans would use a `for` loop. The way to do that would be +to use a `Range`, provided by the standard library, which generates all numbers +in sequence starting from one number and ending before another number. Here’s what the countdown would look like using a `for` loop and another method we’ve not yet talked about, `rev`, to reverse the range: @@ -370,15 +371,15 @@ This code is a bit nicer, isn’t it? ## Summary -You made it! This was a sizable chapter: you learned about variables, scalar -and compound data types, functions, comments, `if` expressions, and loops! To +You made it! This was a sizable chapter: you learned about variables, scalar and +compound data types, functions, comments, `if` expressions, and loops! To practice with the concepts discussed in this chapter, try building programs to do the following: -- Convert temperatures between Fahrenheit and Celsius. -- Generate the *n*th Fibonacci number. -- Print the lyrics to the Christmas carol “The Twelve Days of Christmas,” - taking advantage of the repetition in the song. +* Convert temperatures between Fahrenheit and Celsius. +* Generate the *n*th Fibonacci number. +* Print the lyrics to the Christmas carol “The Twelve Days of Christmas,” taking + advantage of the repetition in the song. When you’re ready to move on, we’ll talk about a concept in Rust that _doesn’t_ commonly exist in other programming languages: ownership. diff --git a/src/ch04-01-what-is-ownership.md b/src/ch04-01-what-is-ownership.md index 60d4cb4ea9..46d3eb7b69 100644 --- a/src/ch04-01-what-is-ownership.md +++ b/src/ch04-01-what-is-ownership.md @@ -1,13 +1,13 @@ ## What Is Ownership? -_Ownership_ is a set of rules that govern how a Rust program manages memory. -All programs have to manage the way they use a computer’s memory while running. -Some languages have garbage collection that regularly looks for no-longer-used -memory as the program runs; in other languages, the programmer must explicitly -allocate and free the memory. Rust uses a third approach: memory is managed -through a system of ownership with a set of rules that the compiler checks. If -any of the rules are violated, the program won’t compile. None of the features -of ownership will slow down your program while it’s running. +_Ownership_ is a set of rules that govern how a Rust program manages memory. All +programs have to manage the way they use a computer’s memory while running. Some +languages have garbage collection that regularly looks for no-longer-used memory +as the program runs; in other languages, the programmer must explicitly allocate +and free the memory. Rust uses a third approach: memory is managed through a +system of ownership with a set of rules that the compiler checks. If any of the +rules are violated, the program won’t compile. None of the features of ownership +will slow down your program while it’s running. Because ownership is a new concept for many programmers, it does take some time to get used to. The good news is that the more experienced you become with Rust @@ -23,22 +23,21 @@ strings. > > Many programming languages don’t require you to think about the stack and the > heap very often. But in a systems programming language like Rust, whether a -> value is on the stack or the heap affects how the language behaves and why -> you have to make certain decisions. Parts of ownership will be described in +> value is on the stack or the heap affects how the language behaves and why you +> have to make certain decisions. Parts of ownership will be described in > relation to the stack and the heap later in this chapter, so here is a brief > explanation in preparation. > > Both the stack and the heap are parts of memory available to your code to use -> at runtime, but they are structured in different ways. The stack stores -> values in the order it gets them and removes the values in the opposite -> order. This is referred to as _last in, first out_. Think of a stack of -> plates: when you add more plates, you put them on top of the pile, and when -> you need a plate, you take one off the top. Adding or removing plates from -> the middle or bottom wouldn’t work as well! Adding data is called _pushing -> onto the stack_, and removing data is called _popping off the stack_. All -> data stored on the stack must have a known, fixed size. Data with an unknown -> size at compile time or a size that might change must be stored on the heap -> instead. +> at runtime, but they are structured in different ways. The stack stores values +> in the order it gets them and removes the values in the opposite order. This +> is referred to as _last in, first out_. Think of a stack of plates: when you +> add more plates, you put them on top of the pile, and when you need a plate, +> you take one off the top. Adding or removing plates from the middle or bottom +> wouldn’t work as well! Adding data is called _pushing onto the stack_, and +> removing data is called _popping off the stack_. All data stored on the stack +> must have a known, fixed size. Data with an unknown size at compile time or a +> size that might change must be stored on the heap instead. > > The heap is less organized: when you put data on the heap, you request a > certain amount of space. The memory allocator finds an empty spot in the heap @@ -56,20 +55,18 @@ strings. > Pushing to the stack is faster than allocating on the heap because the > allocator never has to search for a place to store new data; that location is > always at the top of the stack. Comparatively, allocating space on the heap -> requires more work because the allocator must first find a big enough space -> to hold the data and then perform bookkeeping to prepare for the next -> allocation. +> requires more work because the allocator must first find a big enough space to +> hold the data and then perform bookkeeping to prepare for the next allocation. > > Accessing data in the heap is slower than accessing data on the stack because > you have to follow a pointer to get there. Contemporary processors are faster > if they jump around less in memory. Continuing the analogy, consider a server -> at a restaurant taking orders from many tables. It’s most efficient to get -> all the orders at one table before moving on to the next table. Taking an -> order from table A, then an order from table B, then one from A again, and -> then one from B again would be a much slower process. By the same token, a -> processor can do its job better if it works on data that’s close to other -> data (as it is on the stack) rather than farther away (as it can be on the -> heap). +> at a restaurant taking orders from many tables. It’s most efficient to get all +> the orders at one table before moving on to the next table. Taking an order +> from table A, then an order from table B, then one from A again, and then one +> from B again would be a much slower process. By the same token, a processor +> can do its job better if it works on data that’s close to other data (as it is +> on the stack) rather than farther away (as it can be on the heap). > > When your code calls a function, the values passed into the function > (including, potentially, pointers to data on the heap) and the function’s @@ -88,17 +85,17 @@ strings. First, let’s take a look at the ownership rules. Keep these rules in mind as we work through the examples that illustrate them: -- Each value in Rust has an _owner_. -- There can only be one owner at a time. -- When the owner goes out of scope, the value will be dropped. +* Each value in Rust has an _owner_. +* There can only be one owner at a time. +* When the owner goes out of scope, the value will be dropped. ### Variable Scope Now that we’re past basic Rust syntax, we won’t include all the `fn main() {` code in examples, so if you’re following along, make sure to put the following examples inside a `main` function manually. As a result, our examples will be a -bit more concise, letting us focus on the actual details rather than -boilerplate code. +bit more concise, letting us focus on the actual details rather than boilerplate +code. As a first example of ownership, we’ll look at the _scope_ of some variables. A scope is the range within a program for which an item is valid. Take the @@ -123,8 +120,8 @@ program with comments annotating where the variable `s` would be valid. In other words, there are two important points in time here: -- When `s` comes _into_ scope, it is valid. -- It remains valid until it goes _out of_ scope. +* When `s` comes _into_ scope, it is valid. +* It remains valid until it goes _out of_ scope. At this point, the relationship between scopes and when variables are valid is similar to that in other programming languages. Now we’ll build on top of this @@ -134,27 +131,27 @@ understanding by introducing the `String` type. To illustrate the rules of ownership, we need a data type that is more complex than those we covered in the [“Data Types”][data-types] section -of Chapter 3. The types covered previously are of a known size, can be stored -on the stack and popped off the stack when their scope is over, and can be -quickly and trivially copied to make a new, independent instance if another -part of code needs to use the same value in a different scope. But we want to -look at data that is stored on the heap and explore how Rust knows when to -clean up that data, and the `String` type is a great example. +of Chapter 3. The types covered previously are of a known size, can be stored on +the stack and popped off the stack when their scope is over, and can be quickly +and trivially copied to make a new, independent instance if another part of code +needs to use the same value in a different scope. But we want to look at data +that is stored on the heap and explore how Rust knows when to clean up that +data, and the `String` type is a great example. We’ll concentrate on the parts of `String` that relate to ownership. These -aspects also apply to other complex data types, whether they are provided by -the standard library or created by you. We’ll discuss `String` in more depth in +aspects also apply to other complex data types, whether they are provided by the +standard library or created by you. We’ll discuss `String` in more depth in [Chapter 8][ch8]. We’ve already seen string literals, where a string value is hardcoded into our program. String literals are convenient, but they aren’t suitable for every situation in which we may want to use text. One reason is that they’re -immutable. Another is that not every string value can be known when we write -our code: for example, what if we want to take user input and store it? For -these situations, Rust has a second string type, `String`. This type manages -data allocated on the heap and as such is able to store an amount of text that -is unknown to us at compile time. You can create a `String` from a string -literal using the `from` function, like so: +immutable. Another is that not every string value can be known when we write our +code: for example, what if we want to take user input and store it? For these +situations, Rust has a second string type, `String`. This type manages data +allocated on the heap and as such is able to store an amount of text that is +unknown to us at compile time. You can create a `String` from a string literal +using the `from` function, like so: ```rust let s = String::from("hello"); @@ -162,9 +159,10 @@ let s = String::from("hello"); The double colon `::` operator allows us to namespace this particular `from` function under the `String` type rather than using some sort of name like -`string_from`. We’ll discuss this syntax more in the [“Method -Syntax”][method-syntax] section of Chapter 5, and when we talk -about namespacing with modules in [“Paths for Referring to an Item in the +`string_from`. We’ll discuss this syntax more in the +[“Method Syntax”][method-syntax] section of Chapter 5, and when +we talk about namespacing with modules in +[“Paths for Referring to an Item in the Module Tree”][paths-module-tree] in Chapter 7. This kind of string _can_ be mutated: @@ -173,8 +171,8 @@ This kind of string _can_ be mutated: {{#rustdoc_include ../listings/ch04-understanding-ownership/no-listing-01-can-mutate-string/src/main.rs:here}} ``` -So, what’s the difference here? Why can `String` be mutated but literals -cannot? The difference is in how these two types deal with memory. +So, what’s the difference here? Why can `String` be mutated but literals cannot? +The difference is in how these two types deal with memory. ### Memory and Allocation @@ -186,11 +184,11 @@ binary for each piece of text whose size is unknown at compile time and whose size might change while running the program. With the `String` type, in order to support a mutable, growable piece of text, -we need to allocate an amount of memory on the heap, unknown at compile time, -to hold the contents. This means: +we need to allocate an amount of memory on the heap, unknown at compile time, to +hold the contents. This means: -- The memory must be requested from the memory allocator at runtime. -- We need a way of returning this memory to the allocator when we’re done with +* The memory must be requested from the memory allocator at runtime. +* We need a way of returning this memory to the allocator when we’re done with our `String`. That first part is done by us: when we call `String::from`, its implementation @@ -198,14 +196,14 @@ requests the memory it needs. This is pretty much universal in programming languages. However, the second part is different. In languages with a _garbage collector -(GC)_, the GC keeps track of and cleans up memory that isn’t being used -anymore, and we don’t need to think about it. In most languages without a GC, -it’s our responsibility to identify when memory is no longer being used and to -call code to explicitly free it, just as we did to request it. Doing this -correctly has historically been a difficult programming problem. If we forget, -we’ll waste memory. If we do it too early, we’ll have an invalid variable. If -we do it twice, that’s a bug too. We need to pair exactly one `allocate` with -exactly one `free`. +(GC)_, the GC keeps track of and cleans up memory that isn’t being used anymore, +and we don’t need to think about it. In most languages without a GC, it’s our +responsibility to identify when memory is no longer being used and to call code +to explicitly free it, just as we did to request it. Doing this correctly has +historically been a difficult programming problem. If we forget, we’ll waste +memory. If we do it too early, we’ll have an invalid variable. If we do it +twice, that’s a bug too. We need to pair exactly one `allocate` with exactly one +`free`. Rust takes a different path: the memory is automatically returned once the variable that owns it goes out of scope. Here’s a version of our scope example @@ -215,12 +213,12 @@ from Listing 4-1 using a `String` instead of a string literal: {{#rustdoc_include ../listings/ch04-understanding-ownership/no-listing-02-string-scope/src/main.rs:here}} ``` -There is a natural point at which we can return the memory our `String` needs -to the allocator: when `s` goes out of scope. When a variable goes out of -scope, Rust calls a special function for us. This function is called -[`drop`][drop], and it’s where the author of `String` can put -the code to return the memory. Rust calls `drop` automatically at the closing -curly bracket. +There is a natural point at which we can return the memory our `String` needs to +the allocator: when `s` goes out of scope. When a variable goes out of scope, +Rust calls a special function for us. This function is called +[`drop`][drop], and it’s where the author of `String` can put the +code to return the memory. Rust calls `drop` automatically at the closing curly +bracket. > Note: In C++, this pattern of deallocating resources at the end of an item’s > lifetime is sometimes called _Resource Acquisition Is Initialization (RAII)_. @@ -228,9 +226,9 @@ curly bracket. > patterns. This pattern has a profound impact on the way Rust code is written. It may seem -simple right now, but the behavior of code can be unexpected in more -complicated situations when we want to have multiple variables use the data -we’ve allocated on the heap. Let’s explore some of those situations now. +simple right now, but the behavior of code can be unexpected in more complicated +situations when we want to have multiple variables use the data we’ve allocated +on the heap. Let’s explore some of those situations now. @@ -265,11 +263,11 @@ This looks very similar, so we might assume that the way it works would be the same: that is, the second line would make a copy of the value in `s1` and bind it to `s2`. But this isn’t quite what happens. -Take a look at Figure 4-1 to see what is happening to `String` under the -covers. A `String` is made up of three parts, shown on the left: a pointer to -the memory that holds the contents of the string, a length, and a capacity. -This group of data is stored on the stack. On the right is the memory on the -heap that holds the contents. +Take a look at Figure 4-1 to see what is happening to `String` under the covers. +A `String` is made up of three parts, shown on the left: a pointer to the memory +that holds the contents of the string, a length, and a capacity. This group of +data is stored on the stack. On the right is the memory on the heap that holds +the contents. Two tables: the first table contains the representation of s1 on the
 stack, consisting of its length (5), capacity (5), and a pointer to the first
@@ -277,8 +275,8 @@ value in the second table. The second table contains the representation of the
 string data on the heap, byte by byte. -Figure 4-1: Representation in memory of a `String` -holding the value `"hello"` bound to `s1` +Figure 4-1: Representation in memory of a `String` holding +the value `"hello"` bound to `s1` The length is how much memory, in bytes, the contents of the `String` are currently using. The capacity is the total amount of memory, in bytes, that the @@ -313,9 +311,9 @@ do if Rust copied the heap data as well Earlier, we said that when a variable goes out of scope, Rust automatically calls the `drop` function and cleans up the heap memory for that variable. But Figure 4-2 shows both data pointers pointing to the same location. This is a -problem: when `s2` and `s1` go out of scope, they will both try to free the -same memory. This is known as a _double free_ error and is one of the memory -safety bugs we mentioned previously. Freeing memory twice can lead to memory +problem: when `s2` and `s1` go out of scope, they will both try to free the same +memory. This is known as a _double free_ error and is one of the memory safety +bugs we mentioned previously. Freeing memory twice can lead to memory corruption, which can potentially lead to security vulnerabilities. To ensure memory safety, after the line `let s2 = s1;`, Rust considers `s1` as @@ -336,10 +334,10 @@ invalidated reference: If you’ve heard the terms _shallow copy_ and _deep copy_ while working with other languages, the concept of copying the pointer, length, and capacity -without copying the data probably sounds like making a shallow copy. But -because Rust also invalidates the first variable, instead of being called a -shallow copy, it’s known as a _move_. In this example, we would say that `s1` -was _moved_ into `s2`. So, what actually happens is shown in Figure 4-4. +without copying the data probably sounds like making a shallow copy. But because +Rust also invalidates the first variable, instead of being called a shallow +copy, it’s known as a _move_. In this example, we would say that `s1` was +_moved_ into `s2`. So, what actually happens is shown in Figure 4-4. Three tables: tables s1 and s2 representing those strings on the
 stack, respectively, and both pointing to the same string data on the heap.
@@ -394,10 +392,10 @@ at the end, it will be `). If a type implements the `Copy` -trait, variables that use it do not move, but rather are trivially copied, -making them still valid after assignment to another variable. +Rust has a special annotation called the `Copy` trait that we can place on types +that are stored on the stack, as integers are (we’ll talk more about traits in +[Chapter 10][traits]). If a type implements the `Copy` trait, +variables that use it do not move, but rather are trivially copied, making them +still valid after assignment to another variable. Rust won’t let us annotate a type with `Copy` if the type, or any of its parts, has implemented the `Drop` trait. If the type needs something special to happen when the value goes out of scope and we add the `Copy` annotation to that type, we’ll get a compile-time error. To learn about how to add the `Copy` annotation -to your type to implement the trait, see [“Derivable -Traits”][derivable-traits] in Appendix C. +to your type to implement the trait, see +[“Derivable Traits”][derivable-traits] in Appendix C. So, what types implement the `Copy` trait? You can check the documentation for the given type to be sure, but as a general rule, any group of simple scalar values can implement `Copy`, and nothing that requires allocation or is some -form of resource can implement `Copy`. Here are some of the types that -implement `Copy`: - -- All the integer types, such as `u32`. -- The Boolean type, `bool`, with values `true` and `false`. -- All the floating-point types, such as `f64`. -- The character type, `char`. -- Tuples, if they only contain types that also implement `Copy`. For example, +form of resource can implement `Copy`. Here are some of the types that implement +`Copy`: + +* All the integer types, such as `u32`. +* The Boolean type, `bool`, with values `true` and `false`. +* All the floating-point types, such as `f64`. +* The character type, `char`. +* Tuples, if they only contain types that also implement `Copy`. For example, `(i32, i32)` implements `Copy`, but `(i32, String)` does not. ### Ownership and Functions @@ -474,8 +472,8 @@ showing where variables go into and out of scope. If we tried to use `s` after the call to `takes_ownership`, Rust would throw a compile-time error. These static checks protect us from mistakes. Try adding -code to `main` that uses `s` and `x` to see where you can use them and where -the ownership rules prevent you from doing so. +code to `main` that uses `s` and `x` to see where you can use them and where the +ownership rules prevent you from doing so. ### Return Values and Scope @@ -497,10 +495,10 @@ heap goes out of scope, the value will be cleaned up by `drop` unless ownership of the data has been moved to another variable. While this works, taking ownership and then returning ownership with every -function is a bit tedious. What if we want to let a function use a value but -not take ownership? It’s quite annoying that anything we pass in also needs to -be passed back if we want to use it again, in addition to any data resulting -from the body of the function that we might want to return as well. +function is a bit tedious. What if we want to let a function use a value but not +take ownership? It’s quite annoying that anything we pass in also needs to be +passed back if we want to use it again, in addition to any data resulting from +the body of the function that we might want to return as well. Rust does let us return multiple values using a tuple, as shown in Listing 4-5. diff --git a/src/ch04-02-references-and-borrowing.md b/src/ch04-02-references-and-borrowing.md index 22ceb2bdb1..33ba190f2b 100644 --- a/src/ch04-02-references-and-borrowing.md +++ b/src/ch04-02-references-and-borrowing.md @@ -1,13 +1,13 @@ ## References and Borrowing The issue with the tuple code in Listing 4-5 is that we have to return the -`String` to the calling function so we can still use the `String` after the -call to `calculate_length`, because the `String` was moved into -`calculate_length`. Instead, we can provide a reference to the `String` value. -A _reference_ is like a pointer in that it’s an address we can follow to access -the data stored at that address; that data is owned by some other variable. -Unlike a pointer, a reference is guaranteed to point to a valid value of a -particular type for the life of that reference. +`String` to the calling function so we can still use the `String` after the call +to `calculate_length`, because the `String` was moved into `calculate_length`. +Instead, we can provide a reference to the `String` value. A _reference_ is like +a pointer in that it’s an address we can follow to access the data stored at +that address; that data is owned by some other variable. Unlike a pointer, a +reference is guaranteed to point to a valid value of a particular type for the +life of that reference. Here is how you would define and use a `calculate_length` function that has a reference to an object as a parameter instead of taking ownership of the value: @@ -30,7 +30,8 @@ to some value without taking ownership of it. Figure 4-6 depicts this concept. for s1. The table for s1 contains the stack data for s1 and points to the string data on the heap." src="img/trpl04-06.svg" class="center" /> -Figure 4-6: A diagram of `&String s` pointing at `String +Figure 4-6: A diagram of `&String s` pointing at +`String s1` > Note: The opposite of referencing by using `&` is _dereferencing_, which is @@ -56,11 +57,10 @@ the parameter `s` is a reference. Let’s add some explanatory annotations: ``` The scope in which the variable `s` is valid is the same as any function -parameter’s scope, but the value pointed to by the reference is not dropped -when `s` stops being used, because `s` doesn’t have ownership. When functions -have references as parameters instead of the actual values, we won’t need to -return the values in order to give back ownership, because we never had -ownership. +parameter’s scope, but the value pointed to by the reference is not dropped when +`s` stops being used, because `s` doesn’t have ownership. When functions have +references as parameters instead of the actual values, we won’t need to return +the values in order to give back ownership, because we never had ownership. We call the action of creating a reference _borrowing_. As in real life, if a person owns something, you can borrow it from them. When you’re done, you have @@ -83,13 +83,13 @@ Here’s the error: {{#include ../listings/ch04-understanding-ownership/listing-04-06/output.txt}} ``` -Just as variables are immutable by default, so are references. We’re not -allowed to modify something we have a reference to. +Just as variables are immutable by default, so are references. We’re not allowed +to modify something we have a reference to. ### Mutable References -We can fix the code from Listing 4-6 to allow us to modify a borrowed value -with just a few small tweaks that use, instead, a _mutable reference_: +We can fix the code from Listing 4-6 to allow us to modify a borrowed value with +just a few small tweaks that use, instead, a _mutable reference_: @@ -100,13 +100,14 @@ with just a few small tweaks that use, instead, a _mutable reference_: First we change `s` to be `mut`. Then we create a mutable reference with `&mut -s` where we call the `change` function, and update the function signature to -accept a mutable reference with `some_string: &mut String`. This makes it very -clear that the `change` function will mutate the value it borrows. +s` +where we call the `change` function, and update the function signature to accept +a mutable reference with `some_string: &mut String`. This makes it very clear +that the `change` function will mutate the value it borrows. Mutable references have one big restriction: if you have a mutable reference to -a value, you can have no other references to that value. This code that -attempts to create two mutable references to `s` will fail: +a value, you can have no other references to that value. This code that attempts +to create two mutable references to `s` will fail: @@ -124,20 +125,20 @@ Here’s the error: This error says that this code is invalid because we cannot borrow `s` as mutable more than once at a time. The first mutable borrow is in `r1` and must -last until it’s used in the `println!`, but between the creation of that -mutable reference and its usage, we tried to create another mutable reference -in `r2` that borrows the same data as `r1`. +last until it’s used in the `println!`, but between the creation of that mutable +reference and its usage, we tried to create another mutable reference in `r2` +that borrows the same data as `r1`. The restriction preventing multiple mutable references to the same data at the same time allows for mutation but in a very controlled fashion. It’s something -that new Rustaceans struggle with because most languages let you mutate -whenever you’d like. The benefit of having this restriction is that Rust can -prevent data races at compile time. A _data race_ is similar to a race -condition and happens when these three behaviors occur: +that new Rustaceans struggle with because most languages let you mutate whenever +you’d like. The benefit of having this restriction is that Rust can prevent data +races at compile time. A _data race_ is similar to a race condition and happens +when these three behaviors occur: -- Two or more pointers access the same data at the same time. -- At least one of the pointers is being used to write to the data. -- There’s no mechanism being used to synchronize access to the data. +* Two or more pointers access the same data at the same time. +* At least one of the pointers is being used to write to the data. +* There’s no mechanism being used to synchronize access to the data. Data races cause undefined behavior and can be difficult to diagnose and fix when you’re trying to track them down at runtime; Rust prevents this problem by @@ -168,8 +169,8 @@ to the same value. Users of an immutable reference don’t expect the value to suddenly change out from under them! However, multiple immutable references are allowed because no -one who is just reading the data has the ability to affect anyone else’s -reading of the data. +one who is just reading the data has the ability to affect anyone else’s reading +of the data. Note that a reference’s scope starts from where it is introduced and continues through the last time that reference is used. For instance, this code will @@ -181,25 +182,24 @@ occurs before the mutable reference is introduced: ``` The scopes of the immutable references `r1` and `r2` end after the `println!` -where they are last used, which is before the mutable reference `r3` is -created. These scopes don’t overlap, so this code is allowed: the compiler can -tell that the reference is no longer being used at a point before the end of -the scope. +where they are last used, which is before the mutable reference `r3` is created. +These scopes don’t overlap, so this code is allowed: the compiler can tell that +the reference is no longer being used at a point before the end of the scope. -Even though borrowing errors may be frustrating at times, remember that it’s -the Rust compiler pointing out a potential bug early (at compile time rather -than at runtime) and showing you exactly where the problem is. Then you don’t -have to track down why your data isn’t what you thought it was. +Even though borrowing errors may be frustrating at times, remember that it’s the +Rust compiler pointing out a potential bug early (at compile time rather than at +runtime) and showing you exactly where the problem is. Then you don’t have to +track down why your data isn’t what you thought it was. ### Dangling References In languages with pointers, it’s easy to erroneously create a _dangling -pointer_—a pointer that references a location in memory that may have been -given to someone else—by freeing some memory while preserving a pointer to that -memory. In Rust, by contrast, the compiler guarantees that references will -never be dangling references: if you have a reference to some data, the -compiler will ensure that the data will not go out of scope before the -reference to the data does. +pointer_—a pointer that references a location in memory that may have been given +to someone else—by freeing some memory while preserving a pointer to that +memory. In Rust, by contrast, the compiler guarantees that references will never +be dangling references: if you have a reference to some data, the compiler will +ensure that the data will not go out of scope before the reference to the data +does. Let’s try to create a dangling reference to see how Rust prevents them with a compile-time error: @@ -219,8 +219,8 @@ Here’s the error: ``` This error message refers to a feature we haven’t covered yet: lifetimes. We’ll -discuss lifetimes in detail in Chapter 10. But, if you disregard the parts -about lifetimes, the message does contain the key to why this code is a problem: +discuss lifetimes in detail in Chapter 10. But, if you disregard the parts about +lifetimes, the message does contain the key to why this code is a problem: ```text this function's return type contains a borrowed value, but there is no value @@ -256,8 +256,8 @@ deallocated. Let’s recap what we’ve discussed about references: -- At any given time, you can have _either_ one mutable reference _or_ any - number of immutable references. -- References must always be valid. +* At any given time, you can have _either_ one mutable reference _or_ any number + of immutable references. +* References must always be valid. Next, we’ll look at a different kind of reference: slices. diff --git a/src/ch04-03-slices.md b/src/ch04-03-slices.md index 6a0bb0815d..8cd9b0a884 100644 --- a/src/ch04-03-slices.md +++ b/src/ch04-03-slices.md @@ -5,9 +5,9 @@ _Slices_ let you reference a contiguous sequence of elements in a slice is a kind of reference, so it does not have ownership. Here’s a small programming problem: write a function that takes a string of -words separated by spaces and returns the first word it finds in that string. -If the function doesn’t find a space in the string, the whole string must be -one word, so the entire string should be returned. +words separated by spaces and returns the first word it finds in that string. If +the function doesn’t find a space in the string, the whole string must be one +word, so the entire string should be returned. Let’s work through how we’d write the signature of this function without using slices, to understand the problem that slices will solve: @@ -43,23 +43,23 @@ Next, we create an iterator over the array of bytes using the `iter` method: {{#rustdoc_include ../listings/ch04-understanding-ownership/listing-04-07/src/main.rs:iter}} ``` -We’ll discuss iterators in more detail in [Chapter 13][ch13]. -For now, know that `iter` is a method that returns each element in a collection -and that `enumerate` wraps the result of `iter` and returns each element as -part of a tuple instead. The first element of the tuple returned from -`enumerate` is the index, and the second element is a reference to the element. -This is a bit more convenient than calculating the index ourselves. +We’ll discuss iterators in more detail in [Chapter 13][ch13]. For +now, know that `iter` is a method that returns each element in a collection and +that `enumerate` wraps the result of `iter` and returns each element as part of +a tuple instead. The first element of the tuple returned from `enumerate` is the +index, and the second element is a reference to the element. This is a bit more +convenient than calculating the index ourselves. Because the `enumerate` method returns a tuple, we can use patterns to -destructure that tuple. We’ll be discussing patterns more in [Chapter -6][ch6]. In the `for` loop, we specify a pattern that has `i` -for the index in the tuple and `&item` for the single byte in the tuple. -Because we get a reference to the element from `.iter().enumerate()`, we use -`&` in the pattern. +destructure that tuple. We’ll be discussing patterns more in +[Chapter 6][ch6]. In the `for` loop, we specify a pattern that +has `i` for the index in the tuple and `&item` for the single byte in the tuple. +Because we get a reference to the element from `.iter().enumerate()`, we use `&` +in the pattern. -Inside the `for` loop, we search for the byte that represents the space by -using the byte literal syntax. If we find a space, we return the position. -Otherwise, we return the length of the string by using `s.len()`. +Inside the `for` loop, we search for the byte that represents the space by using +the byte literal syntax. If we find a space, we return the position. Otherwise, +we return the length of the string by using `s.len()`. ```rust,ignore {{#rustdoc_include ../listings/ch04-understanding-ownership/listing-04-07/src/main.rs:inside_for}} @@ -69,8 +69,8 @@ We now have a way to find out the index of the end of the first word in the string, but there’s a problem. We’re returning a `usize` on its own, but it’s only a meaningful number in the context of the `&String`. In other words, because it’s a separate value from the `String`, there’s no guarantee that it -will still be valid in the future. Consider the program in Listing 4-8 that -uses the `first_word` function from Listing 4-7. +will still be valid in the future. Consider the program in Listing 4-8 that uses +the `first_word` function from Listing 4-7. @@ -81,9 +81,9 @@ uses the `first_word` function from Listing 4-7. This program compiles without any errors and would also do so if we used `word` -after calling `s.clear()`. Because `word` isn’t connected to the state of `s` -at all, `word` still contains the value `5`. We could use that value `5` with -the variable `s` to try to extract the first word out, but this would be a bug +after calling `s.clear()`. Because `word` isn’t connected to the state of `s` at +all, `word` still contains the value `5`. We could use that value `5` with the +variable `s` to try to extract the first word out, but this would be a bug because the contents of `s` have changed since we saved `5` in `word`. Having to worry about the index in `word` getting out of sync with the data in @@ -115,7 +115,8 @@ using a range within brackets by specifying `[starting_index..ending_index]`, where `starting_index` is the first position in the slice and `ending_index` is one more than the last position in the slice. Internally, the slice data structure stores the starting position and the length of the slice, which -corresponds to `ending_index` minus `starting_index`. So, in the case of `let +corresponds to `ending_index` minus `starting_index`. So, in the case of +`let world = &s[6..11];`, `world` would be a slice that contains a pointer to the byte at index 6 of `s` with a length value of `5`. @@ -130,8 +131,8 @@ src="img/trpl04-07.svg" class="center" style="width: 50%;" /> Figure 4-7: String slice referring to part of a `String` -With Rust’s `..` range syntax, if you want to start at index 0, you can drop -the value before the two periods. In other words, these are equal: +With Rust’s `..` range syntax, if you want to start at index 0, you can drop the +value before the two periods. In other words, these are equal: ```rust let s = String::from("hello"); @@ -140,8 +141,8 @@ let slice = &s[0..2]; let slice = &s[..2]; ``` -By the same token, if your slice includes the last byte of the `String`, you -can drop the trailing number. That means these are equal: +By the same token, if your slice includes the last byte of the `String`, you can +drop the trailing number. That means these are equal: ```rust let s = String::from("hello"); @@ -152,8 +153,8 @@ let slice = &s[3..len]; let slice = &s[3..]; ``` -You can also drop both values to take a slice of the entire string. So these -are equal: +You can also drop both values to take a slice of the entire string. So these are +equal: ```rust let s = String::from("hello"); @@ -166,13 +167,14 @@ let slice = &s[..]; > Note: String slice range indices must occur at valid UTF-8 character > boundaries. If you attempt to create a string slice in the middle of a -> multibyte character, your program will exit with an error. For the purposes -> of introducing string slices, we are assuming ASCII only in this section; a -> more thorough discussion of UTF-8 handling is in the [“Storing UTF-8 Encoded -> Text with Strings”][strings] section of Chapter 8. +> multibyte character, your program will exit with an error. For the purposes of +> introducing string slices, we are assuming ASCII only in this section; a more +> thorough discussion of UTF-8 handling is in the +> [“Storing UTF-8 Encoded Text with Strings”][strings] section of +> Chapter 8. -With all this information in mind, let’s rewrite `first_word` to return a -slice. The type that signifies “string slice” is written as `&str`: +With all this information in mind, let’s rewrite `first_word` to return a slice. +The type that signifies “string slice” is written as `&str`: @@ -198,9 +200,9 @@ fn second_word(s: &String) -> &str { ``` We now have a straightforward API that’s much harder to mess up because the -compiler will ensure the references into the `String` remain valid. Remember -the bug in the program in Listing 4-8, when we got the index to the end of the -first word but then cleared the string so our index was invalid? That code was +compiler will ensure the references into the `String` remain valid. Remember the +bug in the program in Listing 4-8, when we got the index to the end of the first +word but then cleared the string so our index was invalid? That code was logically incorrect but didn’t show any immediate errors. The problems would show up later if we kept trying to use the first word index with an emptied string. Slices make this bug impossible and let us know we have a problem with @@ -223,12 +225,12 @@ Here’s the compiler error: Recall from the borrowing rules that if we have an immutable reference to something, we cannot also take a mutable reference. Because `clear` needs to -truncate the `String`, it needs to get a mutable reference. The `println!` -after the call to `clear` uses the reference in `word`, so the immutable -reference must still be active at that point. Rust disallows the mutable -reference in `clear` and the immutable reference in `word` from existing at the -same time, and compilation fails. Not only has Rust made our API easier to use, -but it has also eliminated an entire class of errors at compile time! +truncate the `String`, it needs to get a mutable reference. The `println!` after +the call to `clear` uses the reference in `word`, so the immutable reference +must still be active at that point. Rust disallows the mutable reference in +`clear` and the immutable reference in `word` from existing at the same time, +and compilation fails. Not only has Rust made our API easier to use, but it has +also eliminated an entire class of errors at compile time! @@ -249,8 +251,8 @@ immutable reference. #### String Slices as Parameters -Knowing that you can take slices of literals and `String` values leads us to -one more improvement on `first_word`, and that’s its signature: +Knowing that you can take slices of literals and `String` values leads us to one +more improvement on `first_word`, and that’s its signature: ```rust,ignore fn first_word(s: &String) -> &str { @@ -287,8 +289,8 @@ makes our API more general and useful without losing any functionality: ### Other Slices -String slices, as you might imagine, are specific to strings. But there’s a -more general slice type too. Consider this array: +String slices, as you might imagine, are specific to strings. But there’s a more +general slice type too. Consider this array: ```rust let a = [1, 2, 3, 4, 5]; @@ -319,8 +321,8 @@ owner of data automatically clean up that data when the owner goes out of scope means you don’t have to write and debug extra code to get this control. Ownership affects how lots of other parts of Rust work, so we’ll talk about -these concepts further throughout the rest of the book. Let’s move on to -Chapter 5 and look at grouping pieces of data together in a `struct`. +these concepts further throughout the rest of the book. Let’s move on to Chapter +5 and look at grouping pieces of data together in a `struct`. [ch13]: ch13-02-iterators.html [ch6]: ch06-02-match.html#patterns-that-bind-to-values diff --git a/src/ch05-00-structs.md b/src/ch05-00-structs.md index ee064224d9..6d9794a94e 100644 --- a/src/ch05-00-structs.md +++ b/src/ch05-00-structs.md @@ -1,11 +1,11 @@ # Using Structs to Structure Related Data -A _struct_, or _structure_, is a custom data type that lets you package -together and name multiple related values that make up a meaningful group. If -you’re familiar with an object-oriented language, a _struct_ is like an -object’s data attributes. In this chapter, we’ll compare and contrast tuples -with structs to build on what you already know and demonstrate when structs are -a better way to group data. +A _struct_, or _structure_, is a custom data type that lets you package together +and name multiple related values that make up a meaningful group. If you’re +familiar with an object-oriented language, a _struct_ is like an object’s data +attributes. In this chapter, we’ll compare and contrast tuples with structs to +build on what you already know and demonstrate when structs are a better way to +group data. We’ll demonstrate how to define and instantiate structs. We’ll discuss how to define associated functions, especially the kind of associated functions called diff --git a/src/ch05-01-defining-structs.md b/src/ch05-01-defining-structs.md index 91a9392436..00c308ef90 100644 --- a/src/ch05-01-defining-structs.md +++ b/src/ch05-01-defining-structs.md @@ -27,9 +27,9 @@ stating the name of the struct and then add curly brackets containing _key: value_ pairs, where the keys are the names of the fields and the values are the data we want to store in those fields. We don’t have to specify the fields in the same order in which we declared them in the struct. In other words, the -struct definition is like a general template for the type, and instances fill -in that template with particular data to create values of the type. For -example, we can declare a particular user as shown in Listing 5-2. +struct definition is like a general template for the type, and instances fill in +that template with particular data to create values of the type. For example, we +can declare a particular user as shown in Listing 5-2. @@ -42,8 +42,8 @@ example, we can declare a particular user as shown in Listing 5-2. To get a specific value from a struct, we use dot notation. For example, to access this user’s email address, we use `user1.email`. If the instance is mutable, we can change a value by using the dot notation and assigning into a -particular field. Listing 5-3 shows how to change the value in the `email` -field of a mutable `User` instance. +particular field. Listing 5-3 shows how to change the value in the `email` field +of a mutable `User` instance. @@ -55,8 +55,8 @@ field of a mutable `User` instance. Note that the entire instance must be mutable; Rust doesn’t allow us to mark only certain fields as mutable. As with any expression, we can construct a new -instance of the struct as the last expression in the function body to -implicitly return that new instance. +instance of the struct as the last expression in the function body to implicitly +return that new instance. Listing 5-4 shows a `build_user` function that returns a `User` instance with the given email and username. The `active` field gets the value of `true`, and @@ -102,9 +102,9 @@ than `email: email`. ### Creating Instances from Other Instances with Struct Update Syntax -It’s often useful to create a new instance of a struct that includes most of -the values from another instance, but changes some. You can do this using -_struct update syntax_. +It’s often useful to create a new instance of a struct that includes most of the +values from another instance, but changes some. You can do this using _struct +update syntax_. First, in Listing 5-6 we show how to create a new `User` instance in `user2` regularly, without the update syntax. We set a new value for `email` but @@ -130,25 +130,25 @@ explicitly set should have the same value as the fields in the given instance. -The code in Listing 5-7 also creates an instance in `user2` that has a -different value for `email` but has the same values for the `username`, -`active`, and `sign_in_count` fields from `user1`. The `..user1` must come last -to specify that any remaining fields should get their values from the -corresponding fields in `user1`, but we can choose to specify values for as -many fields as we want in any order, regardless of the order of the fields in -the struct’s definition. +The code in Listing 5-7 also creates an instance in `user2` that has a different +value for `email` but has the same values for the `username`, `active`, and +`sign_in_count` fields from `user1`. The `..user1` must come last to specify +that any remaining fields should get their values from the corresponding fields +in `user1`, but we can choose to specify values for as many fields as we want in +any order, regardless of the order of the fields in the struct’s definition. Note that the struct update syntax uses `=` like an assignment; this is because -it moves the data, just as we saw in the [“Variables and Data Interacting with -Move”][move] section. In this example, we can no longer use -`user1` as a whole after creating `user2` because the `String` in the -`username` field of `user1` was moved into `user2`. If we had given `user2` new -`String` values for both `email` and `username`, and thus only used the -`active` and `sign_in_count` values from `user1`, then `user1` would still be -valid after creating `user2`. Both `active` and `sign_in_count` are types that -implement the `Copy` trait, so the behavior we discussed in the [“Stack-Only -Data: Copy”][copy] section would apply. We can still use -`user1.email` in this example, since its value was _not_ moved out. +it moves the data, just as we saw in the +[“Variables and Data Interacting with Move”][move] section. In +this example, we can no longer use `user1` as a whole after creating `user2` +because the `String` in the `username` field of `user1` was moved into `user2`. +If we had given `user2` new `String` values for both `email` and `username`, and +thus only used the `active` and `sign_in_count` values from `user1`, then +`user1` would still be valid after creating `user2`. Both `active` and +`sign_in_count` are types that implement the `Copy` trait, so the behavior we +discussed in the [“Stack-Only Data: Copy”][copy] section would +apply. We can still use `user1.email` in this example, since its value was _not_ +moved out. ### Using Tuple Structs Without Named Fields to Create Different Types @@ -173,24 +173,24 @@ tuple structs named `Color` and `Point`: Note that the `black` and `origin` values are different types because they’re instances of different tuple structs. Each struct you define is its own type, -even though the fields within the struct might have the same types. For -example, a function that takes a parameter of type `Color` cannot take a -`Point` as an argument, even though both types are made up of three `i32` -values. Otherwise, tuple struct instances are similar to tuples in that you can -destructure them into their individual pieces, and you can use a `.` followed -by the index to access an individual value. Unlike tuples, tuple structs -require you to name the type of the struct when you destructure them. For -example, we would write `let Point(x, y, z) = point`. +even though the fields within the struct might have the same types. For example, +a function that takes a parameter of type `Color` cannot take a `Point` as an +argument, even though both types are made up of three `i32` values. Otherwise, +tuple struct instances are similar to tuples in that you can destructure them +into their individual pieces, and you can use a `.` followed by the index to +access an individual value. Unlike tuples, tuple structs require you to name the +type of the struct when you destructure them. For example, we would write +`let Point(x, y, z) = point`. ### Unit-Like Structs Without Any Fields You can also define structs that don’t have any fields! These are called -_unit-like structs_ because they behave similarly to `()`, the unit type that -we mentioned in [“The Tuple Type”][tuples] section. Unit-like +_unit-like structs_ because they behave similarly to `()`, the unit type that we +mentioned in [“The Tuple Type”][tuples] section. Unit-like structs can be useful when you need to implement a trait on some type but don’t -have any data that you want to store in the type itself. We’ll discuss traits -in Chapter 10. Here’s an example of declaring and instantiating a unit struct -named `AlwaysEqual`: +have any data that you want to store in the type itself. We’ll discuss traits in +Chapter 10. Here’s an example of declaring and instantiating a unit struct named +`AlwaysEqual`: @@ -200,15 +200,15 @@ named `AlwaysEqual`: -To define `AlwaysEqual`, we use the `struct` keyword, the name we want, and -then a semicolon. No need for curly brackets or parentheses! Then we can get an +To define `AlwaysEqual`, we use the `struct` keyword, the name we want, and then +a semicolon. No need for curly brackets or parentheses! Then we can get an instance of `AlwaysEqual` in the `subject` variable in a similar way: using the name we defined, without any curly brackets or parentheses. Imagine that later -we’ll implement behavior for this type such that every instance of -`AlwaysEqual` is always equal to every instance of any other type, perhaps to -have a known result for testing purposes. We wouldn’t need any data to -implement that behavior! You’ll see in Chapter 10 how to define traits and -implement them on any type, including unit-like structs. +we’ll implement behavior for this type such that every instance of `AlwaysEqual` +is always equal to every instance of any other type, perhaps to have a known +result for testing purposes. We wouldn’t need any data to implement that +behavior! You’ll see in Chapter 10 how to define traits and implement them on +any type, including unit-like structs. > ### Ownership of Struct Data > diff --git a/src/ch05-02-example-structs.md b/src/ch05-02-example-structs.md index 49a109c6cf..a6ea2bb7e9 100644 --- a/src/ch05-02-example-structs.md +++ b/src/ch05-02-example-structs.md @@ -34,11 +34,11 @@ The issue with this code is evident in the signature of `area`: ``` The `area` function is supposed to calculate the area of one rectangle, but the -function we wrote has two parameters, and it’s not clear anywhere in our -program that the parameters are related. It would be more readable and more -manageable to group width and height together. We’ve already discussed one way -we might do that in [“The Tuple Type”][the-tuple-type] section -of Chapter 3: by using tuples. +function we wrote has two parameters, and it’s not clear anywhere in our program +that the parameters are related. It would be more readable and more manageable +to group width and height together. We’ve already discussed one way we might do +that in [“The Tuple Type”][the-tuple-type] section of Chapter 3: +by using tuples. ### Refactoring with Tuples @@ -59,10 +59,10 @@ the tuple, making our calculation less obvious. Mixing up the width and height wouldn’t matter for the area calculation, but if we want to draw the rectangle on the screen, it would matter! We would have to -keep in mind that `width` is the tuple index `0` and `height` is the tuple -index `1`. This would be even harder for someone else to figure out and keep in -mind if they were to use our code. Because we haven’t conveyed the meaning of -our data in our code, it’s now easier to introduce errors. +keep in mind that `width` is the tuple index `0` and `height` is the tuple index +`1`. This would be even harder for someone else to figure out and keep in mind +if they were to use our code. Because we haven’t conveyed the meaning of our +data in our code, it’s now easier to introduce errors. ### Refactoring with Structs: Adding More Meaning @@ -78,33 +78,33 @@ parts, as shown in Listing 5-10. -Here we’ve defined a struct and named it `Rectangle`. Inside the curly -brackets, we defined the fields as `width` and `height`, both of which have -type `u32`. Then, in `main`, we created a particular instance of `Rectangle` -that has a width of `30` and a height of `50`. +Here we’ve defined a struct and named it `Rectangle`. Inside the curly brackets, +we defined the fields as `width` and `height`, both of which have type `u32`. +Then, in `main`, we created a particular instance of `Rectangle` that has a +width of `30` and a height of `50`. Our `area` function is now defined with one parameter, which we’ve named -`rectangle`, whose type is an immutable borrow of a struct `Rectangle` -instance. As mentioned in Chapter 4, we want to borrow the struct rather than -take ownership of it. This way, `main` retains its ownership and can continue -using `rect1`, which is the reason we use the `&` in the function signature and -where we call the function. +`rectangle`, whose type is an immutable borrow of a struct `Rectangle` instance. +As mentioned in Chapter 4, we want to borrow the struct rather than take +ownership of it. This way, `main` retains its ownership and can continue using +`rect1`, which is the reason we use the `&` in the function signature and where +we call the function. The `area` function accesses the `width` and `height` fields of the `Rectangle` -instance (note that accessing fields of a borrowed struct instance does not -move the field values, which is why you often see borrows of structs). Our -function signature for `area` now says exactly what we mean: calculate the area -of `Rectangle`, using its `width` and `height` fields. This conveys that the -width and height are related to each other, and it gives descriptive names to -the values rather than using the tuple index values of `0` and `1`. This is a -win for clarity. +instance (note that accessing fields of a borrowed struct instance does not move +the field values, which is why you often see borrows of structs). Our function +signature for `area` now says exactly what we mean: calculate the area of +`Rectangle`, using its `width` and `height` fields. This conveys that the width +and height are related to each other, and it gives descriptive names to the +values rather than using the tuple index values of `0` and `1`. This is a win +for clarity. ### Adding Useful Functionality with Derived Traits It’d be useful to be able to print an instance of `Rectangle` while we’re debugging our program and see the values for all its fields. Listing 5-11 tries -using the [`println!` macro][println] as we have used in -previous chapters. This won’t work, however. +using the [`println!` macro][println] as we have used in previous +chapters. This won’t work, however. @@ -122,14 +122,14 @@ When we compile this code, we get an error with this core message: The `println!` macro can do many kinds of formatting, and by default, the curly brackets tell `println!` to use formatting known as `Display`: output intended -for direct end user consumption. The primitive types we’ve seen so far -implement `Display` by default because there’s only one way you’d want to show -a `1` or any other primitive type to a user. But with structs, the way -`println!` should format the output is less clear because there are more -display possibilities: Do you want commas or not? Do you want to print the -curly brackets? Should all the fields be shown? Due to this ambiguity, Rust -doesn’t try to guess what we want, and structs don’t have a provided -implementation of `Display` to use with `println!` and the `{}` placeholder. +for direct end user consumption. The primitive types we’ve seen so far implement +`Display` by default because there’s only one way you’d want to show a `1` or +any other primitive type to a user. But with structs, the way `println!` should +format the output is less clear because there are more display possibilities: Do +you want commas or not? Do you want to print the curly brackets? Should all the +fields be shown? Due to this ambiguity, Rust doesn’t try to guess what we want, +and structs don’t have a provided implementation of `Display` to use with +`println!` and the `{}` placeholder. If we continue reading the errors, we’ll find this helpful note: @@ -137,11 +137,12 @@ If we continue reading the errors, we’ll find this helpful note: {{#include ../listings/ch05-using-structs-to-structure-related-data/listing-05-11/output.txt:9:10}} ``` -Let’s try it! The `println!` macro call will now look like `println!("rect1 is -{rect1:?}");`. Putting the specifier `:?` inside the curly brackets tells -`println!` we want to use an output format called `Debug`. The `Debug` trait -enables us to print our struct in a way that is useful for developers so we can -see its value while we’re debugging our code. +Let’s try it! The `println!` macro call will now look like +`println!("rect1 is +{rect1:?}");`. Putting the specifier `:?` inside the curly +brackets tells `println!` we want to use an output format called `Debug`. The +`Debug` trait enables us to print our struct in a way that is useful for +developers so we can see its value while we’re debugging our code. Compile the code with this change. Drat! We still get an error: @@ -157,8 +158,8 @@ But again, the compiler gives us a helpful note: Rust _does_ include functionality to print out debugging information, but we have to explicitly opt in to make that functionality available for our struct. -To do that, we add the outer attribute `#[derive(Debug)]` just before the -struct definition, as shown in Listing 5-12. +To do that, we add the outer attribute `#[derive(Debug)]` just before the struct +definition, as shown in Listing 5-12. @@ -177,19 +178,19 @@ following output: Nice! It’s not the prettiest output, but it shows the values of all the fields for this instance, which would definitely help during debugging. When we have -larger structs, it’s useful to have output that’s a bit easier to read; in -those cases, we can use `{:#?}` instead of `{:?}` in the `println!` string. In -this example, using the `{:#?}` style will output the following: +larger structs, it’s useful to have output that’s a bit easier to read; in those +cases, we can use `{:#?}` instead of `{:?}` in the `println!` string. In this +example, using the `{:#?}` style will output the following: ```console {{#include ../listings/ch05-using-structs-to-structure-related-data/output-only-02-pretty-debug/output.txt}} ``` -Another way to print out a value using the `Debug` format is to use the [`dbg!` -macro][dbg], which takes ownership of an expression (as opposed -to `println!`, which takes a reference), prints the file and line number of -where that `dbg!` macro call occurs in your code along with the resultant value -of that expression, and returns ownership of the value. +Another way to print out a value using the `Debug` format is to use the +[`dbg!` macro][dbg], which takes ownership of an expression (as +opposed to `println!`, which takes a reference), prints the file and line number +of where that `dbg!` macro call occurs in your code along with the resultant +value of that expression, and returns ownership of the value. > Note: Calling the `dbg!` macro prints to the standard error console stream > (`stderr`), as opposed to `println!`, which prints to the standard output @@ -204,11 +205,11 @@ Here’s an example where we’re interested in the value that gets assigned to {{#rustdoc_include ../listings/ch05-using-structs-to-structure-related-data/no-listing-05-dbg-macro/src/main.rs}} ``` -We can put `dbg!` around the expression `30 * scale` and, because `dbg!` -returns ownership of the expression’s value, the `width` field will get the -same value as if we didn’t have the `dbg!` call there. We don’t want `dbg!` to -take ownership of `rect1`, so we use a reference to `rect1` in the next call. -Here’s what the output of this example looks like: +We can put `dbg!` around the expression `30 * scale` and, because `dbg!` returns +ownership of the expression’s value, the `width` field will get the same value +as if we didn’t have the `dbg!` call there. We don’t want `dbg!` to take +ownership of `rect1`, so we use a reference to `rect1` in the next call. Here’s +what the output of this example looks like: ```console {{#include ../listings/ch05-using-structs-to-structure-related-data/no-listing-05-dbg-macro/output.txt}} @@ -222,13 +223,13 @@ the `Rectangle` struct. This output uses the pretty `Debug` formatting of the `Rectangle` type. The `dbg!` macro can be really helpful when you’re trying to figure out what your code is doing! -In addition to the `Debug` trait, Rust has provided a number of traits for us -to use with the `derive` attribute that can add useful behavior to our custom +In addition to the `Debug` trait, Rust has provided a number of traits for us to +use with the `derive` attribute that can add useful behavior to our custom types. Those traits and their behaviors are listed in [Appendix C][app-c]. We’ll cover how to implement these traits with custom behavior as well as how to create your own traits in Chapter 10. There are also many -attributes other than `derive`; for more information, see [the “Attributes” -section of the Rust Reference][attributes]. +attributes other than `derive`; for more information, see +[the “Attributes” section of the Rust Reference][attributes]. Our `area` function is very specific: it only computes the area of rectangles. It would be helpful to tie this behavior more closely to our `Rectangle` struct diff --git a/src/ch05-03-method-syntax.md b/src/ch05-03-method-syntax.md index 542f07d517..c07e7fd827 100644 --- a/src/ch05-03-method-syntax.md +++ b/src/ch05-03-method-syntax.md @@ -4,16 +4,16 @@ _Methods_ are similar to functions: we declare them with the `fn` keyword and a name, they can have parameters and a return value, and they contain some code that’s run when the method is called from somewhere else. Unlike functions, methods are defined within the context of a struct (or an enum or a trait -object, which we cover in [Chapter 6][enums] and [Chapter -17][trait-objects], respectively), and their first parameter is -always `self`, which represents the instance of the struct the method is being -called on. +object, which we cover in [Chapter 6][enums] and +[Chapter 17][trait-objects], respectively), and their first +parameter is always `self`, which represents the instance of the struct the +method is being called on. ### Defining Methods Let’s change the `area` function that has a `Rectangle` instance as a parameter -and instead make an `area` method defined on the `Rectangle` struct, as shown -in Listing 5-13. +and instead make an `area` method defined on the `Rectangle` struct, as shown in +Listing 5-13. @@ -24,14 +24,14 @@ in Listing 5-13. To define the function within the context of `Rectangle`, we start an `impl` -(implementation) block for `Rectangle`. Everything within this `impl` block -will be associated with the `Rectangle` type. Then we move the `area` function -within the `impl` curly brackets and change the first (and in this case, only) +(implementation) block for `Rectangle`. Everything within this `impl` block will +be associated with the `Rectangle` type. Then we move the `area` function within +the `impl` curly brackets and change the first (and in this case, only) parameter to be `self` in the signature and everywhere within the body. In `main`, where we called the `area` function and passed `rect1` as an argument, we can instead use _method syntax_ to call the `area` method on our `Rectangle` -instance. The method syntax goes after an instance: we add a dot followed by -the method name, parentheses, and any arguments. +instance. The method syntax goes after an instance: we add a dot followed by the +method name, parentheses, and any arguments. In the signature for `area`, we use `&self` instead of `rectangle: &Rectangle`. The `&self` is actually short for `self: &Self`. Within an `impl` block, the @@ -49,16 +49,16 @@ version: we don’t want to take ownership, and we just want to read the data in the struct, not write to it. If we wanted to change the instance that we’ve called the method on as part of what the method does, we’d use `&mut self` as the first parameter. Having a method that takes ownership of the instance by -using just `self` as the first parameter is rare; this technique is usually -used when the method transforms `self` into something else and you want to -prevent the caller from using the original instance after the transformation. +using just `self` as the first parameter is rare; this technique is usually used +when the method transforms `self` into something else and you want to prevent +the caller from using the original instance after the transformation. -The main reason for using methods instead of functions, in addition to -providing method syntax and not having to repeat the type of `self` in every -method’s signature, is for organization. We’ve put all the things we can do -with an instance of a type in one `impl` block rather than making future users -of our code search for capabilities of `Rectangle` in various places in the -library we provide. +The main reason for using methods instead of functions, in addition to providing +method syntax and not having to repeat the type of `self` in every method’s +signature, is for organization. We’ve put all the things we can do with an +instance of a type in one `impl` block rather than making future users of our +code search for capabilities of `Rectangle` in various places in the library we +provide. Note that we can choose to give a method the same name as one of the struct’s fields. For example, we can define a method on `Rectangle` that is also named @@ -79,22 +79,22 @@ the instance’s `width` field is greater than `0` and `false` if the value is method `width`. When we don’t use parentheses, Rust knows we mean the field `width`. -Often, but not always, when we give a method the same name as a field we want -it to only return the value in the field and do nothing else. Methods like this -are called _getters_, and Rust does not implement them automatically for struct +Often, but not always, when we give a method the same name as a field we want it +to only return the value in the field and do nothing else. Methods like this are +called _getters_, and Rust does not implement them automatically for struct fields as some other languages do. Getters are useful because you can make the field private but the method public, and thus enable read-only access to that field as part of the type’s public API. We will discuss what public and private -are and how to designate a field or method as public or private in [Chapter -7][public]. +are and how to designate a field or method as public or private in +[Chapter 7][public]. > ### Where’s the `->` Operator? > > In C and C++, two different operators are used for calling methods: you use > `.` if you’re calling a method on the object directly and `->` if you’re > calling the method on a pointer to the object and need to dereference the -> pointer first. In other words, if `object` is a pointer, -> `object->something()` is similar to `(*object).something()`. +> pointer first. In other words, if `object` is a pointer, `object->something()` +> is similar to `(*object).something()`. > > Rust doesn’t have an equivalent to the `->` operator; instead, Rust has a > feature called _automatic referencing and dereferencing_. Calling methods is @@ -137,11 +137,11 @@ are and how to designate a field or method as public or private in [Chapter ### Methods with More Parameters Let’s practice using methods by implementing a second method on the `Rectangle` -struct. This time we want an instance of `Rectangle` to take another instance -of `Rectangle` and return `true` if the second `Rectangle` can fit completely -within `self` (the first `Rectangle`); otherwise, it should return `false`. -That is, once we’ve defined the `can_hold` method, we want to be able to write -the program shown in Listing 5-14. +struct. This time we want an instance of `Rectangle` to take another instance of +`Rectangle` and return `true` if the second `Rectangle` can fit completely +within `self` (the first `Rectangle`); otherwise, it should return `false`. That +is, once we’ve defined the `can_hold` method, we want to be able to write the +program shown in Listing 5-14. @@ -168,11 +168,11 @@ parameter will be by looking at the code that calls the method: `rect2`, an instance of `Rectangle`. This makes sense because we only need to read `rect2` (rather than write, which would mean we’d need a mutable borrow), and we want `main` to retain ownership of `rect2` so we can use it again after -calling the `can_hold` method. The return value of `can_hold` will be a -Boolean, and the implementation will check whether the width and height of -`self` are greater than the width and height of the other `Rectangle`, -respectively. Let’s add the new `can_hold` method to the `impl` block from -Listing 5-13, shown in Listing 5-15. +calling the `can_hold` method. The return value of `can_hold` will be a Boolean, +and the implementation will check whether the width and height of `self` are +greater than the width and height of the other `Rectangle`, respectively. Let’s +add the new `can_hold` method to the `impl` block from Listing 5-13, shown in +Listing 5-15. @@ -216,15 +216,15 @@ is `Rectangle`. To call this associated function, we use the `::` syntax with the struct name; `let sq = Rectangle::square(3);` is an example. This function is namespaced by -the struct: the `::` syntax is used for both associated functions and -namespaces created by modules. We’ll discuss modules in [Chapter -7][modules]. +the struct: the `::` syntax is used for both associated functions and namespaces +created by modules. We’ll discuss modules in +[Chapter 7][modules]. ### Multiple `impl` Blocks -Each struct is allowed to have multiple `impl` blocks. For example, Listing -5-15 is equivalent to the code shown in Listing 5-16, which has each method in -its own `impl` block. +Each struct is allowed to have multiple `impl` blocks. For example, Listing 5-15 +is equivalent to the code shown in Listing 5-16, which has each method in its +own `impl` block. diff --git a/src/ch06-01-defining-an-enum.md b/src/ch06-01-defining-an-enum.md index d61578777e..4d56bec805 100644 --- a/src/ch06-01-defining-an-enum.md +++ b/src/ch06-01-defining-an-enum.md @@ -6,12 +6,12 @@ value is one of a possible set of values. For example, we may want to say that `Rectangle` is one of a set of possible shapes that also includes `Circle` and `Triangle`. To do this, Rust allows us to encode these possibilities as an enum. -Let’s look at a situation we might want to express in code and see why enums -are useful and more appropriate than structs in this case. Say we need to work -with IP addresses. Currently, two major standards are used for IP addresses: -version four and version six. Because these are the only possibilities for an -IP address that our program will come across, we can _enumerate_ all possible -variants, which is where enumeration gets its name. +Let’s look at a situation we might want to express in code and see why enums are +useful and more appropriate than structs in this case. Say we need to work with +IP addresses. Currently, two major standards are used for IP addresses: version +four and version six. Because these are the only possibilities for an IP address +that our program will come across, we can _enumerate_ all possible variants, +which is where enumeration gets its name. Any IP address can be either a version four or a version six address, but not both at the same time. That property of IP addresses makes the enum data @@ -40,8 +40,8 @@ We can create instances of each of the two variants of `IpAddrKind` like this: Note that the variants of the enum are namespaced under its identifier, and we use a double colon to separate the two. This is useful because now both values -`IpAddrKind::V4` and `IpAddrKind::V6` are of the same type: `IpAddrKind`. We -can then, for instance, define a function that takes any `IpAddrKind`: +`IpAddrKind::V4` and `IpAddrKind::V6` are of the same type: `IpAddrKind`. We can +then, for instance, define a function that takes any `IpAddrKind`: ```rust {{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/no-listing-01-defining-enums/src/main.rs:fn}} @@ -54,10 +54,10 @@ And we can call this function with either variant: ``` Using enums has even more advantages. Thinking more about our IP address type, -at the moment we don’t have a way to store the actual IP address _data_; we -only know what _kind_ it is. Given that you just learned about structs in -Chapter 5, you might be tempted to tackle this problem with structs as shown in -Listing 6-1. +at the moment we don’t have a way to store the actual IP address _data_; we only +know what _kind_ it is. Given that you just learned about structs in Chapter 5, +you might be tempted to tackle this problem with structs as shown in Listing +6-1. @@ -69,12 +69,12 @@ Listing 6-1. Here, we’ve defined a struct `IpAddr` that has two fields: a `kind` field that is of type `IpAddrKind` (the enum we defined previously) and an `address` field -of type `String`. We have two instances of this struct. The first is `home`, -and it has the value `IpAddrKind::V4` as its `kind` with associated address -data of `127.0.0.1`. The second instance is `loopback`. It has the other -variant of `IpAddrKind` as its `kind` value, `V6`, and has address `::1` -associated with it. We’ve used a struct to bundle the `kind` and `address` -values together, so now the variant is associated with the value. +of type `String`. We have two instances of this struct. The first is `home`, and +it has the value `IpAddrKind::V4` as its `kind` with associated address data of +`127.0.0.1`. The second instance is `loopback`. It has the other variant of +`IpAddrKind` as its `kind` value, `V6`, and has address `::1` associated with +it. We’ve used a struct to bundle the `kind` and `address` values together, so +now the variant is associated with the value. However, representing the same concept using just an enum is more concise: rather than an enum inside a struct, we can put data directly into each enum @@ -95,23 +95,23 @@ enum. There’s another advantage to using an enum rather than a struct: each variant can have different types and amounts of associated data. Version four IP -addresses will always have four numeric components that will have values -between 0 and 255. If we wanted to store `V4` addresses as four `u8` values but -still express `V6` addresses as one `String` value, we wouldn’t be able to with -a struct. Enums handle this case with ease: +addresses will always have four numeric components that will have values between +0 and 255. If we wanted to store `V4` addresses as four `u8` values but still +express `V6` addresses as one `String` value, we wouldn’t be able to with a +struct. Enums handle this case with ease: ```rust {{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/no-listing-03-variants-with-different-data/src/main.rs:here}} ``` We’ve shown several different ways to define data structures to store version -four and version six IP addresses. However, as it turns out, wanting to store -IP addresses and encode which kind they are is so common that [the standard -library has a definition we can use!][IpAddr] Let’s look at how -the standard library defines `IpAddr`: it has the exact enum and variants that -we’ve defined and used, but it embeds the address data inside the variants in -the form of two different structs, which are defined differently for each -variant: +four and version six IP addresses. However, as it turns out, wanting to store IP +addresses and encode which kind they are is so common that +[the standard library has a definition we can use!][IpAddr] Let’s +look at how the standard library defines `IpAddr`: it has the exact enum and +variants that we’ve defined and used, but it embeds the address data inside the +variants in the form of two different structs, which are defined differently for +each variant: ```rust struct Ipv4Addr { @@ -130,8 +130,8 @@ enum IpAddr { This code illustrates that you can put any kind of data inside an enum variant: strings, numeric types, or structs, for example. You can even include another -enum! Also, standard library types are often not much more complicated than -what you might come up with. +enum! Also, standard library types are often not much more complicated than what +you might come up with. Note that even though the standard library contains a definition for `IpAddr`, we can still create and use our own definition without conflict because we @@ -151,10 +151,10 @@ variety of types embedded in its variants. This enum has four variants with different types: -- `Quit` has no data associated with it at all. -- `Move` has named fields, like a struct does. -- `Write` includes a single `String`. -- `ChangeColor` includes three `i32` values. +* `Quit` has no data associated with it at all. +* `Move` has named fields, like a struct does. +* `Write` includes a single `String`. +* `ChangeColor` includes three `i32` values. Defining an enum with variants such as the ones in Listing 6-2 is similar to defining different kinds of struct definitions, except the enum doesn’t use the @@ -188,22 +188,21 @@ useful: `Option`. ### The `Option` Enum and Its Advantages Over Null Values -This section explores a case study of `Option`, which is another enum defined -by the standard library. The `Option` type encodes the very common scenario in +This section explores a case study of `Option`, which is another enum defined by +the standard library. The `Option` type encodes the very common scenario in which a value could be something or it could be nothing. -For example, if you request the first item in a non-empty list, you would get -a value. If you request the first item in an empty list, you would get nothing. -Expressing this concept in terms of the type system means the compiler can -check whether you’ve handled all the cases you should be handling; this -functionality can prevent bugs that are extremely common in other programming -languages. +For example, if you request the first item in a non-empty list, you would get a +value. If you request the first item in an empty list, you would get nothing. +Expressing this concept in terms of the type system means the compiler can check +whether you’ve handled all the cases you should be handling; this functionality +can prevent bugs that are extremely common in other programming languages. Programming language design is often thought of in terms of which features you include, but the features you exclude are important too. Rust doesn’t have the null feature that many other languages have. _Null_ is a value that means there -is no value there. In languages with null, variables can always be in one of -two states: null or not-null. +is no value there. In languages with null, variables can always be in one of two +states: null or not-null. In his 2009 presentation “Null References: The Billion Dollar Mistake,” Tony Hoare, the inventor of null, has this to say: @@ -225,8 +224,8 @@ However, the concept that null is trying to express is still a useful one: a null is a value that is currently invalid or absent for some reason. The problem isn’t really with the concept but with the particular -implementation. As such, Rust does not have nulls, but it does have an enum -that can encode the concept of a value being present or absent. This enum is +implementation. As such, Rust does not have nulls, but it does have an enum that +can encode the concept of a value being present or absent. This enum is `Option`, and it is [defined by the standard library][option] as follows: @@ -245,11 +244,11 @@ prefix. The `Option` enum is still just a regular enum, and `Some(T)` and The `` syntax is a feature of Rust we haven’t talked about yet. It’s a generic type parameter, and we’ll cover generics in more detail in Chapter 10. -For now, all you need to know is that `` means that the `Some` variant of -the `Option` enum can hold one piece of data of any type, and that each -concrete type that gets used in place of `T` makes the overall `Option` type -a different type. Here are some examples of using `Option` values to hold -number types and string types: +For now, all you need to know is that `` means that the `Some` variant of the +`Option` enum can hold one piece of data of any type, and that each concrete +type that gets used in place of `T` makes the overall `Option` type a +different type. Here are some examples of using `Option` values to hold number +types and string types: ```rust {{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/no-listing-06-option-examples/src/main.rs:here}} @@ -259,8 +258,8 @@ The type of `some_number` is `Option`. The type of `some_char` is `Option`, which is a different type. Rust can infer these types because we’ve specified a value inside the `Some` variant. For `absent_number`, Rust requires us to annotate the overall `Option` type: the compiler can’t infer the -type that the corresponding `Some` variant will hold by looking only at a -`None` value. Here, we tell Rust that we mean for `absent_number` to be of type +type that the corresponding `Some` variant will hold by looking only at a `None` +value. Here, we tell Rust that we mean for `absent_number` to be of type `Option`. When we have a `Some` value, we know that a value is present and the value is @@ -283,30 +282,29 @@ If we run this code, we get an error message like this one: {{#include ../listings/ch06-enums-and-pattern-matching/no-listing-07-cant-use-option-directly/output.txt}} ``` -Intense! In effect, this error message means that Rust doesn’t understand how -to add an `i8` and an `Option`, because they’re different types. When we -have a value of a type like `i8` in Rust, the compiler will ensure that we -always have a valid value. We can proceed confidently without having to check -for null before using that value. Only when we have an `Option` (or -whatever type of value we’re working with) do we have to worry about possibly -not having a value, and the compiler will make sure we handle that case before -using the value. +Intense! In effect, this error message means that Rust doesn’t understand how to +add an `i8` and an `Option`, because they’re different types. When we have a +value of a type like `i8` in Rust, the compiler will ensure that we always have +a valid value. We can proceed confidently without having to check for null +before using that value. Only when we have an `Option` (or whatever type of +value we’re working with) do we have to worry about possibly not having a value, +and the compiler will make sure we handle that case before using the value. In other words, you have to convert an `Option` to a `T` before you can perform `T` operations with it. Generally, this helps catch one of the most common issues with null: assuming that something isn’t null when it actually is. Eliminating the risk of incorrectly assuming a not-null value helps you to be -more confident in your code. In order to have a value that can possibly be -null, you must explicitly opt in by making the type of that value `Option`. -Then, when you use that value, you are required to explicitly handle the case -when the value is null. Everywhere that a value has a type that isn’t an -`Option`, you _can_ safely assume that the value isn’t null. This was a -deliberate design decision for Rust to limit null’s pervasiveness and increase -the safety of Rust code. - -So how do you get the `T` value out of a `Some` variant when you have a value -of type `Option` so that you can use that value? The `Option` enum has a +more confident in your code. In order to have a value that can possibly be null, +you must explicitly opt in by making the type of that value `Option`. Then, +when you use that value, you are required to explicitly handle the case when the +value is null. Everywhere that a value has a type that isn’t an `Option`, you +_can_ safely assume that the value isn’t null. This was a deliberate design +decision for Rust to limit null’s pervasiveness and increase the safety of Rust +code. + +So how do you get the `T` value out of a `Some` variant when you have a value of +type `Option` so that you can use that value? The `Option` enum has a large number of methods that are useful in a variety of situations; you can check them out in [its documentation][docs]. Becoming familiar with the methods on `Option` will be extremely useful in your journey with diff --git a/src/ch06-02-match.md b/src/ch06-02-match.md index 7cbae3edf9..31d7760e1f 100644 --- a/src/ch06-02-match.md +++ b/src/ch06-02-match.md @@ -4,13 +4,13 @@ ## The `match` Control Flow Construct -Rust has an extremely powerful control flow construct called `match` that -allows you to compare a value against a series of patterns and then execute -code based on which pattern matches. Patterns can be made up of literal values, -variable names, wildcards, and many other things; [Chapter -18][ch19-00-patterns] covers all the different kinds of patterns -and what they do. The power of `match` comes from the expressiveness of the -patterns and the fact that the compiler confirms that all possible cases are +Rust has an extremely powerful control flow construct called `match` that allows +you to compare a value against a series of patterns and then execute code based +on which pattern matches. Patterns can be made up of literal values, variable +names, wildcards, and many other things; +[Chapter 18][ch19-00-patterns] covers all the different kinds of +patterns and what they do. The power of `match` comes from the expressiveness of +the patterns and the fact that the compiler confirms that all possible cases are handled. Think of a `match` expression as being like a coin-sorting machine: coins slide @@ -21,8 +21,8 @@ the value falls into the associated code block to be used during execution. Speaking of coins, let’s use them as an example using `match`! We can write a function that takes an unknown US coin and, in a similar way as the counting -machine, determines which coin it is and returns its value in cents, as shown -in Listing 6-3. +machine, determines which coin it is and returns its value in cents, as shown in +Listing 6-3. @@ -32,8 +32,8 @@ in Listing 6-3. -Let’s break down the `match` in the `value_in_cents` function. First we list -the `match` keyword followed by an expression, which in this case is the value +Let’s break down the `match` in the `value_in_cents` function. First we list the +`match` keyword followed by an expression, which in this case is the value `coin`. This seems very similar to a conditional expression used with `if`, but there’s a big difference: with `if`, the condition needs to evaluate to a Boolean value, but here it can be any type. The type of `coin` in this example @@ -72,11 +72,11 @@ values that match the pattern. This is how we can extract values out of enum variants. As an example, let’s change one of our enum variants to hold data inside it. -From 1999 through 2008, the United States minted quarters with different -designs for each of the 50 states on one side. No other coins got state -designs, so only quarters have this extra value. We can add this information to -our `enum` by changing the `Quarter` variant to include a `UsState` value -stored inside it, which we’ve done in Listing 6-4. +From 1999 through 2008, the United States minted quarters with different designs +for each of the 50 states on one side. No other coins got state designs, so only +quarters have this extra value. We can add this information to our `enum` by +changing the `Quarter` variant to include a `UsState` value stored inside it, +which we’ve done in Listing 6-4. @@ -86,10 +86,10 @@ stored inside it, which we’ve done in Listing 6-4. -Let’s imagine that a friend is trying to collect all 50 state quarters. While -we sort our loose change by coin type, we’ll also call out the name of the -state associated with each quarter so that if it’s one our friend doesn’t have, -they can add it to their collection. +Let’s imagine that a friend is trying to collect all 50 state quarters. While we +sort our loose change by coin type, we’ll also call out the name of the state +associated with each quarter so that if it’s one our friend doesn’t have, they +can add it to their collection. In the match expression for this code, we add a variable called `state` to the pattern that matches values of the variant `Coin::Quarter`. When a @@ -104,20 +104,19 @@ If we were to call `value_in_cents(Coin::Quarter(UsState::Alaska))`, `coin` would be `Coin::Quarter(UsState::Alaska)`. When we compare that value with each of the match arms, none of them match until we reach `Coin::Quarter(state)`. At that point, the binding for `state` will be the value `UsState::Alaska`. We can -then use that binding in the `println!` expression, thus getting the inner -state value out of the `Coin` enum variant for `Quarter`. +then use that binding in the `println!` expression, thus getting the inner state +value out of the `Coin` enum variant for `Quarter`. ### Matching with `Option` In the previous section, we wanted to get the inner `T` value out of the `Some` -case when using `Option`; we can also handle `Option` using `match`, as -we did with the `Coin` enum! Instead of comparing coins, we’ll compare the -variants of `Option`, but the way the `match` expression works remains the -same. +case when using `Option`; we can also handle `Option` using `match`, as we +did with the `Coin` enum! Instead of comparing coins, we’ll compare the variants +of `Option`, but the way the `match` expression works remains the same. Let’s say we want to write a function that takes an `Option` and, if -there’s a value inside, adds 1 to that value. If there isn’t a value inside, -the function should return the `None` value and not attempt to perform any +there’s a value inside, adds 1 to that value. If there isn’t a value inside, the +function should return the `None` value and not attempt to perform any operations. This function is very easy to write, thanks to `match`, and will look like @@ -132,24 +131,24 @@ Listing 6-5. Let’s examine the first execution of `plus_one` in more detail. When we call -`plus_one(five)`, the variable `x` in the body of `plus_one` will have the -value `Some(5)`. We then compare that against each match arm: +`plus_one(five)`, the variable `x` in the body of `plus_one` will have the value +`Some(5)`. We then compare that against each match arm: ```rust,ignore {{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/listing-06-05/src/main.rs:first_arm}} ``` -The `Some(5)` value doesn’t match the pattern `None`, so we continue to the -next arm: +The `Some(5)` value doesn’t match the pattern `None`, so we continue to the next +arm: ```rust,ignore {{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/listing-06-05/src/main.rs:second_arm}} ``` -Does `Some(5)` match `Some(i)`? It does! We have the same variant. The `i` -binds to the value contained in `Some`, so `i` takes the value `5`. The code in -the match arm is then executed, so we add 1 to the value of `i` and create a -new `Some` value with our total `6` inside. +Does `Some(5)` match `Some(i)`? It does! We have the same variant. The `i` binds +to the value contained in `Some`, so `i` takes the value `5`. The code in the +match arm is then executed, so we add 1 to the value of `i` and create a new +`Some` value with our total `6` inside. Now let’s consider the second call of `plus_one` in Listing 6-5, where `x` is `None`. We enter the `match` and compare to the first arm: @@ -163,23 +162,23 @@ It matches! There’s no value to add to, so the program stops and returns the arms are compared. Combining `match` and enums is useful in many situations. You’ll see this -pattern a lot in Rust code: `match` against an enum, bind a variable to the -data inside, and then execute code based on it. It’s a bit tricky at first, but -once you get used to it, you’ll wish you had it in all languages. It’s -consistently a user favorite. +pattern a lot in Rust code: `match` against an enum, bind a variable to the data +inside, and then execute code based on it. It’s a bit tricky at first, but once +you get used to it, you’ll wish you had it in all languages. It’s consistently a +user favorite. ### Matches Are Exhaustive There’s one other aspect of `match` we need to discuss: the arms’ patterns must -cover all possibilities. Consider this version of our `plus_one` function, -which has a bug and won’t compile: +cover all possibilities. Consider this version of our `plus_one` function, which +has a bug and won’t compile: ```rust,ignore,does_not_compile {{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/no-listing-10-non-exhaustive-match/src/main.rs:here}} ``` -We didn’t handle the `None` case, so this code will cause a bug. Luckily, it’s -a bug Rust knows how to catch. If we try to compile this code, we’ll get this +We didn’t handle the `None` case, so this code will cause a bug. Luckily, it’s a +bug Rust knows how to catch. If we try to compile this code, we’ll get this error: ```console @@ -200,26 +199,25 @@ for all other values take one default action. Imagine we’re implementing a gam where, if you roll a 3 on a dice roll, your player doesn’t move, but instead gets a new fancy hat. If you roll a 7, your player loses a fancy hat. For all other values, your player moves that number of spaces on the game board. Here’s -a `match` that implements that logic, with the result of the dice roll -hardcoded rather than a random value, and all other logic represented by -functions without bodies because actually implementing them is out of scope for -this example: +a `match` that implements that logic, with the result of the dice roll hardcoded +rather than a random value, and all other logic represented by functions without +bodies because actually implementing them is out of scope for this example: ```rust {{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/no-listing-15-binding-catchall/src/main.rs:here}} ``` -For the first two arms, the patterns are the literal values `3` and `7`. For -the last arm that covers every other possible value, the pattern is the -variable we’ve chosen to name `other`. The code that runs for the `other` arm -uses the variable by passing it to the `move_player` function. +For the first two arms, the patterns are the literal values `3` and `7`. For the +last arm that covers every other possible value, the pattern is the variable +we’ve chosen to name `other`. The code that runs for the `other` arm uses the +variable by passing it to the `move_player` function. -This code compiles, even though we haven’t listed all the possible values a -`u8` can have, because the last pattern will match all values not specifically +This code compiles, even though we haven’t listed all the possible values a `u8` +can have, because the last pattern will match all values not specifically listed. This catch-all pattern meets the requirement that `match` must be -exhaustive. Note that we have to put the catch-all arm last because the -patterns are evaluated in order. If we put the catch-all arm earlier, the other -arms would never run, so Rust will warn us if we add arms after a catch-all! +exhaustive. Note that we have to put the catch-all arm last because the patterns +are evaluated in order. If we put the catch-all arm earlier, the other arms +would never run, so Rust will warn us if we add arms after a catch-all! Rust also has a pattern we can use when we want a catch-all but don’t want to _use_ the value in the catch-all pattern: `_` is a special pattern that matches @@ -239,8 +237,9 @@ ignoring all other values in the last arm; we haven’t forgotten anything. Finally, we’ll change the rules of the game one more time so that nothing else happens on your turn if you roll anything other than a 3 or a 7. We can express -that by using the unit value (the empty tuple type we mentioned in [“The Tuple -Type”][tuples] section) as the code that goes with the `_` arm: +that by using the unit value (the empty tuple type we mentioned in +[“The Tuple Type”][tuples] section) as the code that goes with +the `_` arm: ```rust {{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/no-listing-17-underscore-unit/src/main.rs:here}} @@ -250,10 +249,10 @@ Here, we’re telling Rust explicitly that we aren’t going to use any other va that doesn’t match a pattern in an earlier arm, and we don’t want to run any code in this case. -There’s more about patterns and matching that we’ll cover in [Chapter -19][ch19-00-patterns]. For now, we’re going to move on to the -`if let` syntax, which can be useful in situations where the `match` expression -is a bit wordy. +There’s more about patterns and matching that we’ll cover in +[Chapter 19][ch19-00-patterns]. For now, we’re going to move on +to the `if let` syntax, which can be useful in situations where the `match` +expression is a bit wordy. [tuples]: ch03-02-data-types.html#the-tuple-type [ch19-00-patterns]: ch19-00-patterns.html diff --git a/src/ch06-03-if-let.md b/src/ch06-03-if-let.md index 50ad9321f9..cd90b0d3b7 100644 --- a/src/ch06-03-if-let.md +++ b/src/ch06-03-if-let.md @@ -2,9 +2,8 @@ The `if let` syntax lets you combine `if` and `let` into a less verbose way to handle values that match one pattern while ignoring the rest. Consider the -program in Listing 6-6 that matches on an `Option` value in the -`config_max` variable but only wants to execute code if the value is the `Some` -variant. +program in Listing 6-6 that matches on an `Option` value in the `config_max` +variable but only wants to execute code if the value is the `Some` variant. @@ -16,12 +15,13 @@ variant. If the value is `Some`, we print out the value in the `Some` variant by binding the value to the variable `max` in the pattern. We don’t want to do anything -with the `None` value. To satisfy the `match` expression, we have to add `_ => -()` after processing just one variant, which is annoying boilerplate code to -add. +with the `None` value. To satisfy the `match` expression, we have to add +`_ => +()` after processing just one variant, which is annoying boilerplate code +to add. -Instead, we could write this in a shorter way using `if let`. The following -code behaves the same as the `match` in Listing 6-6: +Instead, we could write this in a shorter way using `if let`. The following code +behaves the same as the `match` in Listing 6-6: ```rust {{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/no-listing-12-if-let/src/main.rs:here}} @@ -30,16 +30,16 @@ code behaves the same as the `match` in Listing 6-6: The syntax `if let` takes a pattern and an expression separated by an equal sign. It works the same way as a `match`, where the expression is given to the `match` and the pattern is its first arm. In this case, the pattern is -`Some(max)`, and the `max` binds to the value inside the `Some`. We can then -use `max` in the body of the `if let` block in the same way we used `max` in -the corresponding `match` arm. The code in the `if let` block only runs if the -value matches the pattern. +`Some(max)`, and the `max` binds to the value inside the `Some`. We can then use +`max` in the body of the `if let` block in the same way we used `max` in the +corresponding `match` arm. The code in the `if let` block only runs if the value +matches the pattern. Using `if let` means less typing, less indentation, and less boilerplate code. However, you lose the exhaustive checking that `match` enforces. Choosing between `match` and `if let` depends on what you’re doing in your particular -situation and whether gaining conciseness is an appropriate trade-off for -losing exhaustive checking. +situation and whether gaining conciseness is an appropriate trade-off for losing +exhaustive checking. In other words, you can think of `if let` as syntax sugar for a `match` that runs code when the value matches one pattern and then ignores all other values. @@ -69,9 +69,9 @@ express using a `match`, remember that `if let` is in your Rust toolbox as well. We’ve now covered how to use enums to create custom types that can be one of a set of enumerated values. We’ve shown how the standard library’s `Option` -type helps you use the type system to prevent errors. When enum values have -data inside them, you can use `match` or `if let` to extract and use those -values, depending on how many cases you need to handle. +type helps you use the type system to prevent errors. When enum values have data +inside them, you can use `match` or `if let` to extract and use those values, +depending on how many cases you need to handle. Your Rust programs can now express concepts in your domain using structs and enums. Creating custom types to use in your API ensures type safety: the diff --git a/src/ch07-00-managing-growing-projects-with-packages-crates-and-modules.md b/src/ch07-00-managing-growing-projects-with-packages-crates-and-modules.md index 1786711d8d..69f711af63 100644 --- a/src/ch07-00-managing-growing-projects-with-packages-crates-and-modules.md +++ b/src/ch07-00-managing-growing-projects-with-packages-crates-and-modules.md @@ -2,17 +2,17 @@ As you write large programs, organizing your code will become increasingly important. By grouping related functionality and separating code with distinct -features, you’ll clarify where to find code that implements a particular -feature and where to go to change how a feature works. +features, you’ll clarify where to find code that implements a particular feature +and where to go to change how a feature works. The programs we’ve written so far have been in one module in one file. As a project grows, you should organize code by splitting it into multiple modules and then multiple files. A package can contain multiple binary crates and optionally one library crate. As a package grows, you can extract parts into -separate crates that become external dependencies. This chapter covers all -these techniques. For very large projects comprising a set of interrelated -packages that evolve together, Cargo provides _workspaces_, which we’ll cover -in the [“Cargo Workspaces”][workspaces] section in Chapter 14. +separate crates that become external dependencies. This chapter covers all these +techniques. For very large projects comprising a set of interrelated packages +that evolve together, Cargo provides _workspaces_, which we’ll cover in the +[“Cargo Workspaces”][workspaces] section in Chapter 14. We’ll also discuss encapsulating implementation details, which lets you reuse code at a higher level: once you’ve implemented an operation, other code can @@ -24,22 +24,22 @@ you have to keep in your head. A related concept is scope: the nested context in which code is written has a set of names that are defined as “in scope.” When reading, writing, and -compiling code, programmers and compilers need to know whether a particular -name at a particular spot refers to a variable, function, struct, enum, module, +compiling code, programmers and compilers need to know whether a particular name +at a particular spot refers to a variable, function, struct, enum, module, constant, or other item and what that item means. You can create scopes and change which names are in or out of scope. You can’t have two items with the same name in the same scope; tools are available to resolve name conflicts. -Rust has a number of features that allow you to manage your code’s -organization, including which details are exposed, which details are private, -and what names are in each scope in your programs. These features, sometimes -collectively referred to as the _module system_, include: +Rust has a number of features that allow you to manage your code’s organization, +including which details are exposed, which details are private, and what names +are in each scope in your programs. These features, sometimes collectively +referred to as the _module system_, include: -- **Packages:** A Cargo feature that lets you build, test, and share crates -- **Crates:** A tree of modules that produces a library or executable -- **Modules** and **use:** Let you control the organization, scope, and - privacy of paths -- **Paths:** A way of naming an item, such as a struct, function, or module +* **Packages:** A Cargo feature that lets you build, test, and share crates +* **Crates:** A tree of modules that produces a library or executable +* **Modules** and **use:** Let you control the organization, scope, and privacy + of paths +* **Paths:** A way of naming an item, such as a struct, function, or module In this chapter, we’ll cover all these features, discuss how they interact, and explain how to use them to manage scope. By the end, you should have a solid diff --git a/src/ch07-01-packages-and-crates.md b/src/ch07-01-packages-and-crates.md index 281f27b7b6..6e984f8b7e 100644 --- a/src/ch07-01-packages-and-crates.md +++ b/src/ch07-01-packages-and-crates.md @@ -9,18 +9,19 @@ section of Chapter 1), the compiler considers that file to be a crate. Crates can contain modules, and the modules may be defined in other files that get compiled with the crate, as we’ll see in the coming sections. -A crate can come in one of two forms: a binary crate or a library crate. -_Binary crates_ are programs you can compile to an executable that you can run, -such as a command-line program or a server. Each must have a function called -`main` that defines what happens when the executable runs. All the crates we’ve -created so far have been binary crates. +A crate can come in one of two forms: a binary crate or a library crate. _Binary +crates_ are programs you can compile to an executable that you can run, such as +a command-line program or a server. Each must have a function called `main` that +defines what happens when the executable runs. All the crates we’ve created so +far have been binary crates. _Library crates_ don’t have a `main` function, and they don’t compile to an executable. Instead, they define functionality intended to be shared with -multiple projects. For example, the `rand` crate we used in [Chapter -2][rand] provides functionality that generates random numbers. -Most of the time when Rustaceans say “crate”, they mean library crate, and they -use “crate” interchangeably with the general programming concept of a “library”. +multiple projects. For example, the `rand` crate we used in +[Chapter 2][rand] provides functionality that generates random +numbers. Most of the time when Rustaceans say “crate”, they mean library crate, +and they use “crate” interchangeably with the general programming concept of a +“library”. The _crate root_ is a source file that the Rust compiler starts from and makes up the root module of your crate (we’ll explain modules in depth in the @@ -33,9 +34,9 @@ build those crates. Cargo is actually a package that contains the binary crate for the command-line tool you’ve been using to build your code. The Cargo package also contains a library crate that the binary crate depends on. Other projects can depend on the Cargo library crate to use the same logic the Cargo -command-line tool uses. A package can contain as many binary crates as you -like, but at most only one library crate. A package must contain at least one -crate, whether that’s a library or binary crate. +command-line tool uses. A package can contain as many binary crates as you like, +but at most only one library crate. A package must contain at least one crate, +whether that’s a library or binary crate. Let’s walk through what happens when we create a package. First we enter the command `cargo new my-project`: @@ -51,14 +52,14 @@ main.rs ``` After we run `cargo new my-project`, we use `ls` to see what Cargo creates. In -the project directory, there’s a _Cargo.toml_ file, giving us a package. -There’s also a _src_ directory that contains _main.rs_. Open _Cargo.toml_ in -your text editor, and note there’s no mention of _src/main.rs_. Cargo follows a -convention that _src/main.rs_ is the crate root of a binary crate with the same -name as the package. Likewise, Cargo knows that if the package directory -contains _src/lib.rs_, the package contains a library crate with the same name -as the package, and _src/lib.rs_ is its crate root. Cargo passes the crate root -files to `rustc` to build the library or binary. +the project directory, there’s a _Cargo.toml_ file, giving us a package. There’s +also a _src_ directory that contains _main.rs_. Open _Cargo.toml_ in your text +editor, and note there’s no mention of _src/main.rs_. Cargo follows a convention +that _src/main.rs_ is the crate root of a binary crate with the same name as the +package. Likewise, Cargo knows that if the package directory contains +_src/lib.rs_, the package contains a library crate with the same name as the +package, and _src/lib.rs_ is its crate root. Cargo passes the crate root files +to `rustc` to build the library or binary. Here, we have a package that only contains _src/main.rs_, meaning it only contains a binary crate named `my-project`. If a package contains _src/main.rs_ diff --git a/src/ch07-02-defining-modules-to-control-scope-and-privacy.md b/src/ch07-02-defining-modules-to-control-scope-and-privacy.md index 29924e6fe2..cecf63e73d 100644 --- a/src/ch07-02-defining-modules-to-control-scope-and-privacy.md +++ b/src/ch07-02-defining-modules-to-control-scope-and-privacy.md @@ -13,36 +13,38 @@ in the compiler, and how most developers organize their code. We’ll be going through examples of each of these rules throughout this chapter, but this is a great place to refer to as a reminder of how modules work. -- **Start from the crate root**: When compiling a crate, the compiler first +* **Start from the crate root**: When compiling a crate, the compiler first looks in the crate root file (usually _src/lib.rs_ for a library crate or _src/main.rs_ for a binary crate) for code to compile. -- **Declaring modules**: In the crate root file, you can declare new modules; +* **Declaring modules**: In the crate root file, you can declare new modules; say you declare a “garden” module with `mod garden;`. The compiler will look for the module’s code in these places: - - Inline, within curly brackets that replace the semicolon following `mod + * Inline, within curly brackets that replace the semicolon following + `mod garden` - - In the file _src/garden.rs_ - - In the file _src/garden/mod.rs_ -- **Declaring submodules**: In any file other than the crate root, you can + * In the file _src/garden.rs_ + * In the file _src/garden/mod.rs_ +* **Declaring submodules**: In any file other than the crate root, you can declare submodules. For example, you might declare `mod vegetables;` in _src/garden.rs_. The compiler will look for the submodule’s code within the directory named for the parent module in these places: - - Inline, directly following `mod vegetables`, within curly brackets instead + * Inline, directly following `mod vegetables`, within curly brackets instead of the semicolon - - In the file _src/garden/vegetables.rs_ - - In the file _src/garden/vegetables/mod.rs_ -- **Paths to code in modules**: Once a module is part of your crate, you can - refer to code in that module from anywhere else in that same crate, as long - as the privacy rules allow, using the path to the code. For example, an + * In the file _src/garden/vegetables.rs_ + * In the file _src/garden/vegetables/mod.rs_ +* **Paths to code in modules**: Once a module is part of your crate, you can + refer to code in that module from anywhere else in that same crate, as long as + the privacy rules allow, using the path to the code. For example, an `Asparagus` type in the garden vegetables module would be found at `crate::garden::vegetables::Asparagus`. -- **Private vs. public**: Code within a module is private from its parent - modules by default. To make a module public, declare it with `pub mod` - instead of `mod`. To make items within a public module public as well, use - `pub` before their declarations. -- **The `use` keyword**: Within a scope, the `use` keyword creates shortcuts to +* **Private vs. public**: Code within a module is private from its parent + modules by default. To make a module public, declare it with `pub mod` instead + of `mod`. To make items within a public module public as well, use `pub` + before their declarations. +* **The `use` keyword**: Within a scope, the `use` keyword creates shortcuts to items to reduce repetition of long paths. In any scope that can refer to - `crate::garden::vegetables::Asparagus`, you can create a shortcut with `use + `crate::garden::vegetables::Asparagus`, you can create a shortcut with + `use crate::garden::vegetables::Asparagus;` and from then on you only need to write `Asparagus` to make use of that type in the scope. @@ -105,18 +107,18 @@ restaurant. We’ll define the signatures of functions but leave their bodies empty to concentrate on the organization of the code rather than the implementation of a restaurant. -In the restaurant industry, some parts of a restaurant are referred to as -_front of house_ and others as _back of house_. Front of house is where -customers are; this encompasses where the hosts seat customers, servers take -orders and payment, and bartenders make drinks. Back of house is where the -chefs and cooks work in the kitchen, dishwashers clean up, and managers do -administrative work. +In the restaurant industry, some parts of a restaurant are referred to as _front +of house_ and others as _back of house_. Front of house is where customers are; +this encompasses where the hosts seat customers, servers take orders and +payment, and bartenders make drinks. Back of house is where the chefs and cooks +work in the kitchen, dishwashers clean up, and managers do administrative work. To structure our crate in this way, we can organize its functions into nested -modules. Create a new library named `restaurant` by running `cargo new -restaurant --lib`. Then enter the code in Listing 7-1 into _src/lib.rs_ to -define some modules and function signatures; this code is the front of house -section. +modules. Create a new library named `restaurant` by running +`cargo new +restaurant --lib`. Then enter the code in Listing 7-1 into +_src/lib.rs_ to define some modules and function signatures; this code is the +front of house section. @@ -126,18 +128,18 @@ section. -We define a module with the `mod` keyword followed by the name of the module -(in this case, `front_of_house`). The body of the module then goes inside curly +We define a module with the `mod` keyword followed by the name of the module (in +this case, `front_of_house`). The body of the module then goes inside curly brackets. Inside modules, we can place other modules, as in this case with the modules `hosting` and `serving`. Modules can also hold definitions for other items, such as structs, enums, constants, traits, and—as in Listing 7-1—functions. -By using modules, we can group related definitions together and name why -they’re related. Programmers using this code can navigate the code based on the -groups rather than having to read through all the definitions, making it easier -to find the definitions relevant to them. Programmers adding new functionality -to this code would know where to place the code to keep the program organized. +By using modules, we can group related definitions together and name why they’re +related. Programmers using this code can navigate the code based on the groups +rather than having to read through all the definitions, making it easier to find +the definitions relevant to them. Programmers adding new functionality to this +code would know where to place the code to keep the program organized. Earlier, we mentioned that _src/main.rs_ and _src/lib.rs_ are called crate roots. The reason for their name is that the contents of either of these two @@ -165,10 +167,10 @@ crate This tree shows how some of the modules nest inside other modules; for example, `hosting` nests inside `front_of_house`. The tree also shows that some modules are _siblings_, meaning they’re defined in the same module; `hosting` and -`serving` are siblings defined within `front_of_house`. If module A is -contained inside module B, we say that module A is the _child_ of module B and -that module B is the _parent_ of module A. Notice that the entire module tree -is rooted under the implicit module named `crate`. +`serving` are siblings defined within `front_of_house`. If module A is contained +inside module B, we say that module A is the _child_ of module B and that module +B is the _parent_ of module A. Notice that the entire module tree is rooted +under the implicit module named `crate`. The module tree might remind you of the filesystem’s directory tree on your computer; this is a very apt comparison! Just like directories in a filesystem, diff --git a/src/ch07-03-paths-for-referring-to-an-item-in-the-module-tree.md b/src/ch07-03-paths-for-referring-to-an-item-in-the-module-tree.md index a336f94a69..ab5f1db597 100644 --- a/src/ch07-03-paths-for-referring-to-an-item-in-the-module-tree.md +++ b/src/ch07-03-paths-for-referring-to-an-item-in-the-module-tree.md @@ -6,10 +6,10 @@ know its path. A path can take two forms: -- An _absolute path_ is the full path starting from a crate root; for code - from an external crate, the absolute path begins with the crate name, and for - code from the current crate, it starts with the literal `crate`. -- A _relative path_ starts from the current module and uses `self`, `super`, or +* An _absolute path_ is the full path starting from a crate root; for code from + an external crate, the absolute path begins with the crate name, and for code + from the current crate, it starts with the literal `crate`. +* A _relative path_ starts from the current module and uses `self`, `super`, or an identifier in the current module. Both absolute and relative paths are followed by one or more identifiers @@ -17,8 +17,7 @@ separated by double colons (`::`). Returning to Listing 7-1, say we want to call the `add_to_waitlist` function. This is the same as asking: what’s the path of the `add_to_waitlist` function? -Listing 7-3 contains Listing 7-1 with some of the modules and functions -removed. +Listing 7-3 contains Listing 7-1 with some of the modules and functions removed. We’ll show two ways to call the `add_to_waitlist` function from a new function, `eat_at_restaurant`, defined in the crate root. These paths are correct, but @@ -26,8 +25,9 @@ there’s another problem remaining that will prevent this example from compilin as is. We’ll explain why in a bit. The `eat_at_restaurant` function is part of our library crate’s public API, so -we mark it with the `pub` keyword. In the [“Exposing Paths with the `pub` -Keyword”][pub] section, we’ll go into more detail about `pub`. +we mark it with the `pub` keyword. In the +[“Exposing Paths with the `pub` Keyword”][pub] section, we’ll go +into more detail about `pub`. @@ -37,8 +37,8 @@ Keyword”][pub] section, we’ll go into more detail about `pub` -The first time we call the `add_to_waitlist` function in `eat_at_restaurant`, -we use an absolute path. The `add_to_waitlist` function is defined in the same +The first time we call the `add_to_waitlist` function in `eat_at_restaurant`, we +use an absolute path. The `add_to_waitlist` function is defined in the same crate as `eat_at_restaurant`, which means we can use the `crate` keyword to start an absolute path. We then include each of the successive modules until we make our way to `add_to_waitlist`. You can imagine a filesystem with the same @@ -50,21 +50,20 @@ The second time we call `add_to_waitlist` in `eat_at_restaurant`, we use a relative path. The path starts with `front_of_house`, the name of the module defined at the same level of the module tree as `eat_at_restaurant`. Here the filesystem equivalent would be using the path -`front_of_house/hosting/add_to_waitlist`. Starting with a module name means -that the path is relative. +`front_of_house/hosting/add_to_waitlist`. Starting with a module name means that +the path is relative. Choosing whether to use a relative or absolute path is a decision you’ll make -based on your project, and it depends on whether you’re more likely to move -item definition code separately from or together with the code that uses the -item. For example, if we moved the `front_of_house` module and the -`eat_at_restaurant` function into a module named `customer_experience`, we’d -need to update the absolute path to `add_to_waitlist`, but the relative path -would still be valid. However, if we moved the `eat_at_restaurant` function -separately into a module named `dining`, the absolute path to the -`add_to_waitlist` call would stay the same, but the relative path would need to -be updated. Our preference in general is to specify absolute paths because it’s -more likely we’ll want to move code definitions and item calls independently of -each other. +based on your project, and it depends on whether you’re more likely to move item +definition code separately from or together with the code that uses the item. +For example, if we moved the `front_of_house` module and the `eat_at_restaurant` +function into a module named `customer_experience`, we’d need to update the +absolute path to `add_to_waitlist`, but the relative path would still be valid. +However, if we moved the `eat_at_restaurant` function separately into a module +named `dining`, the absolute path to the `add_to_waitlist` call would stay the +same, but the relative path would need to be updated. Our preference in general +is to specify absolute paths because it’s more likely we’ll want to move code +definitions and item calls independently of each other. Let’s try to compile Listing 7-3 and find out why it won’t compile yet! The errors we get are shown in Listing 7-4. @@ -77,20 +76,20 @@ errors we get are shown in Listing 7-4. -The error messages say that module `hosting` is private. In other words, we -have the correct paths for the `hosting` module and the `add_to_waitlist` -function, but Rust won’t let us use them because it doesn’t have access to the -private sections. In Rust, all items (functions, methods, structs, enums, -modules, and constants) are private to parent modules by default. If you want -to make an item like a function or struct private, you put it in a module. +The error messages say that module `hosting` is private. In other words, we have +the correct paths for the `hosting` module and the `add_to_waitlist` function, +but Rust won’t let us use them because it doesn’t have access to the private +sections. In Rust, all items (functions, methods, structs, enums, modules, and +constants) are private to parent modules by default. If you want to make an item +like a function or struct private, you put it in a module. Items in a parent module can’t use the private items inside child modules, but items in child modules can use the items in their ancestor modules. This is because child modules wrap and hide their implementation details, but the child modules can see the context in which they’re defined. To continue with our metaphor, think of the privacy rules as being like the back office of a -restaurant: what goes on in there is private to restaurant customers, but -office managers can see and do everything in the restaurant they operate. +restaurant: what goes on in there is private to restaurant customers, but office +managers can see and do everything in the restaurant they operate. Rust chose to have the module system function this way so that hiding inner implementation details is the default. That way, you know which parts of the @@ -130,8 +129,8 @@ access `hosting`. But the _contents_ of `hosting` are still private; making the module public doesn’t make its contents public. The `pub` keyword on a module only lets code in its ancestor modules refer to it, not access its inner code. Because modules are containers, there’s not much we can do by only making the -module public; we need to go further and choose to make one or more of the -items within the module public as well. +module public; we need to go further and choose to make one or more of the items +within the module public as well. The errors in Listing 7-6 say that the `add_to_waitlist` function is private. The privacy rules apply to structs, enums, functions, and methods as well as @@ -148,15 +147,15 @@ keyword before its definition, as in Listing 7-7. -Now the code will compile! To see why adding the `pub` keyword lets us use -these paths in `eat_at_restaurant` with respect to the privacy rules, let’s look -at the absolute and the relative paths. +Now the code will compile! To see why adding the `pub` keyword lets us use these +paths in `eat_at_restaurant` with respect to the privacy rules, let’s look at +the absolute and the relative paths. In the absolute path, we start with `crate`, the root of our crate’s module tree. The `front_of_house` module is defined in the crate root. While `front_of_house` isn’t public, because the `eat_at_restaurant` function is -defined in the same module as `front_of_house` (that is, `eat_at_restaurant` -and `front_of_house` are siblings), we can refer to `front_of_house` from +defined in the same module as `front_of_house` (that is, `eat_at_restaurant` and +`front_of_house` are siblings), we can refer to `front_of_house` from `eat_at_restaurant`. Next is the `hosting` module marked with `pub`. We can access the parent module of `hosting`, so we can access `hosting`. Finally, the `add_to_waitlist` function is marked with `pub` and we can access its parent @@ -173,19 +172,19 @@ function call is valid! If you plan on sharing your library crate so other projects can use your code, your public API is your contract with users of your crate that determines how they can interact with your code. There are many considerations around managing -changes to your public API to make it easier for people to depend on your -crate. These considerations are out of the scope of this book; if you’re -interested in this topic, see [The Rust API Guidelines][api-guidelines]. +changes to your public API to make it easier for people to depend on your crate. +These considerations are out of the scope of this book; if you’re interested in +this topic, see [The Rust API Guidelines][api-guidelines]. > #### Best Practices for Packages with a Binary and a Library > -> We mentioned that a package can contain both a _src/main.rs_ binary crate -> root as well as a _src/lib.rs_ library crate root, and both crates will have -> the package name by default. Typically, packages with this pattern of -> containing both a library and a binary crate will have just enough code in the -> binary crate to start an executable that calls code within the library crate. -> This lets other projects benefit from most of the functionality that the -> package provides because the library crate’s code can be shared. +> We mentioned that a package can contain both a _src/main.rs_ binary crate root +> as well as a _src/lib.rs_ library crate root, and both crates will have the +> package name by default. Typically, packages with this pattern of containing +> both a library and a binary crate will have just enough code in the binary +> crate to start an executable that calls code within the library crate. This +> lets other projects benefit from most of the functionality that the package +> provides because the library crate’s code can be shared. > > The module tree should be defined in _src/lib.rs_. Then, any public items can > be used in the binary crate by starting paths with the name of the package. @@ -195,23 +194,22 @@ interested in this topic, see [The Rust API Guidelines][api-guidelines]. > client! > > In [Chapter 12][ch12], we’ll demonstrate this organizational -> practice with a command-line program that will contain both a binary crate -> and a library crate. +> practice with a command-line program that will contain both a binary crate and +> a library crate. ### Starting Relative Paths with `super` -We can construct relative paths that begin in the parent module, rather than -the current module or the crate root, by using `super` at the start of the -path. This is like starting a filesystem path with the `..` syntax. Using -`super` allows us to reference an item that we know is in the parent module, -which can make rearranging the module tree easier when the module is closely -related to the parent but the parent might be moved elsewhere in the module -tree someday. - -Consider the code in Listing 7-8 that models the situation in which a chef -fixes an incorrect order and personally brings it out to the customer. The -function `fix_incorrect_order` defined in the `back_of_house` module calls the -function `deliver_order` defined in the parent module by specifying the path to +We can construct relative paths that begin in the parent module, rather than the +current module or the crate root, by using `super` at the start of the path. +This is like starting a filesystem path with the `..` syntax. Using `super` +allows us to reference an item that we know is in the parent module, which can +make rearranging the module tree easier when the module is closely related to +the parent but the parent might be moved elsewhere in the module tree someday. + +Consider the code in Listing 7-8 that models the situation in which a chef fixes +an incorrect order and personally brings it out to the customer. The function +`fix_incorrect_order` defined in the `back_of_house` module calls the function +`deliver_order` defined in the parent module by specifying the path to `deliver_order`, starting with `super`. @@ -223,13 +221,13 @@ function `deliver_order` defined in the parent module by specifying the path to The `fix_incorrect_order` function is in the `back_of_house` module, so we can -use `super` to go to the parent module of `back_of_house`, which in this case -is `crate`, the root. From there, we look for `deliver_order` and find it. -Success! We think the `back_of_house` module and the `deliver_order` function -are likely to stay in the same relationship to each other and get moved -together should we decide to reorganize the crate’s module tree. Therefore, we -used `super` so we’ll have fewer places to update code in the future if this -code gets moved to a different module. +use `super` to go to the parent module of `back_of_house`, which in this case is +`crate`, the root. From there, we look for `deliver_order` and find it. Success! +We think the `back_of_house` module and the `deliver_order` function are likely +to stay in the same relationship to each other and get moved together should we +decide to reorganize the crate’s module tree. Therefore, we used `super` so +we’ll have fewer places to update code in the future if this code gets moved to +a different module. ### Making Structs and Enums Public @@ -252,18 +250,18 @@ customers can’t choose the fruit or even see which fruit they’ll get. -Because the `toast` field in the `back_of_house::Breakfast` struct is public, -in `eat_at_restaurant` we can write and read to the `toast` field using dot +Because the `toast` field in the `back_of_house::Breakfast` struct is public, in +`eat_at_restaurant` we can write and read to the `toast` field using dot notation. Notice that we can’t use the `seasonal_fruit` field in `eat_at_restaurant`, because `seasonal_fruit` is private. Try uncommenting the line modifying the `seasonal_fruit` field value to see what error you get! Also, note that because `back_of_house::Breakfast` has a private field, the -struct needs to provide a public associated function that constructs an -instance of `Breakfast` (we’ve named it `summer` here). If `Breakfast` didn’t -have such a function, we couldn’t create an instance of `Breakfast` in -`eat_at_restaurant` because we couldn’t set the value of the private -`seasonal_fruit` field in `eat_at_restaurant`. +struct needs to provide a public associated function that constructs an instance +of `Breakfast` (we’ve named it `summer` here). If `Breakfast` didn’t have such a +function, we couldn’t create an instance of `Breakfast` in `eat_at_restaurant` +because we couldn’t set the value of the private `seasonal_fruit` field in +`eat_at_restaurant`. In contrast, if we make an enum public, all of its variants are then public. We only need the `pub` before the `enum` keyword, as shown in Listing 7-10. @@ -281,9 +279,9 @@ variants in `eat_at_restaurant`. Enums aren’t very useful unless their variants are public; it would be annoying to have to annotate all enum variants with `pub` in every case, so the default -for enum variants is to be public. Structs are often useful without their -fields being public, so struct fields follow the general rule of everything -being private by default unless annotated with `pub`. +for enum variants is to be public. Structs are often useful without their fields +being public, so struct fields follow the general rule of everything being +private by default unless annotated with `pub`. There’s one more situation involving `pub` that we haven’t covered, and that is our last module system feature: the `use` keyword. We’ll cover `use` by itself diff --git a/src/ch07-04-bringing-paths-into-scope-with-the-use-keyword.md b/src/ch07-04-bringing-paths-into-scope-with-the-use-keyword.md index 97b2b656a8..276e39761f 100644 --- a/src/ch07-04-bringing-paths-into-scope-with-the-use-keyword.md +++ b/src/ch07-04-bringing-paths-into-scope-with-the-use-keyword.md @@ -3,8 +3,8 @@ Having to write out the paths to call functions can feel inconvenient and repetitive. In Listing 7-7, whether we chose the absolute or relative path to the `add_to_waitlist` function, every time we wanted to call `add_to_waitlist` -we had to specify `front_of_house` and `hosting` too. Fortunately, there’s a -way to simplify this process: we can create a shortcut to a path with the `use` +we had to specify `front_of_house` and `hosting` too. Fortunately, there’s a way +to simplify this process: we can create a shortcut to a path with the `use` keyword once, and then use the shorter name everywhere else in the scope. In Listing 7-11, we bring the `crate::front_of_house::hosting` module into the @@ -20,11 +20,11 @@ scope of the `eat_at_restaurant` function so we only have to specify -Adding `use` and a path in a scope is similar to creating a symbolic link in -the filesystem. By adding `use crate::front_of_house::hosting` in the crate -root, `hosting` is now a valid name in that scope, just as though the `hosting` -module had been defined in the crate root. Paths brought into scope with `use` -also check privacy, like any other paths. +Adding `use` and a path in a scope is similar to creating a symbolic link in the +filesystem. By adding `use crate::front_of_house::hosting` in the crate root, +`hosting` is now a valid name in that scope, just as though the `hosting` module +had been defined in the crate root. Paths brought into scope with `use` also +check privacy, like any other paths. Note that `use` only creates the shortcut for the particular scope in which the `use` occurs. Listing 7-12 moves the `eat_at_restaurant` function into a new @@ -53,9 +53,10 @@ the shortcut in the parent module with `super::hosting` within the child ### Creating Idiomatic `use` Paths -In Listing 7-11, you might have wondered why we specified `use -crate::front_of_house::hosting` and then called `hosting::add_to_waitlist` in -`eat_at_restaurant`, rather than specifying the `use` path all the way out to +In Listing 7-11, you might have wondered why we specified +`use +crate::front_of_house::hosting` and then called `hosting::add_to_waitlist` +in `eat_at_restaurant`, rather than specifying the `use` path all the way out to the `add_to_waitlist` function to achieve the same result, as in Listing 7-13. @@ -75,9 +76,8 @@ while still minimizing repetition of the full path. The code in Listing 7-13 is unclear as to where `add_to_waitlist` is defined. On the other hand, when bringing in structs, enums, and other items with `use`, -it’s idiomatic to specify the full path. Listing 7-14 shows the idiomatic way -to bring the standard library’s `HashMap` struct into the scope of a binary -crate. +it’s idiomatic to specify the full path. Listing 7-14 shows the idiomatic way to +bring the standard library’s `HashMap` struct into the scope of a binary crate. @@ -125,8 +125,8 @@ the code in Listing 7-15 by renaming one of the two `Result` types using `as`. In the second `use` statement, we chose the new name `IoResult` for the `std::io::Result` type, which won’t conflict with the `Result` from `std::fmt` -that we’ve also brought into scope. Listing 7-15 and Listing 7-16 are -considered idiomatic, so the choice is up to you! +that we’ve also brought into scope. Listing 7-15 and Listing 7-16 are considered +idiomatic, so the choice is up to you! ### Re-exporting Names with `pub use` @@ -151,9 +151,10 @@ changed to `pub use`. Before this change, external code would have to call the `add_to_waitlist` function by using the path `restaurant::front_of_house::hosting::add_to_waitlist()`, which also would have -required the `front_of_house` module to be marked as `pub`. Now that this `pub -use` has re-exported the `hosting` module from the root module, external code -can use the path `restaurant::hosting::add_to_waitlist()` instead. +required the `front_of_house` module to be marked as `pub`. Now that this +`pub +use` has re-exported the `hosting` module from the root module, external +code can use the path `restaurant::hosting::add_to_waitlist()` instead. Re-exporting is useful when the internal structure of your code is different from how programmers calling your code would think about the domain. For @@ -163,7 +164,8 @@ probably won’t think about the parts of the restaurant in those terms. With `pub use`, we can write our code with one structure but expose a different structure. Doing so makes our library well organized for programmers working on the library and programmers calling the library. We’ll look at another example -of `pub use` and how it affects your crate’s documentation in the [“Exporting a +of `pub use` and how it affects your crate’s documentation in the +[“Exporting a Convenient Public API with `pub use`”][ch14-pub-use] section of Chapter 14. @@ -187,15 +189,15 @@ added this line to _Cargo.toml_: -Adding `rand` as a dependency in _Cargo.toml_ tells Cargo to download the -`rand` package and any dependencies from [crates.io](https://crates.io/) and -make `rand` available to our project. +Adding `rand` as a dependency in _Cargo.toml_ tells Cargo to download the `rand` +package and any dependencies from [crates.io](https://crates.io/) and make +`rand` available to our project. Then, to bring `rand` definitions into the scope of our package, we added a -`use` line starting with the name of the crate, `rand`, and listed the items -we wanted to bring into scope. Recall that in the [“Generating a Random -Number”][rand] section in Chapter 2, we brought the `Rng` trait -into scope and called the `rand::thread_rng` function: +`use` line starting with the name of the crate, `rand`, and listed the items we +wanted to bring into scope. Recall that in the +[“Generating a Random Number”][rand] section in Chapter 2, we +brought the `Rng` trait into scope and called the `rand::thread_rng` function: ```rust,ignore {{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-03/src/main.rs:ch07-04}} @@ -234,10 +236,10 @@ bring items from `std` into scope: -Instead, we can use nested paths to bring the same items into scope in one -line. We do this by specifying the common part of the path, followed by two -colons, and then curly brackets around a list of the parts of the paths that -differ, as shown in Listing 7-18. +Instead, we can use nested paths to bring the same items into scope in one line. +We do this by specifying the common part of the path, followed by two colons, +and then curly brackets around a list of the parts of the paths that differ, as +shown in Listing 7-18. @@ -247,9 +249,9 @@ differ, as shown in Listing 7-18. -In bigger programs, bringing many items into scope from the same crate or -module using nested paths can reduce the number of separate `use` statements -needed by a lot! +In bigger programs, bringing many items into scope from the same crate or module +using nested paths can reduce the number of separate `use` statements needed by +a lot! We can use a nested path at any level in a path, which is useful when combining two `use` statements that share a subpath. For example, Listing 7-19 shows two @@ -289,13 +291,14 @@ use std::collections::*; This `use` statement brings all public items defined in `std::collections` into the current scope. Be careful when using the glob operator! Glob can make it -harder to tell what names are in scope and where a name used in your program -was defined. - -The glob operator is often used when testing to bring everything under test -into the `tests` module; we’ll talk about that in the [“How to Write -Tests”][writing-tests] section in Chapter 11. The glob operator -is also sometimes used as part of the prelude pattern: see [the standard library documentation](../std/prelude/index.html#other-preludes) +harder to tell what names are in scope and where a name used in your program was +defined. + +The glob operator is often used when testing to bring everything under test into +the `tests` module; we’ll talk about that in the +[“How to Write Tests”][writing-tests] section in Chapter 11. The +glob operator is also sometimes used as part of the prelude pattern: see +[the standard library documentation](../std/prelude/index.html#other-preludes) for more information on that pattern. [ch14-pub-use]: ch14-02-publishing-to-crates-io.html#exporting-a-convenient-public-api-with-pub-use diff --git a/src/ch07-05-separating-modules-into-different-files.md b/src/ch07-05-separating-modules-into-different-files.md index 4de9b6908f..14c96973b1 100644 --- a/src/ch07-05-separating-modules-into-different-files.md +++ b/src/ch07-05-separating-modules-into-different-files.md @@ -10,10 +10,10 @@ modules defined in the crate root file. In this case, the crate root file is _src/lib.rs_, but this procedure also works with binary crates whose crate root file is _src/main.rs_. -First we’ll extract the `front_of_house` module to its own file. Remove the -code inside the curly brackets for the `front_of_house` module, leaving only -the `mod front_of_house;` declaration, so that _src/lib.rs_ contains the code -shown in Listing 7-21. Note that this won’t compile until we create the +First we’ll extract the `front_of_house` module to its own file. Remove the code +inside the curly brackets for the `front_of_house` module, leaving only the +`mod front_of_house;` declaration, so that _src/lib.rs_ contains the code shown +in Listing 7-21. Note that this won’t compile until we create the _src/front_of_house.rs_ file in Listing 7-22. @@ -25,9 +25,9 @@ _src/front_of_house.rs_ file in Listing 7-22. Next, place the code that was in the curly brackets into a new file named -_src/front_of_house.rs_, as shown in Listing 7-22. The compiler knows to look -in this file because it came across the module declaration in the crate root -with the name `front_of_house`. +_src/front_of_house.rs_, as shown in Listing 7-22. The compiler knows to look in +this file because it came across the module declaration in the crate root with +the name `front_of_house`. @@ -41,18 +41,18 @@ Note that you only need to load a file using a `mod` declaration _once_ in your module tree. Once the compiler knows the file is part of the project (and knows where in the module tree the code resides because of where you’ve put the `mod` statement), other files in your project should refer to the loaded file’s code -using a path to where it was declared, as covered in the [“Paths for Referring -to an Item in the Module Tree”][paths] section. In other words, -`mod` is _not_ an “include” operation that you may have seen in other -programming languages. +using a path to where it was declared, as covered in the +[“Paths for Referring to an Item in the Module Tree”][paths] +section. In other words, `mod` is _not_ an “include” operation that you may have +seen in other programming languages. Next, we’ll extract the `hosting` module to its own file. The process is a bit different because `hosting` is a child module of `front_of_house`, not of the root module. We’ll place the file for `hosting` in a new directory that will be named for its ancestors in the module tree, in this case _src/front_of_house_. -To start moving `hosting`, we change _src/front_of_house.rs_ to contain only -the declaration of the `hosting` module: +To start moving `hosting`, we change _src/front_of_house.rs_ to contain only the +declaration of the `hosting` module: @@ -73,31 +73,31 @@ contain the definitions made in the `hosting` module: -If we instead put _hosting.rs_ in the _src_ directory, the compiler would -expect the _hosting.rs_ code to be in a `hosting` module declared in the crate -root, and not declared as a child of the `front_of_house` module. The -compiler’s rules for which files to check for which modules’ code mean the -directories and files more closely match the module tree. +If we instead put _hosting.rs_ in the _src_ directory, the compiler would expect +the _hosting.rs_ code to be in a `hosting` module declared in the crate root, +and not declared as a child of the `front_of_house` module. The compiler’s rules +for which files to check for which modules’ code mean the directories and files +more closely match the module tree. > ### Alternate File Paths > -> So far we’ve covered the most idiomatic file paths the Rust compiler uses, -> but Rust also supports an older style of file path. For a module named +> So far we’ve covered the most idiomatic file paths the Rust compiler uses, but +> Rust also supports an older style of file path. For a module named > `front_of_house` declared in the crate root, the compiler will look for the > module’s code in: > -> - _src/front_of_house.rs_ (what we covered) -> - _src/front_of_house/mod.rs_ (older style, still supported path) +> * _src/front_of_house.rs_ (what we covered) +> * _src/front_of_house/mod.rs_ (older style, still supported path) > > For a module named `hosting` that is a submodule of `front_of_house`, the > compiler will look for the module’s code in: > -> - _src/front_of_house/hosting.rs_ (what we covered) -> - _src/front_of_house/hosting/mod.rs_ (older style, still supported path) +> * _src/front_of_house/hosting.rs_ (what we covered) +> * _src/front_of_house/hosting/mod.rs_ (older style, still supported path) > -> If you use both styles for the same module, you’ll get a compiler error. -> Using a mix of both styles for different modules in the same project is -> allowed, but might be confusing for people navigating your project. +> If you use both styles for the same module, you’ll get a compiler error. Using +> a mix of both styles for different modules in the same project is allowed, but +> might be confusing for people navigating your project. > > The main downside to the style that uses files named _mod.rs_ is that your > project can end up with many files named _mod.rs_, which can get confusing @@ -108,11 +108,10 @@ the same. The function calls in `eat_at_restaurant` will work without any modification, even though the definitions live in different files. This technique lets you move modules to new files as they grow in size. -Note that the `pub use crate::front_of_house::hosting` statement in -_src/lib.rs_ also hasn’t changed, nor does `use` have any impact on what files -are compiled as part of the crate. The `mod` keyword declares modules, and Rust -looks in a file with the same name as the module for the code that goes into -that module. +Note that the `pub use crate::front_of_house::hosting` statement in _src/lib.rs_ +also hasn’t changed, nor does `use` have any impact on what files are compiled +as part of the crate. The `mod` keyword declares modules, and Rust looks in a +file with the same name as the module for the code that goes into that module. ## Summary diff --git a/src/ch08-00-common-collections.md b/src/ch08-00-common-collections.md index 7cc5c424b8..0cddb0f497 100644 --- a/src/ch08-00-common-collections.md +++ b/src/ch08-00-common-collections.md @@ -6,14 +6,14 @@ collections can contain multiple values. Unlike the built-in array and tuple types, the data these collections point to is stored on the heap, which means the amount of data does not need to be known at compile time and can grow or shrink as the program runs. Each kind of collection has different capabilities -and costs, and choosing an appropriate one for your current situation is a -skill you’ll develop over time. In this chapter, we’ll discuss three -collections that are used very often in Rust programs: +and costs, and choosing an appropriate one for your current situation is a skill +you’ll develop over time. In this chapter, we’ll discuss three collections that +are used very often in Rust programs: -- A _vector_ allows you to store a variable number of values next to each other. -- A _string_ is a collection of characters. We’ve mentioned the `String` type +* A _vector_ allows you to store a variable number of values next to each other. +* A _string_ is a collection of characters. We’ve mentioned the `String` type previously, but in this chapter we’ll talk about it in depth. -- A _hash map_ allows you to associate a value with a specific key. It’s a +* A _hash map_ allows you to associate a value with a specific key. It’s a particular implementation of the more general data structure called a _map_. To learn about the other kinds of collections provided by the standard library, diff --git a/src/ch08-01-vectors.md b/src/ch08-01-vectors.md index d711c9904e..16a574972b 100644 --- a/src/ch08-01-vectors.md +++ b/src/ch08-01-vectors.md @@ -21,20 +21,20 @@ Listing 8-1. Note that we added a type annotation here. Because we aren’t inserting any values into this vector, Rust doesn’t know what kind of elements we intend to -store. This is an important point. Vectors are implemented using generics; -we’ll cover how to use generics with your own types in Chapter 10. For now, -know that the `Vec` type provided by the standard library can hold any type. -When we create a vector to hold a specific type, we can specify the type within -angle brackets. In Listing 8-1, we’ve told Rust that the `Vec` in `v` will -hold elements of the `i32` type. - -More often, you’ll create a `Vec` with initial values and Rust will infer -the type of value you want to store, so you rarely need to do this type -annotation. Rust conveniently provides the `vec!` macro, which will create a -new vector that holds the values you give it. Listing 8-2 creates a new -`Vec` that holds the values `1`, `2`, and `3`. The integer type is `i32` -because that’s the default integer type, as we discussed in the [“Data -Types”][data-types] section of Chapter 3. +store. This is an important point. Vectors are implemented using generics; we’ll +cover how to use generics with your own types in Chapter 10. For now, know that +the `Vec` type provided by the standard library can hold any type. When we +create a vector to hold a specific type, we can specify the type within angle +brackets. In Listing 8-1, we’ve told Rust that the `Vec` in `v` will hold +elements of the `i32` type. + +More often, you’ll create a `Vec` with initial values and Rust will infer the +type of value you want to store, so you rarely need to do this type annotation. +Rust conveniently provides the `vec!` macro, which will create a new vector that +holds the values you give it. Listing 8-2 creates a new `Vec` that holds +the values `1`, `2`, and `3`. The integer type is `i32` because that’s the +default integer type, as we discussed in the +[“Data Types”][data-types] section of Chapter 3. @@ -44,14 +44,14 @@ Types”][data-types] section of Chapter 3. -Because we’ve given initial `i32` values, Rust can infer that the type of `v` -is `Vec`, and the type annotation isn’t necessary. Next, we’ll look at how -to modify a vector. +Because we’ve given initial `i32` values, Rust can infer that the type of `v` is +`Vec`, and the type annotation isn’t necessary. Next, we’ll look at how to +modify a vector. ### Updating a Vector -To create a vector and then add elements to it, we can use the `push` method, -as shown in Listing 8-3. +To create a vector and then add elements to it, we can use the `push` method, as +shown in Listing 8-3. @@ -61,10 +61,10 @@ as shown in Listing 8-3. -As with any variable, if we want to be able to change its value, we need to -make it mutable using the `mut` keyword, as discussed in Chapter 3. The numbers -we place inside are all of type `i32`, and Rust infers this from the data, so -we don’t need the `Vec` annotation. +As with any variable, if we want to be able to change its value, we need to make +it mutable using the `mut` keyword, as discussed in Chapter 3. The numbers we +place inside are all of type `i32`, and Rust infers this from the data, so we +don’t need the `Vec` annotation. ### Reading Elements of Vectors @@ -90,10 +90,10 @@ method with the index passed as an argument, we get an `Option<&T>` that we can use with `match`. Rust provides these two ways to reference an element so you can choose how the -program behaves when you try to use an index value outside the range of -existing elements. As an example, let’s see what happens when we have a vector -of five elements and then we try to access an element at index 100 with each -technique, as shown in Listing 8-5. +program behaves when you try to use an index value outside the range of existing +elements. As an example, let’s see what happens when we have a vector of five +elements and then we try to access an element at index 100 with each technique, +as shown in Listing 8-5. @@ -123,10 +123,9 @@ When the program has a valid reference, the borrow checker enforces the ownership and borrowing rules (covered in Chapter 4) to ensure this reference and any other references to the contents of the vector remain valid. Recall the rule that states you can’t have mutable and immutable references in the same -scope. That rule applies in Listing 8-6, where we hold an immutable reference -to the first element in a vector and try to add an element to the end. This -program won’t work if we also try to refer to that element later in the -function. +scope. That rule applies in Listing 8-6, where we hold an immutable reference to +the first element in a vector and try to add an element to the end. This program +won’t work if we also try to refer to that element later in the function. @@ -147,13 +146,13 @@ to the first element care about changes at the end of the vector? This error is due to the way vectors work: because vectors put the values next to each other in memory, adding a new element onto the end of the vector might require allocating new memory and copying the old elements to the new space, if there -isn’t enough room to put all the elements next to each other where the vector -is currently stored. In that case, the reference to the first element would be -pointing to deallocated memory. The borrowing rules prevent programs from -ending up in that situation. +isn’t enough room to put all the elements next to each other where the vector is +currently stored. In that case, the reference to the first element would be +pointing to deallocated memory. The borrowing rules prevent programs from ending +up in that situation. -> Note: For more on the implementation details of the `Vec` type, see [“The -> Rustonomicon”][nomicon]. +> Note: For more on the implementation details of the `Vec` type, see +> [“The Rustonomicon”][nomicon]. ### Iterating Over the Values in a Vector @@ -171,8 +170,8 @@ to use a `for` loop to get immutable references to each element in a vector of We can also iterate over mutable references to each element in a mutable vector -in order to make changes to all the elements. The `for` loop in Listing 8-8 -will add `50` to each element. +in order to make changes to all the elements. The `for` loop in Listing 8-8 will +add `50` to each element. @@ -182,9 +181,10 @@ will add `50` to each element. -To change the value that the mutable reference refers to, we have to use the -`*` dereference operator to get to the value in `i` before we can use the `+=` -operator. We’ll talk more about the dereference operator in the [“Following the +To change the value that the mutable reference refers to, we have to use the `*` +dereference operator to get to the value in `i` before we can use the `+=` +operator. We’ll talk more about the dereference operator in the +[“Following the Pointer to the Value with the Dereference Operator”][deref] section of Chapter 15. @@ -192,23 +192,23 @@ Iterating over a vector, whether immutably or mutably, is safe because of the borrow checker’s rules. If we attempted to insert or remove items in the `for` loop bodies in Listing 8-7 and Listing 8-8, we would get a compiler error similar to the one we got with the code in Listing 8-6. The reference to the -vector that the `for` loop holds prevents simultaneous modification of the -whole vector. +vector that the `for` loop holds prevents simultaneous modification of the whole +vector. ### Using an Enum to Store Multiple Types Vectors can only store values that are of the same type. This can be inconvenient; there are definitely use cases for needing to store a list of -items of different types. Fortunately, the variants of an enum are defined -under the same enum type, so when we need one type to represent elements of -different types, we can define and use an enum! +items of different types. Fortunately, the variants of an enum are defined under +the same enum type, so when we need one type to represent elements of different +types, we can define and use an enum! -For example, say we want to get values from a row in a spreadsheet in which -some of the columns in the row contain integers, some floating-point numbers, -and some strings. We can define an enum whose variants will hold the different -value types, and all the enum variants will be considered the same type: that -of the enum. Then we can create a vector to hold that enum and so, ultimately, -hold different types. We’ve demonstrated this in Listing 8-9. +For example, say we want to get values from a row in a spreadsheet in which some +of the columns in the row contain integers, some floating-point numbers, and +some strings. We can define an enum whose variants will hold the different value +types, and all the enum variants will be considered the same type: that of the +enum. Then we can create a vector to hold that enum and so, ultimately, hold +different types. We’ve demonstrated this in Listing 8-9. @@ -230,8 +230,8 @@ If you don’t know the exhaustive set of types a program will get at runtime to store in a vector, the enum technique won’t work. Instead, you can use a trait object, which we’ll cover in Chapter 18. -Now that we’ve discussed some of the most common ways to use vectors, be sure -to review [the API documentation][vec-api] for all of the many +Now that we’ve discussed some of the most common ways to use vectors, be sure to +review [the API documentation][vec-api] for all of the many useful methods defined on `Vec` by the standard library. For example, in addition to `push`, a `pop` method removes and returns the last element. diff --git a/src/ch08-02-strings.md b/src/ch08-02-strings.md index 6df7dd83cb..16871d131a 100644 --- a/src/ch08-02-strings.md +++ b/src/ch08-02-strings.md @@ -1,18 +1,18 @@ ## Storing UTF-8 Encoded Text with Strings We talked about strings in Chapter 4, but we’ll look at them in more depth now. -New Rustaceans commonly get stuck on strings for a combination of three -reasons: Rust’s propensity for exposing possible errors, strings being a more -complicated data structure than many programmers give them credit for, and -UTF-8. These factors combine in a way that can seem difficult when you’re -coming from other programming languages. - -We discuss strings in the context of collections because strings are -implemented as a collection of bytes, plus some methods to provide useful -functionality when those bytes are interpreted as text. In this section, we’ll -talk about the operations on `String` that every collection type has, such as -creating, updating, and reading. We’ll also discuss the ways in which `String` -is different from the other collections, namely how indexing into a `String` is +New Rustaceans commonly get stuck on strings for a combination of three reasons: +Rust’s propensity for exposing possible errors, strings being a more complicated +data structure than many programmers give them credit for, and UTF-8. These +factors combine in a way that can seem difficult when you’re coming from other +programming languages. + +We discuss strings in the context of collections because strings are implemented +as a collection of bytes, plus some methods to provide useful functionality when +those bytes are interpreted as text. In this section, we’ll talk about the +operations on `String` that every collection type has, such as creating, +updating, and reading. We’ll also discuss the ways in which `String` is +different from the other collections, namely how indexing into a `String` is complicated by the differences between how people and computers interpret `String` data. @@ -27,18 +27,18 @@ string slices. The `String` type, which is provided by Rust’s standard library rather than coded into the core language, is a growable, mutable, owned, UTF-8 encoded -string type. When Rustaceans refer to “strings” in Rust, they might be -referring to either the `String` or the string slice `&str` types, not just one -of those types. Although this section is largely about `String`, both types are -used heavily in Rust’s standard library, and both `String` and string slices -are UTF-8 encoded. +string type. When Rustaceans refer to “strings” in Rust, they might be referring +to either the `String` or the string slice `&str` types, not just one of those +types. Although this section is largely about `String`, both types are used +heavily in Rust’s standard library, and both `String` and string slices are +UTF-8 encoded. ### Creating a New String Many of the same operations available with `Vec` are available with `String` -as well because `String` is actually implemented as a wrapper around a vector -of bytes with some extra guarantees, restrictions, and capabilities. An example -of a function that works the same way with `Vec` and `String` is the `new` +as well because `String` is actually implemented as a wrapper around a vector of +bytes with some extra guarantees, restrictions, and capabilities. An example of +a function that works the same way with `Vec` and `String` is the `new` function to create an instance, shown in Listing 8-11. @@ -66,8 +66,8 @@ two examples. This code creates a string containing `initial contents`. We can also use the function `String::from` to create a `String` from a string -literal. The code in Listing 8-13 is equivalent to the code in Listing 8-12 -that uses `to_string`. +literal. The code in Listing 8-13 is equivalent to the code in Listing 8-12 that +uses `to_string`. @@ -158,11 +158,10 @@ the `+` operator, as shown in Listing 8-18. -The string `s3` will contain `Hello, world!`. The reason `s1` is no longer -valid after the addition, and the reason we used a reference to `s2`, has to do -with the signature of the method that’s called when we use the `+` operator. -The `+` operator uses the `add` method, whose signature looks something like -this: +The string `s3` will contain `Hello, world!`. The reason `s1` is no longer valid +after the addition, and the reason we used a reference to `s2`, has to do with +the signature of the method that’s called when we use the `+` operator. The `+` +operator uses the `add` method, whose signature looks something like this: ```rust,ignore fn add(self, s: &str) -> String { @@ -171,8 +170,8 @@ fn add(self, s: &str) -> String { In the standard library, you’ll see `add` defined using generics and associated types. Here, we’ve substituted in concrete types, which is what happens when we call this method with `String` values. We’ll discuss generics in Chapter 10. -This signature gives us the clues we need in order to understand the tricky -bits of the `+` operator. +This signature gives us the clues we need in order to understand the tricky bits +of the `+` operator. First, `s2` has an `&`, meaning that we’re adding a _reference_ of the second string to the first string. This is because of the `s` parameter in the `add` @@ -180,21 +179,21 @@ function: we can only add a `&str` to a `String`; we can’t add two `String` values together. But wait—the type of `&s2` is `&String`, not `&str`, as specified in the second parameter to `add`. So why does Listing 8-18 compile? -The reason we’re able to use `&s2` in the call to `add` is that the compiler -can _coerce_ the `&String` argument into a `&str`. When we call the `add` -method, Rust uses a _deref coercion_, which here turns `&s2` into `&s2[..]`. -We’ll discuss deref coercion in more depth in Chapter 15. Because `add` does -not take ownership of the `s` parameter, `s2` will still be a valid `String` -after this operation. +The reason we’re able to use `&s2` in the call to `add` is that the compiler can +_coerce_ the `&String` argument into a `&str`. When we call the `add` method, +Rust uses a _deref coercion_, which here turns `&s2` into `&s2[..]`. We’ll +discuss deref coercion in more depth in Chapter 15. Because `add` does not take +ownership of the `s` parameter, `s2` will still be a valid `String` after this +operation. -Second, we can see in the signature that `add` takes ownership of `self` -because `self` does _not_ have an `&`. This means `s1` in Listing 8-18 will be -moved into the `add` call and will no longer be valid after that. So, although +Second, we can see in the signature that `add` takes ownership of `self` because +`self` does _not_ have an `&`. This means `s1` in Listing 8-18 will be moved +into the `add` call and will no longer be valid after that. So, although `let s3 = s1 + &s2;` looks like it will copy both strings and create a new one, this statement actually takes ownership of `s1`, appends a copy of the contents -of `s2`, and then returns ownership of the result. In other words, it looks -like it’s making a lot of copies, but it isn’t; the implementation is more -efficient than copying. +of `s2`, and then returns ownership of the result. In other words, it looks like +it’s making a lot of copies, but it isn’t; the implementation is more efficient +than copying. If we need to concatenate multiple strings, the behavior of the `+` operator gets unwieldy: @@ -204,8 +203,8 @@ gets unwieldy: ``` At this point, `s` will be `tic-tac-toe`. With all of the `+` and `"` -characters, it’s difficult to see what’s going on. For combining strings in -more complicated ways, we can instead use the `format!` macro: +characters, it’s difficult to see what’s going on. For combining strings in more +complicated ways, we can instead use the `format!` macro: ```rust {{#rustdoc_include ../listings/ch08-common-collections/no-listing-02-format/src/main.rs:here}} @@ -214,15 +213,15 @@ more complicated ways, we can instead use the `format!` macro: This code also sets `s` to `tic-tac-toe`. The `format!` macro works like `println!`, but instead of printing the output to the screen, it returns a `String` with the contents. The version of the code using `format!` is much -easier to read, and the code generated by the `format!` macro uses references -so that this call doesn’t take ownership of any of its parameters. +easier to read, and the code generated by the `format!` macro uses references so +that this call doesn’t take ownership of any of its parameters. ### Indexing into Strings -In many other programming languages, accessing individual characters in a -string by referencing them by index is a valid and common operation. However, -if you try to access parts of a `String` using indexing syntax in Rust, you’ll -get an error. Consider the invalid code in Listing 8-19. +In many other programming languages, accessing individual characters in a string +by referencing them by index is a valid and common operation. However, if you +try to access parts of a `String` using indexing syntax in Rust, you’ll get an +error. Consider the invalid code in Listing 8-19. @@ -276,19 +275,19 @@ You already know that `answer` will not be `З`, the first letter. When encoded in UTF-8, the first byte of `З` is `208` and the second is `151`, so it would seem that `answer` should in fact be `208`, but `208` is not a valid character on its own. Returning `208` is likely not what a user would want if they asked -for the first letter of this string; however, that’s the only data that Rust -has at byte index 0. Users generally don’t want the byte value returned, even -if the string contains only Latin letters: if `&"hi"[0]` were valid code that -returned the byte value, it would return `104`, not `h`. +for the first letter of this string; however, that’s the only data that Rust has +at byte index 0. Users generally don’t want the byte value returned, even if the +string contains only Latin letters: if `&"hi"[0]` were valid code that returned +the byte value, it would return `104`, not `h`. The answer, then, is that to avoid returning an unexpected value and causing -bugs that might not be discovered immediately, Rust doesn’t compile this code -at all and prevents misunderstandings early in the development process. +bugs that might not be discovered immediately, Rust doesn’t compile this code at +all and prevents misunderstandings early in the development process. #### Bytes and Scalar Values and Grapheme Clusters! Oh My! -Another point about UTF-8 is that there are actually three relevant ways to -look at strings from Rust’s perspective: as bytes, scalar values, and grapheme +Another point about UTF-8 is that there are actually three relevant ways to look +at strings from Rust’s perspective: as bytes, scalar values, and grapheme clusters (the closest thing to what we would call _letters_). If we look at the Hindi word “नमस्ते” written in the Devanagari script, it is @@ -300,8 +299,8 @@ stored as a vector of `u8` values that looks like this: ``` That’s 18 bytes and is how computers ultimately store this data. If we look at -them as Unicode scalar values, which are what Rust’s `char` type is, those -bytes look like this: +them as Unicode scalar values, which are what Rust’s `char` type is, those bytes +look like this: ```text ['न', 'म', 'स', '्', 'त', 'े'] @@ -320,11 +319,11 @@ Rust provides different ways of interpreting the raw string data that computers store so that each program can choose the interpretation it needs, no matter what human language the data is in. -A final reason Rust doesn’t allow us to index into a `String` to get a -character is that indexing operations are expected to always take constant time -(O(1)). But it isn’t possible to guarantee that performance with a `String`, -because Rust would have to walk through the contents from the beginning to the -index to determine how many valid characters there were. +A final reason Rust doesn’t allow us to index into a `String` to get a character +is that indexing operations are expected to always take constant time (O(1)). +But it isn’t possible to guarantee that performance with a `String`, because +Rust would have to walk through the contents from the beginning to the index to +determine how many valid characters there were. ### Slicing Strings @@ -354,15 +353,15 @@ index were accessed in a vector: {{#include ../listings/ch08-common-collections/output-only-01-not-char-boundary/output.txt}} ``` -You should use caution when creating string slices with ranges, because doing -so can crash your program. +You should use caution when creating string slices with ranges, because doing so +can crash your program. ### Methods for Iterating Over Strings -The best way to operate on pieces of strings is to be explicit about whether -you want characters or bytes. For individual Unicode scalar values, use the -`chars` method. Calling `chars` on “Зд” separates out and returns two values of -type `char`, and you can iterate over the result to access each element: +The best way to operate on pieces of strings is to be explicit about whether you +want characters or bytes. For individual Unicode scalar values, use the `chars` +method. Calling `chars` on “Зд” separates out and returns two values of type +`char`, and you can iterate over the result to access each element: ```rust for c in "Зд".chars() { diff --git a/src/ch08-03-hash-maps.md b/src/ch08-03-hash-maps.md index 3db150419b..3f4f1d18d0 100644 --- a/src/ch08-03-hash-maps.md +++ b/src/ch08-03-hash-maps.md @@ -7,11 +7,11 @@ Many programming languages support this kind of data structure, but they often use a different name, such as _hash_, _map_, _object_, _hash table_, _dictionary_, or _associative array_, just to name a few. -Hash maps are useful when you want to look up data not by using an index, as -you can with vectors, but by using a key that can be of any type. For example, -in a game, you could keep track of each team’s score in a hash map in which -each key is a team’s name and the values are each team’s score. Given a team -name, you can retrieve its score. +Hash maps are useful when you want to look up data not by using an index, as you +can with vectors, but by using a key that can be of any type. For example, in a +game, you could keep track of each team’s score in a hash map in which each key +is a team’s name and the values are each team’s score. Given a team name, you +can retrieve its score. We’ll go over the basic API of hash maps in this section, but many more goodies are hiding in the functions defined on `HashMap` by the standard library. @@ -35,18 +35,18 @@ Yellow team starts with 50. Note that we need to first `use` the `HashMap` from the collections portion of the standard library. Of our three common collections, this one is the least often used, so it’s not included in the features brought into scope -automatically in the prelude. Hash maps also have less support from the -standard library; there’s no built-in macro to construct them, for example. +automatically in the prelude. Hash maps also have less support from the standard +library; there’s no built-in macro to construct them, for example. Just like vectors, hash maps store their data on the heap. This `HashMap` has keys of type `String` and values of type `i32`. Like vectors, hash maps are -homogeneous: all of the keys must have the same type, and all of the values -must have the same type. +homogeneous: all of the keys must have the same type, and all of the values must +have the same type. ### Accessing Values in a Hash Map -We can get a value out of the hash map by providing its key to the `get` -method, as shown in Listing 8-21. +We can get a value out of the hash map by providing its key to the `get` method, +as shown in Listing 8-21. @@ -60,8 +60,8 @@ Here, `score` will have the value that’s associated with the Blue team, and th result will be `10`. The `get` method returns an `Option<&V>`; if there’s no value for that key in the hash map, `get` will return `None`. This program handles the `Option` by calling `copied` to get an `Option` rather than an -`Option<&i32>`, then `unwrap_or` to set `score` to zero if `scores` doesn’t -have an entry for the key. +`Option<&i32>`, then `unwrap_or` to set `score` to zero if `scores` doesn’t have +an entry for the key. We can iterate over each key–value pair in a hash map in a similar manner as we do with vectors, using a `for` loop: @@ -91,29 +91,30 @@ the hash map will be the owner of those values, as demonstrated in Listing 8-22. -We aren’t able to use the variables `field_name` and `field_value` after -they’ve been moved into the hash map with the call to `insert`. +We aren’t able to use the variables `field_name` and `field_value` after they’ve +been moved into the hash map with the call to `insert`. If we insert references to values into the hash map, the values won’t be moved into the hash map. The values that the references point to must be valid for at least as long as the hash map is valid. We’ll talk more about these issues in -the [“Validating References with +the +[“Validating References with Lifetimes”][validating-references-with-lifetimes] section in Chapter 10. ### Updating a Hash Map -Although the number of key and value pairs is growable, each unique key can -only have one value associated with it at a time (but not vice versa: for -example, both the Blue team and the Yellow team could have the value `10` -stored in the `scores` hash map). +Although the number of key and value pairs is growable, each unique key can only +have one value associated with it at a time (but not vice versa: for example, +both the Blue team and the Yellow team could have the value `10` stored in the +`scores` hash map). -When you want to change the data in a hash map, you have to decide how to -handle the case when a key already has a value assigned. You could replace the -old value with the new value, completely disregarding the old value. You could -keep the old value and ignore the new value, only adding the new value if the -key _doesn’t_ already have a value. Or you could combine the old value and the -new value. Let’s look at how to do each of these! +When you want to change the data in a hash map, you have to decide how to handle +the case when a key already has a value assigned. You could replace the old +value with the new value, completely disregarding the old value. You could keep +the old value and ignore the new value, only adding the new value if the key +_doesn’t_ already have a value. Or you could combine the old value and the new +value. Let’s look at how to do each of these! #### Overwriting a Value @@ -142,15 +143,15 @@ overwritten. It’s common to check whether a particular key already exists in the hash map with a value and then to take the following actions: if the key does exist in -the hash map, the existing value should remain the way it is; if the key -doesn’t exist, insert it and a value for it. +the hash map, the existing value should remain the way it is; if the key doesn’t +exist, insert it and a value for it. -Hash maps have a special API for this called `entry` that takes the key you -want to check as a parameter. The return value of the `entry` method is an enum +Hash maps have a special API for this called `entry` that takes the key you want +to check as a parameter. The return value of the `entry` method is an enum called `Entry` that represents a value that might or might not exist. Let’s say -we want to check whether the key for the Yellow team has a value associated -with it. If it doesn’t, we want to insert the value `50`, and the same for the -Blue team. Using the `entry` API, the code looks like Listing 8-24. +we want to check whether the key for the Yellow team has a value associated with +it. If it doesn’t, we want to insert the value `50`, and the same for the Blue +team. Using the `entry` API, the code looks like Listing 8-24. @@ -169,17 +170,17 @@ logic ourselves and, in addition, plays more nicely with the borrow checker. Running the code in Listing 8-24 will print `{"Yellow": 50, "Blue": 10}`. The first call to `entry` will insert the key for the Yellow team with the value `50` because the Yellow team doesn’t have a value already. The second call to -`entry` will not change the hash map because the Blue team already has the -value `10`. +`entry` will not change the hash map because the Blue team already has the value +`10`. #### Updating a Value Based on the Old Value Another common use case for hash maps is to look up a key’s value and then update it based on the old value. For instance, Listing 8-25 shows code that -counts how many times each word appears in some text. We use a hash map with -the words as keys and increment the value to keep track of how many times we’ve -seen that word. If it’s the first time we’ve seen a word, we’ll first insert -the value `0`. +counts how many times each word appears in some text. We use a hash map with the +words as keys and increment the value to keep track of how many times we’ve seen +that word. If it’s the first time we’ve seen a word, we’ll first insert the +value `0`. @@ -191,8 +192,8 @@ the value `0`. This code will print `{"world": 2, "hello": 1, "wonderful": 1}`. You might see the same key–value pairs printed in a different order: recall from the -[“Accessing Values in a Hash Map”][access] section that -iterating over a hash map happens in an arbitrary order. +[“Accessing Values in a Hash Map”][access] section that iterating +over a hash map happens in an arbitrary order. The `split_whitespace` method returns an iterator over subslices, separated by whitespace, of the value in `text`. The `or_insert` method returns a mutable @@ -208,13 +209,13 @@ By default, `HashMap` uses a hashing function called _SipHash_ that can provide resistance to denial-of-service (DoS) attacks involving hash tables[^siphash]. This is not the fastest hashing algorithm available, but the trade-off for better security that comes with the drop in -performance is worth it. If you profile your code and find that the default -hash function is too slow for your purposes, you can switch to another function -by specifying a different hasher. A _hasher_ is a type that implements the +performance is worth it. If you profile your code and find that the default hash +function is too slow for your purposes, you can switch to another function by +specifying a different hasher. A _hasher_ is a type that implements the `BuildHasher` trait. We’ll talk about traits and how to implement them in [Chapter 10][traits]. You don’t necessarily have to implement -your own hasher from scratch; [crates.io](https://crates.io/) -has libraries shared by other Rust users that provide hashers implementing many +your own hasher from scratch; [crates.io](https://crates.io/) has +libraries shared by other Rust users that provide hashers implementing many common hashing algorithms. [^siphash]: [https://en.wikipedia.org/wiki/SipHash](https://en.wikipedia.org/wiki/SipHash) @@ -226,8 +227,8 @@ necessary in programs when you need to store, access, and modify data. Here are some exercises you should now be equipped to solve: 1. Given a list of integers, use a vector and return the median (when sorted, - the value in the middle position) and mode (the value that occurs most - often; a hash map will be helpful here) of the list. + the value in the middle position) and mode (the value that occurs most often; + a hash map will be helpful here) of the list. 1. Convert strings to pig latin. The first consonant of each word is moved to the end of the word and _ay_ is added, so _first_ becomes _irst-fay_. Words that start with a vowel have _hay_ added to the end instead (_apple_ becomes @@ -241,8 +242,8 @@ some exercises you should now be equipped to solve: The standard library API documentation describes methods that vectors, strings, and hash maps have that will be helpful for these exercises! -We’re getting into more complex programs in which operations can fail, so it’s -a perfect time to discuss error handling. We’ll do that next! +We’re getting into more complex programs in which operations can fail, so it’s a +perfect time to discuss error handling. We’ll do that next! [validating-references-with-lifetimes]: ch10-03-lifetime-syntax.html#validating-references-with-lifetimes [access]: #accessing-values-in-a-hash-map diff --git a/src/ch09-00-error-handling.md b/src/ch09-00-error-handling.md index a5240b1997..11cb2ad9de 100644 --- a/src/ch09-00-error-handling.md +++ b/src/ch09-00-error-handling.md @@ -4,8 +4,8 @@ Errors are a fact of life in software, so Rust has a number of features for handling situations in which something goes wrong. In many cases, Rust requires you to acknowledge the possibility of an error and take some action before your code will compile. This requirement makes your program more robust by ensuring -that you’ll discover errors and handle them appropriately before you’ve -deployed your code to production! +that you’ll discover errors and handle them appropriately before you’ve deployed +your code to production! Rust groups errors into two major categories: _recoverable_ and _unrecoverable_ errors. For a recoverable error, such as a _file not found_ error, we most diff --git a/src/ch09-01-unrecoverable-errors-with-panic.md b/src/ch09-01-unrecoverable-errors-with-panic.md index 792932f5f4..dfe6f6de99 100644 --- a/src/ch09-01-unrecoverable-errors-with-panic.md +++ b/src/ch09-01-unrecoverable-errors-with-panic.md @@ -3,11 +3,11 @@ Sometimes bad things happen in your code, and there’s nothing you can do about it. In these cases, Rust has the `panic!` macro. There are two ways to cause a panic in practice: by taking an action that causes our code to panic (such as -accessing an array past the end) or by explicitly calling the `panic!` macro. -In both cases, we cause a panic in our program. By default, these panics will -print a failure message, unwind, clean up the stack, and quit. Via an -environment variable, you can also have Rust display the call stack when a -panic occurs to make it easier to track down the source of the panic. +accessing an array past the end) or by explicitly calling the `panic!` macro. In +both cases, we cause a panic in our program. By default, these panics will print +a failure message, unwind, clean up the stack, and quit. Via an environment +variable, you can also have Rust display the call stack when a panic occurs to +make it easier to track down the source of the panic. > ### Unwinding the Stack or Aborting in Response to a Panic > @@ -47,25 +47,25 @@ When you run the program, you’ll see something like this: The call to `panic!` causes the error message contained in the last two lines. The first line shows our panic message and the place in our source code where -the panic occurred: _src/main.rs:2:5_ indicates that it’s the second line, -fifth character of our _src/main.rs_ file. +the panic occurred: _src/main.rs:2:5_ indicates that it’s the second line, fifth +character of our _src/main.rs_ file. -In this case, the line indicated is part of our code, and if we go to that -line, we see the `panic!` macro call. In other cases, the `panic!` call might -be in code that our code calls, and the filename and line number reported by -the error message will be someone else’s code where the `panic!` macro is -called, not the line of our code that eventually led to the `panic!` call. +In this case, the line indicated is part of our code, and if we go to that line, +we see the `panic!` macro call. In other cases, the `panic!` call might be in +code that our code calls, and the filename and line number reported by the error +message will be someone else’s code where the `panic!` macro is called, not the +line of our code that eventually led to the `panic!` call. We can use the backtrace of the functions the `panic!` call came from to figure -out the part of our code that is causing the problem. To understand how to use -a `panic!` backtrace, let’s look at another example and see what it’s like when -a `panic!` call comes from a library because of a bug in our code instead of -from our code calling the macro directly. Listing 9-1 has some code that -attempts to access an index in a vector beyond the range of valid indexes. +out the part of our code that is causing the problem. To understand how to use a +`panic!` backtrace, let’s look at another example and see what it’s like when a +`panic!` call comes from a library because of a bug in our code instead of from +our code calling the macro directly. Listing 9-1 has some code that attempts to +access an index in a vector beyond the range of valid indexes. @@ -78,16 +78,16 @@ attempts to access an index in a vector beyond the range of valid indexes. Here, we’re attempting to access the 100th element of our vector (which is at index 99 because indexing starts at zero), but the vector has only three elements. In this situation, Rust will panic. Using `[]` is supposed to return -an element, but if you pass an invalid index, there’s no element that Rust -could return here that would be correct. +an element, but if you pass an invalid index, there’s no element that Rust could +return here that would be correct. In C, attempting to read beyond the end of a data structure is undefined behavior. You might get whatever is at the location in memory that would -correspond to that element in the data structure, even though the memory -doesn’t belong to that structure. This is called a _buffer overread_ and can -lead to security vulnerabilities if an attacker is able to manipulate the index -in such a way as to read data they shouldn’t be allowed to that is stored after -the data structure. +correspond to that element in the data structure, even though the memory doesn’t +belong to that structure. This is called a _buffer overread_ and can lead to +security vulnerabilities if an attacker is able to manipulate the index in such +a way as to read data they shouldn’t be allowed to that is stored after the data +structure. To protect your program from this sort of vulnerability, if you try to read an element at an index that doesn’t exist, Rust will stop execution and refuse to @@ -103,14 +103,14 @@ This error points at line 4 of our _main.rs_ where we attempt to access index The `note:` line tells us that we can set the `RUST_BACKTRACE` environment variable to get a backtrace of exactly what happened to cause the error. A _backtrace_ is a list of all the functions that have been called to get to this -point. Backtraces in Rust work as they do in other languages: the key to -reading the backtrace is to start from the top and read until you see files you -wrote. That’s the spot where the problem originated. The lines above that spot -are code that your code has called; the lines below are code that called your -code. These before-and-after lines might include core Rust code, standard -library code, or crates that you’re using. Let’s try getting a backtrace by -setting the `RUST_BACKTRACE` environment variable to any value except `0`. -Listing 9-2 shows output similar to what you’ll see. +point. Backtraces in Rust work as they do in other languages: the key to reading +the backtrace is to start from the top and read until you see files you wrote. +That’s the spot where the problem originated. The lines above that spot are code +that your code has called; the lines below are code that called your code. These +before-and-after lines might include core Rust code, standard library code, or +crates that you’re using. Let’s try getting a backtrace by setting the +`RUST_BACKTRACE` environment variable to any value except `0`. Listing 9-2 shows +output similar to what you’ll see. section later in this -chapter. Next, we’ll look at how to recover from an error using `Result`. +handle error conditions in the +[“To `panic!` or Not to `panic!`”][to-panic-or-not-to-panic] +section later in this chapter. Next, we’ll look at how to recover from an error +using `Result`. [to-panic-or-not-to-panic]: ch09-03-to-panic-or-not-to-panic.html#to-panic-or-not-to-panic diff --git a/src/ch09-02-recoverable-errors-with-result.md b/src/ch09-02-recoverable-errors-with-result.md index 13117d7dfb..dd26a6a215 100644 --- a/src/ch09-02-recoverable-errors-with-result.md +++ b/src/ch09-02-recoverable-errors-with-result.md @@ -18,13 +18,13 @@ enum Result { ``` The `T` and `E` are generic type parameters: we’ll discuss generics in more -detail in Chapter 10. What you need to know right now is that `T` represents -the type of the value that will be returned in a success case within the `Ok` +detail in Chapter 10. What you need to know right now is that `T` represents the +type of the value that will be returned in a success case within the `Ok` variant, and `E` represents the type of the error that will be returned in a failure case within the `Err` variant. Because `Result` has these generic type -parameters, we can use the `Result` type and the functions defined on it in -many different situations where the success value and error value we want to -return may differ. +parameters, we can use the `Result` type and the functions defined on it in many +different situations where the success value and error value we want to return +may differ. Let’s call a function that returns a `Result` value because the function could fail. In Listing 9-3 we try to open a file. @@ -54,10 +54,9 @@ In the case where it fails, the value in `greeting_file_result` will be an instance of `Err` that contains more information about the kind of error that occurred. -We need to add to the code in Listing 9-3 to take different actions depending -on the value `File::open` returns. Listing 9-4 shows one way to handle the -`Result` using a basic tool, the `match` expression that we discussed in -Chapter 6. +We need to add to the code in Listing 9-3 to take different actions depending on +the value `File::open` returns. Listing 9-4 shows one way to handle the `Result` +using a basic tool, the `match` expression that we discussed in Chapter 6. @@ -68,18 +67,18 @@ Chapter 6. Note that, like the `Option` enum, the `Result` enum and its variants have been -brought into scope by the prelude, so we don’t need to specify `Result::` -before the `Ok` and `Err` variants in the `match` arms. +brought into scope by the prelude, so we don’t need to specify `Result::` before +the `Ok` and `Err` variants in the `match` arms. -When the result is `Ok`, this code will return the inner `file` value out of -the `Ok` variant, and we then assign that file handle value to the variable +When the result is `Ok`, this code will return the inner `file` value out of the +`Ok` variant, and we then assign that file handle value to the variable `greeting_file`. After the `match`, we can use the file handle for reading or writing. The other arm of the `match` handles the case where we get an `Err` value from `File::open`. In this example, we’ve chosen to call the `panic!` macro. If -there’s no file named _hello.txt_ in our current directory and we run this -code, we’ll see the following output from the `panic!` macro: +there’s no file named _hello.txt_ in our current directory and we run this code, +we’ll see the following output from the `panic!` macro: ```console {{#include ../listings/ch09-error-handling/listing-09-04/output.txt}} @@ -109,8 +108,8 @@ tests to fail lol --> The type of the value that `File::open` returns inside the `Err` variant is -`io::Error`, which is a struct provided by the standard library. This struct -has a method `kind` that we can call to get an `io::ErrorKind` value. The enum +`io::Error`, which is a struct provided by the standard library. This struct has +a method `kind` that we can call to get an `io::ErrorKind` value. The enum `io::ErrorKind` is provided by the standard library and has variants representing the different kinds of errors that might result from an `io` operation. The variant we want to use is `ErrorKind::NotFound`, which indicates @@ -122,8 +121,8 @@ by `error.kind()` is the `NotFound` variant of the `ErrorKind` enum. If it is, we try to create the file with `File::create`. However, because `File::create` could also fail, we need a second arm in the inner `match` expression. When the file can’t be created, a different error message is printed. The second arm of -the outer `match` stays the same, so the program panics on any error besides -the missing file error. +the outer `match` stays the same, so the program panics on any error besides the +missing file error. > #### Alternatives to Using `match` with `Result` > @@ -163,12 +162,12 @@ the missing file error. #### Shortcuts for Panic on Error: `unwrap` and `expect` Using `match` works well enough, but it can be a bit verbose and doesn’t always -communicate intent well. The `Result` type has many helper methods -defined on it to do various, more specific tasks. The `unwrap` method is a -shortcut method implemented just like the `match` expression we wrote in -Listing 9-4. If the `Result` value is the `Ok` variant, `unwrap` will return -the value inside the `Ok`. If the `Result` is the `Err` variant, `unwrap` will -call the `panic!` macro for us. Here is an example of `unwrap` in action: +communicate intent well. The `Result` type has many helper methods defined +on it to do various, more specific tasks. The `unwrap` method is a shortcut +method implemented just like the `match` expression we wrote in Listing 9-4. If +the `Result` value is the `Ok` variant, `unwrap` will return the value inside +the `Ok`. If the `Result` is the `Err` variant, `unwrap` will call the `panic!` +macro for us. Here is an example of `unwrap` in action: @@ -207,8 +206,8 @@ your intent and make tracking down the source of a panic easier. The syntax of We use `expect` in the same way as `unwrap`: to return the file handle or call the `panic!` macro. The error message used by `expect` in its call to `panic!` -will be the parameter that we pass to `expect`, rather than the default -`panic!` message that `unwrap` uses. Here’s what it looks like: +will be the parameter that we pass to `expect`, rather than the default `panic!` +message that `unwrap` uses. Here’s what it looks like: This function can be written in a much shorter way, but we’re going to start by -doing a lot of it manually in order to explore error handling; at the end, -we’ll show the shorter way. Let’s look at the return type of the function -first: `Result`. This means the function is returning a -value of the type `Result`, where the generic parameter `T` has been -filled in with the concrete type `String` and the generic type `E` has been -filled in with the concrete type `io::Error`. +doing a lot of it manually in order to explore error handling; at the end, we’ll +show the shorter way. Let’s look at the return type of the function first: +`Result`. This means the function is returning a value of the +type `Result`, where the generic parameter `T` has been filled in with the +concrete type `String` and the generic type `E` has been filled in with the +concrete type `io::Error`. If this function succeeds without any problems, the code that calls this function will receive an `Ok` value that holds a `String`—the `username` that @@ -271,33 +270,33 @@ this function’s body that might fail: the `File::open` function and the The body of the function starts by calling the `File::open` function. Then we handle the `Result` value with a `match` similar to the `match` in Listing 9-4. -If `File::open` succeeds, the file handle in the pattern variable `file` -becomes the value in the mutable variable `username_file` and the function -continues. In the `Err` case, instead of calling `panic!`, we use the `return` -keyword to return early out of the function entirely and pass the error value -from `File::open`, now in the pattern variable `e`, back to the calling code as -this function’s error value. - -So, if we have a file handle in `username_file`, the function then creates a -new `String` in variable `username` and calls the `read_to_string` method on -the file handle in `username_file` to read the contents of the file into -`username`. The `read_to_string` method also returns a `Result` because it -might fail, even though `File::open` succeeded. So we need another `match` to -handle that `Result`: if `read_to_string` succeeds, then our function has -succeeded, and we return the username from the file that’s now in `username` -wrapped in an `Ok`. If `read_to_string` fails, we return the error value in the -same way that we returned the error value in the `match` that handled the -return value of `File::open`. However, we don’t need to explicitly say -`return`, because this is the last expression in the function. - -The code that calls this code will then handle getting either an `Ok` value -that contains a username or an `Err` value that contains an `io::Error`. It’s -up to the calling code to decide what to do with those values. If the calling -code gets an `Err` value, it could call `panic!` and crash the program, use a -default username, or look up the username from somewhere other than a file, for -example. We don’t have enough information on what the calling code is actually -trying to do, so we propagate all the success or error information upward for -it to handle appropriately. +If `File::open` succeeds, the file handle in the pattern variable `file` becomes +the value in the mutable variable `username_file` and the function continues. In +the `Err` case, instead of calling `panic!`, we use the `return` keyword to +return early out of the function entirely and pass the error value from +`File::open`, now in the pattern variable `e`, back to the calling code as this +function’s error value. + +So, if we have a file handle in `username_file`, the function then creates a new +`String` in variable `username` and calls the `read_to_string` method on the +file handle in `username_file` to read the contents of the file into `username`. +The `read_to_string` method also returns a `Result` because it might fail, even +though `File::open` succeeded. So we need another `match` to handle that +`Result`: if `read_to_string` succeeds, then our function has succeeded, and we +return the username from the file that’s now in `username` wrapped in an `Ok`. +If `read_to_string` fails, we return the error value in the same way that we +returned the error value in the `match` that handled the return value of +`File::open`. However, we don’t need to explicitly say `return`, because this is +the last expression in the function. + +The code that calls this code will then handle getting either an `Ok` value that +contains a username or an `Err` value that contains an `io::Error`. It’s up to +the calling code to decide what to do with those values. If the calling code +gets an `Err` value, it could call `panic!` and crash the program, use a default +username, or look up the username from somewhere other than a file, for example. +We don’t have enough information on what the calling code is actually trying to +do, so we propagate all the success or error information upward for it to handle +appropriately. This pattern of propagating errors is so common in Rust that Rust provides the question mark operator `?` to make this easier. @@ -322,11 +321,10 @@ don't want to include it for rustdoc testing purposes. --> The `?` placed after a `Result` value is defined to work in almost the same way as the `match` expressions we defined to handle the `Result` values in Listing -9-6. If the value of the `Result` is an `Ok`, the value inside the `Ok` will -get returned from this expression, and the program will continue. If the value -is an `Err`, the `Err` will be returned from the whole function as if we had -used the `return` keyword so the error value gets propagated to the calling -code. +9-6. If the value of the `Result` is an `Ok`, the value inside the `Ok` will get +returned from this expression, and the program will continue. If the value is an +`Err`, the `Err` will be returned from the whole function as if we had used the +`return` keyword so the error value gets propagated to the calling code. There is a difference between what the `match` expression from Listing 9-6 does and what the `?` operator does: error values that have the `?` operator called @@ -334,8 +332,8 @@ on them go through the `from` function, defined in the `From` trait in the standard library, which is used to convert values from one type into another. When the `?` operator calls the `from` function, the error type received is converted into the error type defined in the return type of the current -function. This is useful when a function returns one error type to represent -all the ways a function might fail, even if parts might fail for many different +function. This is useful when a function returns one error type to represent all +the ways a function might fail, even if parts might fail for many different reasons. For example, we could change the `read_username_from_file` function in Listing @@ -401,15 +399,15 @@ the longer way first. The `?` operator can only be used in functions whose return type is compatible with the value the `?` is used on. This is because the `?` operator is defined -to perform an early return of a value out of the function, in the same manner -as the `match` expression we defined in Listing 9-6. In Listing 9-6, the -`match` was using a `Result` value, and the early return arm returned an -`Err(e)` value. The return type of the function has to be a `Result` so that -it’s compatible with this `return`. +to perform an early return of a value out of the function, in the same manner as +the `match` expression we defined in Listing 9-6. In Listing 9-6, the `match` +was using a `Result` value, and the early return arm returned an `Err(e)` value. +The return type of the function has to be a `Result` so that it’s compatible +with this `return`. -In Listing 9-10, let’s look at the error we’ll get if we use the `?` operator -in a `main` function with a return type that is incompatible with the type of -the value we use `?` on. +In Listing 9-10, let’s look at the error we’ll get if we use the `?` operator in +a `main` function with a return type that is incompatible with the type of the +value we use `?` on. @@ -438,15 +436,14 @@ on as long as you have no restrictions preventing that. The other choice is to use a `match` or one of the `Result` methods to handle the `Result` in whatever way is appropriate. -The error message also mentioned that `?` can be used with `Option` values -as well. As with using `?` on `Result`, you can only use `?` on `Option` in a +The error message also mentioned that `?` can be used with `Option` values as +well. As with using `?` on `Result`, you can only use `?` on `Option` in a function that returns an `Option`. The behavior of the `?` operator when called -on an `Option` is similar to its behavior when called on a `Result`: -if the value is `None`, the `None` will be returned early from the function at -that point. If the value is `Some`, the value inside the `Some` is the -resultant value of the expression, and the function continues. Listing 9-11 has -an example of a function that finds the last character of the first line in the -given text. +on an `Option` is similar to its behavior when called on a `Result`: if +the value is `None`, the `None` will be returned early from the function at that +point. If the value is `Some`, the value inside the `Some` is the resultant +value of the expression, and the function continues. Listing 9-11 has an example +of a function that finds the last character of the first line in the given text. @@ -459,23 +456,23 @@ given text. This function returns `Option` because it’s possible that there is a character there, but it’s also possible that there isn’t. This code takes the `text` string slice argument and calls the `lines` method on it, which returns -an iterator over the lines in the string. Because this function wants to -examine the first line, it calls `next` on the iterator to get the first value -from the iterator. If `text` is the empty string, this call to `next` will -return `None`, in which case we use `?` to stop and return `None` from -`last_char_of_first_line`. If `text` is not the empty string, `next` will -return a `Some` value containing a string slice of the first line in `text`. +an iterator over the lines in the string. Because this function wants to examine +the first line, it calls `next` on the iterator to get the first value from the +iterator. If `text` is the empty string, this call to `next` will return `None`, +in which case we use `?` to stop and return `None` from +`last_char_of_first_line`. If `text` is not the empty string, `next` will return +a `Some` value containing a string slice of the first line in `text`. The `?` extracts the string slice, and we can call `chars` on that string slice to get an iterator of its characters. We’re interested in the last character in -this first line, so we call `last` to return the last item in the iterator. -This is an `Option` because it’s possible that the first line is the empty -string; for example, if `text` starts with a blank line but has characters on -other lines, as in `"\nhi"`. However, if there is a last character on the first -line, it will be returned in the `Some` variant. The `?` operator in the middle -gives us a concise way to express this logic, allowing us to implement the -function in one line. If we couldn’t use the `?` operator on `Option`, we’d -have to implement this logic using more method calls or a `match` expression. +this first line, so we call `last` to return the last item in the iterator. This +is an `Option` because it’s possible that the first line is the empty string; +for example, if `text` starts with a blank line but has characters on other +lines, as in `"\nhi"`. However, if there is a last character on the first line, +it will be returned in the `Some` variant. The `?` operator in the middle gives +us a concise way to express this logic, allowing us to implement the function in +one line. If we couldn’t use the `?` operator on `Option`, we’d have to +implement this logic using more method calls or a `match` expression. Note that you can use the `?` operator on a `Result` in a function that returns `Result`, and you can use the `?` operator on an `Option` in a function that @@ -505,12 +502,12 @@ code will now compile. The `Box` type is a _trait object_, which we’ll talk about in the [“Using Trait Objects that Allow for Values of Different Types”][trait-objects] section in Chapter 18. For now, you can -read `Box` to mean “any kind of error.” Using `?` on a `Result` -value in a `main` function with the error type `Box` is allowed -because it allows any `Err` value to be returned early. Even though the body of -this `main` function will only ever return errors of type `std::io::Error`, by -specifying `Box`, this signature will continue to be correct even if -more code that returns other errors is added to the body of `main`. +read `Box` to mean “any kind of error.” Using `?` on a `Result` value +in a `main` function with the error type `Box` is allowed because it +allows any `Err` value to be returned early. Even though the body of this `main` +function will only ever return errors of type `std::io::Error`, by specifying +`Box`, this signature will continue to be correct even if more code +that returns other errors is added to the body of `main`. When a `main` function returns a `Result<(), E>`, the executable will exit with a value of `0` if `main` returns `Ok(())` and will exit with a nonzero value if @@ -519,11 +516,11 @@ they exit: programs that exit successfully return the integer `0`, and programs that error return some integer other than `0`. Rust also returns integers from executables to be compatible with this convention. -The `main` function may return any types that implement [the -`std::process::Termination` trait][termination], which contains -a function `report` that returns an `ExitCode`. Consult the standard library -documentation for more information on implementing the `Termination` trait for -your own types. +The `main` function may return any types that implement +[the `std::process::Termination` trait][termination], which +contains a function `report` that returns an `ExitCode`. Consult the standard +library documentation for more information on implementing the `Termination` +trait for your own types. Now that we’ve discussed the details of calling `panic!` or returning `Result`, let’s return to the topic of how to decide which is appropriate to use in which diff --git a/src/ch09-03-to-panic-or-not-to-panic.md b/src/ch09-03-to-panic-or-not-to-panic.md index 90d9eacfd4..25da566488 100644 --- a/src/ch09-03-to-panic-or-not-to-panic.md +++ b/src/ch09-03-to-panic-or-not-to-panic.md @@ -6,21 +6,21 @@ for any error situation, whether there’s a possible way to recover or not, but then you’re making the decision that a situation is unrecoverable on behalf of the calling code. When you choose to return a `Result` value, you give the calling code options. The calling code could choose to attempt to recover in a -way that’s appropriate for its situation, or it could decide that an `Err` -value in this case is unrecoverable, so it can call `panic!` and turn your -recoverable error into an unrecoverable one. Therefore, returning `Result` is a -good default choice when you’re defining a function that might fail. +way that’s appropriate for its situation, or it could decide that an `Err` value +in this case is unrecoverable, so it can call `panic!` and turn your recoverable +error into an unrecoverable one. Therefore, returning `Result` is a good default +choice when you’re defining a function that might fail. -In situations such as examples, prototype code, and tests, it’s more -appropriate to write code that panics instead of returning a `Result`. Let’s -explore why, then discuss situations in which the compiler can’t tell that -failure is impossible, but you as a human can. The chapter will conclude with -some general guidelines on how to decide whether to panic in library code. +In situations such as examples, prototype code, and tests, it’s more appropriate +to write code that panics instead of returning a `Result`. Let’s explore why, +then discuss situations in which the compiler can’t tell that failure is +impossible, but you as a human can. The chapter will conclude with some general +guidelines on how to decide whether to panic in library code. ### Examples, Prototype Code, and Tests -When you’re writing an example to illustrate some concept, also including -robust error-handling code can make the example less clear. In examples, it’s +When you’re writing an example to illustrate some concept, also including robust +error-handling code can make the example less clear. In examples, it’s understood that a call to a method like `unwrap` that could panic is meant as a placeholder for the way you’d want your application to handle errors, which can differ based on what the rest of your code is doing. @@ -40,54 +40,55 @@ It would also be appropriate to call `unwrap` or `expect` when you have some other logic that ensures the `Result` will have an `Ok` value, but the logic isn’t something the compiler understands. You’ll still have a `Result` value that you need to handle: whatever operation you’re calling still has the -possibility of failing in general, even though it’s logically impossible in -your particular situation. If you can ensure by manually inspecting the code -that you’ll never have an `Err` variant, it’s perfectly acceptable to call -`unwrap`, and even better to document the reason you think you’ll never have an -`Err` variant in the `expect` text. Here’s an example: +possibility of failing in general, even though it’s logically impossible in your +particular situation. If you can ensure by manually inspecting the code that +you’ll never have an `Err` variant, it’s perfectly acceptable to call `unwrap`, +and even better to document the reason you think you’ll never have an `Err` +variant in the `expect` text. Here’s an example: ```rust {{#rustdoc_include ../listings/ch09-error-handling/no-listing-08-unwrap-that-cant-fail/src/main.rs:here}} ``` We’re creating an `IpAddr` instance by parsing a hardcoded string. We can see -that `127.0.0.1` is a valid IP address, so it’s acceptable to use `expect` -here. However, having a hardcoded, valid string doesn’t change the return type -of the `parse` method: we still get a `Result` value, and the compiler will -still make us handle the `Result` as if the `Err` variant is a possibility -because the compiler isn’t smart enough to see that this string is always a -valid IP address. If the IP address string came from a user rather than being -hardcoded into the program and therefore _did_ have a possibility of failure, -we’d definitely want to handle the `Result` in a more robust way instead. -Mentioning the assumption that this IP address is hardcoded will prompt us to -change `expect` to better error-handling code if, in the future, we need to get -the IP address from some other source instead. +that `127.0.0.1` is a valid IP address, so it’s acceptable to use `expect` here. +However, having a hardcoded, valid string doesn’t change the return type of the +`parse` method: we still get a `Result` value, and the compiler will still make +us handle the `Result` as if the `Err` variant is a possibility because the +compiler isn’t smart enough to see that this string is always a valid IP +address. If the IP address string came from a user rather than being hardcoded +into the program and therefore _did_ have a possibility of failure, we’d +definitely want to handle the `Result` in a more robust way instead. Mentioning +the assumption that this IP address is hardcoded will prompt us to change +`expect` to better error-handling code if, in the future, we need to get the IP +address from some other source instead. ### Guidelines for Error Handling It’s advisable to have your code panic when it’s possible that your code could end up in a bad state. In this context, a _bad state_ is when some assumption, guarantee, contract, or invariant has been broken, such as when invalid values, -contradictory values, or missing values are passed to your code—plus one or -more of the following: +contradictory values, or missing values are passed to your code—plus one or more +of the following: -- The bad state is something that is unexpected, as opposed to something that +* The bad state is something that is unexpected, as opposed to something that will likely happen occasionally, like a user entering data in the wrong format. -- Your code after this point needs to rely on not being in this bad state, +* Your code after this point needs to rely on not being in this bad state, rather than checking for the problem at every step. -- There’s not a good way to encode this information in the types you use. We’ll - work through an example of what we mean in the [“Encoding States and Behavior - as Types”][encoding] section of Chapter 18. - -If someone calls your code and passes in values that don’t make sense, it’s -best to return an error if you can so the user of the library can decide what -they want to do in that case. However, in cases where continuing could be -insecure or harmful, the best choice might be to call `panic!` and alert the -person using your library to the bug in their code so they can fix it during -development. Similarly, `panic!` is often appropriate if you’re calling -external code that is out of your control and it returns an invalid state that -you have no way of fixing. +* There’s not a good way to encode this information in the types you use. We’ll + work through an example of what we mean in the + [“Encoding States and Behavior as Types”][encoding] section of + Chapter 18. + +If someone calls your code and passes in values that don’t make sense, it’s best +to return an error if you can so the user of the library can decide what they +want to do in that case. However, in cases where continuing could be insecure or +harmful, the best choice might be to call `panic!` and alert the person using +your library to the bug in their code so they can fix it during development. +Similarly, `panic!` is often appropriate if you’re calling external code that is +out of your control and it returns an invalid state that you have no way of +fixing. However, when failure is expected, it’s more appropriate to return a `Result` than to make a `panic!` call. Examples include a parser being given malformed @@ -106,9 +107,9 @@ _contracts_: their behavior is only guaranteed if the inputs meet particular requirements. Panicking when the contract is violated makes sense because a contract violation always indicates a caller-side bug, and it’s not a kind of error you want the calling code to have to explicitly handle. In fact, there’s -no reasonable way for calling code to recover; the calling _programmers_ need -to fix the code. Contracts for a function, especially when a violation will -cause a panic, should be explained in the API documentation for the function. +no reasonable way for calling code to recover; the calling _programmers_ need to +fix the code. Contracts for a function, especially when a violation will cause a +panic, should be explained in the API documentation for the function. However, having lots of error checks in all of your functions would be verbose and annoying. Fortunately, you can use Rust’s type system (and thus the type @@ -129,16 +130,16 @@ Let’s take the idea of using Rust’s type system to ensure we have a valid va one step further and look at creating a custom type for validation. Recall the guessing game in Chapter 2 in which our code asked the user to guess a number between 1 and 100. We never validated that the user’s guess was between those -numbers before checking it against our secret number; we only validated that -the guess was positive. In this case, the consequences were not very dire: our +numbers before checking it against our secret number; we only validated that the +guess was positive. In this case, the consequences were not very dire: our output of “Too high” or “Too low” would still be correct. But it would be a useful enhancement to guide the user toward valid guesses and have different -behavior when the user guesses a number that’s out of range versus when the -user types, for example, letters instead. +behavior when the user guesses a number that’s out of range versus when the user +types, for example, letters instead. One way to do this would be to parse the guess as an `i32` instead of only a -`u32` to allow potentially negative numbers, and then add a check for the -number being in range, like so: +`u32` to allow potentially negative numbers, and then add a check for the number +being in range, like so: @@ -174,22 +175,22 @@ receives a value between 1 and 100. -First we define a struct named `Guess` that has a field named `value` that -holds an `i32`. This is where the number will be stored. +First we define a struct named `Guess` that has a field named `value` that holds +an `i32`. This is where the number will be stored. Then we implement an associated function named `new` on `Guess` that creates -instances of `Guess` values. The `new` function is defined to have one -parameter named `value` of type `i32` and to return a `Guess`. The code in the -body of the `new` function tests `value` to make sure it’s between 1 and 100. -If `value` doesn’t pass this test, we make a `panic!` call, which will alert -the programmer who is writing the calling code that they have a bug they need -to fix, because creating a `Guess` with a `value` outside this range would -violate the contract that `Guess::new` is relying on. The conditions in which -`Guess::new` might panic should be discussed in its public-facing API -documentation; we’ll cover documentation conventions indicating the possibility -of a `panic!` in the API documentation that you create in Chapter 14. If -`value` does pass the test, we create a new `Guess` with its `value` field set -to the `value` parameter and return the `Guess`. +instances of `Guess` values. The `new` function is defined to have one parameter +named `value` of type `i32` and to return a `Guess`. The code in the body of the +`new` function tests `value` to make sure it’s between 1 and 100. If `value` +doesn’t pass this test, we make a `panic!` call, which will alert the programmer +who is writing the calling code that they have a bug they need to fix, because +creating a `Guess` with a `value` outside this range would violate the contract +that `Guess::new` is relying on. The conditions in which `Guess::new` might +panic should be discussed in its public-facing API documentation; we’ll cover +documentation conventions indicating the possibility of a `panic!` in the API +documentation that you create in Chapter 14. If `value` does pass the test, we +create a new `Guess` with its `value` field set to the `value` parameter and +return the `Guess`. Next, we implement a method named `value` that borrows `self`, doesn’t have any other parameters, and returns an `i32`. This kind of method is sometimes called diff --git a/src/ch10-00-generics.md b/src/ch10-00-generics.md index 20289557f1..2f1d0e72ec 100644 --- a/src/ch10-00-generics.md +++ b/src/ch10-00-generics.md @@ -1,15 +1,15 @@ # Generic Types, Traits, and Lifetimes -Every programming language has tools for effectively handling the duplication -of concepts. In Rust, one such tool is _generics_: abstract stand-ins for -concrete types or other properties. We can express the behavior of generics or -how they relate to other generics without knowing what will be in their place -when compiling and running the code. +Every programming language has tools for effectively handling the duplication of +concepts. In Rust, one such tool is _generics_: abstract stand-ins for concrete +types or other properties. We can express the behavior of generics or how they +relate to other generics without knowing what will be in their place when +compiling and running the code. Functions can take parameters of some generic type, instead of a concrete type -like `i32` or `String`, in the same way they take parameters with unknown -values to run the same code on multiple concrete values. In fact, we’ve already -used generics in Chapter 6 with `Option`, in Chapter 8 with `Vec` and +like `i32` or `String`, in the same way they take parameters with unknown values +to run the same code on multiple concrete values. In fact, we’ve already used +generics in Chapter 6 with `Option`, in Chapter 8 with `Vec` and `HashMap`, and in Chapter 9 with `Result`. In this chapter, you’ll explore how to define your own types, functions, and methods with generics! @@ -19,14 +19,13 @@ differ only in the types of their parameters. We’ll also explain how to use generic types in struct and enum definitions. Then you’ll learn how to use _traits_ to define behavior in a generic way. You -can combine traits with generic types to constrain a generic type to accept -only those types that have a particular behavior, as opposed to just any type. +can combine traits with generic types to constrain a generic type to accept only +those types that have a particular behavior, as opposed to just any type. -Finally, we’ll discuss _lifetimes_: a variety of generics that give the -compiler information about how references relate to each other. Lifetimes allow -us to give the compiler enough information about borrowed values so that it can -ensure references will be valid in more situations than it could without our -help. +Finally, we’ll discuss _lifetimes_: a variety of generics that give the compiler +information about how references relate to each other. Lifetimes allow us to +give the compiler enough information about borrowed values so that it can ensure +references will be valid in more situations than it could without our help. ## Removing Duplication by Extracting a Function @@ -34,13 +33,13 @@ Generics allow us to replace specific types with a placeholder that represents multiple types to remove code duplication. Before diving into generics syntax, let’s first look at how to remove duplication in a way that doesn’t involve generic types by extracting a function that replaces specific values with a -placeholder that represents multiple values. Then we’ll apply the same -technique to extract a generic function! By looking at how to recognize -duplicated code you can extract into a function, you’ll start to recognize -duplicated code that can use generics. +placeholder that represents multiple values. Then we’ll apply the same technique +to extract a generic function! By looking at how to recognize duplicated code +you can extract into a function, you’ll start to recognize duplicated code that +can use generics. -We’ll begin with the short program in Listing 10-1 that finds the largest -number in a list. +We’ll begin with the short program in Listing 10-1 that finds the largest number +in a list. @@ -55,9 +54,9 @@ to the first number in the list in a variable named `largest`. We then iterate through all the numbers in the list, and if the current number is greater than the number stored in `largest`, we replace the reference in that variable. However, if the current number is less than or equal to the largest number seen -so far, the variable doesn’t change, and the code moves on to the next number -in the list. After considering all the numbers in the list, `largest` should -refer to the largest number, which in this case is 100. +so far, the variable doesn’t change, and the code moves on to the next number in +the list. After considering all the numbers in the list, `largest` should refer +to the largest number, which in this case is 100. We’ve now been tasked with finding the largest number in two different lists of numbers. To do so, we can choose to duplicate the code in Listing 10-1 and use @@ -95,8 +94,7 @@ list of `i32` values we might have in the future. The `largest` function has a parameter called `list`, which represents any concrete slice of `i32` values we might pass into the function. As a result, -when we call the function, the code runs on the specific values that we pass -in. +when we call the function, the code runs on the specific values that we pass in. In summary, here are the steps we took to change the code from Listing 10-2 to Listing 10-3: @@ -107,8 +105,8 @@ Listing 10-3: 1. Update the two instances of duplicated code to call the function instead. Next, we’ll use these same steps with generics to reduce code duplication. In -the same way that the function body can operate on an abstract `list` instead -of specific values, generics allow code to operate on abstract types. +the same way that the function body can operate on an abstract `list` instead of +specific values, generics allow code to operate on abstract types. For example, say we had two functions: one that finds the largest item in a slice of `i32` values and one that finds the largest item in a slice of `char` diff --git a/src/ch10-01-syntax.md b/src/ch10-01-syntax.md index 00e813978e..b51aee700d 100644 --- a/src/ch10-01-syntax.md +++ b/src/ch10-01-syntax.md @@ -30,27 +30,27 @@ the largest `i32` in a slice. The `largest_char` function finds the largest the duplication by introducing a generic type parameter in a single function. To parameterize the types in a new single function, we need to name the type -parameter, just as we do for the value parameters to a function. You can use -any identifier as a type parameter name. But we’ll use `T` because, by -convention, type parameter names in Rust are short, often just one letter, and -Rust’s type-naming convention is UpperCamelCase. Short for _type_, `T` is the -default choice of most Rust programmers. +parameter, just as we do for the value parameters to a function. You can use any +identifier as a type parameter name. But we’ll use `T` because, by convention, +type parameter names in Rust are short, often just one letter, and Rust’s +type-naming convention is UpperCamelCase. Short for _type_, `T` is the default +choice of most Rust programmers. When we use a parameter in the body of the function, we have to declare the parameter name in the signature so the compiler knows what that name means. -Similarly, when we use a type parameter name in a function signature, we have -to declare the type parameter name before we use it. To define the generic -`largest` function, we place type name declarations inside angle brackets, -`<>`, between the name of the function and the parameter list, like this: +Similarly, when we use a type parameter name in a function signature, we have to +declare the type parameter name before we use it. To define the generic +`largest` function, we place type name declarations inside angle brackets, `<>`, +between the name of the function and the parameter list, like this: ```rust,ignore fn largest(list: &[T]) -> &T { ``` We read this definition as: the function `largest` is generic over some type -`T`. This function has one parameter named `list`, which is a slice of values -of type `T`. The `largest` function will return a reference to a value of the -same type `T`. +`T`. This function has one parameter named `list`, which is a slice of values of +type `T`. The `largest` function will return a reference to a value of the same +type `T`. Listing 10-5 shows the combined `largest` function definition using the generic data type in its signature. The listing also shows how we can call the function @@ -74,8 +74,8 @@ If we compile this code right now, we’ll get this error: The help text mentions `std::cmp::PartialOrd`, which is a _trait_, and we’re going to talk about traits in the next section. For now, know that this error states that the body of `largest` won’t work for all possible types that `T` -could be. Because we want to compare values of type `T` in the body, we can -only use types whose values can be ordered. To enable comparisons, the standard +could be. Because we want to compare values of type `T` in the body, we can only +use types whose values can be ordered. To enable comparisons, the standard library has the `std::cmp::PartialOrd` trait that you can implement on types (see Appendix C for more on this trait). By following the help text’s suggestion, we restrict the types valid for `T` to only those that implement @@ -84,9 +84,9 @@ implements `PartialOrd` on both `i32` and `char`. ### In Struct Definitions -We can also define structs to use a generic type parameter in one or more -fields using the `<>` syntax. Listing 10-6 defines a `Point` struct to hold -`x` and `y` coordinate values of any type. +We can also define structs to use a generic type parameter in one or more fields +using the `<>` syntax. Listing 10-6 defines a `Point` struct to hold `x` and +`y` coordinate values of any type. @@ -98,9 +98,8 @@ fields using the `<>` syntax. Listing 10-6 defines a `Point` struct to hold The syntax for using generics in struct definitions is similar to that used in function definitions. First we declare the name of the type parameter inside -angle brackets just after the name of the struct. Then we use the generic -type in the struct definition where we would otherwise specify concrete data -types. +angle brackets just after the name of the struct. Then we use the generic type +in the struct definition where we would otherwise specify concrete data types. Note that because we’ve used only one generic type to define `Point`, this definition says that the `Point` struct is generic over some type `T`, and @@ -140,9 +139,8 @@ and `U` where `x` is of type `T` and `y` is of type `U`. Now all the instances of `Point` shown are allowed! You can use as many generic type parameters in a definition as you want, but using more than a few makes -your code hard to read. If you’re finding you need lots of generic types in -your code, it could indicate that your code needs restructuring into smaller -pieces. +your code hard to read. If you’re finding you need lots of generic types in your +code, it could indicate that your code needs restructuring into smaller pieces. ### In Enum Definitions @@ -178,20 +176,20 @@ The `Result` enum is generic over two types, `T` and `E`, and has two variants: `Ok`, which holds a value of type `T`, and `Err`, which holds a value of type `E`. This definition makes it convenient to use the `Result` enum anywhere we have an operation that might succeed (return a value of some type `T`) or fail -(return an error of some type `E`). In fact, this is what we used to open a -file in Listing 9-3, where `T` was filled in with the type `std::fs::File` when -the file was opened successfully and `E` was filled in with the type +(return an error of some type `E`). In fact, this is what we used to open a file +in Listing 9-3, where `T` was filled in with the type `std::fs::File` when the +file was opened successfully and `E` was filled in with the type `std::io::Error` when there were problems opening the file. When you recognize situations in your code with multiple struct or enum -definitions that differ only in the types of the values they hold, you can -avoid duplication by using generic types instead. +definitions that differ only in the types of the values they hold, you can avoid +duplication by using generic types instead. ### In Method Definitions We can implement methods on structs and enums (as we did in Chapter 5) and use -generic types in their definitions too. Listing 10-9 shows the `Point` -struct we defined in Listing 10-6 with a method named `x` implemented on it. +generic types in their definitions too. Listing 10-9 shows the `Point` struct +we defined in Listing 10-6 with a method named `x` implemented on it. @@ -201,18 +199,18 @@ struct we defined in Listing 10-6 with a method named `x` implemented on it. -Here, we’ve defined a method named `x` on `Point` that returns a reference -to the data in the field `x`. +Here, we’ve defined a method named `x` on `Point` that returns a reference to +the data in the field `x`. Note that we have to declare `T` just after `impl` so we can use `T` to specify that we’re implementing methods on the type `Point`. By declaring `T` as a -generic type after `impl`, Rust can identify that the type in the angle -brackets in `Point` is a generic type rather than a concrete type. We could -have chosen a different name for this generic parameter than the generic -parameter declared in the struct definition, but using the same name is -conventional. Methods written within an `impl` that declares the generic type -will be defined on any instance of the type, no matter what concrete type ends -up substituting for the generic type. +generic type after `impl`, Rust can identify that the type in the angle brackets +in `Point` is a generic type rather than a concrete type. We could have chosen a +different name for this generic parameter than the generic parameter declared in +the struct definition, but using the same name is conventional. Methods written +within an `impl` that declares the generic type will be defined on any instance +of the type, no matter what concrete type ends up substituting for the generic +type. We can also specify constraints on generic types when defining methods on the type. We could, for example, implement methods only on `Point` instances @@ -227,18 +225,18 @@ use the concrete type `f32`, meaning we don’t declare any types after `impl`. -This code means the type `Point` will have a `distance_from_origin` -method; other instances of `Point` where `T` is not of type `f32` will not -have this method defined. The method measures how far our point is from the -point at coordinates (0.0, 0.0) and uses mathematical operations that are -available only for floating-point types. +This code means the type `Point` will have a `distance_from_origin` method; +other instances of `Point` where `T` is not of type `f32` will not have this +method defined. The method measures how far our point is from the point at +coordinates (0.0, 0.0) and uses mathematical operations that are available only +for floating-point types. Generic type parameters in a struct definition aren’t always the same as those you use in that same struct’s method signatures. Listing 10-11 uses the generic types `X1` and `Y1` for the `Point` struct and `X2` `Y2` for the `mixup` method -signature to make the example clearer. The method creates a new `Point` -instance with the `x` value from the `self` `Point` (of type `X1`) and the `y` -value from the passed-in `Point` (of type `Y2`). +signature to make the example clearer. The method creates a new `Point` instance +with the `x` value from the `self` `Point` (of type `X1`) and the `y` value from +the passed-in `Point` (of type `Y2`). @@ -253,15 +251,14 @@ and an `f64` for `y` (with value `10.4`). The `p2` variable is a `Point` struct that has a string slice for `x` (with value `"Hello"`) and a `char` for `y` (with value `c`). Calling `mixup` on `p1` with the argument `p2` gives us `p3`, which will have an `i32` for `x` because `x` came from `p1`. The `p3` variable -will have a `char` for `y` because `y` came from `p2`. The `println!` macro -call will print `p3.x = 5, p3.y = c`. +will have a `char` for `y` because `y` came from `p2`. The `println!` macro call +will print `p3.x = 5, p3.y = c`. The purpose of this example is to demonstrate a situation in which some generic parameters are declared with `impl` and some are declared with the method -definition. Here, the generic parameters `X1` and `Y1` are declared after -`impl` because they go with the struct definition. The generic parameters `X2` -and `Y2` are declared after `fn mixup` because they’re only relevant to the -method. +definition. Here, the generic parameters `X1` and `Y1` are declared after `impl` +because they go with the struct definition. The generic parameters `X2` and `Y2` +are declared after `fn mixup` because they’re only relevant to the method. ### Performance of Code Using Generics @@ -269,28 +266,28 @@ You might be wondering whether there is a runtime cost when using generic type parameters. The good news is that using generic types won’t make your program run any slower than it would with concrete types. -Rust accomplishes this by performing monomorphization of the code using -generics at compile time. _Monomorphization_ is the process of turning generic -code into specific code by filling in the concrete types that are used when -compiled. In this process, the compiler does the opposite of the steps we used -to create the generic function in Listing 10-5: the compiler looks at all the -places where generic code is called and generates code for the concrete types -the generic code is called with. +Rust accomplishes this by performing monomorphization of the code using generics +at compile time. _Monomorphization_ is the process of turning generic code into +specific code by filling in the concrete types that are used when compiled. In +this process, the compiler does the opposite of the steps we used to create the +generic function in Listing 10-5: the compiler looks at all the places where +generic code is called and generates code for the concrete types the generic +code is called with. -Let’s look at how this works by using the standard library’s generic -`Option` enum: +Let’s look at how this works by using the standard library’s generic `Option` +enum: ```rust let integer = Some(5); let float = Some(5.0); ``` -When Rust compiles this code, it performs monomorphization. During that -process, the compiler reads the values that have been used in `Option` -instances and identifies two kinds of `Option`: one is `i32` and the other -is `f64`. As such, it expands the generic definition of `Option` into two -definitions specialized to `i32` and `f64`, thereby replacing the generic -definition with the specific ones. +When Rust compiles this code, it performs monomorphization. During that process, +the compiler reads the values that have been used in `Option` instances and +identifies two kinds of `Option`: one is `i32` and the other is `f64`. As +such, it expands the generic definition of `Option` into two definitions +specialized to `i32` and `f64`, thereby replacing the generic definition with +the specific ones. The monomorphized version of the code looks similar to the following (the compiler uses different names than what we’re using here for illustration): @@ -316,9 +313,9 @@ fn main() { -The generic `Option` is replaced with the specific definitions created by -the compiler. Because Rust compiles generic code into code that specifies the -type in each instance, we pay no runtime cost for using generics. When the code -runs, it performs just as it would if we had duplicated each definition by -hand. The process of monomorphization makes Rust’s generics extremely efficient -at runtime. +The generic `Option` is replaced with the specific definitions created by the +compiler. Because Rust compiles generic code into code that specifies the type +in each instance, we pay no runtime cost for using generics. When the code runs, +it performs just as it would if we had duplicated each definition by hand. The +process of monomorphization makes Rust’s generics extremely efficient at +runtime. diff --git a/src/ch10-02-traits.md b/src/ch10-02-traits.md index 5628eef64b..d0de75fefb 100644 --- a/src/ch10-02-traits.md +++ b/src/ch10-02-traits.md @@ -12,8 +12,8 @@ certain behavior. A type’s behavior consists of the methods we can call on that type. Different types share the same behavior if we can call the same methods on all of those -types. Trait definitions are a way to group method signatures together to -define a set of behaviors necessary to accomplish some purpose. +types. Trait definitions are a way to group method signatures together to define +a set of behaviors necessary to accomplish some purpose. For example, let’s say we have multiple structs that hold various kinds and amounts of text: a `NewsArticle` struct that holds a news story filed in a @@ -37,16 +37,16 @@ definition of a public `Summary` trait that expresses this behavior. Here, we declare a trait using the `trait` keyword and then the trait’s name, which is `Summary` in this case. We also declare the trait as `pub` so that -crates depending on this crate can make use of this trait too, as we’ll see in -a few examples. Inside the curly brackets, we declare the method signatures -that describe the behaviors of the types that implement this trait, which in -this case is `fn summarize(&self) -> String`. +crates depending on this crate can make use of this trait too, as we’ll see in a +few examples. Inside the curly brackets, we declare the method signatures that +describe the behaviors of the types that implement this trait, which in this +case is `fn summarize(&self) -> String`. After the method signature, instead of providing an implementation within curly -brackets, we use a semicolon. Each type implementing this trait must provide -its own custom behavior for the body of the method. The compiler will enforce -that any type that has the `Summary` trait will have the method `summarize` -defined with this signature exactly. +brackets, we use a semicolon. Each type implementing this trait must provide its +own custom behavior for the body of the method. The compiler will enforce that +any type that has the `Summary` trait will have the method `summarize` defined +with this signature exactly. A trait can have multiple methods in its body: the method signatures are listed one per line, and each line ends in a semicolon. @@ -54,9 +54,9 @@ one per line, and each line ends in a semicolon. ### Implementing a Trait on a Type Now that we’ve defined the desired signatures of the `Summary` trait’s methods, -we can implement it on the types in our media aggregator. Listing 10-13 shows -an implementation of the `Summary` trait on the `NewsArticle` struct that uses -the headline, the author, and the location to create the return value of +we can implement it on the types in our media aggregator. Listing 10-13 shows an +implementation of the `Summary` trait on the `NewsArticle` struct that uses the +headline, the author, and the location to create the return value of `summarize`. For the `Tweet` struct, we define `summarize` as the username followed by the entire text of the tweet, assuming that the tweet content is already limited to 280 characters. @@ -88,7 +88,8 @@ library crate: {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/no-listing-01-calling-trait-method/src/main.rs}} ``` -This code prints `1 new tweet: horse_ebooks: of course, as you probably already +This code prints +`1 new tweet: horse_ebooks: of course, as you probably already know, people`. Other crates that depend on the `aggregator` crate can also bring the `Summary` @@ -103,18 +104,18 @@ crate. But we can’t implement external traits on external types. For example, we can’t implement the `Display` trait on `Vec` within our `aggregator` crate because -`Display` and `Vec` are both defined in the standard library and aren’t -local to our `aggregator` crate. This restriction is part of a property called +`Display` and `Vec` are both defined in the standard library and aren’t local +to our `aggregator` crate. This restriction is part of a property called _coherence_, and more specifically the _orphan rule_, so named because the parent type is not present. This rule ensures that other people’s code can’t -break your code and vice versa. Without the rule, two crates could implement -the same trait for the same type, and Rust wouldn’t know which implementation -to use. +break your code and vice versa. Without the rule, two crates could implement the +same trait for the same type, and Rust wouldn’t know which implementation to +use. ### Default Implementations -Sometimes it’s useful to have default behavior for some or all of the methods -in a trait instead of requiring implementations for all methods on every type. +Sometimes it’s useful to have default behavior for some or all of the methods in +a trait instead of requiring implementations for all methods on every type. Then, as we implement the trait on a particular type, we can keep or override each method’s default behavior. @@ -135,8 +136,8 @@ specify an empty `impl` block with `impl Summary for NewsArticle {}`. Even though we’re no longer defining the `summarize` method on `NewsArticle` directly, we’ve provided a default implementation and specified that -`NewsArticle` implements the `Summary` trait. As a result, we can still call -the `summarize` method on an instance of `NewsArticle`, like this: +`NewsArticle` implements the `Summary` trait. As a result, we can still call the +`summarize` method on an instance of `NewsArticle`, like this: ```rust,ignore {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/no-listing-02-calling-default-impl/src/main.rs:here}} @@ -146,13 +147,13 @@ This code prints `New article available! (Read more...)`. Creating a default implementation doesn’t require us to change anything about the implementation of `Summary` on `Tweet` in Listing 10-13. The reason is that -the syntax for overriding a default implementation is the same as the syntax -for implementing a trait method that doesn’t have a default implementation. +the syntax for overriding a default implementation is the same as the syntax for +implementing a trait method that doesn’t have a default implementation. Default implementations can call other methods in the same trait, even if those other methods don’t have a default implementation. In this way, a trait can -provide a lot of useful functionality and only require implementors to specify -a small part of it. For example, we could define the `Summary` trait to have a +provide a lot of useful functionality and only require implementors to specify a +small part of it. For example, we could define the `Summary` trait to have a `summarize_author` method whose implementation is required, and then define a `summarize` method that has a default implementation that calls the `summarize_author` method: @@ -161,8 +162,8 @@ a small part of it. For example, we could define the `Summary` trait to have a {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/no-listing-03-default-impl-calls-other-methods/src/lib.rs:here}} ``` -To use this version of `Summary`, we only need to define `summarize_author` -when we implement the trait on a type: +To use this version of `Summary`, we only need to define `summarize_author` when +we implement the trait on a type: ```rust,ignore {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/no-listing-03-default-impl-calls-other-methods/src/lib.rs:impl}} @@ -172,8 +173,8 @@ After we define `summarize_author`, we can call `summarize` on instances of the `Tweet` struct, and the default implementation of `summarize` will call the definition of `summarize_author` that we’ve provided. Because we’ve implemented `summarize_author`, the `Summary` trait has given us the behavior of the -`summarize` method without requiring us to write any more code. Here’s what -that looks like: +`summarize` method without requiring us to write any more code. Here’s what that +looks like: ```rust,ignore {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/no-listing-03-default-impl-calls-other-methods/src/main.rs:here}} @@ -188,10 +189,10 @@ overriding implementation of that same method. Now that you know how to define and implement traits, we can explore how to use traits to define functions that accept many different types. We’ll use the -`Summary` trait we implemented on the `NewsArticle` and `Tweet` types in -Listing 10-13 to define a `notify` function that calls the `summarize` method -on its `item` parameter, which is of some type that implements the `Summary` -trait. To do this, we use the `impl Trait` syntax, like this: +`Summary` trait we implemented on the `NewsArticle` and `Tweet` types in Listing +10-13 to define a `notify` function that calls the `summarize` method on its +`item` parameter, which is of some type that implements the `Summary` trait. To +do this, we use the `impl Trait` syntax, like this: ```rust,ignore {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/no-listing-04-traits-as-parameters/src/lib.rs:here}} @@ -199,11 +200,11 @@ trait. To do this, we use the `impl Trait` syntax, like this: Instead of a concrete type for the `item` parameter, we specify the `impl` keyword and the trait name. This parameter accepts any type that implements the -specified trait. In the body of `notify`, we can call any methods on `item` -that come from the `Summary` trait, such as `summarize`. We can call `notify` -and pass in any instance of `NewsArticle` or `Tweet`. Code that calls the -function with any other type, such as a `String` or an `i32`, won’t compile -because those types don’t implement `Summary`. +specified trait. In the body of `notify`, we can call any methods on `item` that +come from the `Summary` trait, such as `summarize`. We can call `notify` and +pass in any instance of `NewsArticle` or `Tweet`. Code that calls the function +with any other type, such as a `String` or an `i32`, won’t compile because those +types don’t implement `Summary`. @@ -242,16 +243,16 @@ trait bound, like this: pub fn notify(item1: &T, item2: &T) { ``` -The generic type `T` specified as the type of the `item1` and `item2` -parameters constrains the function such that the concrete type of the value -passed as an argument for `item1` and `item2` must be the same. +The generic type `T` specified as the type of the `item1` and `item2` parameters +constrains the function such that the concrete type of the value passed as an +argument for `item1` and `item2` must be the same. #### Specifying Multiple Trait Bounds with the `+` Syntax We can also specify more than one trait bound. Say we wanted `notify` to use display formatting as well as `summarize` on `item`: we specify in the `notify` -definition that `item` must implement both `Display` and `Summary`. We can do -so using the `+` syntax: +definition that `item` must implement both `Display` and `Summary`. We can do so +using the `+` syntax: ```rust,ignore pub fn notify(item: &(impl Summary + Display)) { @@ -291,8 +292,8 @@ bounds. ### Returning Types That Implement Traits -We can also use the `impl Trait` syntax in the return position to return a -value of some type that implements a trait, as shown here: +We can also use the `impl Trait` syntax in the return position to return a value +of some type that implements a trait, as shown here: ```rust,ignore {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/no-listing-05-returning-impl-trait/src/lib.rs:here}} @@ -320,8 +321,8 @@ return type specified as `impl Summary` wouldn’t work: Returning either a `NewsArticle` or a `Tweet` isn’t allowed due to restrictions around how the `impl Trait` syntax is implemented in the compiler. We’ll cover -how to write a function with this behavior in the [“Using Trait Objects That -Allow for Values of Different +how to write a function with this behavior in the +[“Using Trait Objects That Allow for Values of Different Types”][using-trait-objects-that-allow-for-values-of-different-types] section of Chapter 18. @@ -331,11 +332,11 @@ By using a trait bound with an `impl` block that uses generic type parameters, we can implement methods conditionally for types that implement the specified traits. For example, the type `Pair` in Listing 10-15 always implements the `new` function to return a new instance of `Pair` (recall from the -[“Defining Methods”][methods] section of Chapter 5 that `Self` -is a type alias for the type of the `impl` block, which in this case is -`Pair`). But in the next `impl` block, `Pair` only implements the -`cmp_display` method if its inner type `T` implements the `PartialOrd` trait -that enables comparison _and_ the `Display` trait that enables printing. +[“Defining Methods”][methods] section of Chapter 5 that `Self` is +a type alias for the type of the `impl` block, which in this case is `Pair`). +But in the next `impl` block, `Pair` only implements the `cmp_display` method +if its inner type `T` implements the `PartialOrd` trait that enables comparison +_and_ the `Display` trait that enables printing. @@ -345,12 +346,12 @@ that enables comparison _and_ the `Display` trait that enables printing. -We can also conditionally implement a trait for any type that implements -another trait. Implementations of a trait on any type that satisfies the trait -bounds are called _blanket implementations_ and are used extensively in the -Rust standard library. For example, the standard library implements the -`ToString` trait on any type that implements the `Display` trait. The `impl` -block in the standard library looks similar to this code: +We can also conditionally implement a trait for any type that implements another +trait. Implementations of a trait on any type that satisfies the trait bounds +are called _blanket implementations_ and are used extensively in the Rust +standard library. For example, the standard library implements the `ToString` +trait on any type that implements the `Display` trait. The `impl` block in the +standard library looks similar to this code: ```rust,ignore impl ToString for T { @@ -375,12 +376,12 @@ reduce duplication but also specify to the compiler that we want the generic type to have particular behavior. The compiler can then use the trait bound information to check that all the concrete types used with our code provide the correct behavior. In dynamically typed languages, we would get an error at -runtime if we called a method on a type which didn’t define the method. But -Rust moves these errors to compile time so we’re forced to fix the problems -before our code is even able to run. Additionally, we don’t have to write code -that checks for behavior at runtime because we’ve already checked at compile -time. Doing so improves performance without having to give up the flexibility -of generics. +runtime if we called a method on a type which didn’t define the method. But Rust +moves these errors to compile time so we’re forced to fix the problems before +our code is even able to run. Additionally, we don’t have to write code that +checks for behavior at runtime because we’ve already checked at compile time. +Doing so improves performance without having to give up the flexibility of +generics. [using-trait-objects-that-allow-for-values-of-different-types]: ch18-02-trait-objects.html#using-trait-objects-that-allow-for-values-of-different-types [methods]: ch05-03-method-syntax.html#defining-methods diff --git a/src/ch10-03-lifetime-syntax.md b/src/ch10-03-lifetime-syntax.md index 0a356ec641..89d74d34f4 100644 --- a/src/ch10-03-lifetime-syntax.md +++ b/src/ch10-03-lifetime-syntax.md @@ -1,23 +1,24 @@ ## Validating References with Lifetimes -Lifetimes are another kind of generic that we’ve already been using. Rather -than ensuring that a type has the behavior we want, lifetimes ensure that -references are valid as long as we need them to be. - -One detail we didn’t discuss in the [“References and -Borrowing”][references-and-borrowing] section in Chapter 4 is -that every reference in Rust has a _lifetime_, which is the scope for which -that reference is valid. Most of the time, lifetimes are implicit and inferred, -just like most of the time, types are inferred. We must annotate types only -when multiple types are possible. In a similar way, we must annotate lifetimes -when the lifetimes of references could be related in a few different ways. Rust -requires us to annotate the relationships using generic lifetime parameters to -ensure the actual references used at runtime will definitely be valid. +Lifetimes are another kind of generic that we’ve already been using. Rather than +ensuring that a type has the behavior we want, lifetimes ensure that references +are valid as long as we need them to be. + +One detail we didn’t discuss in the +[“References and Borrowing”][references-and-borrowing] section in +Chapter 4 is that every reference in Rust has a _lifetime_, which is the scope +for which that reference is valid. Most of the time, lifetimes are implicit and +inferred, just like most of the time, types are inferred. We must annotate types +only when multiple types are possible. In a similar way, we must annotate +lifetimes when the lifetimes of references could be related in a few different +ways. Rust requires us to annotate the relationships using generic lifetime +parameters to ensure the actual references used at runtime will definitely be +valid. Annotating lifetimes is not a concept most other programming languages have, so this is going to feel unfamiliar. Although we won’t cover lifetimes in their -entirety in this chapter, we’ll discuss common ways you might encounter -lifetime syntax so you can get comfortable with the concept. +entirety in this chapter, we’ll discuss common ways you might encounter lifetime +syntax so you can get comfortable with the concept. ### Preventing Dangling References with Lifetimes @@ -53,12 +54,12 @@ we try to use it. Here is the error message: ``` The error message says that the variable `x` “does not live long enough.” The -reason is that `x` will be out of scope when the inner scope ends on line 7. -But `r` is still valid for the outer scope; because its scope is larger, we say -that it “lives longer.” If Rust allowed this code to work, `r` would be -referencing memory that was deallocated when `x` went out of scope, and -anything we tried to do with `r` wouldn’t work correctly. So how does Rust -determine that this code is invalid? It uses a borrow checker. +reason is that `x` will be out of scope when the inner scope ends on line 7. But +`r` is still valid for the outer scope; because its scope is larger, we say that +it “lives longer.” If Rust allowed this code to work, `r` would be referencing +memory that was deallocated when `x` went out of scope, and anything we tried to +do with `r` wouldn’t work correctly. So how does Rust determine that this code +is invalid? It uses a borrow checker. ### The Borrow Checker @@ -74,12 +75,12 @@ whether all borrows are valid. Listing 10-17 shows the same code as Listing -Here, we’ve annotated the lifetime of `r` with `'a` and the lifetime of `x` -with `'b`. As you can see, the inner `'b` block is much smaller than the outer -`'a` lifetime block. At compile time, Rust compares the size of the two -lifetimes and sees that `r` has a lifetime of `'a` but that it refers to memory -with a lifetime of `'b`. The program is rejected because `'b` is shorter than -`'a`: the subject of the reference doesn’t live as long as the reference. +Here, we’ve annotated the lifetime of `r` with `'a` and the lifetime of `x` with +`'b`. As you can see, the inner `'b` block is much smaller than the outer `'a` +lifetime block. At compile time, Rust compares the size of the two lifetimes and +sees that `r` has a lifetime of `'a` but that it refers to memory with a +lifetime of `'b`. The program is rejected because `'b` is shorter than `'a`: the +subject of the reference doesn’t live as long as the reference. Listing 10-18 fixes the code so it doesn’t have a dangling reference and it compiles without any errors. @@ -104,8 +105,8 @@ lifetimes of parameters and return values in the context of functions. We’ll write a function that returns the longer of two string slices. This function will take two string slices and return a single string slice. After -we’ve implemented the `longest` function, the code in Listing 10-19 should -print `The longest string is abcd`. +we’ve implemented the `longest` function, the code in Listing 10-19 should print +`The longest string is abcd`. @@ -117,10 +118,10 @@ print `The longest string is abcd`. Note that we want the function to take string slices, which are references, rather than strings, because we don’t want the `longest` function to take -ownership of its parameters. Refer to the [“String Slices as -Parameters”][string-slices-as-parameters] section in Chapter 4 -for more discussion about why the parameters we use in Listing 10-19 are the -ones we want. +ownership of its parameters. Refer to the +[“String Slices as Parameters”][string-slices-as-parameters] +section in Chapter 4 for more discussion about why the parameters we use in +Listing 10-19 are the ones we want. If we try to implement the `longest` function as shown in Listing 10-20, it won’t compile. @@ -139,11 +140,11 @@ Instead, we get the following error that talks about lifetimes: {{#include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-20/output.txt}} ``` -The help text reveals that the return type needs a generic lifetime parameter -on it because Rust can’t tell whether the reference being returned refers to -`x` or `y`. Actually, we don’t know either, because the `if` block in the body -of this function returns a reference to `x` and the `else` block returns a -reference to `y`! +The help text reveals that the return type needs a generic lifetime parameter on +it because Rust can’t tell whether the reference being returned refers to `x` or +`y`. Actually, we don’t know either, because the `if` block in the body of this +function returns a reference to `x` and the `else` block returns a reference to +`y`! When we’re defining this function, we don’t know the concrete values that will be passed into this function, so we don’t know whether the `if` case or the @@ -153,8 +154,8 @@ Listings 10-17 and 10-18 to determine whether the reference we return will always be valid. The borrow checker can’t determine this either, because it doesn’t know how the lifetimes of `x` and `y` relate to the lifetime of the return value. To fix this error, we’ll add generic lifetime parameters that -define the relationship between the references so the borrow checker can -perform its analysis. +define the relationship between the references so the borrow checker can perform +its analysis. ### Lifetime Annotation Syntax @@ -165,8 +166,8 @@ when the signature specifies a generic type parameter, functions can accept references with any lifetime by specifying a generic lifetime parameter. Lifetime annotations have a slightly unusual syntax: the names of lifetime -parameters must start with an apostrophe (`'`) and are usually all lowercase -and very short, like generic types. Most people use the name `'a` for the first +parameters must start with an apostrophe (`'`) and are usually all lowercase and +very short, like generic types. Most people use the name `'a` for the first lifetime annotation. We place lifetime parameter annotations after the `&` of a reference, using a space to separate the annotation from the reference’s type. @@ -209,13 +210,13 @@ This code should compile and produce the result we want when we use it with the `main` function in Listing 10-19. The function signature now tells Rust that for some lifetime `'a`, the function -takes two parameters, both of which are string slices that live at least as -long as lifetime `'a`. The function signature also tells Rust that the string -slice returned from the function will live at least as long as lifetime `'a`. -In practice, it means that the lifetime of the reference returned by the -`longest` function is the same as the smaller of the lifetimes of the values -referred to by the function arguments. These relationships are what we want -Rust to use when analyzing this code. +takes two parameters, both of which are string slices that live at least as long +as lifetime `'a`. The function signature also tells Rust that the string slice +returned from the function will live at least as long as lifetime `'a`. In +practice, it means that the lifetime of the reference returned by the `longest` +function is the same as the smaller of the lifetimes of the values referred to +by the function arguments. These relationships are what we want Rust to use when +analyzing this code. Remember, when we specify the lifetime parameters in this function signature, we’re not changing the lifetimes of any values passed in or returned. Rather, @@ -225,15 +226,15 @@ know exactly how long `x` and `y` will live, only that some scope can be substituted for `'a` that will satisfy this signature. When annotating lifetimes in functions, the annotations go in the function -signature, not in the function body. The lifetime annotations become part of -the contract of the function, much like the types in the signature. Having -function signatures contain the lifetime contract means the analysis the Rust -compiler does can be simpler. If there’s a problem with the way a function is -annotated or the way it is called, the compiler errors can point to the part of -our code and the constraints more precisely. If, instead, the Rust compiler -made more inferences about what we intended the relationships of the lifetimes -to be, the compiler might only be able to point to a use of our code many steps -away from the cause of the problem. +signature, not in the function body. The lifetime annotations become part of the +contract of the function, much like the types in the signature. Having function +signatures contain the lifetime contract means the analysis the Rust compiler +does can be simpler. If there’s a problem with the way a function is annotated +or the way it is called, the compiler errors can point to the part of our code +and the constraints more precisely. If, instead, the Rust compiler made more +inferences about what we intended the relationships of the lifetimes to be, the +compiler might only be able to point to a use of our code many steps away from +the cause of the problem. When we pass concrete references to `longest`, the concrete lifetime that is substituted for `'a` is the part of the scope of `x` that overlaps with the @@ -244,8 +245,8 @@ the returned reference will also be valid for the length of the smaller of the lifetimes of `x` and `y`. Let’s look at how the lifetime annotations restrict the `longest` function by -passing in references that have different concrete lifetimes. Listing 10-22 is -a straightforward example. +passing in references that have different concrete lifetimes. Listing 10-22 is a +straightforward example. @@ -258,7 +259,8 @@ a straightforward example. In this example, `string1` is valid until the end of the outer scope, `string2` is valid until the end of the inner scope, and `result` references something that is valid until the end of the inner scope. Run this code and you’ll see -that the borrow checker approves; it will compile and print `The longest string +that the borrow checker approves; it will compile and print +`The longest string is long string is long`. Next, let’s try an example that shows that the lifetime of the reference in @@ -266,8 +268,8 @@ Next, let’s try an example that shows that the lifetime of the reference in declaration of the `result` variable outside the inner scope but leave the assignment of the value to the `result` variable inside the scope with `string2`. Then we’ll move the `println!` that uses `result` to outside the -inner scope, after the inner scope has ended. The code in Listing 10-23 will -not compile. +inner scope, after the inner scope has ended. The code in Listing 10-23 will not +compile. @@ -298,9 +300,9 @@ the lifetimes of the references passed in. Therefore, the borrow checker disallows the code in Listing 10-23 as possibly having an invalid reference. Try designing more experiments that vary the values and lifetimes of the -references passed in to the `longest` function and how the returned reference -is used. Make hypotheses about whether or not your experiments will pass the -borrow checker before you compile; then check to see if you’re right! +references passed in to the `longest` function and how the returned reference is +used. Make hypotheses about whether or not your experiments will pass the borrow +checker before you compile; then check to see if you’re right! ### Thinking in Terms of Lifetimes @@ -338,22 +340,22 @@ compile: -Here, even though we’ve specified a lifetime parameter `'a` for the return -type, this implementation will fail to compile because the return value -lifetime is not related to the lifetime of the parameters at all. Here is the -error message we get: +Here, even though we’ve specified a lifetime parameter `'a` for the return type, +this implementation will fail to compile because the return value lifetime is +not related to the lifetime of the parameters at all. Here is the error message +we get: ```console {{#include ../listings/ch10-generic-types-traits-and-lifetimes/no-listing-09-unrelated-lifetime/output.txt}} ``` -The problem is that `result` goes out of scope and gets cleaned up at the end -of the `longest` function. We’re also trying to return a reference to `result` -from the function. There is no way we can specify lifetime parameters that -would change the dangling reference, and Rust won’t let us create a dangling +The problem is that `result` goes out of scope and gets cleaned up at the end of +the `longest` function. We’re also trying to return a reference to `result` from +the function. There is no way we can specify lifetime parameters that would +change the dangling reference, and Rust won’t let us create a dangling reference. In this case, the best fix would be to return an owned data type -rather than a reference so the calling function is then responsible for -cleaning up the value. +rather than a reference so the calling function is then responsible for cleaning +up the value. Ultimately, lifetime syntax is about connecting the lifetimes of various parameters and return values of functions. Once they’re connected, Rust has @@ -362,9 +364,9 @@ would create dangling pointers or otherwise violate memory safety. ### Lifetime Annotations in Struct Definitions -So far, the structs we’ve defined all hold owned types. We can define structs -to hold references, but in that case we would need to add a lifetime annotation -on every reference in the struct’s definition. Listing 10-24 has a struct named +So far, the structs we’ve defined all hold owned types. We can define structs to +hold references, but in that case we would need to add a lifetime annotation on +every reference in the struct’s definition. Listing 10-24 has a struct named `ImportantExcerpt` that holds a string slice. @@ -378,9 +380,9 @@ on every reference in the struct’s definition. Listing 10-24 has a struct name This struct has the single field `part` that holds a string slice, which is a reference. As with generic data types, we declare the name of the generic lifetime parameter inside angle brackets after the name of the struct so we can -use the lifetime parameter in the body of the struct definition. This -annotation means an instance of `ImportantExcerpt` can’t outlive the reference -it holds in its `part` field. +use the lifetime parameter in the body of the struct definition. This annotation +means an instance of `ImportantExcerpt` can’t outlive the reference it holds in +its `part` field. The `main` function here creates an instance of the `ImportantExcerpt` struct that holds a reference to the first sentence of the `String` owned by the @@ -404,21 +406,21 @@ without lifetime annotations. -The reason this function compiles without lifetime annotations is historical: -in early versions (pre-1.0) of Rust, this code wouldn’t have compiled because -every reference needed an explicit lifetime. At that time, the function -signature would have been written like this: +The reason this function compiles without lifetime annotations is historical: in +early versions (pre-1.0) of Rust, this code wouldn’t have compiled because every +reference needed an explicit lifetime. At that time, the function signature +would have been written like this: ```rust,ignore fn first_word<'a>(s: &'a str) -> &'a str { ``` -After writing a lot of Rust code, the Rust team found that Rust programmers -were entering the same lifetime annotations over and over in particular -situations. These situations were predictable and followed a few deterministic -patterns. The developers programmed these patterns into the compiler’s code so -the borrow checker could infer the lifetimes in these situations and wouldn’t -need explicit annotations. +After writing a lot of Rust code, the Rust team found that Rust programmers were +entering the same lifetime annotations over and over in particular situations. +These situations were predictable and followed a few deterministic patterns. The +developers programmed these patterns into the compiler’s code so the borrow +checker could infer the lifetimes in these situations and wouldn’t need explicit +annotations. This piece of Rust history is relevant because it’s possible that more deterministic patterns will emerge and be added to the compiler. In the future, @@ -426,37 +428,39 @@ even fewer lifetime annotations might be required. The patterns programmed into Rust’s analysis of references are called the _lifetime elision rules_. These aren’t rules for programmers to follow; they’re -a set of particular cases that the compiler will consider, and if your code -fits these cases, you don’t need to write the lifetimes explicitly. +a set of particular cases that the compiler will consider, and if your code fits +these cases, you don’t need to write the lifetimes explicitly. The elision rules don’t provide full inference. If there is still ambiguity as -to what lifetimes the references have after Rust applies the rules, the -compiler won’t guess what the lifetime of the remaining references should be. -Instead of guessing, the compiler will give you an error that you can resolve -by adding the lifetime annotations. +to what lifetimes the references have after Rust applies the rules, the compiler +won’t guess what the lifetime of the remaining references should be. Instead of +guessing, the compiler will give you an error that you can resolve by adding the +lifetime annotations. Lifetimes on function or method parameters are called _input lifetimes_, and lifetimes on return values are called _output lifetimes_. -The compiler uses three rules to figure out the lifetimes of the references -when there aren’t explicit annotations. The first rule applies to input -lifetimes, and the second and third rules apply to output lifetimes. If the -compiler gets to the end of the three rules and there are still references for -which it can’t figure out lifetimes, the compiler will stop with an error. -These rules apply to `fn` definitions as well as `impl` blocks. +The compiler uses three rules to figure out the lifetimes of the references when +there aren’t explicit annotations. The first rule applies to input lifetimes, +and the second and third rules apply to output lifetimes. If the compiler gets +to the end of the three rules and there are still references for which it can’t +figure out lifetimes, the compiler will stop with an error. These rules apply to +`fn` definitions as well as `impl` blocks. The first rule is that the compiler assigns a lifetime parameter to each -parameter that’s a reference. In other words, a function with one parameter -gets one lifetime parameter: `fn foo<'a>(x: &'a i32)`; a function with two -parameters gets two separate lifetime parameters: `fn foo<'a, 'b>(x: &'a i32, -y: &'b i32)`; and so on. +parameter that’s a reference. In other words, a function with one parameter gets +one lifetime parameter: `fn foo<'a>(x: &'a i32)`; a function with two parameters +gets two separate lifetime parameters: `fn foo<'a, 'b>(x: &'a i32, +y: &'b i32)`; +and so on. The second rule is that, if there is exactly one input lifetime parameter, that -lifetime is assigned to all output lifetime parameters: `fn foo<'a>(x: &'a i32) +lifetime is assigned to all output lifetime parameters: +`fn foo<'a>(x: &'a i32) -> &'a i32`. -The third rule is that, if there are multiple input lifetime parameters, but -one of them is `&self` or `&mut self` because this is a method, the lifetime of +The third rule is that, if there are multiple input lifetime parameters, but one +of them is `&self` or `&mut self` because this is a method, the lifetime of `self` is assigned to all output lifetime parameters. This third rule makes methods much nicer to read and write because fewer symbols are necessary. @@ -478,8 +482,8 @@ fn first_word<'a>(s: &'a str) -> &str { ``` The second rule applies because there is exactly one input lifetime. The second -rule specifies that the lifetime of the one input parameter gets assigned to -the output lifetime, so the signature is now this: +rule specifies that the lifetime of the one input parameter gets assigned to the +output lifetime, so the signature is now this: ```rust,ignore fn first_word<'a>(s: &'a str) -> &'a str { @@ -518,9 +522,9 @@ annotate lifetimes in method signatures very often. ### Lifetime Annotations in Method Definitions When we implement methods on a struct with lifetimes, we use the same syntax as -that of generic type parameters shown in Listing 10-11. Where we declare and -use the lifetime parameters depends on whether they’re related to the struct -fields or the method parameters and return values. +that of generic type parameters shown in Listing 10-11. Where we declare and use +the lifetime parameters depends on whether they’re related to the struct fields +or the method parameters and return values. Lifetime names for struct fields always need to be declared after the `impl` keyword and then used after the struct’s name because those lifetimes are part @@ -550,15 +554,15 @@ Here is an example where the third lifetime elision rule applies: ``` There are two input lifetimes, so Rust applies the first lifetime elision rule -and gives both `&self` and `announcement` their own lifetimes. Then, because -one of the parameters is `&self`, the return type gets the lifetime of `&self`, -and all lifetimes have been accounted for. +and gives both `&self` and `announcement` their own lifetimes. Then, because one +of the parameters is `&self`, the return type gets the lifetime of `&self`, and +all lifetimes have been accounted for. ### The Static Lifetime One special lifetime we need to discuss is `'static`, which denotes that the -affected reference _can_ live for the entire duration of the program. All -string literals have the `'static` lifetime, which we can annotate as follows: +affected reference _can_ live for the entire duration of the program. All string +literals have the `'static` lifetime, which we can annotate as follows: ```rust let s: &'static str = "I have a static lifetime."; @@ -568,12 +572,12 @@ The text of this string is stored directly in the program’s binary, which is always available. Therefore, the lifetime of all string literals is `'static`. You might see suggestions to use the `'static` lifetime in error messages. But -before specifying `'static` as the lifetime for a reference, think about -whether the reference you have actually lives the entire lifetime of your -program or not, and whether you want it to. Most of the time, an error message -suggesting the `'static` lifetime results from attempting to create a dangling -reference or a mismatch of the available lifetimes. In such cases, the solution -is to fix those problems, not to specify the `'static` lifetime. +before specifying `'static` as the lifetime for a reference, think about whether +the reference you have actually lives the entire lifetime of your program or +not, and whether you want it to. Most of the time, an error message suggesting +the `'static` lifetime results from attempting to create a dangling reference or +a mismatch of the available lifetimes. In such cases, the solution is to fix +those problems, not to specify the `'static` lifetime. ## Generic Type Parameters, Trait Bounds, and Lifetimes Together @@ -584,14 +588,14 @@ bounds, and lifetimes all in one function! {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/no-listing-11-generics-traits-and-lifetimes/src/main.rs:here}} ``` -This is the `longest` function from Listing 10-21 that returns the longer of -two string slices. But now it has an extra parameter named `ann` of the generic -type `T`, which can be filled in by any type that implements the `Display` -trait as specified by the `where` clause. This extra parameter will be printed -using `{}`, which is why the `Display` trait bound is necessary. Because -lifetimes are a type of generic, the declarations of the lifetime parameter -`'a` and the generic type parameter `T` go in the same list inside the angle -brackets after the function name. +This is the `longest` function from Listing 10-21 that returns the longer of two +string slices. But now it has an extra parameter named `ann` of the generic type +`T`, which can be filled in by any type that implements the `Display` trait as +specified by the `where` clause. This extra parameter will be printed using +`{}`, which is why the `Display` trait bound is necessary. Because lifetimes are +a type of generic, the declarations of the lifetime parameter `'a` and the +generic type parameter `T` go in the same list inside the angle brackets after +the function name. ## Summary diff --git a/src/ch11-00-testing.md b/src/ch11-00-testing.md index f6e799d581..8d3eac96f8 100644 --- a/src/ch11-00-testing.md +++ b/src/ch11-00-testing.md @@ -1,9 +1,9 @@ # Writing Automated Tests -In his 1972 essay “The Humble Programmer,” Edsger W. Dijkstra said that -“Program testing can be a very effective way to show the presence of bugs, but -it is hopelessly inadequate for showing their absence.” That doesn’t mean we -shouldn’t try to test as much as we can! +In his 1972 essay “The Humble Programmer,” Edsger W. Dijkstra said that “Program +testing can be a very effective way to show the presence of bugs, but it is +hopelessly inadequate for showing their absence.” That doesn’t mean we shouldn’t +try to test as much as we can! Correctness in our programs is the extent to which our code does what we intend it to do. Rust is designed with a high degree of concern about the correctness @@ -14,11 +14,11 @@ everything. As such, Rust includes support for writing automated software tests. Say we write a function `add_two` that adds 2 to whatever number is passed to it. This function’s signature accepts an integer as a parameter and returns an integer as a result. When we implement and compile that function, Rust does all -the type checking and borrow checking that you’ve learned so far to ensure -that, for instance, we aren’t passing a `String` value or an invalid reference -to this function. But Rust _can’t_ check that this function will do precisely -what we intend, which is return the parameter plus 2 rather than, say, the -parameter plus 10 or the parameter minus 50! That’s where tests come in. +the type checking and borrow checking that you’ve learned so far to ensure that, +for instance, we aren’t passing a `String` value or an invalid reference to this +function. But Rust _can’t_ check that this function will do precisely what we +intend, which is return the parameter plus 2 rather than, say, the parameter +plus 10 or the parameter minus 50! That’s where tests come in. We can write tests that assert, for example, that when we pass `3` to the `add_two` function, the returned value is `5`. We can run these tests whenever @@ -27,7 +27,7 @@ changed. Testing is a complex skill: although we can’t cover in one chapter every detail about how to write good tests, in this chapter we will discuss the mechanics of -Rust’s testing facilities. We’ll talk about the annotations and macros -available to you when writing your tests, the default behavior and options -provided for running your tests, and how to organize tests into unit tests and -integration tests. +Rust’s testing facilities. We’ll talk about the annotations and macros available +to you when writing your tests, the default behavior and options provided for +running your tests, and how to organize tests into unit tests and integration +tests. diff --git a/src/ch11-01-writing-tests.md b/src/ch11-01-writing-tests.md index 93009f4d09..21f3ae0a18 100644 --- a/src/ch11-01-writing-tests.md +++ b/src/ch11-01-writing-tests.md @@ -4,9 +4,9 @@ Tests are Rust functions that verify that the non-test code is functioning in the expected manner. The bodies of test functions typically perform these three actions: -- Set up any needed data or state. -- Run the code you want to test. -- Assert that the results are what you expect. +* Set up any needed data or state. +* Run the code you want to test. +* Assert that the results are what you expect. Let’s look at the features Rust provides specifically for writing tests that take these actions, which include the `test` attribute, a few macros, and the @@ -15,18 +15,17 @@ take these actions, which include the `test` attribute, a few macros, and the ### The Anatomy of a Test Function At its simplest, a test in Rust is a function that’s annotated with the `test` -attribute. Attributes are metadata about pieces of Rust code; one example is -the `derive` attribute we used with structs in Chapter 5. To change a function -into a test function, add `#[test]` on the line before `fn`. When you run your -tests with the `cargo test` command, Rust builds a test runner binary that runs -the annotated functions and reports on whether each test function passes or -fails. +attribute. Attributes are metadata about pieces of Rust code; one example is the +`derive` attribute we used with structs in Chapter 5. To change a function into +a test function, add `#[test]` on the line before `fn`. When you run your tests +with the `cargo test` command, Rust builds a test runner binary that runs the +annotated functions and reports on whether each test function passes or fails. Whenever we make a new library project with Cargo, a test module with a test function in it is automatically generated for us. This module gives you a -template for writing your tests so you don’t have to look up the exact -structure and syntax every time you start a new project. You can add as many -additional test functions and as many test modules as you want! +template for writing your tests so you don’t have to look up the exact structure +and syntax every time you start a new project. You can add as many additional +test functions and as many test modules as you want! We’ll explore some aspects of how tests work by experimenting with the template test before we actually test any code. Then we’ll write some real-world tests @@ -63,10 +62,10 @@ cd ../../.. For now, let’s focus solely on the `it_works` function. Note the `#[test]` -annotation: this attribute indicates this is a test function, so the test -runner knows to treat this function as a test. We might also have non-test -functions in the `tests` module to help set up common scenarios or perform -common operations, so we always need to indicate which functions are tests. +annotation: this attribute indicates this is a test function, so the test runner +knows to treat this function as a test. We might also have non-test functions in +the `tests` module to help set up common scenarios or perform common operations, +so we always need to indicate which functions are tests. The example function body uses the `assert_eq!` macro to assert that `result`, which contains the result of adding 2 and 2, equals 4. This assertion serves as @@ -84,34 +83,38 @@ The `cargo test` command runs all tests in our project, as shown in Listing -Cargo compiled and ran the test. We see the line `running 1 test`. The next -line shows the name of the generated test function, called `tests::it_works`, -and that the result of running that test is `ok`. The overall summary `test -result: ok.` means that all the tests passed, and the portion that reads `1 +Cargo compiled and ran the test. We see the line `running 1 test`. The next line +shows the name of the generated test function, called `tests::it_works`, and +that the result of running that test is `ok`. The overall summary +`test +result: ok.` means that all the tests passed, and the portion that reads +`1 passed; 0 failed` totals the number of tests that passed or failed. It’s possible to mark a test as ignored so it doesn’t run in a particular -instance; we’ll cover that in the [“Ignoring Some Tests Unless Specifically -Requested”][ignoring] section later in this chapter. Because we -haven’t done that here, the summary shows `0 ignored`. +instance; we’ll cover that in the +[“Ignoring Some Tests Unless Specifically Requested”][ignoring] +section later in this chapter. Because we haven’t done that here, the summary +shows `0 ignored`. The `0 measured` statistic is for benchmark tests that measure performance. Benchmark tests are, as of this writing, only available in nightly Rust. See [the documentation about benchmark tests][bench] to learn more. -We can pass an argument to the `cargo test` command to run only tests whose -name matches a string; this is called _filtering_ and we’ll cover that in the +We can pass an argument to the `cargo test` command to run only tests whose name +matches a string; this is called _filtering_ and we’ll cover that in the [“Running a Subset of Tests by Name”][subset] section. Here we -haven’t filtered the tests being run, so the end of the summary shows `0 +haven’t filtered the tests being run, so the end of the summary shows +`0 filtered out`. The next part of the test output starting at `Doc-tests adder` is for the results of any documentation tests. We don’t have any documentation tests yet, but Rust can compile any code examples that appear in our API documentation. This feature helps keep your docs and your code in sync! We’ll discuss how to -write documentation tests in the [“Documentation Comments as -Tests”][doc-comments] section of Chapter 14. For now, we’ll -ignore the `Doc-tests` output. +write documentation tests in the +[“Documentation Comments as Tests”][doc-comments] section of +Chapter 14. For now, we’ll ignore the `Doc-tests` output. Let’s start to customize the test to our own needs. First, change the name of the `it_works` function to a different name, such as `exploration`, like so: @@ -132,9 +135,9 @@ Then run `cargo test` again. The output now shows `exploration` instead of Now we’ll add another test, but this time we’ll make a test that fails! Tests fail when something in the test function panics. Each test is run in a new thread, and when the main thread sees that a test thread has died, the test is -marked as failed. In Chapter 9, we talked about how the simplest way to panic -is to call the `panic!` macro. Enter the new test as a function named -`another`, so your _src/lib.rs_ file looks like Listing 11-3. +marked as failed. In Chapter 9, we talked about how the simplest way to panic is +to call the `panic!` macro. Enter the new test as a function named `another`, so +your _src/lib.rs_ file looks like Listing 11-3. @@ -160,15 +163,15 @@ rg panicked listings/ch11-writing-automated-tests/listing-11-03/output.txt check the line number of the panic matches the line number in the following paragraph --> -Instead of `ok`, the line `test tests::another` shows `FAILED`. Two new -sections appear between the individual results and the summary: the first -displays the detailed reason for each test failure. In this case, we get the -details that `another` failed because it `panicked at 'Make this test fail'` on -line 17 in the _src/lib.rs_ file. The next section lists just the names of all -the failing tests, which is useful when there are lots of tests and lots of -detailed failing test output. We can use the name of a failing test to run just -that test to more easily debug it; we’ll talk more about ways to run tests in -the [“Controlling How Tests Are Run”][controlling-how-tests-are-run] section. The summary line displays at the end: overall, our test result is `FAILED`. We @@ -181,10 +184,10 @@ let’s look at some macros other than `panic!` that are useful in tests. The `assert!` macro, provided by the standard library, is useful when you want to ensure that some condition in a test evaluates to `true`. We give the -`assert!` macro an argument that evaluates to a Boolean. If the value is -`true`, nothing happens and the test passes. If the value is `false`, the -`assert!` macro calls `panic!` to cause the test to fail. Using the `assert!` -macro helps us check that our code is functioning in the way we intend. +`assert!` macro an argument that evaluates to a Boolean. If the value is `true`, +nothing happens and the test passes. If the value is `false`, the `assert!` +macro calls `panic!` to cause the test to fail. Using the `assert!` macro helps +us check that our code is functioning in the way we intend. In Chapter 5, Listing 5-15, we used a `Rectangle` struct and a `can_hold` method, which are repeated here in Listing 11-5. Let’s put this code in the @@ -198,11 +201,11 @@ _src/lib.rs_ file, then write some tests for it using the `assert!` macro. -The `can_hold` method returns a Boolean, which means it’s a perfect use case -for the `assert!` macro. In Listing 11-6, we write a test that exercises the -`can_hold` method by creating a `Rectangle` instance that has a width of 8 and -a height of 7 and asserting that it can hold another `Rectangle` instance that -has a width of 5 and a height of 1. +The `can_hold` method returns a Boolean, which means it’s a perfect use case for +the `assert!` macro. In Listing 11-6, we write a test that exercises the +`can_hold` method by creating a `Rectangle` instance that has a width of 8 and a +height of 7 and asserting that it can hold another `Rectangle` instance that has +a width of 5 and a height of 1. @@ -212,13 +215,14 @@ has a width of 5 and a height of 1. -Note the `use super::*;` line inside the `tests` module. The `tests` module is -a regular module that follows the usual visibility rules we covered in Chapter -7 in the [“Paths for Referring to an Item in the Module +Note the `use super::*;` line inside the `tests` module. The `tests` module is a +regular module that follows the usual visibility rules we covered in Chapter 7 +in the +[“Paths for Referring to an Item in the Module Tree”][paths-for-referring-to-an-item-in-the-module-tree] section. Because the `tests` module is an inner module, we need to bring the -code under test in the outer module into the scope of the inner module. We use -a glob here, so anything we define in the outer module is available to this +code under test in the outer module into the scope of the inner module. We use a +glob here, so anything we define in the outer module is available to this `tests` module. We’ve named our test `larger_can_hold_smaller`, and we’ve created the two @@ -249,8 +253,8 @@ result, our test will pass if `can_hold` returns `false`: Two tests that pass! Now let’s see what happens to our test results when we introduce a bug in our code. We’ll change the implementation of the `can_hold` -method by replacing the greater-than sign with a less-than sign when it -compares the widths: +method by replacing the greater-than sign with a less-than sign when it compares +the widths: ```rust,not_desired_behavior,noplayground {{#rustdoc_include ../listings/ch11-writing-automated-tests/no-listing-03-introducing-a-bug/src/lib.rs:here}} @@ -269,15 +273,15 @@ less than 5. ### Testing Equality with the `assert_eq!` and `assert_ne!` Macros A common way to verify functionality is to test for equality between the result -of the code under test and the value you expect the code to return. You could -do this by using the `assert!` macro and passing it an expression using the -`==` operator. However, this is such a common test that the standard library -provides a pair of macros—`assert_eq!` and `assert_ne!`—to perform this test -more conveniently. These macros compare two arguments for equality or -inequality, respectively. They’ll also print the two values if the assertion -fails, which makes it easier to see _why_ the test failed; conversely, the -`assert!` macro only indicates that it got a `false` value for the `==` -expression, without printing the values that led to the `false` value. +of the code under test and the value you expect the code to return. You could do +this by using the `assert!` macro and passing it an expression using the `==` +operator. However, this is such a common test that the standard library provides +a pair of macros—`assert_eq!` and `assert_ne!`—to perform this test more +conveniently. These macros compare two arguments for equality or inequality, +respectively. They’ll also print the two values if the assertion fails, which +makes it easier to see _why_ the test failed; conversely, the `assert!` macro +only indicates that it got a `false` value for the `==` expression, without +printing the values that led to the `false` value. In Listing 11-7, we write a function named `add_two` that adds `2` to its parameter, then we test this function using the `assert_eq!` macro. @@ -314,28 +318,28 @@ Run the tests again: {{#include ../listings/ch11-writing-automated-tests/no-listing-04-bug-in-add-two/output.txt}} ``` -Our test caught the bug! The `it_adds_two` test failed, and the message tells -us ``assertion `left == right` failed`` and what the `left` and `right` values -are. This message helps us start debugging: the `left` argument, where we had -the result of calling `add_two(2)`, was `5` but the `right` argument was `4`. -You can imagine that this would be especially helpful when we have a lot of -tests going on. +Our test caught the bug! The `it_adds_two` test failed, and the message tells us +``assertion `left == right` failed`` and what the `left` and `right` values are. +This message helps us start debugging: the `left` argument, where we had the +result of calling `add_two(2)`, was `5` but the `right` argument was `4`. You +can imagine that this would be especially helpful when we have a lot of tests +going on. Note that in some languages and test frameworks, the parameters to equality assertion functions are called `expected` and `actual`, and the order in which we specify the arguments matters. However, in Rust, they’re called `left` and -`right`, and the order in which we specify the value we expect and the value -the code produces doesn’t matter. We could write the assertion in this test as -`assert_eq!(4, result)`, which would produce the same failure message -that displays `` assertion failed: `(left == right)` ``. +`right`, and the order in which we specify the value we expect and the value the +code produces doesn’t matter. We could write the assertion in this test as +`assert_eq!(4, result)`, which would produce the same failure message that +displays `` assertion failed: `(left == right)` ``. The `assert_ne!` macro will pass if the two values we give it are not equal and fail if they’re equal. This macro is most useful for cases when we’re not sure what a value _will_ be, but we know what the value definitely _shouldn’t_ be. For example, if we’re testing a function that is guaranteed to change its input -in some way, but the way in which the input is changed depends on the day of -the week that we run our tests, the best thing to assert might be that the -output of the function is not equal to the input. +in some way, but the way in which the input is changed depends on the day of the +week that we run our tests, the best thing to assert might be that the output of +the function is not equal to the input. Under the surface, the `assert_eq!` and `assert_ne!` macros use the operators `==` and `!=`, respectively. When the assertions fail, these macros print their @@ -355,16 +359,16 @@ details about these and other derivable traits. You can also add a custom message to be printed with the failure message as optional arguments to the `assert!`, `assert_eq!`, and `assert_ne!` macros. Any arguments specified after the required arguments are passed along to the -`format!` macro (discussed in Chapter 8 in the [“Concatenation with the `+` -Operator or the `format!` +`format!` macro (discussed in Chapter 8 in the +[“Concatenation with the `+` Operator or the `format!` Macro”][concatenation-with-the--operator-or-the-format-macro] section), so you can pass a format string that contains `{}` placeholders and values to go in those placeholders. Custom messages are useful for documenting what an assertion means; when a test fails, you’ll have a better idea of what the problem is with the code. -For example, let’s say we have a function that greets people by name and we -want to test that the name we pass into the function appears in the output: +For example, let’s say we have a function that greets people by name and we want +to test that the name we pass into the function appears in the output: Filename: src/lib.rs @@ -372,12 +376,12 @@ want to test that the name we pass into the function appears in the output: {{#rustdoc_include ../listings/ch11-writing-automated-tests/no-listing-05-greeter/src/lib.rs}} ``` -The requirements for this program haven’t been agreed upon yet, and we’re -pretty sure the `Hello` text at the beginning of the greeting will change. We -decided we don’t want to have to update the test when the requirements change, -so instead of checking for exact equality to the value returned from the -`greeting` function, we’ll just assert that the output contains the text of the -input parameter. +The requirements for this program haven’t been agreed upon yet, and we’re pretty +sure the `Hello` text at the beginning of the greeting will change. We decided +we don’t want to have to update the test when the requirements change, so +instead of checking for exact equality to the value returned from the `greeting` +function, we’ll just assert that the output contains the text of the input +parameter. Now let’s introduce a bug into this code by changing `greeting` to exclude `name` to see what the default test failure looks like: @@ -415,14 +419,14 @@ debug what happened instead of what we were expecting to happen. In addition to checking return values, it’s important to check that our code handles error conditions as we expect. For example, consider the `Guess` type -that we created in Chapter 9, Listing 9-13. Other code that uses `Guess` -depends on the guarantee that `Guess` instances will contain only values -between 1 and 100. We can write a test that ensures that attempting to create a -`Guess` instance with a value outside that range panics. +that we created in Chapter 9, Listing 9-13. Other code that uses `Guess` depends +on the guarantee that `Guess` instances will contain only values between 1 +and 100. We can write a test that ensures that attempting to create a `Guess` +instance with a value outside that range panics. -We do this by adding the attribute `should_panic` to our test function. The -test passes if the code inside the function panics; the test fails if the code -inside the function doesn’t panic. +We do this by adding the attribute `should_panic` to our test function. The test +passes if the code inside the function panics; the test fails if the code inside +the function doesn’t panic. Listing 11-8 shows a test that checks that the error conditions of `Guess::new` happen when we expect them to. @@ -443,8 +447,8 @@ passes: {{#include ../listings/ch11-writing-automated-tests/listing-11-08/output.txt}} ``` -Looks good! Now let’s introduce a bug in our code by removing the condition -that the `new` function will panic if the value is greater than 100: +Looks good! Now let’s introduce a bug in our code by removing the condition that +the `new` function will panic if the value is greater than 100: ```rust,not_desired_behavior,noplayground {{#rustdoc_include ../listings/ch11-writing-automated-tests/no-listing-08-guess-with-bug/src/lib.rs:here}} @@ -460,14 +464,13 @@ We don’t get a very helpful message in this case, but when we look at the test function, we see that it’s annotated with `#[should_panic]`. The failure we got means that the code in the test function did not cause a panic. -Tests that use `should_panic` can be imprecise. A `should_panic` test would -pass even if the test panics for a different reason from the one we were -expecting. To make `should_panic` tests more precise, we can add an optional -`expected` parameter to the `should_panic` attribute. The test harness will -make sure that the failure message contains the provided text. For example, -consider the modified code for `Guess` in Listing 11-9 where the `new` function -panics with different messages depending on whether the value is too small or -too large. +Tests that use `should_panic` can be imprecise. A `should_panic` test would pass +even if the test panics for a different reason from the one we were expecting. +To make `should_panic` tests more precise, we can add an optional `expected` +parameter to the `should_panic` attribute. The test harness will make sure that +the failure message contains the provided text. For example, consider the +modified code for `Guess` in Listing 11-9 where the `new` function panics with +different messages depending on whether the value is too small or too large. @@ -480,14 +483,16 @@ too large. This test will pass because the value we put in the `should_panic` attribute’s `expected` parameter is a substring of the message that the `Guess::new` function panics with. We could have specified the entire panic message that we -expect, which in this case would be `Guess value must be less than or equal to -100, got 200`. What you choose to specify depends on how much of the panic -message is unique or dynamic and how precise you want your test to be. In this -case, a substring of the panic message is enough to ensure that the code in the -test function executes the `else if value > 100` case. - -To see what happens when a `should_panic` test with an `expected` message -fails, let’s again introduce a bug into our code by swapping the bodies of the +expect, which in this case would be +`Guess value must be less than or equal to +100, got 200`. What you choose to +specify depends on how much of the panic message is unique or dynamic and how +precise you want your test to be. In this case, a substring of the panic message +is enough to ensure that the code in the test function executes the +`else if value > 100` case. + +To see what happens when a `should_panic` test with an `expected` message fails, +let’s again introduce a bug into our code by swapping the bodies of the `if value < 1` and the `else if value > 100` blocks: ```rust,ignore,not_desired_behavior @@ -501,15 +506,18 @@ This time when we run the `should_panic` test, it will fail: ``` The failure message indicates that this test did indeed panic as we expected, -but the panic message did not include the expected string `less than or equal -to 100`. The panic message that we did get in this case was `Guess value must -be greater than or equal to 1, got 200.` Now we can start figuring out where -our bug is! +but the panic message did not include the expected string +`less than or equal +to 100`. The panic message that we did get in this case was +`Guess value must +be greater than or equal to 1, got 200.` Now we can start +figuring out where our bug is! ### Using `Result` in Tests Our tests so far all panic when they fail. We can also write tests that use -`Result`! Here’s the test from Listing 11-1, rewritten to use `Result`! Here’s the test from Listing 11-1, rewritten to use +`Result` and return an `Err` instead of panicking: ```rust,noplayground @@ -522,16 +530,18 @@ body of the function, rather than calling the `assert_eq!` macro, we return fails. Writing tests so they return a `Result` enables you to use the question -mark operator in the body of tests, which can be a convenient way to write -tests that should fail if any operation within them returns an `Err` variant. +mark operator in the body of tests, which can be a convenient way to write tests +that should fail if any operation within them returns an `Err` variant. You can’t use the `#[should_panic]` annotation on tests that use `Result`. To assert that an operation returns an `Err` variant, _don’t_ use the -question mark operator on the `Result` value. Instead, use +E>`. +To assert that an operation returns an `Err` variant, _don’t_ use the question +mark operator on the `Result` value. Instead, use `assert!(value.is_err())`. Now that you know several ways to write tests, let’s look at what is happening -when we run our tests and explore the different options we can use with `cargo +when we run our tests and explore the different options we can use with +`cargo test`. [concatenation-with-the--operator-or-the-format-macro]: ch08-02-strings.html#concatenation-with-the--operator-or-the-format-macro diff --git a/src/ch11-02-running-tests.md b/src/ch11-02-running-tests.md index dc4afc2af6..e5a24a2359 100644 --- a/src/ch11-02-running-tests.md +++ b/src/ch11-02-running-tests.md @@ -1,20 +1,20 @@ ## Controlling How Tests Are Run Just as `cargo run` compiles your code and then runs the resultant binary, -`cargo test` compiles your code in test mode and runs the resultant test -binary. The default behavior of the binary produced by `cargo test` is to run -all the tests in parallel and capture output generated during test runs, -preventing the output from being displayed and making it easier to read the -output related to the test results. You can, however, specify command line -options to change this default behavior. +`cargo test` compiles your code in test mode and runs the resultant test binary. +The default behavior of the binary produced by `cargo test` is to run all the +tests in parallel and capture output generated during test runs, preventing the +output from being displayed and making it easier to read the output related to +the test results. You can, however, specify command line options to change this +default behavior. Some command line options go to `cargo test`, and some go to the resultant test -binary. To separate these two types of arguments, you list the arguments that -go to `cargo test` followed by the separator `--` and then the ones that go to -the test binary. Running `cargo test --help` displays the options you can use -with `cargo test`, and running `cargo test -- --help` displays the options you -can use after the separator. Those options are also documented in [the “Tests” -section][tests] of the [the rustc book][rustc]. +binary. To separate these two types of arguments, you list the arguments that go +to `cargo test` followed by the separator `--` and then the ones that go to the +test binary. Running `cargo test --help` displays the options you can use with +`cargo test`, and running `cargo test -- --help` displays the options you can +use after the separator. Those options are also documented in +[the “Tests” section][tests] of the [the rustc book][rustc]. [tests]: https://doc.rust-lang.org/rustc/tests/index.html [rustc]: https://doc.rust-lang.org/rustc/index.html @@ -33,14 +33,14 @@ the data in that file and asserts that the file contains a particular value, which is different in each test. Because the tests run at the same time, one test might overwrite the file in the time between another test writing and reading the file. The second test will then fail, not because the code is -incorrect but because the tests have interfered with each other while running -in parallel. One solution is to make sure each test writes to a different file; +incorrect but because the tests have interfered with each other while running in +parallel. One solution is to make sure each test writes to a different file; another solution is to run the tests one at a time. If you don’t want to run the tests in parallel or if you want more fine-grained control over the number of threads used, you can send the `--test-threads` flag -and the number of threads you want to use to the test binary. Take a look at -the following example: +and the number of threads you want to use to the test binary. Take a look at the +following example: ```console $ cargo test -- --test-threads=1 @@ -76,10 +76,10 @@ When we run these tests with `cargo test`, we’ll see the following output: {{#include ../listings/ch11-writing-automated-tests/listing-11-10/output.txt}} ``` -Note that nowhere in this output do we see `I got the value 4`, which is -printed when the test that passes runs. That output has been captured. The -output from the test that failed, `I got the value 8`, appears in the section -of the test summary output, which also shows the cause of the test failure. +Note that nowhere in this output do we see `I got the value 4`, which is printed +when the test that passes runs. That output has been captured. The output from +the test that failed, `I got the value 8`, appears in the section of the test +summary output, which also shows the cause of the test failure. If we want to see printed values for passing tests as well, we can tell Rust to also show the output of successful tests with `--show-output`: @@ -99,8 +99,8 @@ see the following output: Sometimes, running a full test suite can take a long time. If you’re working on code in a particular area, you might want to run only the tests pertaining to -that code. You can choose which tests to run by passing `cargo test` the name -or names of the test(s) you want to run as an argument. +that code. You can choose which tests to run by passing `cargo test` the name or +names of the test(s) you want to run as an argument. To demonstrate how to run a subset of tests, we’ll first create three tests for our `add_two` function, as shown in Listing 11-11, and choose which ones to run. @@ -147,14 +147,14 @@ run those two by running `cargo test add`: This command ran all tests with `add` in the name and filtered out the test named `one_hundred`. Also note that the module in which a test appears becomes -part of the test’s name, so we can run all the tests in a module by filtering -on the module’s name. +part of the test’s name, so we can run all the tests in a module by filtering on +the module’s name. ### Ignoring Some Tests Unless Specifically Requested Sometimes a few specific tests can be very time-consuming to execute, so you -might want to exclude them during most runs of `cargo test`. Rather than -listing as arguments all tests you do want to run, you can instead annotate the +might want to exclude them during most runs of `cargo test`. Rather than listing +as arguments all tests you do want to run, you can instead annotate the time-consuming tests using the `ignore` attribute to exclude them, as shown here: @@ -164,22 +164,22 @@ here: {{#rustdoc_include ../listings/ch11-writing-automated-tests/no-listing-11-ignore-a-test/src/lib.rs:here}} ``` -After `#[test]`, we add the `#[ignore]` line to the test we want to exclude. -Now when we run our tests, `it_works` runs, but `expensive_test` doesn’t: +After `#[test]`, we add the `#[ignore]` line to the test we want to exclude. Now +when we run our tests, `it_works` runs, but `expensive_test` doesn’t: ```console {{#include ../listings/ch11-writing-automated-tests/no-listing-11-ignore-a-test/output.txt}} ``` -The `expensive_test` function is listed as `ignored`. If we want to run only -the ignored tests, we can use `cargo test -- --ignored`: +The `expensive_test` function is listed as `ignored`. If we want to run only the +ignored tests, we can use `cargo test -- --ignored`: ```console {{#include ../listings/ch11-writing-automated-tests/output-only-04-running-ignored/output.txt}} ``` -By controlling which tests run, you can make sure your `cargo test` results -will be returned quickly. When you’re at a point where it makes sense to check -the results of the `ignored` tests and you have time to wait for the results, -you can run `cargo test -- --ignored` instead. If you want to run all tests -whether they’re ignored or not, you can run `cargo test -- --include-ignored`. +By controlling which tests run, you can make sure your `cargo test` results will +be returned quickly. When you’re at a point where it makes sense to check the +results of the `ignored` tests and you have time to wait for the results, you +can run `cargo test -- --ignored` instead. If you want to run all tests whether +they’re ignored or not, you can run `cargo test -- --include-ignored`. diff --git a/src/ch11-03-test-organization.md b/src/ch11-03-test-organization.md index d0c6ef3003..24487b8299 100644 --- a/src/ch11-03-test-organization.md +++ b/src/ch11-03-test-organization.md @@ -17,20 +17,21 @@ library are doing what you expect them to, separately and together. The purpose of unit tests is to test each unit of code in isolation from the rest of the code to quickly pinpoint where code is and isn’t working as expected. You’ll put unit tests in the _src_ directory in each file with the -code that they’re testing. The convention is to create a module named `tests` -in each file to contain the test functions and to annotate the module with +code that they’re testing. The convention is to create a module named `tests` in +each file to contain the test functions and to annotate the module with `cfg(test)`. #### The Tests Module and `#[cfg(test)]` The `#[cfg(test)]` annotation on the `tests` module tells Rust to compile and run the test code only when you run `cargo test`, not when you run `cargo -build`. This saves compile time when you only want to build the library and -saves space in the resultant compiled artifact because the tests are not -included. You’ll see that because integration tests go in a different -directory, they don’t need the `#[cfg(test)]` annotation. However, because unit -tests go in the same files as the code, you’ll use `#[cfg(test)]` to specify -that they shouldn’t be included in the compiled result. +build`. +This saves compile time when you only want to build the library and saves space +in the resultant compiled artifact because the tests are not included. You’ll +see that because integration tests go in a different directory, they don’t need +the `#[cfg(test)]` annotation. However, because unit tests go in the same files +as the code, you’ll use `#[cfg(test)]` to specify that they shouldn’t be +included in the compiled result. Recall that when we generated the new `adder` project in the first section of this chapter, Cargo generated this code for us: @@ -54,8 +55,8 @@ module, in addition to the functions annotated with `#[test]`. There’s debate within the testing community about whether or not private functions should be tested directly, and other languages make it difficult or impossible to test private functions. Regardless of which testing ideology you -adhere to, Rust’s privacy rules do allow you to test private functions. -Consider the code in Listing 11-12 with the private function `internal_adder`. +adhere to, Rust’s privacy rules do allow you to test private functions. Consider +the code in Listing 11-12 with the private function `internal_adder`. @@ -66,13 +67,13 @@ Consider the code in Listing 11-12 with the private function `internal_adder`. Note that the `internal_adder` function is not marked as `pub`. Tests are just -Rust code, and the `tests` module is just another module. As we discussed in -the [“Paths for Referring to an Item in the Module Tree”][paths] +Rust code, and the `tests` module is just another module. As we discussed in the +[“Paths for Referring to an Item in the Module Tree”][paths] section, items in child modules can use the items in their ancestor modules. In this test, we bring all of the `tests` module’s parent’s items into scope with `use super::*`, and then the test can call `internal_adder`. If you don’t think -private functions should be tested, there’s nothing in Rust that will compel -you to do so. +private functions should be tested, there’s nothing in Rust that will compel you +to do so. ### Integration Tests @@ -86,9 +87,9 @@ tests, you first need a _tests_ directory. #### The _tests_ Directory -We create a _tests_ directory at the top level of our project directory, next -to _src_. Cargo knows to look for integration test files in this directory. We -can then make as many test files as we want, and Cargo will compile each of the +We create a _tests_ directory at the top level of our project directory, next to +_src_. Cargo knows to look for integration test files in this directory. We can +then make as many test files as we want, and Cargo will compile each of the files as an individual crate. Let’s create an integration test. With the code in Listing 11-12 still in the @@ -116,8 +117,10 @@ Enter the code in Listing 11-13 into the _tests/integration_test.rs_ file. Each file in the _tests_ directory is a separate crate, so we need to bring our -library into each test crate’s scope. For that reason we add `use -adder::add_two;` at the top of the code, which we didn’t need in the unit tests. +library into each test crate’s scope. For that reason we add +`use +adder::add_two;` at the top of the code, which we didn’t need in the unit +tests. We don’t need to annotate any code in _tests/integration_test.rs_ with `#[cfg(test)]`. Cargo treats the _tests_ directory specially and compiles files @@ -137,9 +140,10 @@ The first section for the unit tests is the same as we’ve been seeing: one lin for each unit test (one named `internal` that we added in Listing 11-12) and then a summary line for the unit tests. -The integration tests section starts with the line `Running -tests/integration_test.rs`. Next, there is a line for each test function in -that integration test and a summary line for the results of the integration +The integration tests section starts with the line +`Running +tests/integration_test.rs`. Next, there is a line for each test function +in that integration test and a summary line for the results of the integration test just before the `Doc-tests adder` section starts. Each integration test file has its own section, so if we add more files in the @@ -163,13 +167,14 @@ _tests_ directory to help organize them; for example, you can group the test functions by the functionality they’re testing. As mentioned earlier, each file in the _tests_ directory is compiled as its own separate crate, which is useful for creating separate scopes to more closely imitate the way end users will be -using your crate. However, this means files in the _tests_ directory don’t -share the same behavior as files in _src_ do, as you learned in Chapter 7 -regarding how to separate code into modules and files. +using your crate. However, this means files in the _tests_ directory don’t share +the same behavior as files in _src_ do, as you learned in Chapter 7 regarding +how to separate code into modules and files. The different behavior of _tests_ directory files is most noticeable when you -have a set of helper functions to use in multiple integration test files and -you try to follow the steps in the [“Separating Modules into Different +have a set of helper functions to use in multiple integration test files and you +try to follow the steps in the +[“Separating Modules into Different Files”][separating-modules-into-files] section of Chapter 7 to extract them into a common module. For example, if we create _tests/common.rs_ and place a function named `setup` in it, we can add some code to `setup` that @@ -206,10 +211,10 @@ project directory now looks like this: └── integration_test.rs ``` -This is the older naming convention that Rust also understands that we -mentioned in the [“Alternate File Paths”][alt-paths] section of -Chapter 7. Naming the file this way tells Rust not to treat the `common` module -as an integration test file. When we move the `setup` function code into +This is the older naming convention that Rust also understands that we mentioned +in the [“Alternate File Paths”][alt-paths] section of Chapter 7. +Naming the file this way tells Rust not to treat the `common` module as an +integration test file. When we move the `setup` function code into _tests/common/mod.rs_ and delete the _tests/common.rs_ file, the section in the test output will no longer appear. Files in subdirectories of the _tests_ directory don’t get compiled as separate crates or have sections in the test @@ -225,8 +230,8 @@ function from the `it_adds_two` test in _tests/integration_test.rs_: {{#rustdoc_include ../listings/ch11-writing-automated-tests/no-listing-13-fix-shared-test-code-problem/tests/integration_test.rs}} ``` -Note that the `mod common;` declaration is the same as the module declaration -we demonstrated in Listing 7-21. Then, in the test function, we can call the +Note that the `mod common;` declaration is the same as the module declaration we +demonstrated in Listing 7-21. Then, in the test function, we can call the `common::setup()` function. #### Integration Tests for Binary Crates @@ -255,8 +260,8 @@ in the same way external code will use it. Even though Rust’s type system and ownership rules help prevent some kinds of bugs, tests are still important to reduce logic bugs having to do with how your code is expected to behave. -Let’s combine the knowledge you learned in this chapter and in previous -chapters to work on a project! +Let’s combine the knowledge you learned in this chapter and in previous chapters +to work on a project! [paths]: ch07-03-paths-for-referring-to-an-item-in-the-module-tree.html [separating-modules-into-files]: ch07-05-separating-modules-into-different-files.html diff --git a/src/ch12-00-an-io-project.md b/src/ch12-00-an-io-project.md index e421cc12f0..b81ac70eea 100644 --- a/src/ch12-00-an-io-project.md +++ b/src/ch12-00-an-io-project.md @@ -10,8 +10,8 @@ an ideal language for creating command line tools, so for our project, we’ll make our own version of the classic command line search tool `grep` (**g**lobally search a **r**egular **e**xpression and **p**rint). In the simplest use case, `grep` searches a specified file for a specified string. To -do so, `grep` takes as its arguments a file path and a string. Then it reads -the file, finds lines in that file that contain the string argument, and prints +do so, `grep` takes as its arguments a file path and a string. Then it reads the +file, finds lines in that file that contain the string argument, and prints those lines. Along the way, we’ll show how to make our command line tool use the terminal @@ -21,19 +21,19 @@ We’ll also print error messages to the standard error console stream (`stderr` instead of standard output (`stdout`) so that, for example, the user can redirect successful output to a file while still seeing error messages onscreen. -One Rust community member, Andrew Gallant, has already created a fully -featured, very fast version of `grep`, called `ripgrep`. By comparison, our -version will be fairly simple, but this chapter will give you some of the -background knowledge you need to understand a real-world project such as -`ripgrep`. +One Rust community member, Andrew Gallant, has already created a fully featured, +very fast version of `grep`, called `ripgrep`. By comparison, our version will +be fairly simple, but this chapter will give you some of the background +knowledge you need to understand a real-world project such as `ripgrep`. Our `grep` project will combine a number of concepts you’ve learned so far: -- Organizing code ([Chapter 7][ch7]) -- Using vectors and strings ([Chapter 8][ch8]) -- Handling errors ([Chapter 9][ch9]) -- Using traits and lifetimes where appropriate ([Chapter 10][ch10]) -- Writing tests ([Chapter 11][ch11]) +* Organizing code ([Chapter 7][ch7]) +* Using vectors and strings ([Chapter 8][ch8]) +* Handling errors ([Chapter 9][ch9]) +* Using traits and lifetimes where appropriate + ([Chapter 10][ch10]) +* Writing tests ([Chapter 11][ch11]) We’ll also briefly introduce closures, iterators, and trait objects, which [Chapter 13][ch13] and [Chapter 18][ch18] will diff --git a/src/ch12-01-accepting-command-line-arguments.md b/src/ch12-01-accepting-command-line-arguments.md index 34e4504249..29648d54be 100644 --- a/src/ch12-01-accepting-command-line-arguments.md +++ b/src/ch12-01-accepting-command-line-arguments.md @@ -1,8 +1,8 @@ ## Accepting Command Line Arguments Let’s create a new project with, as always, `cargo new`. We’ll call our project -`minigrep` to distinguish it from the `grep` tool that you might already have -on your system. +`minigrep` to distinguish it from the `grep` tool that you might already have on +your system. ```console $ cargo new minigrep @@ -13,31 +13,31 @@ $ cd minigrep The first task is to make `minigrep` accept its two command line arguments: the file path and a string to search for. That is, we want to be able to run our program with `cargo run`, two hyphens to indicate the following arguments are -for our program rather than for `cargo`, a string to search for, and a path to -a file to search in, like so: +for our program rather than for `cargo`, a string to search for, and a path to a +file to search in, like so: ```console $ cargo run -- searchstring example-filename.txt ``` -Right now, the program generated by `cargo new` cannot process arguments we -give it. Some existing libraries on [crates.io](https://crates.io/) can help -with writing a program that accepts command line arguments, but because you’re -just learning this concept, let’s implement this capability ourselves. +Right now, the program generated by `cargo new` cannot process arguments we give +it. Some existing libraries on [crates.io](https://crates.io/) can help with +writing a program that accepts command line arguments, but because you’re just +learning this concept, let’s implement this capability ourselves. ### Reading the Argument Values -To enable `minigrep` to read the values of command line arguments we pass to -it, we’ll need the `std::env::args` function provided in Rust’s standard -library. This function returns an iterator of the command line arguments passed -to `minigrep`. We’ll cover iterators fully in [Chapter 13][ch13]. For now, you only need to know two details about iterators: iterators produce a series of values, and we can call the `collect` method on an iterator to turn it into a collection, such as a vector, that contains all the elements the iterator produces. -The code in Listing 12-1 allows your `minigrep` program to read any command -line arguments passed to it, and then collect the values into a vector. +The code in Listing 12-1 allows your `minigrep` program to read any command line +arguments passed to it, and then collect the values into a vector. @@ -47,23 +47,24 @@ line arguments passed to it, and then collect the values into a vector. -First we bring the `std::env` module into scope with a `use` statement so we -can use its `args` function. Notice that the `std::env::args` function is -nested in two levels of modules. As we discussed in [Chapter -7][ch7-idiomatic-use], in cases where the desired function is -nested in more than one module, we’ve chosen to bring the parent module into -scope rather than the function. By doing so, we can easily use other functions -from `std::env`. It’s also less ambiguous than adding `use std::env::args` and -then calling the function with just `args`, because `args` might easily be -mistaken for a function that’s defined in the current module. +First we bring the `std::env` module into scope with a `use` statement so we can +use its `args` function. Notice that the `std::env::args` function is nested in +two levels of modules. As we discussed in +[Chapter 7][ch7-idiomatic-use], in cases where the desired +function is nested in more than one module, we’ve chosen to bring the parent +module into scope rather than the function. By doing so, we can easily use other +functions from `std::env`. It’s also less ambiguous than adding +`use std::env::args` and then calling the function with just `args`, because +`args` might easily be mistaken for a function that’s defined in the current +module. > ### The `args` Function and Invalid Unicode > > Note that `std::env::args` will panic if any argument contains invalid -> Unicode. If your program needs to accept arguments containing invalid -> Unicode, use `std::env::args_os` instead. That function returns an iterator -> that produces `OsString` values instead of `String` values. We’ve chosen to -> use `std::env::args` here for simplicity because `OsString` values differ per +> Unicode. If your program needs to accept arguments containing invalid Unicode, +> use `std::env::args_os` instead. That function returns an iterator that +> produces `OsString` values instead of `String` values. We’ve chosen to use +> `std::env::args` here for simplicity because `OsString` values differ per > platform and are more complex to work with than `String` values. On the first line of `main`, we call `env::args`, and we immediately use @@ -71,8 +72,8 @@ On the first line of `main`, we call `env::args`, and we immediately use by the iterator. We can use the `collect` function to create many kinds of collections, so we explicitly annotate the type of `args` to specify that we want a vector of strings. Although you very rarely need to annotate types in -Rust, `collect` is one function you do often need to annotate because Rust -isn’t able to infer the kind of collection you want. +Rust, `collect` is one function you do often need to annotate because Rust isn’t +able to infer the kind of collection you want. Finally, we print the vector using the debug macro. Let’s try running the code first with no arguments and then with two arguments: @@ -85,13 +86,13 @@ first with no arguments and then with two arguments: {{#include ../listings/ch12-an-io-project/output-only-01-with-args/output.txt}} ``` -Notice that the first value in the vector is `"target/debug/minigrep"`, which -is the name of our binary. This matches the behavior of the arguments list in -C, letting programs use the name by which they were invoked in their execution. +Notice that the first value in the vector is `"target/debug/minigrep"`, which is +the name of our binary. This matches the behavior of the arguments list in C, +letting programs use the name by which they were invoked in their execution. It’s often convenient to have access to the program name in case you want to -print it in messages or change the behavior of the program based on what -command line alias was used to invoke the program. But for the purposes of this -chapter, we’ll ignore it and save only the two arguments we need. +print it in messages or change the behavior of the program based on what command +line alias was used to invoke the program. But for the purposes of this chapter, +we’ll ignore it and save only the two arguments we need. ### Saving the Argument Values in Variables @@ -116,16 +117,16 @@ will be the file path, so we put a reference to the second argument in the variable `file_path`. We temporarily print the values of these variables to prove that the code is -working as we intend. Let’s run this program again with the arguments `test` -and `sample.txt`: +working as we intend. Let’s run this program again with the arguments `test` and +`sample.txt`: ```console {{#include ../listings/ch12-an-io-project/listing-12-02/output.txt}} ``` Great, the program is working! The values of the arguments we need are being -saved into the right variables. Later we’ll add some error handling to deal -with certain potential erroneous situations, such as when the user provides no +saved into the right variables. Later we’ll add some error handling to deal with +certain potential erroneous situations, such as when the user provides no arguments; for now, we’ll ignore that situation and work on adding file-reading capabilities instead. diff --git a/src/ch12-02-reading-a-file.md b/src/ch12-02-reading-a-file.md index bc6d79139b..7e2af3cf6d 100644 --- a/src/ch12-02-reading-a-file.md +++ b/src/ch12-02-reading-a-file.md @@ -3,9 +3,8 @@ Now we’ll add functionality to read the file specified in the `file_path` argument. First we need a sample file to test it with: we’ll use a file with a small amount of text over multiple lines with some repeated words. Listing 12-3 -has an Emily Dickinson poem that will work well! Create a file called -_poem.txt_ at the root level of your project, and enter the poem “I’m Nobody! -Who are you?” +has an Emily Dickinson poem that will work well! Create a file called _poem.txt_ +at the root level of your project, and enter the poem “I’m Nobody! Who are you?” @@ -45,12 +44,12 @@ second argument: {{#rustdoc_include ../listings/ch12-an-io-project/listing-12-04/output.txt}} ``` -Great! The code read and then printed the contents of the file. But the code -has a few flaws. At the moment, the `main` function has multiple -responsibilities: generally, functions are clearer and easier to maintain if -each function is responsible for only one idea. The other problem is that we’re -not handling errors as well as we could. The program is still small, so these -flaws aren’t a big problem, but as the program grows, it will be harder to fix -them cleanly. It’s a good practice to begin refactoring early on when -developing a program because it’s much easier to refactor smaller amounts of -code. We’ll do that next. +Great! The code read and then printed the contents of the file. But the code has +a few flaws. At the moment, the `main` function has multiple responsibilities: +generally, functions are clearer and easier to maintain if each function is +responsible for only one idea. The other problem is that we’re not handling +errors as well as we could. The program is still small, so these flaws aren’t a +big problem, but as the program grows, it will be harder to fix them cleanly. +It’s a good practice to begin refactoring early on when developing a program +because it’s much easier to refactor smaller amounts of code. We’ll do that +next. diff --git a/src/ch12-03-improving-error-handling-and-modularity.md b/src/ch12-03-improving-error-handling-and-modularity.md index 0bf85bdd61..d04b862112 100644 --- a/src/ch12-03-improving-error-handling-and-modularity.md +++ b/src/ch12-03-improving-error-handling-and-modularity.md @@ -17,11 +17,12 @@ it will be to keep track of the purpose of each. It’s best to group the configuration variables into one structure to make their purpose clear. The third problem is that we’ve used `expect` to print an error message when -reading the file fails, but the error message just prints `Should have been -able to read the file`. Reading a file can fail in a number of ways: for -example, the file could be missing, or we might not have permission to open it. -Right now, regardless of the situation, we’d print the same error message for -everything, which wouldn’t give the user any information! +reading the file fails, but the error message just prints +`Should have been +able to read the file`. Reading a file can fail in a number of +ways: for example, the file could be missing, or we might not have permission to +open it. Right now, regardless of the situation, we’d print the same error +message for everything, which wouldn’t give the user any information! Fourth, we use `expect` to handle an error, and if the user runs our program without specifying enough arguments, they’ll get an `index out of bounds` error @@ -41,27 +42,27 @@ community has developed guidelines for splitting the separate concerns of a binary program when `main` starts getting large. This process has the following steps: -- Split your program into a _main.rs_ file and a _lib.rs_ file and move your +* Split your program into a _main.rs_ file and a _lib.rs_ file and move your program’s logic to _lib.rs_. -- As long as your command line parsing logic is small, it can remain in +* As long as your command line parsing logic is small, it can remain in _main.rs_. -- When the command line parsing logic starts getting complicated, extract it +* When the command line parsing logic starts getting complicated, extract it from _main.rs_ and move it to _lib.rs_. The responsibilities that remain in the `main` function after this process should be limited to the following: -- Calling the command line parsing logic with the argument values -- Setting up any other configuration -- Calling a `run` function in _lib.rs_ -- Handling the error if `run` returns an error +* Calling the command line parsing logic with the argument values +* Setting up any other configuration +* Calling a `run` function in _lib.rs_ +* Handling the error if `run` returns an error -This pattern is about separating concerns: _main.rs_ handles running the -program and _lib.rs_ handles all the logic of the task at hand. Because you -can’t test the `main` function directly, this structure lets you test all of -your program’s logic by moving it into functions in _lib.rs_. The code that -remains in _main.rs_ will be small enough to verify its correctness by reading -it. Let’s rework our program by following this process. +This pattern is about separating concerns: _main.rs_ handles running the program +and _lib.rs_ handles all the logic of the task at hand. Because you can’t test +the `main` function directly, this structure lets you test all of your program’s +logic by moving it into functions in _lib.rs_. The code that remains in +_main.rs_ will be small enough to verify its correctness by reading it. Let’s +rework our program by following this process. #### Extracting the Argument Parser @@ -79,14 +80,13 @@ function `parse_config`, which we’ll define in _src/main.rs_ for the moment. We’re still collecting the command line arguments into a vector, but instead of -assigning the argument value at index 1 to the variable `query` and the -argument value at index 2 to the variable `file_path` within the `main` -function, we pass the whole vector to the `parse_config` function. The -`parse_config` function then holds the logic that determines which argument -goes in which variable and passes the values back to `main`. We still create -the `query` and `file_path` variables in `main`, but `main` no longer has the -responsibility of determining how the command line arguments and variables -correspond. +assigning the argument value at index 1 to the variable `query` and the argument +value at index 2 to the variable `file_path` within the `main` function, we pass +the whole vector to the `parse_config` function. The `parse_config` function +then holds the logic that determines which argument goes in which variable and +passes the values back to `main`. We still create the `query` and `file_path` +variables in `main`, but `main` no longer has the responsibility of determining +how the command line arguments and variables correspond. This rework may seem like overkill for our small program, but we’re refactoring in small, incremental steps. After making this change, run the program again to @@ -96,9 +96,9 @@ often, to help identify the cause of problems when they occur. #### Grouping Configuration Values We can take another small step to improve the `parse_config` function further. -At the moment, we’re returning a tuple, but then we immediately break that -tuple into individual parts again. This is a sign that perhaps we don’t have -the right abstraction yet. +At the moment, we’re returning a tuple, but then we immediately break that tuple +into individual parts again. This is a sign that perhaps we don’t have the right +abstraction yet. Another indicator that shows there’s room for improvement is the `config` part of `parse_config`, which implies that the two values we return are related and @@ -121,20 +121,20 @@ Listing 12-6 shows the improvements to the `parse_config` function. We’ve added a struct named `Config` defined to have fields named `query` and `file_path`. The signature of `parse_config` now indicates that it returns a -`Config` value. In the body of `parse_config`, where we used to return -string slices that reference `String` values in `args`, we now define `Config` -to contain owned `String` values. The `args` variable in `main` is the owner of -the argument values and is only letting the `parse_config` function borrow -them, which means we’d violate Rust’s borrowing rules if `Config` tried to take +`Config` value. In the body of `parse_config`, where we used to return string +slices that reference `String` values in `args`, we now define `Config` to +contain owned `String` values. The `args` variable in `main` is the owner of the +argument values and is only letting the `parse_config` function borrow them, +which means we’d violate Rust’s borrowing rules if `Config` tried to take ownership of the values in `args`. There are a number of ways we could manage the `String` data; the easiest, though somewhat inefficient, route is to call the `clone` method on the values. This will make a full copy of the data for the `Config` instance to own, which -takes more time and memory than storing a reference to the string data. -However, cloning the data also makes our code very straightforward because we -don’t have to manage the lifetimes of the references; in this circumstance, -giving up a little performance to gain simplicity is a worthwhile trade-off. +takes more time and memory than storing a reference to the string data. However, +cloning the data also makes our code very straightforward because we don’t have +to manage the lifetimes of the references; in this circumstance, giving up a +little performance to gain simplicity is a worthwhile trade-off. > ### The Trade-Offs of Using `clone` > @@ -142,12 +142,12 @@ giving up a little performance to gain simplicity is a worthwhile trade-off. > ownership problems because of its runtime cost. In > [Chapter 13][ch13], you’ll learn how to use more efficient > methods in this type of situation. But for now, it’s okay to copy a few -> strings to continue making progress because you’ll make these copies only -> once and your file path and query string are very small. It’s better to have -> a working program that’s a bit inefficient than to try to hyperoptimize code -> on your first pass. As you become more experienced with Rust, it’ll be -> easier to start with the most efficient solution, but for now, it’s -> perfectly acceptable to call `clone`. +> strings to continue making progress because you’ll make these copies only once +> and your file path and query string are very small. It’s better to have a +> working program that’s a bit inefficient than to try to hyperoptimize code on +> your first pass. As you become more experienced with Rust, it’ll be easier to +> start with the most efficient solution, but for now, it’s perfectly acceptable +> to call `clone`. We’ve updated `main` so it places the instance of `Config` returned by `parse_config` into a variable named `config`, and we updated the code that @@ -155,9 +155,9 @@ previously used the separate `query` and `file_path` variables so it now uses the fields on the `Config` struct instead. Now our code more clearly conveys that `query` and `file_path` are related and -that their purpose is to configure how the program will work. Any code that -uses these values knows to find them in the `config` instance in the fields -named for their purpose. +that their purpose is to configure how the program will work. Any code that uses +these values knows to find them in the `config` instance in the fields named for +their purpose. #### Creating a Constructor for `Config` @@ -169,13 +169,13 @@ name the related purpose of `query` and `file_path` and to be able to return the values’ names as struct field names from the `parse_config` function. So now that the purpose of the `parse_config` function is to create a `Config` -instance, we can change `parse_config` from a plain function to a function -named `new` that is associated with the `Config` struct. Making this change -will make the code more idiomatic. We can create instances of types in the -standard library, such as `String`, by calling `String::new`. Similarly, by -changing `parse_config` into a `new` function associated with `Config`, we’ll -be able to create instances of `Config` by calling `Config::new`. Listing 12-7 -shows the changes we need to make. +instance, we can change `parse_config` from a plain function to a function named +`new` that is associated with the `Config` struct. Making this change will make +the code more idiomatic. We can create instances of types in the standard +library, such as `String`, by calling `String::new`. Similarly, by changing +`parse_config` into a `new` function associated with `Config`, we’ll be able to +create instances of `Config` by calling `Config::new`. Listing 12-7 shows the +changes we need to make. @@ -219,11 +219,12 @@ long enough, the program panics and displays a better error message. -This code is similar to [the `Guess::new` function we wrote in Listing +This code is similar to +[the `Guess::new` function we wrote in Listing 9-13][ch9-custom-types], where we called `panic!` when the -`value` argument was out of the range of valid values. Instead of checking for -a range of values here, we’re checking that the length of `args` is at least -`3` and the rest of the function can operate under the assumption that this +`value` argument was out of the range of valid values. Instead of checking for a +range of values here, we’re checking that the length of `args` is at least `3` +and the rest of the function can operate under the assumption that this condition has been met. If `args` has fewer than three items, this condition will be `true`, and we call the `panic!` macro to end the program immediately. @@ -238,9 +239,10 @@ This output is better: we now have a reasonable error message. However, we also have extraneous information we don’t want to give to our users. Perhaps the technique we used in Listing 9-13 isn’t the best one to use here: a call to `panic!` is more appropriate for a programming problem than a usage problem, -[as discussed in Chapter 9][ch9-error-guidelines]. Instead, -we’ll use the other technique you learned about in Chapter 9—[returning a -`Result`][ch9-result] that indicates either success or an error. +[as discussed in Chapter 9][ch9-error-guidelines]. Instead, we’ll +use the other technique you learned about in Chapter +9—[returning a `Result`][ch9-result] that indicates either +success or an error. @@ -250,12 +252,13 @@ we’ll use the other technique you learned about in Chapter 9—[returning a We can instead return a `Result` value that will contain a `Config` instance in the successful case and will describe the problem in the error case. We’re also -going to change the function name from `new` to `build` because many -programmers expect `new` functions to never fail. When `Config::build` is -communicating to `main`, we can use the `Result` type to signal there was a -problem. Then we can change `main` to convert an `Err` variant into a more -practical error for our users without the surrounding text about `thread -'main'` and `RUST_BACKTRACE` that a call to `panic!` causes. +going to change the function name from `new` to `build` because many programmers +expect `new` functions to never fail. When `Config::build` is communicating to +`main`, we can use the `Result` type to signal there was a problem. Then we can +change `main` to convert an `Err` variant into a more practical error for our +users without the surrounding text about `thread +'main'` and `RUST_BACKTRACE` +that a call to `panic!` causes. Listing 12-9 shows the changes we need to make to the return value of the function we’re now calling `Config::build` and the body of the function needed @@ -293,8 +296,8 @@ To handle the error case and print a user-friendly message, we need to update `main` to handle the `Result` being returned by `Config::build`, as shown in Listing 12-10. We’ll also take the responsibility of exiting the command line tool with a nonzero error code away from `panic!` and instead implement it by -hand. A nonzero exit status is a convention to signal to the process that -called our program that the program exited with an error state. +hand. A nonzero exit status is a convention to signal to the process that called +our program that the program exited with an error state. @@ -307,24 +310,24 @@ called our program that the program exited with an error state. In this listing, we’ve used a method we haven’t covered in detail yet: `unwrap_or_else`, which is defined on `Result` by the standard library. Using `unwrap_or_else` allows us to define some custom, non-`panic!` error -handling. If the `Result` is an `Ok` value, this method’s behavior is similar -to `unwrap`: it returns the inner value that `Ok` is wrapping. However, if the +handling. If the `Result` is an `Ok` value, this method’s behavior is similar to +`unwrap`: it returns the inner value that `Ok` is wrapping. However, if the value is an `Err` value, this method calls the code in the _closure_, which is an anonymous function we define and pass as an argument to `unwrap_or_else`. We’ll cover closures in more detail in [Chapter 13][ch13]. For now, you just need to know that `unwrap_or_else` will pass the inner value of -the `Err`, which in this case is the static string `"not enough arguments"` -that we added in Listing 12-9, to our closure in the argument `err` that -appears between the vertical pipes. The code in the closure can then use the -`err` value when it runs. +the `Err`, which in this case is the static string `"not enough arguments"` that +we added in Listing 12-9, to our closure in the argument `err` that appears +between the vertical pipes. The code in the closure can then use the `err` value +when it runs. We’ve added a new `use` line to bring `process` from the standard library into scope. The code in the closure that will be run in the error case is only two lines: we print the `err` value and then call `process::exit`. The -`process::exit` function will stop the program immediately and return the -number that was passed as the exit status code. This is similar to the -`panic!`-based handling we used in Listing 12-8, but we no longer get all the -extra output. Let’s try it: +`process::exit` function will stop the program immediately and return the number +that was passed as the exit status code. This is similar to the `panic!`-based +handling we used in Listing 12-8, but we no longer get all the extra output. +Let’s try it: ```console {{#include ../listings/ch12-an-io-project/listing-12-10/output.txt}} @@ -334,17 +337,18 @@ Great! This output is much friendlier for our users. ### Extracting Logic from `main` -Now that we’ve finished refactoring the configuration parsing, let’s turn to -the program’s logic. As we stated in [“Separation of Concerns for Binary +Now that we’ve finished refactoring the configuration parsing, let’s turn to the +program’s logic. As we stated in +[“Separation of Concerns for Binary Projects”](#separation-of-concerns-for-binary-projects), we’ll extract a function named `run` that will hold all the logic currently in the `main` function that isn’t involved with setting up configuration or handling errors. When we’re done, `main` will be concise and easy to verify by inspection, and we’ll be able to write tests for all the other logic. -Listing 12-11 shows the extracted `run` function. For now, we’re just making -the small, incremental improvement of extracting the function. We’re still -defining the function in _src/main.rs_. +Listing 12-11 shows the extracted `run` function. For now, we’re just making the +small, incremental improvement of extracting the function. We’re still defining +the function in _src/main.rs_. @@ -362,11 +366,11 @@ argument. With the remaining program logic separated into the `run` function, we can improve the error handling, as we did with `Config::build` in Listing 12-9. -Instead of allowing the program to panic by calling `expect`, the `run` -function will return a `Result` when something goes wrong. This will let -us further consolidate the logic around handling errors into `main` in a -user-friendly way. Listing 12-12 shows the changes we need to make to the -signature and body of `run`. +Instead of allowing the program to panic by calling `expect`, the `run` function +will return a `Result` when something goes wrong. This will let us further +consolidate the logic around handling errors into `main` in a user-friendly way. +Listing 12-12 shows the changes we need to make to the signature and body of +`run`. @@ -378,29 +382,29 @@ signature and body of `run`. We’ve made three significant changes here. First, we changed the return type of the `run` function to `Result<(), Box>`. This function previously -returned the unit type, `()`, and we keep that as the value returned in the -`Ok` case. +returned the unit type, `()`, and we keep that as the value returned in the `Ok` +case. For the error type, we used the _trait object_ `Box` (and we’ve -brought `std::error::Error` into scope with a `use` statement at the top). -We’ll cover trait objects in [Chapter 18][ch18]. For now, just -know that `Box` means the function will return a type that -implements the `Error` trait, but we don’t have to specify what particular type -the return value will be. This gives us flexibility to return error values that -may be of different types in different error cases. The `dyn` keyword is short -for _dynamic_. +brought `std::error::Error` into scope with a `use` statement at the top). We’ll +cover trait objects in [Chapter 18][ch18]. For now, just know +that `Box` means the function will return a type that implements the +`Error` trait, but we don’t have to specify what particular type the return +value will be. This gives us flexibility to return error values that may be of +different types in different error cases. The `dyn` keyword is short for +_dynamic_. Second, we’ve removed the call to `expect` in favor of the `?` operator, as we talked about in [Chapter 9][ch9-question-mark]. Rather than `panic!` on an error, `?` will return the error value from the current function for the caller to handle. -Third, the `run` function now returns an `Ok` value in the success case. -We’ve declared the `run` function’s success type as `()` in the signature, -which means we need to wrap the unit type value in the `Ok` value. This -`Ok(())` syntax might look a bit strange at first, but using `()` like this is -the idiomatic way to indicate that we’re calling `run` for its side effects -only; it doesn’t return a value we need. +Third, the `run` function now returns an `Ok` value in the success case. We’ve +declared the `run` function’s success type as `()` in the signature, which means +we need to wrap the unit type value in the `Ok` value. This `Ok(())` syntax +might look a bit strange at first, but using `()` like this is the idiomatic way +to indicate that we’re calling `run` for its side effects only; it doesn’t +return a value we need. When you run this code, it will compile but will display a warning: @@ -427,8 +431,8 @@ with `Config::build` in Listing 12-10, but with a slight difference: We use `if let` rather than `unwrap_or_else` to check whether `run` returns an `Err` value and to call `process::exit(1)` if it does. The `run` function doesn’t return a value that we want to `unwrap` in the same way that -`Config::build` returns the `Config` instance. Because `run` returns `()` in -the success case, we only care about detecting an error, so we don’t need +`Config::build` returns the `Config` instance. Because `run` returns `()` in the +success case, we only care about detecting an error, so we don’t need `unwrap_or_else` to return the unwrapped value, which would only be `()`. The bodies of the `if let` and the `unwrap_or_else` functions are the same in @@ -436,17 +440,17 @@ both cases: we print the error and exit. ### Splitting Code into a Library Crate -Our `minigrep` project is looking good so far! Now we’ll split the -_src/main.rs_ file and put some code into the _src/lib.rs_ file. That way, we -can test the code and have a _src/main.rs_ file with fewer responsibilities. +Our `minigrep` project is looking good so far! Now we’ll split the _src/main.rs_ +file and put some code into the _src/lib.rs_ file. That way, we can test the +code and have a _src/main.rs_ file with fewer responsibilities. Let’s move all the code that isn’t in the `main` function from _src/main.rs_ to _src/lib.rs_: -- The `run` function definition -- The relevant `use` statements -- The definition of `Config` -- The `Config::build` function definition +* The `run` function definition +* The relevant `use` statements +* The definition of `Config` +* The `Config::build` function definition The contents of _src/lib.rs_ should have the signatures shown in Listing 12-13 (we’ve omitted the bodies of the functions for brevity). Note that this won’t @@ -475,18 +479,18 @@ binary crate in _src/main.rs_, as shown in Listing 12-14. -We add a `use minigrep::Config` line to bring the `Config` type from the -library crate into the binary crate’s scope, and we prefix the `run` function -with our crate name. Now all the functionality should be connected and should -work. Run the program with `cargo run` and make sure everything works correctly. +We add a `use minigrep::Config` line to bring the `Config` type from the library +crate into the binary crate’s scope, and we prefix the `run` function with our +crate name. Now all the functionality should be connected and should work. Run +the program with `cargo run` and make sure everything works correctly. Whew! That was a lot of work, but we’ve set ourselves up for success in the future. Now it’s much easier to handle errors, and we’ve made the code more modular. Almost all of our work will be done in _src/lib.rs_ from here on out. Let’s take advantage of this newfound modularity by doing something that would -have been difficult with the old code but is easy with the new code: we’ll -write some tests! +have been difficult with the old code but is easy with the new code: we’ll write +some tests! [ch13]: ch13-00-functional-features.html [ch9-custom-types]: ch09-03-to-panic-or-not-to-panic.html#creating-custom-types-for-validation diff --git a/src/ch12-04-testing-the-librarys-functionality.md b/src/ch12-04-testing-the-librarys-functionality.md index 92dac84aaf..58b5242039 100644 --- a/src/ch12-04-testing-the-librarys-functionality.md +++ b/src/ch12-04-testing-the-librarys-functionality.md @@ -3,8 +3,8 @@ Now that we’ve extracted the logic into _src/lib.rs_ and left the argument collecting and error handling in _src/main.rs_, it’s much easier to write tests for the core functionality of our code. We can call functions directly with -various arguments and check return values without having to call our binary -from the command line. +various arguments and check return values without having to call our binary from +the command line. In this section, we’ll add the searching logic to the `minigrep` program using the test-driven development (TDD) process with the following steps: @@ -31,9 +31,9 @@ Because we don’t need them anymore, let’s remove the `println!` statements f _src/lib.rs_ and _src/main.rs_ that we used to check the program’s behavior. Then, in _src/lib.rs_, we’ll add a `tests` module with a test function, as we did in [Chapter 11][ch11-anatomy]. The test function specifies -the behavior we want the `search` function to have: it will take a query and -the text to search, and it will return only the lines from the text that -contain the query. Listing 12-15 shows this test, which won’t compile yet. +the behavior we want the `search` function to have: it will take a query and the +text to search, and it will return only the lines from the text that contain the +query. Listing 12-15 shows this test, which won’t compile yet. @@ -53,8 +53,9 @@ We aren’t yet able to run this test and watch it fail because the test doesn even compile: the `search` function doesn’t exist yet! In accordance with TDD principles, we’ll add just enough code to get the test to compile and run by adding a definition of the `search` function that always returns an empty -vector, as shown in Listing 12-16. Then the test should compile and fail -because an empty vector doesn’t match a vector containing the line `"safe, +vector, as shown in Listing 12-16. Then the test should compile and fail because +an empty vector doesn’t match a vector containing the line +`"safe, fast, productive."` @@ -95,9 +96,10 @@ syntax. Other programming languages don’t require you to connect arguments to return values in the signature, but this practice will get easier over time. You might -want to compare this example with the examples in the [“Validating References -with Lifetimes”][validating-references-with-lifetimes] section -in Chapter 10. +want to compare this example with the examples in the +[“Validating References +with Lifetimes”][validating-references-with-lifetimes] section in +Chapter 10. Now let’s run the test: @@ -123,8 +125,8 @@ Let’s work through each step, starting with iterating through lines. #### Iterating Through Lines with the `lines` Method Rust has a helpful method to handle line-by-line iteration of strings, -conveniently named `lines`, that works as shown in Listing 12-17. Note that -this won’t compile yet. +conveniently named `lines`, that works as shown in Listing 12-17. Note that this +won’t compile yet. @@ -135,8 +137,8 @@ this won’t compile yet. The `lines` method returns an iterator. We’ll talk about iterators in depth in -[Chapter 13][ch13-iterators], but recall that you saw this way -of using an iterator in [Listing 3-5][ch3-iter], where we used a +[Chapter 13][ch13-iterators], but recall that you saw this way of +using an iterator in [Listing 3-5][ch3-iter], where we used a `for` loop with an iterator to run some code on each item in a collection. #### Searching Each Line for the Query @@ -161,9 +163,9 @@ signature. #### Storing Matching Lines To finish this function, we need a way to store the matching lines that we want -to return. For that, we can make a mutable vector before the `for` loop and -call the `push` method to store a `line` in the vector. After the `for` loop, -we return the vector, as shown in Listing 12-19. +to return. For that, we can make a mutable vector before the `for` loop and call +the `push` method to store a `line` in the vector. After the `for` loop, we +return the vector, as shown in Listing 12-19. @@ -173,8 +175,8 @@ we return the vector, as shown in Listing 12-19. -Now the `search` function should return only the lines that contain `query`, -and our test should pass. Let’s run the test: +Now the `search` function should return only the lines that contain `query`, and +our test should pass. Let’s run the test: ```console {{#include ../listings/ch12-an-io-project/listing-12-19/output.txt}} @@ -185,9 +187,9 @@ Our test passed, so we know it works! At this point, we could consider opportunities for refactoring the implementation of the search function while keeping the tests passing to maintain the same functionality. The code in the search function isn’t too bad, -but it doesn’t take advantage of some useful features of iterators. We’ll -return to this example in [Chapter 13][ch13-iterators], where -we’ll explore iterators in detail, and look at how to improve it. +but it doesn’t take advantage of some useful features of iterators. We’ll return +to this example in [Chapter 13][ch13-iterators], where we’ll +explore iterators in detail, and look at how to improve it. #### Using the `search` Function in the `run` Function diff --git a/src/ch12-05-working-with-environment-variables.md b/src/ch12-05-working-with-environment-variables.md index b91b00ba8a..34ca3ace47 100644 --- a/src/ch12-05-working-with-environment-variables.md +++ b/src/ch12-05-working-with-environment-variables.md @@ -11,10 +11,10 @@ and have all their searches be case insensitive in that terminal session. We first add a new `search_case_insensitive` function that will be called when the environment variable has a value. We’ll continue to follow the TDD process, -so the first step is again to write a failing test. We’ll add a new test for -the new `search_case_insensitive` function and rename our old test from -`one_result` to `case_sensitive` to clarify the differences between the two -tests, as shown in Listing 12-20. +so the first step is again to write a failing test. We’ll add a new test for the +new `search_case_insensitive` function and rename our old test from `one_result` +to `case_sensitive` to clarify the differences between the two tests, as shown +in Listing 12-20. @@ -33,12 +33,12 @@ and should continue to pass as we work on the case-insensitive search. The new test for the case-_insensitive_ search uses `"rUsT"` as its query. In the `search_case_insensitive` function we’re about to add, the query `"rUsT"` -should match the line containing `"Rust:"` with a capital _R_ and match the -line `"Trust me."` even though both have different casing from the query. This -is our failing test, and it will fail to compile because we haven’t yet defined -the `search_case_insensitive` function. Feel free to add a skeleton -implementation that always returns an empty vector, similar to the way we did -for the `search` function in Listing 12-16 to see the test compile and fail. +should match the line containing `"Rust:"` with a capital _R_ and match the line +`"Trust me."` even though both have different casing from the query. This is our +failing test, and it will fail to compile because we haven’t yet defined the +`search_case_insensitive` function. Feel free to add a skeleton implementation +that always returns an empty vector, similar to the way we did for the `search` +function in Listing 12-16 to see the test compile and fail. ### Implementing the `search_case_insensitive` Function @@ -73,8 +73,8 @@ need to add an ampersand because the signature of `contains` is defined to take a string slice. Next, we add a call to `to_lowercase` on each `line` to lowercase all -characters. Now that we’ve converted `line` and `query` to lowercase, we’ll -find matches no matter what the case of the query is. +characters. Now that we’ve converted `line` and `query` to lowercase, we’ll find +matches no matter what the case of the query is. Let’s see if this implementation passes the tests: @@ -84,8 +84,8 @@ Let’s see if this implementation passes the tests: Great! They passed. Now, let’s call the new `search_case_insensitive` function from the `run` function. First we’ll add a configuration option to the `Config` -struct to switch between case-sensitive and case-insensitive search. Adding -this field will cause compiler errors because we aren’t initializing this field +struct to switch between case-sensitive and case-insensitive search. Adding this +field will cause compiler errors because we aren’t initializing this field anywhere yet: Filename: src/lib.rs @@ -95,9 +95,9 @@ anywhere yet: ``` We added the `ignore_case` field that holds a Boolean. Next, we need the `run` -function to check the `ignore_case` field’s value and use that to decide -whether to call the `search` function or the `search_case_insensitive` -function, as shown in Listing 12-22. This still won’t compile yet. +function to check the `ignore_case` field’s value and use that to decide whether +to call the `search` function or the `search_case_insensitive` function, as +shown in Listing 12-22. This still won’t compile yet. @@ -124,18 +124,18 @@ Listing 12-23. Here, we create a new variable, `ignore_case`. To set its value, we call the `env::var` function and pass it the name of the `IGNORE_CASE` environment -variable. The `env::var` function returns a `Result` that will be the -successful `Ok` variant that contains the value of the environment variable if -the environment variable is set to any value. It will return the `Err` variant -if the environment variable is not set. +variable. The `env::var` function returns a `Result` that will be the successful +`Ok` variant that contains the value of the environment variable if the +environment variable is set to any value. It will return the `Err` variant if +the environment variable is not set. We’re using the `is_ok` method on the `Result` to check whether the environment -variable is set, which means the program should do a case-insensitive search. -If the `IGNORE_CASE` environment variable isn’t set to anything, `is_ok` will +variable is set, which means the program should do a case-insensitive search. If +the `IGNORE_CASE` environment variable isn’t set to anything, `is_ok` will return `false` and the program will perform a case-sensitive search. We don’t care about the _value_ of the environment variable, just whether it’s set or -unset, so we’re checking `is_ok` rather than using `unwrap`, `expect`, or any -of the other methods we’ve seen on `Result`. +unset, so we’re checking `is_ok` rather than using `unwrap`, `expect`, or any of +the other methods we’ve seen on `Result`. We pass the value in the `ignore_case` variable to the `Config` instance so the `run` function can read that value and decide whether to call @@ -149,8 +149,8 @@ the word _to_ in all lowercase: {{#include ../listings/ch12-an-io-project/listing-12-23/output.txt}} ``` -Looks like that still works! Now let’s run the program with `IGNORE_CASE` set -to `1` but with the same query _to_: +Looks like that still works! Now let’s run the program with `IGNORE_CASE` set to +`1` but with the same query _to_: ```console $ IGNORE_CASE=1 cargo run -- to poem.txt @@ -163,8 +163,8 @@ run the program as separate commands: PS> $Env:IGNORE_CASE=1; cargo run -- to poem.txt ``` -This will make `IGNORE_CASE` persist for the remainder of your shell session. -It can be unset with the `Remove-Item` cmdlet: +This will make `IGNORE_CASE` persist for the remainder of your shell session. It +can be unset with the `Remove-Item` cmdlet: ```console PS> Remove-Item Env:IGNORE_CASE diff --git a/src/ch12-06-writing-to-stderr-instead-of-stdout.md b/src/ch12-06-writing-to-stderr-instead-of-stdout.md index 8113abf1f6..c989ed8d06 100644 --- a/src/ch12-06-writing-to-stderr-instead-of-stdout.md +++ b/src/ch12-06-writing-to-stderr-instead-of-stdout.md @@ -42,8 +42,8 @@ Problem parsing arguments: not enough arguments ``` Yup, our error message is being printed to standard output. It’s much more -useful for error messages like this to be printed to standard error so only -data from a successful run ends up in the file. We’ll change that. +useful for error messages like this to be printed to standard error so only data +from a successful run ends up in the file. We’ll change that. ### Printing Errors to Standard Error @@ -90,8 +90,8 @@ Are you nobody, too? How dreary to be somebody! ``` -This demonstrates that we’re now using standard output for successful output -and standard error for error output as appropriate. +This demonstrates that we’re now using standard output for successful output and +standard error for error output as appropriate. ## Summary diff --git a/src/ch13-00-functional-features.md b/src/ch13-00-functional-features.md index 8eeccf2b24..1c42406233 100644 --- a/src/ch13-00-functional-features.md +++ b/src/ch13-00-functional-features.md @@ -1,10 +1,10 @@ # Functional Language Features: Iterators and Closures -Rust’s design has taken inspiration from many existing languages and -techniques, and one significant influence is _functional programming_. -Programming in a functional style often includes using functions as values by -passing them in arguments, returning them from other functions, assigning them -to variables for later execution, and so forth. +Rust’s design has taken inspiration from many existing languages and techniques, +and one significant influence is _functional programming_. Programming in a +functional style often includes using functions as values by passing them in +arguments, returning them from other functions, assigning them to variables for +later execution, and so forth. In this chapter, we won’t debate the issue of what functional programming is or isn’t but will instead discuss some features of Rust that are similar to @@ -12,10 +12,10 @@ features in many languages often referred to as functional. More specifically, we’ll cover: -- _Closures_, a function-like construct you can store in a variable -- _Iterators_, a way of processing a series of elements -- How to use closures and iterators to improve the I/O project in Chapter 12 -- The performance of closures and iterators (Spoiler alert: they’re faster than +* _Closures_, a function-like construct you can store in a variable +* _Iterators_, a way of processing a series of elements +* How to use closures and iterators to improve the I/O project in Chapter 12 +* The performance of closures and iterators (Spoiler alert: they’re faster than you might think!) We’ve already covered some other Rust features, such as pattern matching and diff --git a/src/ch13-01-closures.md b/src/ch13-01-closures.md index 3f4e50cfe5..99be83c7df 100644 --- a/src/ch13-01-closures.md +++ b/src/ch13-01-closures.md @@ -23,19 +23,19 @@ We’ll first examine how we can use closures to capture values from the environment they’re defined in for later use. Here’s the scenario: Every so often, our t-shirt company gives away an exclusive, limited-edition shirt to someone on our mailing list as a promotion. People on the mailing list can -optionally add their favorite color to their profile. If the person chosen for -a free shirt has their favorite color set, they get that color shirt. If the +optionally add their favorite color to their profile. If the person chosen for a +free shirt has their favorite color set, they get that color shirt. If the person hasn’t specified a favorite color, they get whatever color the company currently has the most of. There are many ways to implement this. For this example, we’re going to use an enum called `ShirtColor` that has the variants `Red` and `Blue` (limiting the -number of colors available for simplicity). We represent the company’s -inventory with an `Inventory` struct that has a field named `shirts` that -contains a `Vec` representing the shirt colors currently in stock. -The method `giveaway` defined on `Inventory` gets the optional shirt -color preference of the free shirt winner, and returns the shirt color the -person will get. This setup is shown in Listing 13-1: +number of colors available for simplicity). We represent the company’s inventory +with an `Inventory` struct that has a field named `shirts` that contains a +`Vec` representing the shirt colors currently in stock. The method +`giveaway` defined on `Inventory` gets the optional shirt color preference of +the free shirt winner, and returns the shirt color the person will get. This +setup is shown in Listing 13-1: @@ -45,29 +45,29 @@ person will get. This setup is shown in Listing 13-1: -The `store` defined in `main` has two blue shirts and one red shirt remaining -to distribute for this limited-edition promotion. We call the `giveaway` method -for a user with a preference for a red shirt and a user without any preference. +The `store` defined in `main` has two blue shirts and one red shirt remaining to +distribute for this limited-edition promotion. We call the `giveaway` method for +a user with a preference for a red shirt and a user without any preference. Again, this code could be implemented in many ways, and here, to focus on closures, we’ve stuck to concepts you’ve already learned except for the body of the `giveaway` method that uses a closure. In the `giveaway` method, we get the user preference as a parameter of type `Option` and call the -`unwrap_or_else` method on `user_preference`. The [`unwrap_or_else` method on -`Option`][unwrap-or-else] is defined by the standard library. -It takes one argument: a closure without any arguments that returns a value `T` -(the same type stored in the `Some` variant of the `Option`, in this case -`ShirtColor`). If the `Option` is the `Some` variant, `unwrap_or_else` -returns the value from within the `Some`. If the `Option` is the `None` -variant, `unwrap_or_else` calls the closure and returns the value returned by -the closure. +`unwrap_or_else` method on `user_preference`. The +[`unwrap_or_else` method on `Option`][unwrap-or-else] is +defined by the standard library. It takes one argument: a closure without any +arguments that returns a value `T` (the same type stored in the `Some` variant +of the `Option`, in this case `ShirtColor`). If the `Option` is the `Some` +variant, `unwrap_or_else` returns the value from within the `Some`. If the +`Option` is the `None` variant, `unwrap_or_else` calls the closure and +returns the value returned by the closure. We specify the closure expression `|| self.most_stocked()` as the argument to `unwrap_or_else`. This is a closure that takes no parameters itself (if the closure had parameters, they would appear between the two vertical bars). The body of the closure calls `self.most_stocked()`. We’re defining the closure -here, and the implementation of `unwrap_or_else` will evaluate the closure -later if the result is needed. +here, and the implementation of `unwrap_or_else` will evaluate the closure later +if the result is needed. Running this code prints: @@ -117,9 +117,9 @@ argument as we did in Listing 13-1. With type annotations added, the syntax of closures looks more similar to the syntax of functions. Here we define a function that adds 1 to its parameter and -a closure that has the same behavior, for comparison. We’ve added some spaces -to line up the relevant parts. This illustrates how closure syntax is similar -to function syntax except for the use of pipes and the amount of syntax that is +a closure that has the same behavior, for comparison. We’ve added some spaces to +line up the relevant parts. This illustrates how closure syntax is similar to +function syntax except for the use of pipes and the amount of syntax that is optional: ```rust,ignore @@ -169,15 +169,14 @@ error when we next try to use a different type with the same closure. ### Capturing References or Moving Ownership -Closures can capture values from their environment in three ways, which -directly map to the three ways a function can take a parameter: borrowing -immutably, borrowing mutably, and taking ownership. The closure will decide -which of these to use based on what the body of the function does with the -captured values. +Closures can capture values from their environment in three ways, which directly +map to the three ways a function can take a parameter: borrowing immutably, +borrowing mutably, and taking ownership. The closure will decide which of these +to use based on what the body of the function does with the captured values. -In Listing 13-4, we define a closure that captures an immutable reference to -the vector named `list` because it only needs an immutable reference to print -the value: +In Listing 13-4, we define a closure that captures an immutable reference to the +vector named `list` because it only needs an immutable reference to print the +value: @@ -219,22 +218,22 @@ This code compiles, runs, and prints: Note that there’s no longer a `println!` between the definition and the call of the `borrows_mutably` closure: when `borrows_mutably` is defined, it captures a -mutable reference to `list`. We don’t use the closure again after the closure -is called, so the mutable borrow ends. Between the closure definition and the +mutable reference to `list`. We don’t use the closure again after the closure is +called, so the mutable borrow ends. Between the closure definition and the closure call, an immutable borrow to print isn’t allowed because no other -borrows are allowed when there’s a mutable borrow. Try adding a `println!` -there to see what error message you get! +borrows are allowed when there’s a mutable borrow. Try adding a `println!` there +to see what error message you get! If you want to force the closure to take ownership of the values it uses in the -environment even though the body of the closure doesn’t strictly need -ownership, you can use the `move` keyword before the parameter list. +environment even though the body of the closure doesn’t strictly need ownership, +you can use the `move` keyword before the parameter list. This technique is mostly useful when passing a closure to a new thread to move -the data so that it’s owned by the new thread. We’ll discuss threads and why -you would want to use them in detail in Chapter 16 when we talk about -concurrency, but for now, let’s briefly explore spawning a new thread using a -closure that needs the `move` keyword. Listing 13-6 shows Listing 13-4 modified -to print the vector in a new thread rather than in the main thread: +the data so that it’s owned by the new thread. We’ll discuss threads and why you +would want to use them in detail in Chapter 16 when we talk about concurrency, +but for now, let’s briefly explore spawning a new thread using a closure that +needs the `move` keyword. Listing 13-6 shows Listing 13-4 modified to print the +vector in a new thread rather than in the main thread: @@ -246,10 +245,10 @@ to print the vector in a new thread rather than in the main thread: We spawn a new thread, giving the thread a closure to run as an argument. The closure body prints out the list. In Listing 13-4, the closure only captured -`list` using an immutable reference because that's the least amount of access -to `list` needed to print it. In this example, even though the closure body -still only needs an immutable reference, we need to specify that `list` should -be moved into the closure by putting the `move` keyword at the beginning of the +`list` using an immutable reference because that's the least amount of access to +`list` needed to print it. In this example, even though the closure body still +only needs an immutable reference, we need to specify that `list` should be +moved into the closure by putting the `move` keyword at the beginning of the closure definition. The new thread might finish before the rest of the main thread finishes, or the main thread might finish first. If the main thread maintained ownership of `list` but ended before the new thread did and dropped @@ -276,19 +275,19 @@ do any of the following: move a captured value out of the closure, mutate the captured value, neither move nor mutate the value, or capture nothing from the environment to begin with. -The way a closure captures and handles values from the environment affects -which traits the closure implements, and traits are how functions and structs -can specify what kinds of closures they can use. Closures will automatically +The way a closure captures and handles values from the environment affects which +traits the closure implements, and traits are how functions and structs can +specify what kinds of closures they can use. Closures will automatically implement one, two, or all three of these `Fn` traits, in an additive fashion, depending on how the closure’s body handles the values: 1. `FnOnce` applies to closures that can be called once. All closures implement - at least this trait, because all closures can be called. A closure that - moves captured values out of its body will only implement `FnOnce` and none - of the other `Fn` traits, because it can only be called once. + at least this trait, because all closures can be called. A closure that moves + captured values out of its body will only implement `FnOnce` and none of the + other `Fn` traits, because it can only be called once. 2. `FnMut` applies to closures that don’t move captured values out of their - body, but that might mutate the captured values. These closures can be - called more than once. + body, but that might mutate the captured values. These closures can be called + more than once. 3. `Fn` applies to closures that don’t move captured values out of their body and that don’t mutate captured values, as well as closures that capture nothing from their environment. These closures can be called more than once @@ -318,17 +317,17 @@ Recall that `T` is the generic type representing the type of the value in the `Option`, for example, will get a `String`. Next, notice that the `unwrap_or_else` function has the additional generic type -parameter `F`. The `F` type is the type of the parameter named `f`, which is -the closure we provide when calling `unwrap_or_else`. +parameter `F`. The `F` type is the type of the parameter named `f`, which is the +closure we provide when calling `unwrap_or_else`. The trait bound specified on the generic type `F` is `FnOnce() -> T`, which means `F` must be able to be called once, take no arguments, and return a `T`. -Using `FnOnce` in the trait bound expresses the constraint that -`unwrap_or_else` is only going to call `f` at most one time. In the body of -`unwrap_or_else`, we can see that if the `Option` is `Some`, `f` won’t be -called. If the `Option` is `None`, `f` will be called once. Because all -closures implement `FnOnce`, `unwrap_or_else` accepts all three kinds of -closures and is as flexible as it can be. +Using `FnOnce` in the trait bound expresses the constraint that `unwrap_or_else` +is only going to call `f` at most one time. In the body of `unwrap_or_else`, we +can see that if the `Option` is `Some`, `f` won’t be called. If the `Option` is +`None`, `f` will be called once. Because all closures implement `FnOnce`, +`unwrap_or_else` accepts all three kinds of closures and is as flexible as it +can be. > Note: Functions can implement all three of the `Fn` traits too. If what we > want to do doesn’t require capturing a value from the environment, we can use @@ -338,13 +337,13 @@ closures and is as flexible as it can be. > value is `None`. Now let’s look at the standard library method `sort_by_key` defined on slices, -to see how that differs from `unwrap_or_else` and why `sort_by_key` uses -`FnMut` instead of `FnOnce` for the trait bound. The closure gets one argument -in the form of a reference to the current item in the slice being considered, -and returns a value of type `K` that can be ordered. This function is useful -when you want to sort a slice by a particular attribute of each item. In -Listing 13-7, we have a list of `Rectangle` instances and we use `sort_by_key` -to order them by their `width` attribute from low to high: +to see how that differs from `unwrap_or_else` and why `sort_by_key` uses `FnMut` +instead of `FnOnce` for the trait bound. The closure gets one argument in the +form of a reference to the current item in the slice being considered, and +returns a value of type `K` that can be ordered. This function is useful when +you want to sort a slice by a particular attribute of each item. In Listing +13-7, we have a list of `Rectangle` instances and we use `sort_by_key` to order +them by their `width` attribute from low to high: @@ -361,13 +360,14 @@ This code prints: ``` The reason `sort_by_key` is defined to take an `FnMut` closure is that it calls -the closure multiple times: once for each item in the slice. The closure `|r| -r.width` doesn’t capture, mutate, or move out anything from its environment, so -it meets the trait bound requirements. +the closure multiple times: once for each item in the slice. The closure +`|r| +r.width` doesn’t capture, mutate, or move out anything from its environment, +so it meets the trait bound requirements. -In contrast, Listing 13-8 shows an example of a closure that implements just -the `FnOnce` trait, because it moves a value out of the environment. The -compiler won’t let us use this closure with `sort_by_key`: +In contrast, Listing 13-8 shows an example of a closure that implements just the +`FnOnce` trait, because it moves a value out of the environment. The compiler +won’t let us use this closure with `sort_by_key`: @@ -380,14 +380,14 @@ compiler won’t let us use this closure with `sort_by_key`: This is a contrived, convoluted way (that doesn’t work) to try and count the number of times `sort_by_key` calls the closure when sorting `list`. This code attempts to do this counting by pushing `value`—a `String` from the closure’s -environment—into the `sort_operations` vector. The closure captures `value` -then moves `value` out of the closure by transferring ownership of `value` to -the `sort_operations` vector. This closure can be called once; trying to call -it a second time wouldn’t work because `value` would no longer be in the -environment to be pushed into `sort_operations` again! Therefore, this closure -only implements `FnOnce`. When we try to compile this code, we get this error -that `value` can’t be moved out of the closure because the closure must -implement `FnMut`: +environment—into the `sort_operations` vector. The closure captures `value` then +moves `value` out of the closure by transferring ownership of `value` to the +`sort_operations` vector. This closure can be called once; trying to call it a +second time wouldn’t work because `value` would no longer be in the environment +to be pushed into `sort_operations` again! Therefore, this closure only +implements `FnOnce`. When we try to compile this code, we get this error that +`value` can’t be moved out of the closure because the closure must implement +`FnMut`: ```console {{#include ../listings/ch13-functional-features/listing-13-08/output.txt}} @@ -395,10 +395,10 @@ implement `FnMut`: The error points to the line in the closure body that moves `value` out of the environment. To fix this, we need to change the closure body so that it doesn’t -move values out of the environment. To count the number of times the closure -is called, keeping a counter in the environment and incrementing its value in -the closure body is a more straightforward way to calculate that. The closure -in Listing 13-9 works with `sort_by_key` because it is only capturing a mutable +move values out of the environment. To count the number of times the closure is +called, keeping a counter in the environment and incrementing its value in the +closure body is a more straightforward way to calculate that. The closure in +Listing 13-9 works with `sort_by_key` because it is only capturing a mutable reference to the `num_sort_operations` counter and can therefore be called more than once: diff --git a/src/ch13-02-iterators.md b/src/ch13-02-iterators.md index 65573f114c..49de68269a 100644 --- a/src/ch13-02-iterators.md +++ b/src/ch13-02-iterators.md @@ -6,9 +6,9 @@ determining when the sequence has finished. When you use iterators, you don’t have to reimplement that logic yourself. In Rust, iterators are _lazy_, meaning they have no effect until you call -methods that consume the iterator to use it up. For example, the code in -Listing 13-10 creates an iterator over the items in the vector `v1` by calling -the `iter` method defined on `Vec`. This code by itself doesn’t do anything +methods that consume the iterator to use it up. For example, the code in Listing +13-10 creates an iterator over the items in the vector `v1` by calling the +`iter` method defined on `Vec`. This code by itself doesn’t do anything useful. @@ -22,13 +22,13 @@ useful. The iterator is stored in the `v1_iter` variable. Once we’ve created an iterator, we can use it in a variety of ways. In Listing 3-5 in Chapter 3, we iterated over an array using a `for` loop to execute some code on each of its -items. Under the hood this implicitly created and then consumed an iterator, -but we glossed over how exactly that works until now. +items. Under the hood this implicitly created and then consumed an iterator, but +we glossed over how exactly that works until now. In the example in Listing 13-11, we separate the creation of the iterator from the use of the iterator in the `for` loop. When the `for` loop is called using -the iterator in `v1_iter`, each element in the iterator is used in one -iteration of the loop, which prints out each value. +the iterator in `v1_iter`, each element in the iterator is used in one iteration +of the loop, which prints out each value. @@ -38,11 +38,11 @@ iteration of the loop, which prints out each value. -In languages that don’t have iterators provided by their standard libraries, -you would likely write this same functionality by starting a variable at index -0, using that variable to index into the vector to get a value, and -incrementing the variable value in a loop until it reached the total number of -items in the vector. +In languages that don’t have iterators provided by their standard libraries, you +would likely write this same functionality by starting a variable at index 0, +using that variable to index into the vector to get a value, and incrementing +the variable value in a loop until it reached the total number of items in the +vector. Iterators handle all that logic for you, cutting down on repetitive code you could potentially mess up. Iterators give you more flexibility to use the same @@ -51,8 +51,8 @@ index into, like vectors. Let’s examine how iterators do that. ### The `Iterator` Trait and the `next` Method -All iterators implement a trait named `Iterator` that is defined in the -standard library. The definition of the trait looks like this: +All iterators implement a trait named `Iterator` that is defined in the standard +library. The definition of the trait looks like this: ```rust pub trait Iterator { @@ -64,17 +64,16 @@ pub trait Iterator { } ``` -Notice this definition uses some new syntax: `type Item` and `Self::Item`, -which are defining an _associated type_ with this trait. We’ll talk about -associated types in depth in Chapter 20. For now, all you need to know is that -this code says implementing the `Iterator` trait requires that you also define -an `Item` type, and this `Item` type is used in the return type of the `next` -method. In other words, the `Item` type will be the type returned from the -iterator. +Notice this definition uses some new syntax: `type Item` and `Self::Item`, which +are defining an _associated type_ with this trait. We’ll talk about associated +types in depth in Chapter 20. For now, all you need to know is that this code +says implementing the `Iterator` trait requires that you also define an `Item` +type, and this `Item` type is used in the return type of the `next` method. In +other words, the `Item` type will be the type returned from the iterator. -The `Iterator` trait only requires implementors to define one method: the -`next` method, which returns one item of the iterator at a time wrapped in -`Some` and, when iteration is over, returns `None`. +The `Iterator` trait only requires implementors to define one method: the `next` +method, which returns one item of the iterator at a time wrapped in `Some` and, +when iteration is over, returns `None`. We can call the `next` method on iterators directly; Listing 13-12 demonstrates what values are returned from repeated calls to `next` on the iterator created @@ -89,18 +88,18 @@ from the vector. Note that we needed to make `v1_iter` mutable: calling the `next` method on an -iterator changes internal state that the iterator uses to keep track of where -it is in the sequence. In other words, this code _consumes_, or uses up, the +iterator changes internal state that the iterator uses to keep track of where it +is in the sequence. In other words, this code _consumes_, or uses up, the iterator. Each call to `next` eats up an item from the iterator. We didn’t need to make `v1_iter` mutable when we used a `for` loop because the loop took ownership of `v1_iter` and made it mutable behind the scenes. Also note that the values we get from the calls to `next` are immutable references to the values in the vector. The `iter` method produces an iterator -over immutable references. If we want to create an iterator that takes -ownership of `v1` and returns owned values, we can call `into_iter` instead of -`iter`. Similarly, if we want to iterate over mutable references, we can call -`iter_mut` instead of `iter`. +over immutable references. If we want to create an iterator that takes ownership +of `v1` and returns owned values, we can call `into_iter` instead of `iter`. +Similarly, if we want to iterate over mutable references, we can call `iter_mut` +instead of `iter`. ### Methods that Consume the Iterator @@ -115,8 +114,8 @@ Methods that call `next` are called _consuming adapters_, because calling them uses up the iterator. One example is the `sum` method, which takes ownership of the iterator and iterates through the items by repeatedly calling `next`, thus consuming the iterator. As it iterates through, it adds each item to a running -total and returns the total when iteration is complete. Listing 13-13 has a -test illustrating a use of the `sum` method: +total and returns the total when iteration is complete. Listing 13-13 has a test +illustrating a use of the `sum` method: @@ -132,8 +131,8 @@ ownership of the iterator we call it on. ### Methods that Produce Other Iterators _Iterator adapters_ are methods defined on the `Iterator` trait that don’t -consume the iterator. Instead, they produce different iterators by changing -some aspect of the original iterator. +consume the iterator. Instead, they produce different iterators by changing some +aspect of the original iterator. Listing 13-14 shows an example of calling the iterator adapter method `map`, which takes a closure to call on each item as the items are iterated through. @@ -155,9 +154,9 @@ However, this code produces a warning: {{#include ../listings/ch13-functional-features/listing-13-14/output.txt}} ``` -The code in Listing 13-14 doesn’t do anything; the closure we’ve specified -never gets called. The warning reminds us why: iterator adapters are lazy, and -we need to consume the iterator here. +The code in Listing 13-14 doesn’t do anything; the closure we’ve specified never +gets called. The warning reminds us why: iterator adapters are lazy, and we need +to consume the iterator here. To fix this warning and consume the iterator, we’ll use the `collect` method, which we used in Chapter 12 with `env::args` in Listing 12-1. This method @@ -212,15 +211,14 @@ The `shoes_in_size` function takes ownership of a vector of shoes and a shoe size as parameters. It returns a vector containing only shoes of the specified size. -In the body of `shoes_in_size`, we call `into_iter` to create an iterator -that takes ownership of the vector. Then we call `filter` to adapt that -iterator into a new iterator that only contains elements for which the closure -returns `true`. +In the body of `shoes_in_size`, we call `into_iter` to create an iterator that +takes ownership of the vector. Then we call `filter` to adapt that iterator into +a new iterator that only contains elements for which the closure returns `true`. -The closure captures the `shoe_size` parameter from the environment and -compares the value with each shoe’s size, keeping only shoes of the size -specified. Finally, calling `collect` gathers the values returned by the -adapted iterator into a vector that’s returned by the function. +The closure captures the `shoe_size` parameter from the environment and compares +the value with each shoe’s size, keeping only shoes of the size specified. +Finally, calling `collect` gathers the values returned by the adapted iterator +into a vector that’s returned by the function. -The test shows that when we call `shoes_in_size`, we get back only shoes -that have the same size as the value we specified. +The test shows that when we call `shoes_in_size`, we get back only shoes that +have the same size as the value we specified. diff --git a/src/ch13-03-improving-our-io-project.md b/src/ch13-03-improving-our-io-project.md index 489bdc10fa..bf5d150c6f 100644 --- a/src/ch13-03-improving-our-io-project.md +++ b/src/ch13-03-improving-our-io-project.md @@ -10,8 +10,8 @@ concise. Let’s look at how iterators can improve our implementation of the In Listing 12-6, we added code that took a slice of `String` values and created an instance of the `Config` struct by indexing into the slice and cloning the values, allowing the `Config` struct to own those values. In Listing 13-17, -we’ve reproduced the implementation of the `Config::build` function as it was -in Listing 12-23: +we’ve reproduced the implementation of the `Config::build` function as it was in +Listing 12-23: @@ -21,8 +21,8 @@ in Listing 12-23: -At the time, we said not to worry about the inefficient `clone` calls because -we would remove them in the future. Well, that time is now! +At the time, we said not to worry about the inefficient `clone` calls because we +would remove them in the future. Well, that time is now! We needed `clone` here because we have a slice with `String` elements in the parameter `args`, but the `build` function doesn’t own `args`. To return @@ -49,9 +49,9 @@ Open your I/O project’s _src/main.rs_ file, which should look like this: {{#rustdoc_include ../listings/ch13-functional-features/listing-12-24-reproduced/src/main.rs:ch13}} ``` -We’ll first change the start of the `main` function that we had in Listing -12-24 to the code in Listing 13-18, which this time uses an iterator. This -won’t compile until we update `Config::build` as well. +We’ll first change the start of the `main` function that we had in Listing 12-24 +to the code in Listing 13-18, which this time uses an iterator. This won’t +compile until we update `Config::build` as well. @@ -66,10 +66,10 @@ iterator values into a vector and then passing a slice to `Config::build`, now we’re passing ownership of the iterator returned from `env::args` to `Config::build` directly. -Next, we need to update the definition of `Config::build`. In your I/O -project’s _src/lib.rs_ file, let’s change the signature of `Config::build` to -look like Listing 13-19. This still won’t compile because we need to update the -function body. +Next, we need to update the definition of `Config::build`. In your I/O project’s +_src/lib.rs_ file, let’s change the signature of `Config::build` to look like +Listing 13-19. This still won’t compile because we need to update the function +body. @@ -155,18 +155,17 @@ well. ### Choosing Between Loops or Iterators The next logical question is which style you should choose in your own code and -why: the original implementation in Listing 13-21 or the version using -iterators in Listing 13-22. Most Rust programmers prefer to use the iterator -style. It’s a bit tougher to get the hang of at first, but once you get a feel -for the various iterator adapters and what they do, iterators can be easier to -understand. Instead of fiddling with the various bits of looping and building -new vectors, the code focuses on the high-level objective of the loop. This -abstracts away some of the commonplace code so it’s easier to see the concepts -that are unique to this code, such as the filtering condition each element in -the iterator must pass. - -But are the two implementations truly equivalent? The intuitive assumption -might be that the more low-level loop will be faster. Let’s talk about -performance. +why: the original implementation in Listing 13-21 or the version using iterators +in Listing 13-22. Most Rust programmers prefer to use the iterator style. It’s a +bit tougher to get the hang of at first, but once you get a feel for the various +iterator adapters and what they do, iterators can be easier to understand. +Instead of fiddling with the various bits of looping and building new vectors, +the code focuses on the high-level objective of the loop. This abstracts away +some of the commonplace code so it’s easier to see the concepts that are unique +to this code, such as the filtering condition each element in the iterator must +pass. + +But are the two implementations truly equivalent? The intuitive assumption might +be that the more low-level loop will be faster. Let’s talk about performance. [impl-trait]: ch10-02-traits.html#traits-as-parameters diff --git a/src/ch13-04-performance.md b/src/ch13-04-performance.md index d9cc06f99b..35881dfab7 100644 --- a/src/ch13-04-performance.md +++ b/src/ch13-04-performance.md @@ -4,10 +4,10 @@ To determine whether to use loops or iterators, you need to know which implementation is faster: the version of the `search` function with an explicit `for` loop or the version with iterators. -We ran a benchmark by loading the entire contents of _The Adventures of -Sherlock Holmes_ by Sir Arthur Conan Doyle into a `String` and looking for the -word _the_ in the contents. Here are the results of the benchmark on the -version of `search` using the `for` loop and the version using iterators: +We ran a benchmark by loading the entire contents of _The Adventures of Sherlock +Holmes_ by Sir Arthur Conan Doyle into a `String` and looking for the word _the_ +in the contents. Here are the results of the benchmark on the version of +`search` using the `for` loop and the version using iterators: ```text test bench_search_for ... bench: 19,620,300 ns/iter (+/- 915,700) @@ -15,33 +15,33 @@ test bench_search_iter ... bench: 19,234,900 ns/iter (+/- 657,200) ``` The iterator version was slightly faster! We won’t explain the benchmark code -here, because the point is not to prove that the two versions are equivalent -but to get a general sense of how these two implementations compare +here, because the point is not to prove that the two versions are equivalent but +to get a general sense of how these two implementations compare performance-wise. For a more comprehensive benchmark, you should check using various texts of various sizes as the `contents`, different words and words of different lengths -as the `query`, and all kinds of other variations. The point is this: -iterators, although a high-level abstraction, get compiled down to roughly the -same code as if you’d written the lower-level code yourself. Iterators are one -of Rust’s _zero-cost abstractions_, by which we mean using the abstraction -imposes no additional runtime overhead. This is analogous to how Bjarne -Stroustrup, the original designer and implementor of C++, defines -_zero-overhead_ in “Foundations of C++” (2012): +as the `query`, and all kinds of other variations. The point is this: iterators, +although a high-level abstraction, get compiled down to roughly the same code as +if you’d written the lower-level code yourself. Iterators are one of Rust’s +_zero-cost abstractions_, by which we mean using the abstraction imposes no +additional runtime overhead. This is analogous to how Bjarne Stroustrup, the +original designer and implementor of C++, defines _zero-overhead_ in +“Foundations of C++” (2012): > In general, C++ implementations obey the zero-overhead principle: What you > don’t use, you don’t pay for. And further: What you do use, you couldn’t hand > code any better. As another example, the following code is taken from an audio decoder. The -decoding algorithm uses the linear prediction mathematical operation to -estimate future values based on a linear function of the previous samples. This -code uses an iterator chain to do some math on three variables in scope: a -`buffer` slice of data, an array of 12 `coefficients`, and an amount by which -to shift data in `qlp_shift`. We’ve declared the variables within this example -but not given them any values; although this code doesn’t have much meaning -outside of its context, it’s still a concise, real-world example of how Rust -translates high-level ideas to low-level code. +decoding algorithm uses the linear prediction mathematical operation to estimate +future values based on a linear function of the previous samples. This code uses +an iterator chain to do some math on three variables in scope: a `buffer` slice +of data, an array of 12 `coefficients`, and an amount by which to shift data in +`qlp_shift`. We’ve declared the variables within this example but not given them +any values; although this code doesn’t have much meaning outside of its context, +it’s still a concise, real-world example of how Rust translates high-level ideas +to low-level code. ```rust,ignore let buffer: &mut [i32]; @@ -60,9 +60,9 @@ for i in 12..buffer.len() { To calculate the value of `prediction`, this code iterates through each of the 12 values in `coefficients` and uses the `zip` method to pair the coefficient -values with the previous 12 values in `buffer`. Then, for each pair, we -multiply the values together, sum all the results, and shift the bits in the -sum `qlp_shift` bits to the right. +values with the previous 12 values in `buffer`. Then, for each pair, we multiply +the values together, sum all the results, and shift the bits in the sum +`qlp_shift` bits to the right. Calculations in applications like audio decoders often prioritize performance most highly. Here, we’re creating an iterator, using two adapters, and then @@ -71,8 +71,8 @@ as of this writing, it compiles down to the same assembly you’d write by hand. There’s no loop at all corresponding to the iteration over the values in `coefficients`: Rust knows that there are 12 iterations, so it “unrolls” the loop. _Unrolling_ is an optimization that removes the overhead of the loop -controlling code and instead generates repetitive code for each iteration of -the loop. +controlling code and instead generates repetitive code for each iteration of the +loop. All of the coefficients get stored in registers, which means accessing the values is very fast. There are no bounds checks on the array access at runtime. diff --git a/src/ch14-00-more-about-cargo.md b/src/ch14-00-more-about-cargo.md index a6d0d91549..7a62024c46 100644 --- a/src/ch14-00-more-about-cargo.md +++ b/src/ch14-00-more-about-cargo.md @@ -4,11 +4,12 @@ So far we’ve used only the most basic features of Cargo to build, run, and tes our code, but it can do a lot more. In this chapter, we’ll discuss some of its other, more advanced features to show you how to do the following: -- Customize your build through release profiles -- Publish libraries on [crates.io](https://crates.io/) -- Organize large projects with workspaces -- Install binaries from [crates.io](https://crates.io/) -- Extend Cargo using custom commands +* Customize your build through release profiles +* Publish libraries on [crates.io](https://crates.io/) +* Organize large projects with workspaces +* Install binaries from [crates.io](https://crates.io/) +* Extend Cargo using custom commands -Cargo can do even more than the functionality we cover in this chapter, so for -a full explanation of all its features, see [its documentation](https://doc.rust-lang.org/cargo/). +Cargo can do even more than the functionality we cover in this chapter, so for a +full explanation of all its features, see +[its documentation](https://doc.rust-lang.org/cargo/). diff --git a/src/ch14-01-release-profiles.md b/src/ch14-01-release-profiles.md index 6dd52c6425..d701732fd9 100644 --- a/src/ch14-01-release-profiles.md +++ b/src/ch14-01-release-profiles.md @@ -5,10 +5,12 @@ different configurations that allow a programmer to have more control over various options for compiling code. Each profile is configured independently of the others. -Cargo has two main profiles: the `dev` profile Cargo uses when you run `cargo -build` and the `release` profile Cargo uses when you run `cargo build ---release`. The `dev` profile is defined with good defaults for development, -and the `release` profile has good defaults for release builds. +Cargo has two main profiles: the `dev` profile Cargo uses when you run +`cargo +build` and the `release` profile Cargo uses when you run +`cargo build +--release`. The `dev` profile is defined with good defaults for +development, and the `release` profile has good defaults for release builds. These profile names might be familiar from the output of your builds: @@ -45,14 +47,14 @@ opt-level = 3 ``` The `opt-level` setting controls the number of optimizations Rust will apply to -your code, with a range of 0 to 3. Applying more optimizations extends -compiling time, so if you’re in development and compiling your code often, -you’ll want fewer optimizations to compile faster even if the resulting code -runs slower. The default `opt-level` for `dev` is therefore `0`. When you’re -ready to release your code, it’s best to spend more time compiling. You’ll only -compile in release mode once, but you’ll run the compiled program many times, -so release mode trades longer compile time for code that runs faster. That is -why the default `opt-level` for the `release` profile is `3`. +your code, with a range of 0 to 3. Applying more optimizations extends compiling +time, so if you’re in development and compiling your code often, you’ll want +fewer optimizations to compile faster even if the resulting code runs slower. +The default `opt-level` for `dev` is therefore `0`. When you’re ready to release +your code, it’s best to spend more time compiling. You’ll only compile in +release mode once, but you’ll run the compiled program many times, so release +mode trades longer compile time for code that runs faster. That is why the +default `opt-level` for the `release` profile is `3`. You can override a default setting by adding a different value for it in _Cargo.toml_. For example, if we want to use optimization level 1 in the diff --git a/src/ch14-02-publishing-to-crates-io.md b/src/ch14-02-publishing-to-crates-io.md index 5b9b36d321..2683532236 100644 --- a/src/ch14-02-publishing-to-crates-io.md +++ b/src/ch14-02-publishing-to-crates-io.md @@ -13,9 +13,9 @@ how to publish a package. ### Making Useful Documentation Comments Accurately documenting your packages will help other users know how and when to -use them, so it’s worth investing the time to write documentation. In Chapter -3, we discussed how to comment Rust code using two slashes, `//`. Rust also has -a particular kind of comment for documentation, known conveniently as a +use them, so it’s worth investing the time to write documentation. In Chapter 3, +we discussed how to comment Rust code using two slashes, `//`. Rust also has a +particular kind of comment for documentation, known conveniently as a _documentation comment_, that will generate HTML documentation. The HTML displays the contents of documentation comments for public API items intended for programmers interested in knowing how to _use_ your crate as opposed to how @@ -34,18 +34,18 @@ for an `add_one` function in a crate named `my_crate`. -Here, we give a description of what the `add_one` function does, start a -section with the heading `Examples`, and then provide code that demonstrates -how to use the `add_one` function. We can generate the HTML documentation from -this documentation comment by running `cargo doc`. This command runs the -`rustdoc` tool distributed with Rust and puts the generated HTML documentation -in the _target/doc_ directory. +Here, we give a description of what the `add_one` function does, start a section +with the heading `Examples`, and then provide code that demonstrates how to use +the `add_one` function. We can generate the HTML documentation from this +documentation comment by running `cargo doc`. This command runs the `rustdoc` +tool distributed with Rust and puts the generated HTML documentation in the +_target/doc_ directory. -For convenience, running `cargo doc --open` will build the HTML for your -current crate’s documentation (as well as the documentation for all of your -crate’s dependencies) and open the result in a web browser. Navigate to the -`add_one` function and you’ll see how the text in the documentation comments is -rendered, as shown in Figure 14-1: +For convenience, running `cargo doc --open` will build the HTML for your current +crate’s documentation (as well as the documentation for all of your crate’s +dependencies) and open the result in a web browser. Navigate to the `add_one` +function and you’ll see how the text in the documentation comments is rendered, +as shown in Figure 14-1: Rendered HTML documentation for the `add_one` function of `my_crate` @@ -54,31 +54,32 @@ function #### Commonly Used Sections -We used the `# Examples` Markdown heading in Listing 14-1 to create a section -in the HTML with the title “Examples.” Here are some other sections that crate +We used the `# Examples` Markdown heading in Listing 14-1 to create a section in +the HTML with the title “Examples.” Here are some other sections that crate authors commonly use in their documentation: -- **Panics**: The scenarios in which the function being documented could - panic. Callers of the function who don’t want their programs to panic should - make sure they don’t call the function in these situations. -- **Errors**: If the function returns a `Result`, describing the kinds of - errors that might occur and what conditions might cause those errors to be - returned can be helpful to callers so they can write code to handle the - different kinds of errors in different ways. -- **Safety**: If the function is `unsafe` to call (we discuss unsafety in +* **Panics**: The scenarios in which the function being documented could panic. + Callers of the function who don’t want their programs to panic should make + sure they don’t call the function in these situations. +* **Errors**: If the function returns a `Result`, describing the kinds of errors + that might occur and what conditions might cause those errors to be returned + can be helpful to callers so they can write code to handle the different kinds + of errors in different ways. +* **Safety**: If the function is `unsafe` to call (we discuss unsafety in Chapter 20), there should be a section explaining why the function is unsafe and covering the invariants that the function expects callers to uphold. -Most documentation comments don’t need all of these sections, but this is a -good checklist to remind you of the aspects of your code users will be -interested in knowing about. +Most documentation comments don’t need all of these sections, but this is a good +checklist to remind you of the aspects of your code users will be interested in +knowing about. #### Documentation Comments as Tests Adding example code blocks in your documentation comments can help demonstrate -how to use your library, and doing so has an additional bonus: running `cargo -test` will run the code examples in your documentation as tests! Nothing is -better than documentation with examples. But nothing is worse than examples +how to use your library, and doing so has an additional bonus: running +`cargo +test` will run the code examples in your documentation as tests! Nothing +is better than documentation with examples. But nothing is worse than examples that don’t work because the code has changed since the documentation was written. If we run `cargo test` with the documentation for the `add_one` function from Listing 14-1, we will see a section in the test results like this: @@ -105,9 +106,9 @@ that the example and the code are out of sync with each other! #### Commenting Contained Items The style of doc comment `//!` adds documentation to the item that contains the -comments rather than to the items following the comments. We typically use -these doc comments inside the crate root file (_src/lib.rs_ by convention) or -inside a module to document the crate or the module as a whole. +comments rather than to the items following the comments. We typically use these +doc comments inside the crate root file (_src/lib.rs_ by convention) or inside a +module to document the crate or the module as a whole. For example, to add documentation that describes the purpose of the `my_crate` crate that contains the `add_one` function, we add documentation comments that @@ -128,18 +129,18 @@ that contains this comment rather than an item that follows this comment. In this case, that item is the _src/lib.rs_ file, which is the crate root. These comments describe the entire crate. -When we run `cargo doc --open`, these comments will display on the front -page of the documentation for `my_crate` above the list of public items in the -crate, as shown in Figure 14-2: +When we run `cargo doc --open`, these comments will display on the front page of +the documentation for `my_crate` above the list of public items in the crate, as +shown in Figure 14-2: Rendered HTML documentation with a comment for the crate as a whole Figure 14-2: Rendered documentation for `my_crate`, including the comment describing the crate as a whole -Documentation comments within items are useful for describing crates and -modules especially. Use them to explain the overall purpose of the container to -help your users understand the crate’s organization. +Documentation comments within items are useful for describing crates and modules +especially. Use them to explain the overall purpose of the container to help +your users understand the crate’s organization. ### Exporting a Convenient Public API with `pub use` @@ -158,17 +159,17 @@ They might also be annoyed at having to enter `use` `my_crate::some_module::another_module::UsefulType;` rather than `use` `my_crate::UsefulType;`. -The good news is that if the structure _isn’t_ convenient for others to use -from another library, you don’t have to rearrange your internal organization: +The good news is that if the structure _isn’t_ convenient for others to use from +another library, you don’t have to rearrange your internal organization: instead, you can re-export items to make a public structure that’s different -from your private structure by using `pub use`. Re-exporting takes a public -item in one location and makes it public in another location, as if it were -defined in the other location instead. +from your private structure by using `pub use`. Re-exporting takes a public item +in one location and makes it public in another location, as if it were defined +in the other location instead. For example, say we made a library named `art` for modeling artistic concepts. -Within this library are two modules: a `kinds` module containing two enums -named `PrimaryColor` and `SecondaryColor` and a `utils` module containing a -function named `mix`, as shown in Listing 14-3: +Within this library are two modules: a `kinds` module containing two enums named +`PrimaryColor` and `SecondaryColor` and a `utils` module containing a function +named `mix`, as shown in Listing 14-3: @@ -212,9 +213,9 @@ understand how to use the `art` crate, but rather causes confusion because developers who use it have to figure out where to look, and must specify the module names in the `use` statements. -To remove the internal organization from the public API, we can modify the -`art` crate code in Listing 14-3 to add `pub use` statements to re-export the -items at the top level, as shown in Listing 14-5: +To remove the internal organization from the public API, we can modify the `art` +crate code in Listing 14-3 to add `pub use` statements to re-export the items at +the top level, as shown in Listing 14-5: @@ -251,23 +252,25 @@ people who use the crate. Another common use of `pub use` is to re-export definitions of a dependency in the current crate to make that crate's definitions part of your crate’s public API. -Creating a useful public API structure is more of an art than a science, and -you can iterate to find the API that works best for your users. Choosing `pub -use` gives you flexibility in how you structure your crate internally and -decouples that internal structure from what you present to your users. Look at -some of the code of crates you’ve installed to see if their internal structure -differs from their public API. +Creating a useful public API structure is more of an art than a science, and you +can iterate to find the API that works best for your users. Choosing `pub +use` +gives you flexibility in how you structure your crate internally and decouples +that internal structure from what you present to your users. Look at some of the +code of crates you’ve installed to see if their internal structure differs from +their public API. ### Setting Up a Crates.io Account Before you can publish any crates, you need to create an account on [crates.io](https://crates.io/) and get an API token. To do so, -visit the home page at [crates.io](https://crates.io/) and log -in via a GitHub account. (The GitHub account is currently a requirement, but -the site might support other ways of creating an account in the future.) Once -you’re logged in, visit your account settings at +visit the home page at [crates.io](https://crates.io/) and log in +via a GitHub account. (The GitHub account is currently a requirement, but the +site might support other ways of creating an account in the future.) Once you’re +logged in, visit your account settings at [https://crates.io/me/](https://crates.io/me/) and retrieve your -API key. Then run the `cargo login` command and paste your API key when prompted, like this: +API key. Then run the `cargo login` command and paste your API key when +prompted, like this: ```console $ cargo login @@ -275,9 +278,9 @@ abcdefghijklmnopqrstuvwxyz012345 ``` This command will inform Cargo of your API token and store it locally in -_~/.cargo/credentials_. Note that this token is a _secret_: do not share it -with anyone else. If you do share it with anyone for any reason, you should -revoke it and generate a new token on [crates.io](https://crates.io/). ### Adding Metadata to a New Crate @@ -286,8 +289,8 @@ Let’s say you have a crate you want to publish. Before publishing, you’ll ne to add some metadata in the `[package]` section of the crate’s _Cargo.toml_ file. -Your crate will need a unique name. While you’re working on a crate locally, -you can name a crate whatever you’d like. However, crate names on +Your crate will need a unique name. While you’re working on a crate locally, you +can name a crate whatever you’d like. However, crate names on [crates.io](https://crates.io/) are allocated on a first-come, first-served basis. Once a crate name is taken, no one else can publish a crate with that name. Before attempting to publish a crate, search for the name you @@ -302,8 +305,8 @@ use the new name for publishing, like so: name = "guessing_game" ``` -Even if you’ve chosen a unique name, when you run `cargo publish` to publish -the crate at this point, you’ll get a warning and then an error: +Even if you’ve chosen a unique name, when you run `cargo publish` to publish the +crate at this point, you’ll get a warning and then an error: for others to use. Be careful, because a publish is _permanent_. The version can never be @@ -405,8 +408,8 @@ $ cargo publish Uploading guessing_game v0.1.0 (file:///projects/guessing_game) ``` -Congratulations! You’ve now shared your code with the Rust community, and -anyone can easily add your crate as a dependency of their project. +Congratulations! You’ve now shared your code with the Rust community, and anyone +can easily add your crate as a dependency of their project. ### Publishing a New Version of an Existing Crate diff --git a/src/ch14-03-cargo-workspaces.md b/src/ch14-03-cargo-workspaces.md index 8f257b5849..9c7ca482be 100644 --- a/src/ch14-03-cargo-workspaces.md +++ b/src/ch14-03-cargo-workspaces.md @@ -1,22 +1,22 @@ ## Cargo Workspaces In Chapter 12, we built a package that included a binary crate and a library -crate. As your project develops, you might find that the library crate -continues to get bigger and you want to split your package further into -multiple library crates. Cargo offers a feature called _workspaces_ that can -help manage multiple related packages that are developed in tandem. +crate. As your project develops, you might find that the library crate continues +to get bigger and you want to split your package further into multiple library +crates. Cargo offers a feature called _workspaces_ that can help manage multiple +related packages that are developed in tandem. ### Creating a Workspace A _workspace_ is a set of packages that share the same _Cargo.lock_ and output directory. Let’s make a project using a workspace—we’ll use trivial code so we can concentrate on the structure of the workspace. There are multiple ways to -structure a workspace, so we'll just show one common way. We’ll have a -workspace containing a binary and two libraries. The binary, which will provide -the main functionality, will depend on the two libraries. One library will -provide an `add_one` function, and a second library an `add_two` function. -These three crates will be part of the same workspace. We’ll start by creating -a new directory for the workspace: +structure a workspace, so we'll just show one common way. We’ll have a workspace +containing a binary and two libraries. The binary, which will provide the main +functionality, will depend on the two libraries. One library will provide an +`add_one` function, and a second library an `add_two` function. These three +crates will be part of the same workspace. We’ll start by creating a new +directory for the workspace: ```console $ mkdir add @@ -27,11 +27,11 @@ Next, in the _add_ directory, we create the _Cargo.toml_ file that will configure the entire workspace. This file won’t have a `[package]` section. Instead, it will start with a `[workspace]` section that will allow us to add members to the workspace. We also make a point to use the latest and greatest -version of Cargo’s resolver algorithm in our workspace by setting the -`resolver` to `"2"`. +version of Cargo’s resolver algorithm in our workspace by setting the `resolver` +to `"2"`. -by specifying the path to the package with our binary -crate; in this case, that path is _adder_: +by specifying the path to the package with our binary crate; in this case, that +path is _adder_: Filename: Cargo.toml @@ -63,8 +63,8 @@ package to the `members` key in the `[workspace]` definition in the workspace {{#include ../listings/ch14-more-about-cargo/output-only-01-adder-crate/add/Cargo.toml}} ``` -At this point, we can build the workspace by running `cargo build`. The files -in your _add_ directory should look like this: +At this point, we can build the workspace by running `cargo build`. The files in +your _add_ directory should look like this: ```text ├── Cargo.lock @@ -77,15 +77,15 @@ in your _add_ directory should look like this: ``` The workspace has one _target_ directory at the top level that the compiled -artifacts will be placed into; the `adder` package doesn’t have its own -_target_ directory. Even if we were to run `cargo build` from inside the -_adder_ directory, the compiled artifacts would still end up in _add/target_ -rather than _add/adder/target_. Cargo structures the _target_ directory in a -workspace like this because the crates in a workspace are meant to depend on -each other. If each crate had its own _target_ directory, each crate would have -to recompile each of the other crates in the workspace to place the artifacts -in its own _target_ directory. By sharing one _target_ directory, the crates -can avoid unnecessary rebuilding. +artifacts will be placed into; the `adder` package doesn’t have its own _target_ +directory. Even if we were to run `cargo build` from inside the _adder_ +directory, the compiled artifacts would still end up in _add/target_ rather than +_add/adder/target_. Cargo structures the _target_ directory in a workspace like +this because the crates in a workspace are meant to depend on each other. If +each crate had its own _target_ directory, each crate would have to recompile +each of the other crates in the workspace to place the artifacts in its own +_target_ directory. By sharing one _target_ directory, the crates can avoid +unnecessary rebuilding. ### Creating the Second Package in the Workspace @@ -148,12 +148,12 @@ package that has our library. First, we’ll need to add a path dependency on {{#include ../listings/ch14-more-about-cargo/no-listing-02-workspace-with-two-crates/add/adder/Cargo.toml:6:7}} ``` -Cargo doesn’t assume that crates in a workspace will depend on each other, so -we need to be explicit about the dependency relationships. +Cargo doesn’t assume that crates in a workspace will depend on each other, so we +need to be explicit about the dependency relationships. -Next, let’s use the `add_one` function (from the `add_one` crate) in the -`adder` crate. Open the _adder/src/main.rs_ file and add a `use` line at the -top to bring the new `add_one` library crate into scope. Then change the `main` +Next, let’s use the `add_one` function (from the `add_one` crate) in the `adder` +crate. Open the _adder/src/main.rs_ file and add a `use` line at the top to +bring the new `add_one` library crate into scope. Then change the `main` function to call the `add_one` function, as in Listing 14-7. @@ -180,9 +180,9 @@ $ cargo build Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.22s ``` -To run the binary crate from the _add_ directory, we can specify which -package in the workspace we want to run by using the `-p` argument and the -package name with `cargo run`: +To run the binary crate from the _add_ directory, we can specify which package +in the workspace we want to run by using the `-p` argument and the package name +with `cargo run`: . Note that you can only install packages that have binary targets. A _binary target_ is the runnable program that is created if the crate has a _src/main.rs_ file or another file specified @@ -18,7 +18,8 @@ binary target, or both. All binaries installed with `cargo install` are stored in the installation root’s _bin_ folder. If you installed Rust using _rustup.rs_ and don’t have any custom configurations, this directory will be *$HOME/.cargo/bin*. Ensure that -directory is in your `$PATH`to be able to run programs you’ve installed with`cargo install`. +directory is in your `$PATH`to be able to run programs you’ve installed with`cargo +install`. For example, in Chapter 12 we mentioned that there’s a Rust implementation of the `grep` tool called `ripgrep` for searching files. To install `ripgrep`, we @@ -43,5 +44,5 @@ $ cargo install ripgrep The second-to-last line of the output shows the location and the name of the installed binary, which in the case of `ripgrep` is `rg`. As long as the -installation directory is in your `$PATH`, as mentioned previously, you can -then run `rg --help` and start using a faster, rustier tool for searching files! +installation directory is in your `$PATH`, as mentioned previously, you can then +run `rg --help` and start using a faster, rustier tool for searching files! diff --git a/src/ch15-00-smart-pointers.md b/src/ch15-00-smart-pointers.md index 323463bf40..994394ca44 100644 --- a/src/ch15-00-smart-pointers.md +++ b/src/ch15-00-smart-pointers.md @@ -1,16 +1,16 @@ # Smart Pointers A _pointer_ is a general concept for a variable that contains an address in -memory. This address refers to, or “points at,” some other data. The most -common kind of pointer in Rust is a reference, which you learned about in -Chapter 4. References are indicated by the `&` symbol and borrow the value they -point to. They don’t have any special capabilities other than referring to -data, and have no overhead. - -_Smart pointers_, on the other hand, are data structures that act like a -pointer but also have additional metadata and capabilities. The concept of -smart pointers isn’t unique to Rust: smart pointers originated in C++ and exist -in other languages as well. Rust has a variety of smart pointers defined in the +memory. This address refers to, or “points at,” some other data. The most common +kind of pointer in Rust is a reference, which you learned about in Chapter 4. +References are indicated by the `&` symbol and borrow the value they point to. +They don’t have any special capabilities other than referring to data, and have +no overhead. + +_Smart pointers_, on the other hand, are data structures that act like a pointer +but also have additional metadata and capabilities. The concept of smart +pointers isn’t unique to Rust: smart pointers originated in C++ and exist in +other languages as well. Rust has a variety of smart pointers defined in the standard library that provide functionality beyond that provided by references. To explore the general concept, we’ll look at a couple of different examples of smart pointers, including a _reference counting_ smart pointer type. This @@ -25,25 +25,25 @@ Though we didn’t call them as such at the time, we’ve already encountered a smart pointers in this book, including `String` and `Vec` in Chapter 8. Both these types count as smart pointers because they own some memory and allow you to manipulate it. They also have metadata and extra capabilities or guarantees. -`String`, for example, stores its capacity as metadata and has the extra -ability to ensure its data will always be valid UTF-8. - -Smart pointers are usually implemented using structs. Unlike an ordinary -struct, smart pointers implement the `Deref` and `Drop` traits. The `Deref` -trait allows an instance of the smart pointer struct to behave like a reference -so you can write your code to work with either references or smart pointers. -The `Drop` trait allows you to customize the code that’s run when an instance -of the smart pointer goes out of scope. In this chapter, we’ll discuss both -traits and demonstrate why they’re important to smart pointers. - -Given that the smart pointer pattern is a general design pattern used -frequently in Rust, this chapter won’t cover every existing smart pointer. Many -libraries have their own smart pointers, and you can even write your own. We’ll -cover the most common smart pointers in the standard library: - -- `Box` for allocating values on the heap -- `Rc`, a reference counting type that enables multiple ownership -- `Ref` and `RefMut`, accessed through `RefCell`, a type that enforces +`String`, for example, stores its capacity as metadata and has the extra ability +to ensure its data will always be valid UTF-8. + +Smart pointers are usually implemented using structs. Unlike an ordinary struct, +smart pointers implement the `Deref` and `Drop` traits. The `Deref` trait allows +an instance of the smart pointer struct to behave like a reference so you can +write your code to work with either references or smart pointers. The `Drop` +trait allows you to customize the code that’s run when an instance of the smart +pointer goes out of scope. In this chapter, we’ll discuss both traits and +demonstrate why they’re important to smart pointers. + +Given that the smart pointer pattern is a general design pattern used frequently +in Rust, this chapter won’t cover every existing smart pointer. Many libraries +have their own smart pointers, and you can even write your own. We’ll cover the +most common smart pointers in the standard library: + +* `Box` for allocating values on the heap +* `Rc`, a reference counting type that enables multiple ownership +* `Ref` and `RefMut`, accessed through `RefCell`, a type that enforces the borrowing rules at runtime instead of compile time In addition, we’ll cover the _interior mutability_ pattern where an immutable diff --git a/src/ch15-01-box.md b/src/ch15-01-box.md index f53ee6aaa7..b902d3251a 100644 --- a/src/ch15-01-box.md +++ b/src/ch15-01-box.md @@ -5,33 +5,35 @@ The most straightforward smart pointer is a _box_, whose type is written remains on the stack is the pointer to the heap data. Refer to Chapter 4 to review the difference between the stack and the heap. -Boxes don’t have performance overhead, other than storing their data on the -heap instead of on the stack. But they don’t have many extra capabilities -either. You’ll use them most often in these situations: +Boxes don’t have performance overhead, other than storing their data on the heap +instead of on the stack. But they don’t have many extra capabilities either. +You’ll use them most often in these situations: -- When you have a type whose size can’t be known at compile time and you want - to use a value of that type in a context that requires an exact size -- When you have a large amount of data and you want to transfer ownership but +* When you have a type whose size can’t be known at compile time and you want to + use a value of that type in a context that requires an exact size +* When you have a large amount of data and you want to transfer ownership but ensure the data won’t be copied when you do so -- When you want to own a value and you care only that it’s a type that +* When you want to own a value and you care only that it’s a type that implements a particular trait rather than being of a specific type -We’ll demonstrate the first situation in the [“Enabling Recursive Types with +We’ll demonstrate the first situation in the +[“Enabling Recursive Types with Boxes”](#enabling-recursive-types-with-boxes) section. In the second case, transferring ownership of a large amount of data can take a long time because the data is copied around on the stack. To improve performance in this situation, we can store the large amount of data on the heap in a box. -Then, only the small amount of pointer data is copied around on the stack, -while the data it references stays in one place on the heap. The third case is -known as a _trait object_, and Chapter 18 devotes an entire section, [“Using +Then, only the small amount of pointer data is copied around on the stack, while +the data it references stays in one place on the heap. The third case is known +as a _trait object_, and Chapter 18 devotes an entire section, +[“Using Trait Objects That Allow for Values of Different Types,”][trait-objects] just to that topic. So what you learn here you’ll apply again in Chapter 18! ### Using a `Box` to Store Data on the Heap -Before we discuss the heap storage use case for `Box`, we’ll cover the -syntax and how to interact with values stored within a `Box`. +Before we discuss the heap storage use case for `Box`, we’ll cover the syntax +and how to interact with values stored within a `Box`. Listing 15-1 shows how to use a box to store an `i32` value on the heap: @@ -43,13 +45,13 @@ Listing 15-1 shows how to use a box to store an `i32` value on the heap: -We define the variable `b` to have the value of a `Box` that points to the -value `5`, which is allocated on the heap. This program will print `b = 5`; in -this case, we can access the data in the box similar to how we would if this -data were on the stack. Just like any owned value, when a box goes out of -scope, as `b` does at the end of `main`, it will be deallocated. The -deallocation happens both for the box (stored on the stack) and the data it -points to (stored on the heap). +We define the variable `b` to have the value of a `Box` that points to the value +`5`, which is allocated on the heap. This program will print `b = 5`; in this +case, we can access the data in the box similar to how we would if this data +were on the stack. Just like any owned value, when a box goes out of scope, as +`b` does at the end of `main`, it will be deallocated. The deallocation happens +both for the box (stored on the stack) and the data it points to (stored on the +heap). Putting a single value on the heap isn’t very useful, so you won’t use boxes by themselves in this way very often. Having values like a single `i32` on the @@ -60,11 +62,11 @@ wouldn’t be allowed to if we didn’t have boxes. ### Enabling Recursive Types with Boxes A value of _recursive type_ can have another value of the same type as part of -itself. Recursive types pose an issue because at compile time Rust needs to -know how much space a type takes up. However, the nesting of values of -recursive types could theoretically continue infinitely, so Rust can’t know how -much space the value needs. Because boxes have a known size, we can enable -recursive types by inserting a box in the recursive type definition. +itself. Recursive types pose an issue because at compile time Rust needs to know +how much space a type takes up. However, the nesting of values of recursive +types could theoretically continue infinitely, so Rust can’t know how much space +the value needs. Because boxes have a known size, we can enable recursive types +by inserting a box in the recursive type definition. As an example of a recursive type, let’s explore the _cons list_. This is a data type commonly found in functional programming languages. The cons list type @@ -77,9 +79,9 @@ more complex situations involving recursive types. A _cons list_ is a data structure that comes from the Lisp programming language and its dialects and is made up of nested pairs, and is the Lisp version of a linked list. Its name comes from the `cons` function (short for “construct -function”) in Lisp that constructs a new pair from its two arguments. By -calling `cons` on a pair consisting of a value and another pair, we can -construct cons lists made up of recursive pairs. +function”) in Lisp that constructs a new pair from its two arguments. By calling +`cons` on a pair consisting of a value and another pair, we can construct cons +lists made up of recursive pairs. For example, here’s a pseudocode representation of a cons list containing the list 1, 2, 3 with each pair in parentheses: @@ -97,13 +99,13 @@ which is an invalid or absent value. The cons list isn’t a commonly used data structure in Rust. Most of the time when you have a list of items in Rust, `Vec` is a better choice to use. -Other, more complex recursive data types _are_ useful in various situations, -but by starting with the cons list in this chapter, we can explore how boxes -let us define a recursive data type without much distraction. +Other, more complex recursive data types _are_ useful in various situations, but +by starting with the cons list in this chapter, we can explore how boxes let us +define a recursive data type without much distraction. Listing 15-2 contains an enum definition for a cons list. Note that this code -won’t compile yet because the `List` type doesn’t have a known size, which -we’ll demonstrate. +won’t compile yet because the `List` type doesn’t have a known size, which we’ll +demonstrate. @@ -134,8 +136,8 @@ another `Cons` value that holds `2` and another `List` value. This `List` value is one more `Cons` value that holds `3` and a `List` value, which is finally `Nil`, the non-recursive variant that signals the end of the list. -If we try to compile the code in Listing 15-3, we get the error shown in -Listing 15-4: +If we try to compile the code in Listing 15-3, we get the error shown in Listing +15-4: @@ -160,12 +162,12 @@ definitions in Chapter 6: {{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/listing-06-02/src/main.rs:here}} ``` -To determine how much space to allocate for a `Message` value, Rust goes -through each of the variants to see which variant needs the most space. Rust -sees that `Message::Quit` doesn’t need any space, `Message::Move` needs enough -space to store two `i32` values, and so forth. Because only one variant will be -used, the most space a `Message` value will need is the space it would take to -store the largest of its variants. +To determine how much space to allocate for a `Message` value, Rust goes through +each of the variants to see which variant needs the most space. Rust sees that +`Message::Quit` doesn’t need any space, `Message::Move` needs enough space to +store two `i32` values, and so forth. Because only one variant will be used, the +most space a `Message` value will need is the space it would take to store the +largest of its variants. Contrast this with what happens when Rust tries to determine how much space a recursive type like the `List` enum in Listing 15-2 needs. The compiler starts @@ -183,8 +185,8 @@ variant. The `Cons` variant holds a value of type `i32` and a value of type #### Using `Box` to Get a Recursive Type with a Known Size -Because Rust can’t figure out how much space to allocate for recursively -defined types, the compiler gives an error with this helpful suggestion: +Because Rust can’t figure out how much space to allocate for recursively defined +types, the compiler gives an error with this helpful suggestion: section of Chapter 10, to implement a trait, we need to provide -implementations for the trait’s required methods. The `Deref` trait, provided -by the standard library, requires us to implement one method named `deref` that -borrows `self` and returns a reference to the inner data. Listing 15-10 -contains an implementation of `Deref` to add to the definition of `MyBox`: +implementations for the trait’s required methods. The `Deref` trait, provided by +the standard library, requires us to implement one method named `deref` that +borrows `self` and returns a reference to the inner data. Listing 15-10 contains +an implementation of `Deref` to add to the definition of `MyBox`: @@ -143,10 +143,10 @@ contains an implementation of `Deref` to add to the definition of `MyBox`: -The `type Target = T;` syntax defines an associated type for the `Deref` -trait to use. Associated types are a slightly different way of declaring a -generic parameter, but you don’t need to worry about them for now; we’ll cover -them in more detail in Chapter 20. +The `type Target = T;` syntax defines an associated type for the `Deref` trait +to use. Associated types are a slightly different way of declaring a generic +parameter, but you don’t need to worry about them for now; we’ll cover them in +more detail in Chapter 20. We fill in the body of the `deref` method with `&self.0` so `deref` returns a reference to the value we want to access with the `*` operator; recall from the @@ -155,10 +155,10 @@ Types”][tuple-structs] section of Chapter 5 that `.0` accesses the first value in a tuple struct. The `main` function in Listing 15-9 that calls `*` on the `MyBox` value now compiles, and the assertions pass! -Without the `Deref` trait, the compiler can only dereference `&` references. -The `deref` method gives the compiler the ability to take a value of any type -that implements `Deref` and call the `deref` method to get a `&` reference that -it knows how to dereference. +Without the `Deref` trait, the compiler can only dereference `&` references. The +`deref` method gives the compiler the ability to take a value of any type that +implements `Deref` and call the `deref` method to get a `&` reference that it +knows how to dereference. When we entered `*y` in Listing 15-9, behind the scenes Rust actually ran this code: @@ -168,23 +168,23 @@ code: ``` Rust substitutes the `*` operator with a call to the `deref` method and then a -plain dereference so we don’t have to think about whether or not we need to -call the `deref` method. This Rust feature lets us write code that functions +plain dereference so we don’t have to think about whether or not we need to call +the `deref` method. This Rust feature lets us write code that functions identically whether we have a regular reference or a type that implements `Deref`. -The reason the `deref` method returns a reference to a value, and that the -plain dereference outside the parentheses in `*(y.deref())` is still necessary, -is to do with the ownership system. If the `deref` method returned the value -directly instead of a reference to the value, the value would be moved out of -`self`. We don’t want to take ownership of the inner value inside `MyBox` in -this case or in most cases where we use the dereference operator. +The reason the `deref` method returns a reference to a value, and that the plain +dereference outside the parentheses in `*(y.deref())` is still necessary, is to +do with the ownership system. If the `deref` method returned the value directly +instead of a reference to the value, the value would be moved out of `self`. We +don’t want to take ownership of the inner value inside `MyBox` in this case +or in most cases where we use the dereference operator. Note that the `*` operator is replaced with a call to the `deref` method and then a call to the `*` operator just once, each time we use a `*` in our code. -Because the substitution of the `*` operator does not recurse infinitely, we -end up with data of type `i32`, which matches the `5` in `assert_eq!` in -Listing 15-9. +Because the substitution of the `*` operator does not recurse infinitely, we end +up with data of type `i32`, which matches the `5` in `assert_eq!` in Listing +15-9. ### Implicit Deref Coercions with Functions and Methods @@ -192,16 +192,16 @@ _Deref coercion_ converts a reference to a type that implements the `Deref` trait into a reference to another type. For example, deref coercion can convert `&String` to `&str` because `String` implements the `Deref` trait such that it returns `&str`. Deref coercion is a convenience Rust performs on arguments to -functions and methods, and works only on types that implement the `Deref` -trait. It happens automatically when we pass a reference to a particular type’s -value as an argument to a function or method that doesn’t match the parameter -type in the function or method definition. A sequence of calls to the `deref` -method converts the type we provided into the type the parameter needs. +functions and methods, and works only on types that implement the `Deref` trait. +It happens automatically when we pass a reference to a particular type’s value +as an argument to a function or method that doesn’t match the parameter type in +the function or method definition. A sequence of calls to the `deref` method +converts the type we provided into the type the parameter needs. -Deref coercion was added to Rust so that programmers writing function and -method calls don’t need to add as many explicit references and dereferences -with `&` and `*`. The deref coercion feature also lets us write more code that -can work for either references or smart pointers. +Deref coercion was added to Rust so that programmers writing function and method +calls don’t need to add as many explicit references and dereferences with `&` +and `*`. The deref coercion feature also lets us write more code that can work +for either references or smart pointers. To see deref coercion in action, let’s use the `MyBox` type we defined in Listing 15-8 as well as the implementation of `Deref` that we added in Listing @@ -230,10 +230,10 @@ with a reference to a value of type `MyBox`, as shown in Listing 15-12: Here we’re calling the `hello` function with the argument `&m`, which is a reference to a `MyBox` value. Because we implemented the `Deref` trait -on `MyBox` in Listing 15-10, Rust can turn `&MyBox` into `&String` -by calling `deref`. The standard library provides an implementation of `Deref` -on `String` that returns a string slice, and this is in the API documentation -for `Deref`. Rust calls `deref` again to turn the `&String` into `&str`, which +on `MyBox` in Listing 15-10, Rust can turn `&MyBox` into `&String` by +calling `deref`. The standard library provides an implementation of `Deref` on +`String` that returns a string slice, and this is in the API documentation for +`Deref`. Rust calls `deref` again to turn the `&String` into `&str`, which matches the `hello` function’s definition. If Rust didn’t implement deref coercion, we would have to write the code in @@ -269,26 +269,25 @@ operator on mutable references. Rust does deref coercion when it finds types and trait implementations in three cases: -- From `&T` to `&U` when `T: Deref` -- From `&mut T` to `&mut U` when `T: DerefMut` -- From `&mut T` to `&U` when `T: Deref` +* From `&T` to `&U` when `T: Deref` +* From `&mut T` to `&mut U` when `T: DerefMut` +* From `&mut T` to `&U` when `T: Deref` -The first two cases are the same as each other except that the second -implements mutability. The first case states that if you have a `&T`, and `T` -implements `Deref` to some type `U`, you can get a `&U` transparently. The -second case states that the same deref coercion happens for mutable references. +The first two cases are the same as each other except that the second implements +mutability. The first case states that if you have a `&T`, and `T` implements +`Deref` to some type `U`, you can get a `&U` transparently. The second case +states that the same deref coercion happens for mutable references. The third case is trickier: Rust will also coerce a mutable reference to an immutable one. But the reverse is _not_ possible: immutable references will never coerce to mutable references. Because of the borrowing rules, if you have a mutable reference, that mutable reference must be the only reference to that -data (otherwise, the program wouldn’t compile). Converting one mutable -reference to one immutable reference will never break the borrowing rules. -Converting an immutable reference to a mutable reference would require that the -initial immutable reference is the only immutable reference to that data, but -the borrowing rules don’t guarantee that. Therefore, Rust can’t make the -assumption that converting an immutable reference to a mutable reference is -possible. +data (otherwise, the program wouldn’t compile). Converting one mutable reference +to one immutable reference will never break the borrowing rules. Converting an +immutable reference to a mutable reference would require that the initial +immutable reference is the only immutable reference to that data, but the +borrowing rules don’t guarantee that. Therefore, Rust can’t make the assumption +that converting an immutable reference to a mutable reference is possible. [impl-trait]: ch10-02-traits.html#implementing-a-trait-on-a-type [tuple-structs]: ch05-01-defining-structs.html#using-tuple-structs-without-named-fields-to-create-different-types diff --git a/src/ch15-03-drop.md b/src/ch15-03-drop.md index 4f0b96bd97..28b2a19ea1 100644 --- a/src/ch15-03-drop.md +++ b/src/ch15-03-drop.md @@ -2,8 +2,8 @@ The second trait important to the smart pointer pattern is `Drop`, which lets you customize what happens when a value is about to go out of scope. You can -provide an implementation for the `Drop` trait on any type, and that code can -be used to release resources like files or network connections. +provide an implementation for the `Drop` trait on any type, and that code can be +used to release resources like files or network connections. We’re introducing `Drop` in the context of smart pointers because the functionality of the `Drop` trait is almost always used when implementing a @@ -12,17 +12,17 @@ space on the heap that the box points to. In some languages, for some types, the programmer must call code to free memory or resources every time they finish using an instance of those types. Examples -include file handles, sockets, or locks. If they forget, the system might -become overloaded and crash. In Rust, you can specify that a particular bit of -code be run whenever a value goes out of scope, and the compiler will insert -this code automatically. As a result, you don’t need to be careful about -placing cleanup code everywhere in a program that an instance of a particular -type is finished with—you still won’t leak resources! +include file handles, sockets, or locks. If they forget, the system might become +overloaded and crash. In Rust, you can specify that a particular bit of code be +run whenever a value goes out of scope, and the compiler will insert this code +automatically. As a result, you don’t need to be careful about placing cleanup +code everywhere in a program that an instance of a particular type is finished +with—you still won’t leak resources! You specify the code to run when a value goes out of scope by implementing the -`Drop` trait. The `Drop` trait requires you to implement one method named -`drop` that takes a mutable reference to `self`. To see when Rust calls `drop`, -let’s implement `drop` with `println!` statements for now. +`Drop` trait. The `Drop` trait requires you to implement one method named `drop` +that takes a mutable reference to `self`. To see when Rust calls `drop`, let’s +implement `drop` with `println!` statements for now. Listing 15-14 shows a `CustomSmartPointer` struct whose only custom functionality is that it will print `Dropping CustomSmartPointer!` when the @@ -45,9 +45,9 @@ demonstrate visually when Rust will call `drop`. In `main`, we create two instances of `CustomSmartPointer` and then print `CustomSmartPointers created`. At the end of `main`, our instances of -`CustomSmartPointer` will go out of scope, and Rust will call the code we put -in the `drop` method, printing our final message. Note that we didn’t need to -call the `drop` method explicitly. +`CustomSmartPointer` will go out of scope, and Rust will call the code we put in +the `drop` method, printing our final message. Note that we didn’t need to call +the `drop` method explicitly. When we run this program, we’ll see the following output: @@ -57,10 +57,9 @@ When we run this program, we’ll see the following output: Rust automatically called `drop` for us when our instances went out of scope, calling the code we specified. Variables are dropped in the reverse order of -their creation, so `d` was dropped before `c`. This example’s purpose is to -give you a visual guide to how the `drop` method works; usually you would -specify the cleanup code that your type needs to run rather than a print -message. +their creation, so `d` was dropped before `c`. This example’s purpose is to give +you a visual guide to how the `drop` method works; usually you would specify the +cleanup code that your type needs to run rather than a print message. ### Dropping a Value Early with `std::mem::drop` @@ -70,9 +69,9 @@ functionality. Disabling `drop` isn’t usually necessary; the whole point of th you might want to clean up a value early. One example is when using smart pointers that manage locks: you might want to force the `drop` method that releases the lock so that other code in the same scope can acquire the lock. -Rust doesn’t let you call the `Drop` trait’s `drop` method manually; instead -you have to call the `std::mem::drop` function provided by the standard library -if you want to force a value to be dropped before the end of its scope. +Rust doesn’t let you call the `Drop` trait’s `drop` method manually; instead you +have to call the `std::mem::drop` function provided by the standard library if +you want to force a value to be dropped before the end of its scope. If we try to call the `Drop` trait’s `drop` method manually by modifying the `main` function from Listing 15-14, as shown in Listing 15-15, we’ll get a @@ -104,13 +103,13 @@ _double free_ error because Rust would be trying to clean up the same value twice. We can’t disable the automatic insertion of `drop` when a value goes out of -scope, and we can’t call the `drop` method explicitly. So, if we need to force -a value to be cleaned up early, we use the `std::mem::drop` function. +scope, and we can’t call the `drop` method explicitly. So, if we need to force a +value to be cleaned up early, we use the `std::mem::drop` function. The `std::mem::drop` function is different from the `drop` method in the `Drop` -trait. We call it by passing as an argument the value we want to force drop. -The function is in the prelude, so we can modify `main` in Listing 15-15 to -call the `drop` function, as shown in Listing 15-16: +trait. We call it by passing as an argument the value we want to force drop. The +function is in the prelude, so we can modify `main` in Listing 15-15 to call the +`drop` function, as shown in Listing 15-16: @@ -127,19 +126,20 @@ Running this code will print the following: ``` The text ``Dropping CustomSmartPointer with data `some data`!`` is printed -between the `CustomSmartPointer created.` and `CustomSmartPointer dropped -before the end of main.` text, showing that the `drop` method code is called to -drop `c` at that point. - -You can use code specified in a `Drop` trait implementation in many ways to -make cleanup convenient and safe: for instance, you could use it to create your -own memory allocator! With the `Drop` trait and Rust’s ownership system, you -don’t have to remember to clean up because Rust does it automatically. - -You also don’t have to worry about problems resulting from accidentally -cleaning up values still in use: the ownership system that makes sure -references are always valid also ensures that `drop` gets called only once when -the value is no longer being used. +between the `CustomSmartPointer created.` and +`CustomSmartPointer dropped +before the end of main.` text, showing that the +`drop` method code is called to drop `c` at that point. + +You can use code specified in a `Drop` trait implementation in many ways to make +cleanup convenient and safe: for instance, you could use it to create your own +memory allocator! With the `Drop` trait and Rust’s ownership system, you don’t +have to remember to clean up because Rust does it automatically. + +You also don’t have to worry about problems resulting from accidentally cleaning +up values still in use: the ownership system that makes sure references are +always valid also ensures that `drop` gets called only once when the value is no +longer being used. Now that we’ve examined `Box` and some of the characteristics of smart pointers, let’s look at a few other smart pointers defined in the standard diff --git a/src/ch15-04-rc.md b/src/ch15-04-rc.md index 6d1b243fc3..7b716dd476 100644 --- a/src/ch15-04-rc.md +++ b/src/ch15-04-rc.md @@ -7,11 +7,11 @@ point to the same node, and that node is conceptually owned by all of the edges that point to it. A node shouldn’t be cleaned up unless it doesn’t have any edges pointing to it and so has no owners. -You have to enable multiple ownership explicitly by using the Rust type -`Rc`, which is an abbreviation for _reference counting_. The `Rc` type -keeps track of the number of references to a value to determine whether or not -the value is still in use. If there are zero references to a value, the value -can be cleaned up without any references becoming invalid. +You have to enable multiple ownership explicitly by using the Rust type `Rc`, +which is an abbreviation for _reference counting_. The `Rc` type keeps track +of the number of references to a value to determine whether or not the value is +still in use. If there are zero references to a value, the value can be cleaned +up without any references becoming invalid. Imagine `Rc` as a TV in a family room. When one person enters to watch TV, they turn it on. Others can come into the room and watch the TV. When the last @@ -31,9 +31,9 @@ multithreaded programs. ### Using `Rc` to Share Data -Let’s return to our cons list example in Listing 15-5. Recall that we defined -it using `Box`. This time, we’ll create two lists that both share ownership -of a third list. Conceptually, this looks similar to Figure 15-3: +Let’s return to our cons list example in Listing 15-5. Recall that we defined it +using `Box`. This time, we’ll create two lists that both share ownership of a +third list. Conceptually, this looks similar to Figure 15-3: Two lists that share ownership of a third list @@ -41,9 +41,9 @@ of a third list. Conceptually, this looks similar to Figure 15-3: a third list, `a` We’ll create list `a` that contains 5 and then 10. Then we’ll make two more -lists: `b` that starts with 3 and `c` that starts with 4. Both `b` and `c` -lists will then continue on to the first `a` list containing 5 and 10. In other -words, both lists will share the first list containing 5 and 10. +lists: `b` that starts with 3 and `c` that starts with 4. Both `b` and `c` lists +will then continue on to the first `a` list containing 5 and 10. In other words, +both lists will share the first list containing 5 and 10. Trying to implement this scenario using our definition of `List` with `Box` won’t work, as shown in Listing 15-17: @@ -66,11 +66,11 @@ The `Cons` variants own the data they hold, so when we create the `b` list, `a` is moved into `b` and `b` owns `a`. Then, when we try to use `a` again when creating `c`, we’re not allowed to because `a` has been moved. -We could change the definition of `Cons` to hold references instead, but then -we would have to specify lifetime parameters. By specifying lifetime -parameters, we would be specifying that every element in the list will live at -least as long as the entire list. This is the case for the elements and lists -in Listing 15-17, but not in every scenario. +We could change the definition of `Cons` to hold references instead, but then we +would have to specify lifetime parameters. By specifying lifetime parameters, we +would be specifying that every element in the list will live at least as long as +the entire list. This is the case for the elements and lists in Listing 15-17, +but not in every scenario. Instead, we’ll change our definition of `List` to use `Rc` in place of `Box`, as shown in Listing 15-18. Each `Cons` variant will now hold a value @@ -91,22 +91,20 @@ it. -We need to add a `use` statement to bring `Rc` into scope because it’s not -in the prelude. In `main`, we create the list holding 5 and 10 and store it in -a new `Rc` in `a`. Then when we create `b` and `c`, we call the -`Rc::clone` function and pass a reference to the `Rc` in `a` as an -argument. +We need to add a `use` statement to bring `Rc` into scope because it’s not in +the prelude. In `main`, we create the list holding 5 and 10 and store it in a +new `Rc` in `a`. Then when we create `b` and `c`, we call the `Rc::clone` +function and pass a reference to the `Rc` in `a` as an argument. We could have called `a.clone()` rather than `Rc::clone(&a)`, but Rust’s -convention is to use `Rc::clone` in this case. The implementation of -`Rc::clone` doesn’t make a deep copy of all the data like most types’ -implementations of `clone` do. The call to `Rc::clone` only increments the -reference count, which doesn’t take much time. Deep copies of data can take a -lot of time. By using `Rc::clone` for reference counting, we can visually -distinguish between the deep-copy kinds of clones and the kinds of clones that -increase the reference count. When looking for performance problems in the -code, we only need to consider the deep-copy clones and can disregard calls to -`Rc::clone`. +convention is to use `Rc::clone` in this case. The implementation of `Rc::clone` +doesn’t make a deep copy of all the data like most types’ implementations of +`clone` do. The call to `Rc::clone` only increments the reference count, which +doesn’t take much time. Deep copies of data can take a lot of time. By using +`Rc::clone` for reference counting, we can visually distinguish between the +deep-copy kinds of clones and the kinds of clones that increase the reference +count. When looking for performance problems in the code, we only need to +consider the deep-copy clones and can disregard calls to `Rc::clone`. ### Cloning an `Rc` Increases the Reference Count @@ -146,17 +144,17 @@ automatically when an `Rc` value goes out of scope. What we can’t see in this example is that when `b` and then `a` go out of scope at the end of `main`, the count is then 0, and the `Rc` is cleaned up -completely. Using `Rc` allows a single value to have multiple owners, and -the count ensures that the value remains valid as long as any of the owners -still exist. +completely. Using `Rc` allows a single value to have multiple owners, and the +count ensures that the value remains valid as long as any of the owners still +exist. Via immutable references, `Rc` allows you to share data between multiple parts of your program for reading only. If `Rc` allowed you to have multiple mutable references too, you might violate one of the borrowing rules discussed in Chapter 4: multiple mutable borrows to the same place can cause data races and inconsistencies. But being able to mutate data is very useful! In the next -section, we’ll discuss the interior mutability pattern and the `RefCell` -type that you can use in conjunction with an `Rc` to work with this -immutability restriction. +section, we’ll discuss the interior mutability pattern and the `RefCell` type +that you can use in conjunction with an `Rc` to work with this immutability +restriction. [preventing-ref-cycles]: ch15-06-reference-cycles.html#preventing-reference-cycles-turning-an-rct-into-a-weakt diff --git a/src/ch15-05-interior-mutability.md b/src/ch15-05-interior-mutability.md index f1d5547c7e..352cc73cdf 100644 --- a/src/ch15-05-interior-mutability.md +++ b/src/ch15-05-interior-mutability.md @@ -1,12 +1,12 @@ ## `RefCell` and the Interior Mutability Pattern -_Interior mutability_ is a design pattern in Rust that allows you to mutate -data even when there are immutable references to that data; normally, this -action is disallowed by the borrowing rules. To mutate data, the pattern uses -`unsafe` code inside a data structure to bend Rust’s usual rules that govern -mutation and borrowing. Unsafe code indicates to the compiler that we’re -checking the rules manually instead of relying on the compiler to check them -for us; we will discuss unsafe code more in Chapter 20. +_Interior mutability_ is a design pattern in Rust that allows you to mutate data +even when there are immutable references to that data; normally, this action is +disallowed by the borrowing rules. To mutate data, the pattern uses `unsafe` +code inside a data structure to bend Rust’s usual rules that govern mutation and +borrowing. Unsafe code indicates to the compiler that we’re checking the rules +manually instead of relying on the compiler to check them for us; we will +discuss unsafe code more in Chapter 20. We can use types that use the interior mutability pattern only when we can ensure that the borrowing rules will be followed at runtime, even though the @@ -22,9 +22,9 @@ Unlike `Rc`, the `RefCell` type represents single ownership over the data it holds. So, what makes `RefCell` different from a type like `Box`? Recall the borrowing rules you learned in Chapter 4: -- At any given time, you can have _either_ (but not both) one mutable reference +* At any given time, you can have _either_ (but not both) one mutable reference or any number of immutable references. -- References must always be valid. +* References must always be valid. With references and `Box`, the borrowing rules’ invariants are enforced at compile time. With `RefCell`, these invariants are enforced _at runtime_. @@ -37,21 +37,20 @@ runtime performance because all the analysis is completed beforehand. For those reasons, checking the borrowing rules at compile time is the best choice in the majority of cases, which is why this is Rust’s default. -The advantage of checking the borrowing rules at runtime instead is that -certain memory-safe scenarios are then allowed, where they would’ve been -disallowed by the compile-time checks. Static analysis, like the Rust compiler, -is inherently conservative. Some properties of code are impossible to detect by -analyzing the code: the most famous example is the Halting Problem, which is -beyond the scope of this book but is an interesting topic to research. - -Because some analysis is impossible, if the Rust compiler can’t be sure the -code complies with the ownership rules, it might reject a correct program; in -this way, it’s conservative. If Rust accepted an incorrect program, users -wouldn’t be able to trust in the guarantees Rust makes. However, if Rust -rejects a correct program, the programmer will be inconvenienced, but nothing -catastrophic can occur. The `RefCell` type is useful when you’re sure your -code follows the borrowing rules but the compiler is unable to understand and -guarantee that. +The advantage of checking the borrowing rules at runtime instead is that certain +memory-safe scenarios are then allowed, where they would’ve been disallowed by +the compile-time checks. Static analysis, like the Rust compiler, is inherently +conservative. Some properties of code are impossible to detect by analyzing the +code: the most famous example is the Halting Problem, which is beyond the scope +of this book but is an interesting topic to research. + +Because some analysis is impossible, if the Rust compiler can’t be sure the code +complies with the ownership rules, it might reject a correct program; in this +way, it’s conservative. If Rust accepted an incorrect program, users wouldn’t be +able to trust in the guarantees Rust makes. However, if Rust rejects a correct +program, the programmer will be inconvenienced, but nothing catastrophic can +occur. The `RefCell` type is useful when you’re sure your code follows the +borrowing rules but the compiler is unable to understand and guarantee that. Similar to `Rc`, `RefCell` is only for use in single-threaded scenarios and will give you a compile-time error if you try using it in a multithreaded @@ -60,14 +59,13 @@ multithreaded program in Chapter 16. Here is a recap of the reasons to choose `Box`, `Rc`, or `RefCell`: -- `Rc` enables multiple owners of the same data; `Box` and `RefCell` +* `Rc` enables multiple owners of the same data; `Box` and `RefCell` have single owners. -- `Box` allows immutable or mutable borrows checked at compile time; `Rc` +* `Box` allows immutable or mutable borrows checked at compile time; `Rc` allows only immutable borrows checked at compile time; `RefCell` allows immutable or mutable borrows checked at runtime. -- Because `RefCell` allows mutable borrows checked at runtime, you can - mutate the value inside the `RefCell` even when the `RefCell` is - immutable. +* Because `RefCell` allows mutable borrows checked at runtime, you can mutate + the value inside the `RefCell` even when the `RefCell` is immutable. Mutating the value inside an immutable value is the _interior mutability_ pattern. Let’s look at a situation in which interior mutability is useful and @@ -90,12 +88,12 @@ If you tried to compile this code, you’d get the following error: However, there are situations in which it would be useful for a value to mutate itself in its methods but appear immutable to other code. Code outside the -value’s methods would not be able to mutate the value. Using `RefCell` is -one way to get the ability to have interior mutability, but `RefCell` -doesn’t get around the borrowing rules completely: the borrow checker in the -compiler allows this interior mutability, and the borrowing rules are checked -at runtime instead. If you violate the rules, you’ll get a `panic!` instead of -a compiler error. +value’s methods would not be able to mutate the value. Using `RefCell` is one +way to get the ability to have interior mutability, but `RefCell` doesn’t get +around the borrowing rules completely: the borrow checker in the compiler allows +this interior mutability, and the borrowing rules are checked at runtime +instead. If you violate the rules, you’ll get a `panic!` instead of a compiler +error. Let’s work through a practical example where we can use `RefCell` to mutate an immutable value and see why that is useful. @@ -107,27 +105,27 @@ in order to observe particular behavior and assert it’s implemented correctly. This placeholder type is called a _test double_. Think of it in the sense of a “stunt double” in filmmaking, where a person steps in and substitutes for an actor to do a particular tricky scene. Test doubles stand in for other types -when we’re running tests. _Mock objects_ are specific types of test doubles -that record what happens during a test so you can assert that the correct -actions took place. +when we’re running tests. _Mock objects_ are specific types of test doubles that +record what happens during a test so you can assert that the correct actions +took place. -Rust doesn’t have objects in the same sense as other languages have objects, -and Rust doesn’t have mock object functionality built into the standard library -as some other languages do. However, you can definitely create a struct that -will serve the same purposes as a mock object. +Rust doesn’t have objects in the same sense as other languages have objects, and +Rust doesn’t have mock object functionality built into the standard library as +some other languages do. However, you can definitely create a struct that will +serve the same purposes as a mock object. Here’s the scenario we’ll test: we’ll create a library that tracks a value against a maximum value and sends messages based on how close to the maximum -value the current value is. This library could be used to keep track of a -user’s quota for the number of API calls they’re allowed to make, for example. +value the current value is. This library could be used to keep track of a user’s +quota for the number of API calls they’re allowed to make, for example. Our library will only provide the functionality of tracking how close to the maximum a value is and what the messages should be at what times. Applications that use our library will be expected to provide the mechanism for sending the -messages: the application could put a message in the application, send an -email, send a text message, or something else. The library doesn’t need to know -that detail. All it needs is something that implements a trait we’ll provide -called `Messenger`. Listing 15-20 shows the library code: +messages: the application could put a message in the application, send an email, +send a text message, or something else. The library doesn’t need to know that +detail. All it needs is something that implements a trait we’ll provide called +`Messenger`. Listing 15-20 shows the library code: @@ -163,11 +161,11 @@ implement a mock object to do just that, but the borrow checker won’t allow it -This test code defines a `MockMessenger` struct that has a `sent_messages` -field with a `Vec` of `String` values to keep track of the messages it’s told -to send. We also define an associated function `new` to make it convenient to -create new `MockMessenger` values that start with an empty list of messages. We -then implement the `Messenger` trait for `MockMessenger` so we can give a +This test code defines a `MockMessenger` struct that has a `sent_messages` field +with a `Vec` of `String` values to keep track of the messages it’s told to send. +We also define an associated function `new` to make it convenient to create new +`MockMessenger` values that start with an empty list of messages. We then +implement the `Messenger` trait for `MockMessenger` so we can give a `MockMessenger` to a `LimitTracker`. In the definition of the `send` method, we take the message passed in as a parameter and store it in the `MockMessenger` list of `sent_messages`. @@ -177,9 +175,9 @@ In the test, we’re testing what happens when the `LimitTracker` is told to set create a new `MockMessenger`, which will start with an empty list of messages. Then we create a new `LimitTracker` and give it a reference to the new `MockMessenger` and a `max` value of 100. We call the `set_value` method on the -`LimitTracker` with a value of 80, which is more than 75 percent of 100. Then -we assert that the list of messages that the `MockMessenger` is keeping track -of should now have one message in it. +`LimitTracker` with a value of 80, which is more than 75 percent of 100. Then we +assert that the list of messages that the `MockMessenger` is keeping track of +should now have one message in it. However, there’s one problem with this test, as shown here: @@ -194,9 +192,9 @@ signature of `send` wouldn’t match the signature in the `Messenger` trait definition (feel free to try and see what error message you get). This is a situation in which interior mutability can help! We’ll store the -`sent_messages` within a `RefCell`, and then the `send` method will be -able to modify `sent_messages` to store the messages we’ve seen. Listing 15-22 -shows what that looks like: +`sent_messages` within a `RefCell`, and then the `send` method will be able +to modify `sent_messages` to store the messages we’ve seen. Listing 15-22 shows +what that looks like: @@ -232,19 +230,19 @@ methods, which are part of the safe API that belongs to `RefCell`. The returns the smart pointer type `RefMut`. Both types implement `Deref`, so we can treat them like regular references. -The `RefCell` keeps track of how many `Ref` and `RefMut` smart -pointers are currently active. Every time we call `borrow`, the `RefCell` -increases its count of how many immutable borrows are active. When a `Ref` -value goes out of scope, the count of immutable borrows goes down by one. Just -like the compile-time borrowing rules, `RefCell` lets us have many immutable -borrows or one mutable borrow at any point in time. +The `RefCell` keeps track of how many `Ref` and `RefMut` smart pointers +are currently active. Every time we call `borrow`, the `RefCell` increases +its count of how many immutable borrows are active. When a `Ref` value goes +out of scope, the count of immutable borrows goes down by one. Just like the +compile-time borrowing rules, `RefCell` lets us have many immutable borrows +or one mutable borrow at any point in time. If we try to violate these rules, rather than getting a compiler error as we -would with references, the implementation of `RefCell` will panic at -runtime. Listing 15-23 shows a modification of the implementation of `send` in -Listing 15-22. We’re deliberately trying to create two mutable borrows active -for the same scope to illustrate that `RefCell` prevents us from doing this -at runtime. +would with references, the implementation of `RefCell` will panic at runtime. +Listing 15-23 shows a modification of the implementation of `send` in Listing +15-22. We’re deliberately trying to create two mutable borrows active for the +same scope to illustrate that `RefCell` prevents us from doing this at +runtime. @@ -264,20 +262,20 @@ which isn’t allowed. When we run the tests for our library, the code in Listin {{#include ../listings/ch15-smart-pointers/listing-15-23/output.txt}} ``` -Notice that the code panicked with the message `already borrowed: -BorrowMutError`. This is how `RefCell` handles violations of the borrowing -rules at runtime. - -Choosing to catch borrowing errors at runtime rather than compile time, as -we’ve done here, means you’d potentially be finding mistakes in your code later -in the development process: possibly not until your code was deployed to -production. Also, your code would incur a small runtime performance penalty as -a result of keeping track of the borrows at runtime rather than compile time. -However, using `RefCell` makes it possible to write a mock object that can -modify itself to keep track of the messages it has seen while you’re using it -in a context where only immutable values are allowed. You can use `RefCell` -despite its trade-offs to get more functionality than regular references -provide. +Notice that the code panicked with the message +`already borrowed: +BorrowMutError`. This is how `RefCell` handles violations +of the borrowing rules at runtime. + +Choosing to catch borrowing errors at runtime rather than compile time, as we’ve +done here, means you’d potentially be finding mistakes in your code later in the +development process: possibly not until your code was deployed to production. +Also, your code would incur a small runtime performance penalty as a result of +keeping track of the borrows at runtime rather than compile time. However, using +`RefCell` makes it possible to write a mock object that can modify itself to +keep track of the messages it has seen while you’re using it in a context where +only immutable values are allowed. You can use `RefCell` despite its +trade-offs to get more functionality than regular references provide. ### Having Multiple Owners of Mutable Data by Combining `Rc` and `RefCell` @@ -286,13 +284,12 @@ A common way to use `RefCell` is in combination with `Rc`. Recall that access to that data. If you have an `Rc` that holds a `RefCell`, you can get a value that can have multiple owners _and_ that you can mutate! -For example, recall the cons list example in Listing 15-18 where we used -`Rc` to allow multiple lists to share ownership of another list. Because -`Rc` holds only immutable values, we can’t change any of the values in the -list once we’ve created them. Let’s add in `RefCell` to gain the ability to -change the values in the lists. Listing 15-24 shows that by using a -`RefCell` in the `Cons` definition, we can modify the value stored in all -the lists: +For example, recall the cons list example in Listing 15-18 where we used `Rc` +to allow multiple lists to share ownership of another list. Because `Rc` +holds only immutable values, we can’t change any of the values in the list once +we’ve created them. Let’s add in `RefCell` to gain the ability to change the +values in the lists. Listing 15-24 shows that by using a `RefCell` in the +`Cons` definition, we can modify the value stored in all the lists: @@ -304,21 +301,20 @@ the lists: We create a value that is an instance of `Rc>` and store it in a variable named `value` so we can access it directly later. Then we create a -`List` in `a` with a `Cons` variant that holds `value`. We need to clone -`value` so both `a` and `value` have ownership of the inner `5` value rather -than transferring ownership from `value` to `a` or having `a` borrow from -`value`. +`List` in `a` with a `Cons` variant that holds `value`. We need to clone `value` +so both `a` and `value` have ownership of the inner `5` value rather than +transferring ownership from `value` to `a` or having `a` borrow from `value`. -We wrap the list `a` in an `Rc` so when we create lists `b` and `c`, they -can both refer to `a`, which is what we did in Listing 15-18. +We wrap the list `a` in an `Rc` so when we create lists `b` and `c`, they can +both refer to `a`, which is what we did in Listing 15-18. After we’ve created the lists in `a`, `b`, and `c`, we want to add 10 to the value in `value`. We do this by calling `borrow_mut` on `value`, which uses the automatic dereferencing feature we discussed in Chapter 5 (see the section [“Where’s the `->` Operator?”][wheres-the---operator]) to -dereference the `Rc` to the inner `RefCell` value. The `borrow_mut` -method returns a `RefMut` smart pointer, and we use the dereference operator -on it and change the inner value. +dereference the `Rc` to the inner `RefCell` value. The `borrow_mut` method +returns a `RefMut` smart pointer, and we use the dereference operator on it +and change the inner value. When we print `a`, `b`, and `c`, we can see that they all have the modified value of 15 rather than 5: @@ -329,8 +325,8 @@ value of 15 rather than 5: This technique is pretty neat! By using `RefCell`, we have an outwardly immutable `List` value. But we can use the methods on `RefCell` that provide -access to its interior mutability so we can modify our data when we need to. -The runtime checks of the borrowing rules protect us from data races, and it’s +access to its interior mutability so we can modify our data when we need to. The +runtime checks of the borrowing rules protect us from data races, and it’s sometimes worth trading a bit of speed for this flexibility in our data structures. Note that `RefCell` does not work for multithreaded code! `Mutex` is the thread-safe version of `RefCell` and we’ll discuss diff --git a/src/ch15-06-reference-cycles.md b/src/ch15-06-reference-cycles.md index a28df64386..ace159ea1f 100644 --- a/src/ch15-06-reference-cycles.md +++ b/src/ch15-06-reference-cycles.md @@ -2,18 +2,17 @@ Rust’s memory safety guarantees make it difficult, but not impossible, to accidentally create memory that is never cleaned up (known as a _memory leak_). -Preventing memory leaks entirely is not one of Rust’s guarantees, meaning -memory leaks are memory safe in Rust. We can see that Rust allows memory leaks -by using `Rc` and `RefCell`: it’s possible to create references where -items refer to each other in a cycle. This creates memory leaks because the -reference count of each item in the cycle will never reach 0, and the values -will never be dropped. +Preventing memory leaks entirely is not one of Rust’s guarantees, meaning memory +leaks are memory safe in Rust. We can see that Rust allows memory leaks by using +`Rc` and `RefCell`: it’s possible to create references where items refer +to each other in a cycle. This creates memory leaks because the reference count +of each item in the cycle will never reach 0, and the values will never be +dropped. ### Creating a Reference Cycle -Let’s look at how a reference cycle might happen and how to prevent it, -starting with the definition of the `List` enum and a `tail` method in Listing -15-25: +Let’s look at how a reference cycle might happen and how to prevent it, starting +with the definition of the `List` enum and a `tail` method in Listing 15-25: @@ -26,9 +25,9 @@ starting with the definition of the `List` enum and a `tail` method in Listing We’re using another variation of the `List` definition from Listing 15-5. The second element in the `Cons` variant is now `RefCell>`, meaning that instead of having the ability to modify the `i32` value as we did in Listing -15-24, we want to modify the `List` value a `Cons` variant is pointing to. -We’re also adding a `tail` method to make it convenient for us to access the -second item if we have a `Cons` variant. +15-24, we want to modify the `List` value a `Cons` variant is pointing to. We’re +also adding a `tail` method to make it convenient for us to access the second +item if we have a `Cons` variant. In Listing 15-26, we’re adding a `main` function that uses the definitions in Listing 15-25. This code creates a list in `a` and a list in `b` that points to @@ -44,19 +43,19 @@ reference counts are at various points in this process. -We create an `Rc` instance holding a `List` value in the variable `a` -with an initial list of `5, Nil`. We then create an `Rc` instance holding +We create an `Rc` instance holding a `List` value in the variable `a` with +an initial list of `5, Nil`. We then create an `Rc` instance holding another `List` value in the variable `b` that contains the value 10 and points to the list in `a`. -We modify `a` so it points to `b` instead of `Nil`, creating a cycle. We do -that by using the `tail` method to get a reference to the `RefCell>` -in `a`, which we put in the variable `link`. Then we use the `borrow_mut` -method on the `RefCell>` to change the value inside from an `Rc` -that holds a `Nil` value to the `Rc` in `b`. +We modify `a` so it points to `b` instead of `Nil`, creating a cycle. We do that +by using the `tail` method to get a reference to the `RefCell>` in `a`, +which we put in the variable `link`. Then we use the `borrow_mut` method on the +`RefCell>` to change the value inside from an `Rc` that holds a +`Nil` value to the `Rc` in `b`. -When we run this code, keeping the last `println!` commented out for the -moment, we’ll get this output: +When we run this code, keeping the last `println!` commented out for the moment, +we’ll get this output: ```console {{#include ../listings/ch15-smart-pointers/listing-15-26/output.txt}} @@ -65,8 +64,8 @@ moment, we’ll get this output: The reference count of the `Rc` instances in both `a` and `b` are 2 after we change the list in `a` to point to `b`. At the end of `main`, Rust drops the variable `b`, which decreases the reference count of the `b` `Rc` instance -from 2 to 1. The memory that `Rc` has on the heap won’t be dropped at -this point, because its reference count is 1, not 0. Then Rust drops `a`, which +from 2 to 1. The memory that `Rc` has on the heap won’t be dropped at this +point, because its reference count is 1, not 0. Then Rust drops `a`, which decreases the reference count of the `a` `Rc` instance from 2 to 1 as well. This instance’s memory can’t be dropped either, because the other `Rc` instance still refers to it. The memory allocated to the list will @@ -78,19 +77,19 @@ diagram in Figure 15-4. Figure 15-4: A reference cycle of lists `a` and `b` pointing to each other -If you uncomment the last `println!` and run the program, Rust will try to -print this cycle with `a` pointing to `b` pointing to `a` and so forth until it +If you uncomment the last `println!` and run the program, Rust will try to print +this cycle with `a` pointing to `b` pointing to `a` and so forth until it overflows the stack. Compared to a real-world program, the consequences of creating a reference cycle -in this example aren’t very dire: right after we create the reference cycle, -the program ends. However, if a more complex program allocated lots of memory -in a cycle and held onto it for a long time, the program would use more memory -than it needed and might overwhelm the system, causing it to run out of -available memory. - -Creating reference cycles is not easily done, but it’s not impossible either. -If you have `RefCell` values that contain `Rc` values or similar nested +in this example aren’t very dire: right after we create the reference cycle, the +program ends. However, if a more complex program allocated lots of memory in a +cycle and held onto it for a long time, the program would use more memory than +it needed and might overwhelm the system, causing it to run out of available +memory. + +Creating reference cycles is not easily done, but it’s not impossible either. If +you have `RefCell` values that contain `Rc` values or similar nested combinations of types with interior mutability and reference counting, you must ensure that you don’t create cycles; you can’t rely on Rust to catch them. Creating a reference cycle would be a logic bug in your program that you should @@ -103,21 +102,21 @@ As a result, you can have cycles made up of some ownership relationships and some non-ownership relationships, and only the ownership relationships affect whether or not a value can be dropped. In Listing 15-25, we always want `Cons` variants to own their list, so reorganizing the data structure isn’t possible. -Let’s look at an example using graphs made up of parent nodes and child nodes -to see when non-ownership relationships are an appropriate way to prevent -reference cycles. +Let’s look at an example using graphs made up of parent nodes and child nodes to +see when non-ownership relationships are an appropriate way to prevent reference +cycles. ### Preventing Reference Cycles: Turning an `Rc` into a `Weak` -So far, we’ve demonstrated that calling `Rc::clone` increases the -`strong_count` of an `Rc` instance, and an `Rc` instance is only cleaned -up if its `strong_count` is 0. You can also create a _weak reference_ to the -value within an `Rc` instance by calling `Rc::downgrade` and passing a -reference to the `Rc`. Strong references are how you can share ownership of -an `Rc` instance. Weak references don’t express an ownership relationship, -and their count doesn’t affect when an `Rc` instance is cleaned up. They -won’t cause a reference cycle because any cycle involving some weak references -will be broken once the strong reference count of values involved is 0. +So far, we’ve demonstrated that calling `Rc::clone` increases the `strong_count` +of an `Rc` instance, and an `Rc` instance is only cleaned up if its +`strong_count` is 0. You can also create a _weak reference_ to the value within +an `Rc` instance by calling `Rc::downgrade` and passing a reference to the +`Rc`. Strong references are how you can share ownership of an `Rc` +instance. Weak references don’t express an ownership relationship, and their +count doesn’t affect when an `Rc` instance is cleaned up. They won’t cause a +reference cycle because any cycle involving some weak references will be broken +once the strong reference count of values involved is 0. When you call `Rc::downgrade`, you get a smart pointer of type `Weak`. Instead of increasing the `strong_count` in the `Rc` instance by 1, calling @@ -129,11 +128,11 @@ Instead of increasing the `strong_count` in the `Rc` instance by 1, calling Because the value that `Weak` references might have been dropped, to do anything with the value that a `Weak` is pointing to, you must make sure the value still exists. Do this by calling the `upgrade` method on a `Weak` -instance, which will return an `Option>`. You’ll get a result of `Some` -if the `Rc` value has not been dropped yet and a result of `None` if the -`Rc` value has been dropped. Because `upgrade` returns an `Option>`, -Rust will ensure that the `Some` case and the `None` case are handled, and -there won’t be an invalid pointer. +instance, which will return an `Option>`. You’ll get a result of `Some` if +the `Rc` value has not been dropped yet and a result of `None` if the `Rc` +value has been dropped. Because `upgrade` returns an `Option>`, Rust will +ensure that the `Some` case and the `None` case are handled, and there won’t be +an invalid pointer. As an example, rather than using a list whose items know only about the next item, we’ll create a tree whose items know about their children items _and_ @@ -141,8 +140,8 @@ their parent items. #### Creating a Tree Data Structure: a `Node` with Child Nodes -To start, we’ll build a tree with nodes that know about their child nodes. -We’ll create a struct named `Node` that holds its own `i32` value as well as +To start, we’ll build a tree with nodes that know about their child nodes. We’ll +create a struct named `Node` that holds its own `i32` value as well as references to its children `Node` values: Filename: src/main.rs @@ -169,12 +168,12 @@ with the value 5 and `leaf` as one of its children, as shown in Listing 15-27: -We clone the `Rc` in `leaf` and store that in `branch`, meaning the -`Node` in `leaf` now has two owners: `leaf` and `branch`. We can get from -`branch` to `leaf` through `branch.children`, but there’s no way to get from -`leaf` to `branch`. The reason is that `leaf` has no reference to `branch` and -doesn’t know they’re related. We want `leaf` to know that `branch` is its -parent. We’ll do that next. +We clone the `Rc` in `leaf` and store that in `branch`, meaning the `Node` +in `leaf` now has two owners: `leaf` and `branch`. We can get from `branch` to +`leaf` through `branch.children`, but there’s no way to get from `leaf` to +`branch`. The reason is that `leaf` has no reference to `branch` and doesn’t +know they’re related. We want `leaf` to know that `branch` is its parent. We’ll +do that next. #### Adding a Reference from a Child to Its Parent @@ -200,9 +199,9 @@ like this: {{#rustdoc_include ../listings/ch15-smart-pointers/listing-15-28/src/main.rs:here}} ``` -A node will be able to refer to its parent node but doesn’t own its parent. -In Listing 15-28, we update `main` to use this new definition so the `leaf` -node will have a way to refer to its parent, `branch`: +A node will be able to refer to its parent node but doesn’t own its parent. In +Listing 15-28, we update `main` to use this new definition so the `leaf` node +will have a way to refer to its parent, `branch`: @@ -224,14 +223,13 @@ first `println!` statement: leaf parent = None ``` -When we create the `branch` node, it will also have a new `Weak` -reference in the `parent` field, because `branch` doesn’t have a parent node. -We still have `leaf` as one of the children of `branch`. Once we have the -`Node` instance in `branch`, we can modify `leaf` to give it a `Weak` -reference to its parent. We use the `borrow_mut` method on the -`RefCell>` in the `parent` field of `leaf`, and then we use the -`Rc::downgrade` function to create a `Weak` reference to `branch` from -the `Rc` in `branch.` +When we create the `branch` node, it will also have a new `Weak` reference +in the `parent` field, because `branch` doesn’t have a parent node. We still +have `leaf` as one of the children of `branch`. Once we have the `Node` instance +in `branch`, we can modify `leaf` to give it a `Weak` reference to its +parent. We use the `borrow_mut` method on the `RefCell>` in the +`parent` field of `leaf`, and then we use the `Rc::downgrade` function to create +a `Weak` reference to `branch` from the `Rc` in `branch.` When we print the parent of `leaf` again, this time we’ll get a `Some` variant holding `branch`: now `leaf` can access its parent! When we print `leaf`, we @@ -264,14 +262,13 @@ in Listing 15-29: -After `leaf` is created, its `Rc` has a strong count of 1 and a weak -count of 0. In the inner scope, we create `branch` and associate it with -`leaf`, at which point when we print the counts, the `Rc` in `branch` -will have a strong count of 1 and a weak count of 1 (for `leaf.parent` pointing -to `branch` with a `Weak`). When we print the counts in `leaf`, we’ll see -it will have a strong count of 2, because `branch` now has a clone of the -`Rc` of `leaf` stored in `branch.children`, but will still have a weak -count of 0. +After `leaf` is created, its `Rc` has a strong count of 1 and a weak count +of 0. In the inner scope, we create `branch` and associate it with `leaf`, at +which point when we print the counts, the `Rc` in `branch` will have a +strong count of 1 and a weak count of 1 (for `leaf.parent` pointing to `branch` +with a `Weak`). When we print the counts in `leaf`, we’ll see it will have +a strong count of 2, because `branch` now has a clone of the `Rc` of +`leaf` stored in `branch.children`, but will still have a weak count of 0. When the inner scope ends, `branch` goes out of scope and the strong count of the `Rc` decreases to 0, so its `Node` is dropped. The weak count of 1 @@ -287,19 +284,19 @@ All of the logic that manages the counts and value dropping is built into `Rc` and `Weak` and their implementations of the `Drop` trait. By specifying that the relationship from a child to its parent should be a `Weak` reference in the definition of `Node`, you’re able to have parent -nodes point to child nodes and vice versa without creating a reference cycle -and memory leaks. +nodes point to child nodes and vice versa without creating a reference cycle and +memory leaks. ## Summary This chapter covered how to use smart pointers to make different guarantees and trade-offs from those Rust makes by default with regular references. The `Box` type has a known size and points to data allocated on the heap. The -`Rc` type keeps track of the number of references to data on the heap so -that data can have multiple owners. The `RefCell` type with its interior +`Rc` type keeps track of the number of references to data on the heap so that +data can have multiple owners. The `RefCell` type with its interior mutability gives us a type that we can use when we need an immutable type but -need to change an inner value of that type; it also enforces the borrowing -rules at runtime instead of at compile time. +need to change an inner value of that type; it also enforces the borrowing rules +at runtime instead of at compile time. Also discussed were the `Deref` and `Drop` traits, which enable a lot of the functionality of smart pointers. We explored reference cycles that can cause diff --git a/src/ch16-00-concurrency.md b/src/ch16-00-concurrency.md index 27293fd239..70eac6d6cc 100644 --- a/src/ch16-00-concurrency.md +++ b/src/ch16-00-concurrency.md @@ -4,46 +4,45 @@ Handling concurrent programming safely and efficiently is another of Rust’s major goals. _Concurrent programming_, where different parts of a program execute independently, and _parallel programming_, where different parts of a program execute at the same time, are becoming increasingly important as more -computers take advantage of their multiple processors. Historically, -programming in these contexts has been difficult and error prone: Rust hopes to -change that. +computers take advantage of their multiple processors. Historically, programming +in these contexts has been difficult and error prone: Rust hopes to change that. Initially, the Rust team thought that ensuring memory safety and preventing concurrency problems were two separate challenges to be solved with different methods. Over time, the team discovered that the ownership and type systems are -a powerful set of tools to help manage memory safety _and_ concurrency -problems! By leveraging ownership and type checking, many concurrency errors -are compile-time errors in Rust rather than runtime errors. Therefore, rather -than making you spend lots of time trying to reproduce the exact circumstances -under which a runtime concurrency bug occurs, incorrect code will refuse to -compile and present an error explaining the problem. As a result, you can fix -your code while you’re working on it rather than potentially after it has been -shipped to production. We’ve nicknamed this aspect of Rust _fearless_ -_concurrency_. Fearless concurrency allows you to write code that is free of -subtle bugs and is easy to refactor without introducing new bugs. +a powerful set of tools to help manage memory safety _and_ concurrency problems! +By leveraging ownership and type checking, many concurrency errors are +compile-time errors in Rust rather than runtime errors. Therefore, rather than +making you spend lots of time trying to reproduce the exact circumstances under +which a runtime concurrency bug occurs, incorrect code will refuse to compile +and present an error explaining the problem. As a result, you can fix your code +while you’re working on it rather than potentially after it has been shipped to +production. We’ve nicknamed this aspect of Rust _fearless_ _concurrency_. +Fearless concurrency allows you to write code that is free of subtle bugs and is +easy to refactor without introducing new bugs. > Note: For simplicity’s sake, we’ll refer to many of the problems as > _concurrent_ rather than being more precise by saying _concurrent and/or > parallel_. If this book were about concurrency and/or parallelism, we’d be -> more specific. For this chapter, please mentally substitute _concurrent -> and/or parallel_ whenever we use _concurrent_. +> more specific. For this chapter, please mentally substitute _concurrent and/or +> parallel_ whenever we use _concurrent_. Many languages are dogmatic about the solutions they offer for handling concurrent problems. For example, Erlang has elegant functionality for message-passing concurrency but has only obscure ways to share state between -threads. Supporting only a subset of possible solutions is a reasonable -strategy for higher-level languages, because a higher-level language promises -benefits from giving up some control to gain abstractions. However, lower-level -languages are expected to provide the solution with the best performance in any -given situation and have fewer abstractions over the hardware. Therefore, Rust -offers a variety of tools for modeling problems in whatever way is appropriate -for your situation and requirements. +threads. Supporting only a subset of possible solutions is a reasonable strategy +for higher-level languages, because a higher-level language promises benefits +from giving up some control to gain abstractions. However, lower-level languages +are expected to provide the solution with the best performance in any given +situation and have fewer abstractions over the hardware. Therefore, Rust offers +a variety of tools for modeling problems in whatever way is appropriate for your +situation and requirements. Here are the topics we’ll cover in this chapter: -- How to create threads to run multiple pieces of code at the same time -- _Message-passing_ concurrency, where channels send messages between threads -- _Shared-state_ concurrency, where multiple threads have access to some piece +* How to create threads to run multiple pieces of code at the same time +* _Message-passing_ concurrency, where channels send messages between threads +* _Shared-state_ concurrency, where multiple threads have access to some piece of data -- The `Sync` and `Send` traits, which extend Rust’s concurrency guarantees to +* The `Sync` and `Send` traits, which extend Rust’s concurrency guarantees to user-defined types as well as types provided by the standard library diff --git a/src/ch16-01-threads.md b/src/ch16-01-threads.md index 17d56ef7b5..72725071cb 100644 --- a/src/ch16-01-threads.md +++ b/src/ch16-01-threads.md @@ -3,9 +3,9 @@ In most current operating systems, an executed program’s code is run in a _process_, and the operating system will manage multiple processes at once. Within a program, you can also have independent parts that run simultaneously. -The features that run these independent parts are called _threads_. For -example, a web server could have multiple threads so that it could respond to -more than one request at the same time. +The features that run these independent parts are called _threads_. For example, +a web server could have multiple threads so that it could respond to more than +one request at the same time. Splitting the computation in your program into multiple threads to run multiple tasks at the same time can improve performance, but it also adds complexity. @@ -13,17 +13,16 @@ Because threads can run simultaneously, there’s no inherent guarantee about th order in which parts of your code on different threads will run. This can lead to problems, such as: -- Race conditions, where threads are accessing data or resources in an +* Race conditions, where threads are accessing data or resources in an inconsistent order -- Deadlocks, where two threads are waiting for each other, preventing both +* Deadlocks, where two threads are waiting for each other, preventing both threads from continuing -- Bugs that happen only in certain situations and are hard to reproduce and fix +* Bugs that happen only in certain situations and are hard to reproduce and fix reliably -Rust attempts to mitigate the negative effects of using threads, but -programming in a multithreaded context still takes careful thought and requires -a code structure that is different from that in programs running in a single -thread. +Rust attempts to mitigate the negative effects of using threads, but programming +in a multithreaded context still takes careful thought and requires a code +structure that is different from that in programs running in a single thread. Programming languages implement threads in a few different ways, and many operating systems provide an API the language can call for creating new threads. @@ -84,9 +83,9 @@ for the operating system to switch between the threads. ### Waiting for All Threads to Finish Using `join` Handles The code in Listing 16-1 not only stops the spawned thread prematurely most of -the time due to the main thread ending, but because there is no guarantee on -the order in which threads run, we also can’t guarantee that the spawned thread -will get to run at all! +the time due to the main thread ending, but because there is no guarantee on the +order in which threads run, we also can’t guarantee that the spawned thread will +get to run at all! We can fix the problem of the spawned thread not running or ending prematurely by saving the return value of `thread::spawn` in a variable. The return type of @@ -132,8 +131,8 @@ hi number 9 from the spawned thread! The two threads continue alternating, but the main thread waits because of the call to `handle.join()` and does not end until the spawned thread is finished. -But let’s see what happens when we instead move `handle.join()` before the -`for` loop in `main`, like this: +But let’s see what happens when we instead move `handle.join()` before the `for` +loop in `main`, like this: @@ -179,11 +178,11 @@ another. In the [“Capturing References or Moving Ownership”][capture] for more -details. At this point, you just need to know that atomics work like primitive -types but are safe to share across threads. +Fortunately, `Arc` _is_ a type like `Rc` that is safe to use in concurrent +situations. The _a_ stands for _atomic_, meaning it’s an _atomically reference +counted_ type. Atomics are an additional kind of concurrency primitive that we +won’t cover in detail here: see the standard library documentation for +[`std::sync::atomic`][atomic] for more details. At this point, +you just need to know that atomics work like primitive types but are safe to +share across threads. You might then wonder why all primitive types aren’t atomic and why standard library types aren’t implemented to use `Arc` by default. The reason is that @@ -214,10 +215,11 @@ parts, split those parts across threads, and then use a `Mutex` to have each thread update the final result with its part. Note that if you are doing simple numerical operations, there are types simpler -than `Mutex` types provided by the [`std::sync::atomic` module of the -standard library][atomic]. These types provide safe, concurrent, -atomic access to primitive types. We chose to use `Mutex` with a primitive -type for this example so we could concentrate on how `Mutex` works. +than `Mutex` types provided by the +[`std::sync::atomic` module of the standard library][atomic]. +These types provide safe, concurrent, atomic access to primitive types. We chose +to use `Mutex` with a primitive type for this example so we could concentrate +on how `Mutex` works. ### Similarities Between `RefCell`/`Rc` and `Mutex`/`Arc` @@ -235,9 +237,9 @@ creating _deadlocks_. These occur when an operation needs to lock two resources and two threads have each acquired one of the locks, causing them to wait for each other forever. If you’re interested in deadlocks, try creating a Rust program that has a deadlock; then research deadlock mitigation strategies for -mutexes in any language and have a go at implementing them in Rust. The -standard library API documentation for `Mutex` and `MutexGuard` offers -useful information. +mutexes in any language and have a go at implementing them in Rust. The standard +library API documentation for `Mutex` and `MutexGuard` offers useful +information. We’ll round out this chapter by talking about the `Send` and `Sync` traits and how we can use them with custom types. diff --git a/src/ch16-04-extensible-concurrency-sync-and-send.md b/src/ch16-04-extensible-concurrency-sync-and-send.md index 4bae9d68c6..62a7208020 100644 --- a/src/ch16-04-extensible-concurrency-sync-and-send.md +++ b/src/ch16-04-extensible-concurrency-sync-and-send.md @@ -14,17 +14,18 @@ However, two concurrency concepts are embedded in the language: the The `Send` marker trait indicates that ownership of values of the type implementing `Send` can be transferred between threads. Almost every Rust type is `Send`, but there are some exceptions, including `Rc`: this cannot be -`Send` because if you cloned an `Rc` value and tried to transfer ownership -of the clone to another thread, both threads might update the reference count -at the same time. For this reason, `Rc` is implemented for use in +`Send` because if you cloned an `Rc` value and tried to transfer ownership of +the clone to another thread, both threads might update the reference count at +the same time. For this reason, `Rc` is implemented for use in single-threaded situations where you don’t want to pay the thread-safe performance penalty. Therefore, Rust’s type system and trait bounds ensure that you can never accidentally send an `Rc` value across threads unsafely. When we tried to do -this in Listing 16-14, we got the error `the trait Send is not implemented for -Rc>`. When we switched to `Arc`, which is `Send`, the code -compiled. +this in Listing 16-14, we got the error +`the trait Send is not implemented for +Rc>`. When we switched to +`Arc`, which is `Send`, the code compiled. Any type composed entirely of `Send` types is automatically marked as `Send` as well. Almost all primitive types are `Send`, aside from raw pointers, which @@ -41,9 +42,10 @@ can be sent safely to another thread. Similar to `Send`, primitive types are The smart pointer `Rc` is also not `Sync` for the same reasons that it’s not `Send`. The `RefCell` type (which we talked about in Chapter 15) and the family of related `Cell` types are not `Sync`. The implementation of borrow -checking that `RefCell` does at runtime is not thread-safe. The smart -pointer `Mutex` is `Sync` and can be used to share access with multiple -threads as you saw in the [“Sharing a `Mutex` Between Multiple +checking that `RefCell` does at runtime is not thread-safe. The smart pointer +`Mutex` is `Sync` and can be used to share access with multiple threads as +you saw in the +[“Sharing a `Mutex` Between Multiple Threads”][sharing-a-mutext-between-multiple-threads] section. ### Implementing `Send` and `Sync` Manually Is Unsafe @@ -53,12 +55,12 @@ also `Send` and `Sync`, we don’t have to implement those traits manually. As marker traits, they don’t even have any methods to implement. They’re just useful for enforcing invariants related to concurrency. -Manually implementing these traits involves implementing unsafe Rust code. -We’ll talk about using unsafe Rust code in Chapter 20; for now, the important +Manually implementing these traits involves implementing unsafe Rust code. We’ll +talk about using unsafe Rust code in Chapter 20; for now, the important information is that building new concurrent types not made up of `Send` and -`Sync` parts requires careful thought to uphold the safety guarantees. [“The -Rustonomicon”][nomicon] has more information about these guarantees and how to -uphold them. +`Sync` parts requires careful thought to uphold the safety guarantees. +[“The Rustonomicon”][nomicon] has more information about these guarantees and +how to uphold them. ## Summary @@ -69,18 +71,17 @@ discussed here. As mentioned earlier, because very little of how Rust handles concurrency is part of the language, many concurrency solutions are implemented as crates. -These evolve more quickly than the standard library, so be sure to search -online for the current, state-of-the-art crates to use in multithreaded -situations. +These evolve more quickly than the standard library, so be sure to search online +for the current, state-of-the-art crates to use in multithreaded situations. The Rust standard library provides channels for message passing and smart pointer types, such as `Mutex` and `Arc`, that are safe to use in -concurrent contexts. The type system and the borrow checker ensure that the -code using these solutions won’t end up with data races or invalid references. -Once you get your code to compile, you can rest assured that it will happily -run on multiple threads without the kinds of hard-to-track-down bugs common in -other languages. Concurrent programming is no longer a concept to be afraid of: -go forth and make your programs concurrent, fearlessly! +concurrent contexts. The type system and the borrow checker ensure that the code +using these solutions won’t end up with data races or invalid references. Once +you get your code to compile, you can rest assured that it will happily run on +multiple threads without the kinds of hard-to-track-down bugs common in other +languages. Concurrent programming is no longer a concept to be afraid of: go +forth and make your programs concurrent, fearlessly! [sharing-a-mutext-between-multiple-threads]: ch16-03-shared-state.html#sharing-a-mutext-between-multiple-threads [nomicon]: ../nomicon/index.html diff --git a/src/ch17-00-async-await.md b/src/ch17-00-async-await.md index acd0ed3762..842f4cd005 100644 --- a/src/ch17-00-async-await.md +++ b/src/ch17-00-async-await.md @@ -82,8 +82,8 @@ When an individual works on several different tasks before any of them is complete, this is _concurrency_. Maybe you have two different projects checked out on your computer, and when you get bored or stuck on one project, you switch to the other. You’re just one person, so you can’t make progress on both tasks -at the exact same time—but you can multi-task, making progress on multiple -tasks by switching between them. +at the exact same time—but you can multi-task, making progress on multiple tasks +by switching between them.
@@ -135,8 +135,7 @@ still work concurrently. Using tools such as threads, processes, and async, the computer can pause one activity and switch to others before eventually cycling back to that first activity again. On a machine with multiple CPU cores, it can also do work in parallel. One core can be doing one thing while another core -does something completely unrelated, and those actually happen at the same -time. +does something completely unrelated, and those actually happen at the same time. When working with async in Rust, we’re always dealing with concurrency. Depending on the hardware, the operating system, and the async runtime we are @@ -146,8 +145,8 @@ under the hood. Now, let’s dive into how async programming in Rust actually works! In the rest of this chapter, we will: -- see how to use Rust’s `async` and `await` syntax -- explore how to use the async model to solve some of the same challenges we +* see how to use Rust’s `async` and `await` syntax +* explore how to use the async model to solve some of the same challenges we looked at in Chapter 16 -- look at how multithreading and async provide complementary solutions, which +* look at how multithreading and async provide complementary solutions, which you can even use together in many cases diff --git a/src/ch17-01-futures-and-syntax.md b/src/ch17-01-futures-and-syntax.md index 81975f3a7c..108f1b0243 100644 --- a/src/ch17-01-futures-and-syntax.md +++ b/src/ch17-01-futures-and-syntax.md @@ -8,9 +8,9 @@ point in the future. (This same concept shows up in many languages, sometimes under other names such as “task” or “promise”.) Rust provides a `Future` trait as a building block so different async operations can be implemented with different data structures, but with a common interface. In Rust, we say that -types which implement the `Future` trait are futures. Each type which -implements `Future` holds its own information about the progress that has been -made and what "ready" means. +types which implement the `Future` trait are futures. Each type which implements +`Future` holds its own information about the progress that has been made and +what "ready" means. The `async` keyword can be applied to blocks and functions to specify that they can be interrupted and resumed. Within an async block or async function, you can @@ -28,10 +28,10 @@ Most of the time when writing async Rust, we use the `async` and `await` keywords. Rust compiles them into equivalent code using the `Future` trait, much as it compiles `for` loops into equivalent code using the `Iterator` trait. Because Rust provides the `Future` trait, though, you can also implement it for -your own data types when you need to. Many of the functions we’ll see -throughout this chapter return types with their own implementations of `Future`. -We’ll return to the definition of the trait at the end of the chapter and dig -into more of how it works, but this is enough detail to keep us moving forward. +your own data types when you need to. Many of the functions we’ll see throughout +this chapter return types with their own implementations of `Future`. We’ll +return to the definition of the trait at the end of the chapter and dig into +more of how it works, but this is enough detail to keep us moving forward. That may all feel a bit abstract. Let’s write our first async program: a little web scraper. We’ll pass in two URLs from the command line, fetch both of them @@ -47,19 +47,19 @@ Programming Language”). It re-exports all the types, traits, and functions you’ll need, primarily from the [`futures`][futures-crate] and [`tokio`][tokio] crates. -- The `futures` crate is an official home for Rust experimentation for async +* The `futures` crate is an official home for Rust experimentation for async code, and is actually where the `Future` type was originally designed. -- Tokio is the most widely used async runtime in Rust today, especially (but - not only!) for web applications. There are other great runtimes out there, - and they may be more suitable for your purposes. We use Tokio under the hood - for `trpl` because it’s well-tested and widely used. +* Tokio is the most widely used async runtime in Rust today, especially (but not + only!) for web applications. There are other great runtimes out there, and + they may be more suitable for your purposes. We use Tokio under the hood for + `trpl` because it’s well-tested and widely used. In some cases, `trpl` also renames or wraps the original APIs to let us stay focused on the details relevant to this chapter. If you want to understand what -the crate does, we encourage you to check out [its source -code][crate-source]. You’ll be able to see what crate each -re-export comes from, and we’ve left extensive comments explaining what the +the crate does, we encourage you to check out +[its source code][crate-source]. You’ll be able to see what crate +each re-export comes from, and we’ve left extensive comments explaining what the crate does. Create a new binary project named `hello-async` and add the `trpl` crate as a @@ -91,24 +91,23 @@ In Listing 17-1, we define a function named `page_title`, and we mark it with the `async` keyword. Then we use the `trpl::get` function to fetch whatever URL is passed in, and we await the response by using the `await` keyword. Then we get the text of the response by calling its `text` method, and once again await -it with the `await` keyword. Both of these steps are asynchronous. For `get`, -we need to wait for the server to send back the first part of its response, -which will include HTTP headers, cookies, and so on. That part of the response -can be delivered separately from the body of the request. Especially if the -body is very large, it can take some time for it all to arrive. Thus, we have -to wait for the _entirety_ of the response to arrive, so the `text` method is -also async. +it with the `await` keyword. Both of these steps are asynchronous. For `get`, we +need to wait for the server to send back the first part of its response, which +will include HTTP headers, cookies, and so on. That part of the response can be +delivered separately from the body of the request. Especially if the body is +very large, it can take some time for it all to arrive. Thus, we have to wait +for the _entirety_ of the response to arrive, so the `text` method is also +async. We have to explicitly await both of these futures, because futures in Rust are _lazy_: they don’t do anything until you ask them to with `await`. (In fact, -Rust will show a compiler warning if you don’t use a future.) This should -remind you of our discussion of iterators [back in Chapter 13][iterators-lazy]. -Iterators do nothing unless you call their `next` method—whether directly, or -using `for` loops or methods such as `map` which use `next` under the hood. With -futures, the same basic idea applies: they do nothing unless you explicitly ask -them to. This laziness allows Rust to avoid running async code until it’s -actually needed. +Rust will show a compiler warning if you don’t use a future.) This should remind +you of our discussion of iterators [back in Chapter 13][iterators-lazy]. Iterators do nothing unless you call their `next` method—whether +directly, or using `for` loops or methods such as `map` which use `next` under +the hood. With futures, the same basic idea applies: they do nothing unless you +explicitly ask them to. This laziness allows Rust to avoid running async code +until it’s actually needed. > Note: This is different from the behavior we saw when using `thread::spawn` in > the previous chapter, where the closure we passed to another thread started @@ -119,9 +118,9 @@ Once we have `response_text`, we can then parse it into an instance of the `Html` type using `Html::parse`. Instead of a raw string, we now have a data type we can use to work with the HTML as a richer data structure. In particular, we can use the `select_first` method to find the first instance of a given CSS -selector. By passing the string `"title"`, we’ll get the first `` -element in the document, if there is one. Because there may not be any matching -element, `select_first` returns an `Option<ElementRef>`. Finally, we use the +selector. By passing the string `"title"`, we’ll get the first `<title>` element +in the document, if there is one. Because there may not be any matching element, +`select_first` returns an `Option<ElementRef>`. Finally, we use the `Option::map` method, which lets us work with the item in the `Option` if it’s present, and do nothing if it isn’t. (We could also use a `match` expression here, but `map` is more idiomatic.) In the body of the function we supply to @@ -175,21 +174,21 @@ fn page_title(url: &str) -> impl Future<Output = Option<String>> + '_ { Let’s walk through each part of the transformed version: -- It uses the `impl Trait` syntax we discussed back in the [“Traits as - Parameters”][impl-trait]<!-- ignore --> section in Chapter 10. -- The returned trait is a `Future`, with an associated type of `Output`. Notice +* It uses the `impl Trait` syntax we discussed back in the + [“Traits as Parameters”][impl-trait]<!-- ignore --> section in Chapter 10. +* The returned trait is a `Future`, with an associated type of `Output`. Notice that the `Output` type is `Option<String>`, which is the same as the the original return type from the `async fn` version of `page_title`. -- All of the code called in the body of the original function is wrapped in an +* All of the code called in the body of the original function is wrapped in an `async move` block. Remember that blocks are expressions. This whole block is the expression returned from the function. -- This async block produces a value with the type `Option<String>`, as described +* This async block produces a value with the type `Option<String>`, as described above. That value matches the `Output` type in the return type. This is just like other blocks you have seen. -- The new function body is an `async move` block because of how it uses the - `url` parameter. (We’ll talk about `async` vs. `async move` much more later - in the chapter.) -- The new version of the function has a kind of lifetime we haven’t seen before +* The new function body is an `async move` block because of how it uses the + `url` parameter. (We’ll talk about `async` vs. `async move` much more later in + the chapter.) +* The new version of the function has a kind of lifetime we haven’t seen before in the output type: `'_`. Because the function returns a `Future` which refers to a reference—in this case, the reference from the `url` parameter—we need to tell Rust that we mean for that reference to be included. We don’t have to @@ -197,9 +196,9 @@ Let’s walk through each part of the transformed version: reference which could be involved, but we _do_ have to be explicit that the resulting `Future` is bound by that lifetime. -Now we can call `page_title` in `main`. To start, we’ll just get the title -for a single page. In Listing 17-3, we follow the same pattern we used for -getting command line arguments back in Chapter 12. Then we pass the first URL +Now we can call `page_title` in `main`. To start, we’ll just get the title for a +single page. In Listing 17-3, we follow the same pattern we used for getting +command line arguments back in Chapter 12. Then we pass the first URL `page_title`, and await the result. Because the value produced by the future is an `Option<String>`, we use a `match` expression to print different messages to account for whether the page had a `<title>`. @@ -213,8 +212,8 @@ account for whether the page had a `<title>`. </Listing> Unfortunately, this doesn’t compile. The only place we can use the `await` -keyword is in async functions or blocks, and Rust won’t let us mark the -special `main` function as `async`. +keyword is in async functions or blocks, and Rust won’t let us mark the special +`main` function as `async`. <!-- manual-regeneration cd listings/ch17-async-await/listing-17-03 @@ -246,18 +245,18 @@ of RAM, and no ability to do heap allocations. The crates which provide those runtimes also often supply async versions of common functionality such as file or network I/O. -Here, and throughout the rest of this chapter, we’ll use the `run` function -from the `trpl` crate, which takes a future as an argument and runs it to -completion. Behind the scenes, calling `run` sets up a runtime to use to run the -future passed in. Once the future completes, `run` returns whatever value the -future produced. +Here, and throughout the rest of this chapter, we’ll use the `run` function from +the `trpl` crate, which takes a future as an argument and runs it to completion. +Behind the scenes, calling `run` sets up a runtime to use to run the future +passed in. Once the future completes, `run` returns whatever value the future +produced. We could pass the future returned by `page_title` directly to `run`. Once it completed, we would be able to match on the resulting `Option<String>`, the way we tried to do in Listing 17-3. However, for most of the examples in the chapter (and most async code in the real world!), we’ll be doing more than just one -async function call, so instead we’ll pass an `async` block and explicitly -await the result of calling `page_title`, as in Listing 17-4. +async function call, so instead we’ll pass an `async` block and explicitly await +the result of calling `page_title`, as in Listing 17-4. <Listing number="17-4" caption="Awaiting an async block with `trpl::run`" file-name="src/main.rs"> @@ -323,13 +322,14 @@ but `main` is the starting point for the program! Instead, we call the returned by the `async` block until it returns `Ready`. > Note: some runtimes provide macros to make it so you _can_ write an async main -> function. Those macros rewrite `async fn main() { ... }` to be a normal `fn +> function. Those macros rewrite `async fn main() { ... }` to be a normal +> `fn > main` which does the same thing we did by hand in Listing 17-5: call a > function which runs a future to completion the way `trpl::run` does. Let’s put these pieces together and see how we can write concurrent code, by -calling `page_title` with two different URLs passed in from the command line -and racing them. +calling `page_title` with two different URLs passed in from the command line and +racing them. <Listing number="17-5" caption="" file-name="src/main.rs"> @@ -344,9 +344,8 @@ and racing them. In Listing 17-5, we begin by calling `page_title` for each of the user-supplied URLs. We save the futures produced by calling `page_title` as `title_fut_1` and `title_fut_2`. Remember, these don’t do anything yet, because futures are lazy, -and we haven’t yet awaited them. Then we pass the futures to `trpl::race`, -which returns a value to indicate which of the futures passed to it finishes -first. +and we haven’t yet awaited them. Then we pass the futures to `trpl::race`, which +returns a value to indicate which of the futures passed to it finishes first. > Note: Under the hood, `race` is built on a more general function, `select`, > which you will encounter more often in real-world Rust code. A `select` @@ -354,11 +353,10 @@ first. > has some additional complexity that we can skip over for now. Either future can legitimately “win,” so it doesn’t make sense to return a -`Result`. Instead, `race` returns a type we haven’t seen before, -`trpl::Either`. The `Either` type is somewhat similar to a `Result`, in that it -has two cases. Unlike `Result`, though, there is no notion of success or -failure baked into `Either`. Instead, it uses `Left` and `Right` to indicate -“one or the other”. +`Result`. Instead, `race` returns a type we haven’t seen before, `trpl::Either`. +The `Either` type is somewhat similar to a `Result`, in that it has two cases. +Unlike `Result`, though, there is no notion of success or failure baked into +`Either`. Instead, it uses `Left` and `Right` to indicate “one or the other”. ```rust enum Either<A, B> { @@ -372,9 +370,9 @@ that future’s output, and `Right` with the second future argument’s output i _that_ one finishes first. This matches the order the arguments appear when calling the function: the first argument is to the left of the second argument. -We also update `page_title` to return the same URL passed in. That way, if -the page which returns first does not have a `<title>` we can resolve, we can -still print a meaningful message. With that information available, we wrap up by +We also update `page_title` to return the same URL passed in. That way, if the +page which returns first does not have a `<title>` we can resolve, we can still +print a meaningful message. With that information available, we wrap up by updating our `println!` output to indicate both which URL finished first and what the `<title>` was for the web page at that URL, if any. diff --git a/src/ch17-02-concurrency-with-async.md b/src/ch17-02-concurrency-with-async.md index 2348144d6d..69c372cf78 100644 --- a/src/ch17-02-concurrency-with-async.md +++ b/src/ch17-02-concurrency-with-async.md @@ -1,9 +1,9 @@ ## Concurrency With Async -In this section, we’ll apply async to some of the same concurrency challenges -we tackled with threads in chapter 16. Because we already talked about a lot of -the key ideas there, in this section we’ll focus on what’s different between -threads and futures. +In this section, we’ll apply async to some of the same concurrency challenges we +tackled with threads in chapter 16. Because we already talked about a lot of the +key ideas there, in this section we’ll focus on what’s different between threads +and futures. In many cases, the APIs for working with concurrency using async are very similar to those for using threads. In other cases, they end up being shaped @@ -27,8 +27,8 @@ to implement the same counting example as with threads, in Listing 17-6. </Listing> -As our starting point, we set up our `main` function with `trpl::run`, so -that our top-level function can be async. +As our starting point, we set up our `main` function with `trpl::run`, so that +our top-level function can be async. > Note: From this point forward in the chapter, every example will include this > exact same wrapping code with `trpl::run` in `main`, so we’ll often skip it @@ -162,9 +162,9 @@ you choose whether you want fairness or not. Try some of these different variations on awaiting the futures and see what they do: -- Remove the async block from around either or both of the loops. -- Await each async block immediately after defining it. -- Wrap only the first loop in an async block, and await the resulting future +* Remove the async block from around either or both of the loops. +* Await each async block immediately after defining it. +* Wrap only the first loop in an async block, and await the resulting future after the body of second loop. For an extra challenge, see if you can figure out what the output will be in @@ -176,8 +176,8 @@ Sharing data between futures will also be familiar: we’ll use message passing again, but this with async versions of the types and functions. We’ll take a slightly different path than we did in Chapter 16, to illustrate some of the key differences between thread-based and futures-based concurrency. In Listing 17-9, -we’ll begin with just a single async block—_not_ spawning a separate task as -we spawned a separate thread. +we’ll begin with just a single async block—_not_ spawning a separate task as we +spawned a separate thread. <Listing number="17-9" caption="Creating an async channel and assigning the two halves to `tx` and `rx`" file-name="src/main.rs"> @@ -196,10 +196,10 @@ we can send messages from the sender to the receiver. Notice that we don’t hav to spawn a separate thread or even a task; we merely need to await the `rx.recv` call. -The synchronous `Receiver::recv` method in `std::mpsc::channel` blocks until -it receives a message. The `trpl::Receiver::recv` method does not, because it -is async. Instead of blocking, it hands control back to the runtime until either -a message is received or the send side of the channel closes. By contrast, we +The synchronous `Receiver::recv` method in `std::mpsc::channel` blocks until it +receives a message. The `trpl::Receiver::recv` method does not, because it is +async. Instead of blocking, it hands control back to the runtime until either a +message is received or the send side of the channel closes. By contrast, we don’t await the `send` call, because it doesn’t block. It doesn’t need to, because the channel we’re sending it into is unbounded. @@ -211,8 +211,8 @@ because the channel we’re sending it into is unbounded. > runtimes, `run` is actually named `block_on` for exactly this reason. Notice two things about this example: First, the message will arrive right away! -Second, although we use a future here, there’s no concurrency yet. Everything -in the listing happens in sequence, just as it would if there were no futures +Second, although we use a future here, there’s no concurrency yet. Everything in +the listing happens in sequence, just as it would if there were no futures involved. Let’s address the first part by sending a series of messages, and sleep in @@ -230,9 +230,9 @@ between them, as shown in Listing 17-10: In addition to sending the messages, we need to receive them. In this case, we could do that manually, by just doing `rx.recv().await` four times, because we -know how many messages are coming in. In the real world, though, we’ll -generally be waiting on some _unknown_ number of messages. In that case, we need -to keep waiting until we determine that there are no more messages. +know how many messages are coming in. In the real world, though, we’ll generally +be waiting on some _unknown_ number of messages. In that case, we need to keep +waiting until we determine that there are no more messages. In Listing 16-10, we used a `for` loop to process all the items received from a synchronous channel. However, Rust doesn’t yet have a way to write a `for` loop @@ -297,19 +297,19 @@ With the updated code in Listing 17-11, the messages get printed at The program still never exits, though, because of the way `while let` loop interacts with `trpl::join`: -- The future returned from `trpl::join` only completes once _both_ futures +* The future returned from `trpl::join` only completes once _both_ futures passed to it have completed. -- The `tx` future completes once it finishes sleeping after sending the last +* The `tx` future completes once it finishes sleeping after sending the last message in `vals`. -- The `rx` future won’t complete until the `while let` loop ends. -- The `while let` loop won’t end until awaiting `rx.recv` produces `None`. -- Awaiting `rx.recv` will only return `None` once the other end of the channel +* The `rx` future won’t complete until the `while let` loop ends. +* The `while let` loop won’t end until awaiting `rx.recv` produces `None`. +* Awaiting `rx.recv` will only return `None` once the other end of the channel is closed. -- The channel will only close if we call `rx.close` or when the sender side, +* The channel will only close if we call `rx.close` or when the sender side, `tx`, is dropped. -- We don’t call `rx.close` anywhere, and `tx` won’t be dropped until the +* We don’t call `rx.close` anywhere, and `tx` won’t be dropped until the outermost async block passed to `trpl::run` ends. -- The block can’t end because it is blocked on `trpl::join` completing, which +* The block can’t end because it is blocked on `trpl::join` completing, which takes us back to the top of this list! We could manually close `rx` by calling `rx.close` somewhere, but that doesn’t @@ -318,12 +318,12 @@ make the program shut down, but we could miss messages. We need some other way to make sure that `tx` gets dropped _before_ the end of the function. Right now, the async block where we send the messages only borrows `tx` because -sending a message doesn’t require ownership, but if we could move `tx` into -that async block, it would be dropped once that block ends. In Chapter 13, we -learned how to use the `move` keyword with closures, and in Chapter 16, we saw -that we often need to move data into closures when working with threads. The -same basic dynamics apply to async blocks, so the `move` keyword works with -async blocks just as it does with closures. +sending a message doesn’t require ownership, but if we could move `tx` into that +async block, it would be dropped once that block ends. In Chapter 13, we learned +how to use the `move` keyword with closures, and in Chapter 16, we saw that we +often need to move data into closures when working with threads. The same basic +dynamics apply to async blocks, so the `move` keyword works with async blocks +just as it does with closures. In Listing 17-12, we change the async block for sending messages from a plain `async` block to an `async move` block. When we run _this_ version of the code, diff --git a/src/ch17-03-more-futures.md b/src/ch17-03-more-futures.md index c850ffc2f4..605b41d0e4 100644 --- a/src/ch17-03-more-futures.md +++ b/src/ch17-03-more-futures.md @@ -22,8 +22,8 @@ know the number of futures ahead of time. In real-world Rust, though, pushing futures into a collection and then waiting on some or all the futures in that collection to complete is a common pattern. -To check all the futures in some collection, we’ll need to iterate over and -join on _all_ of them. The `trpl::join_all` function accepts any type which +To check all the futures in some collection, we’ll need to iterate over and join +on _all_ of them. The `trpl::join_all` function accepts any type which implements the `Iterator` trait, which we learned about back in Chapter 13, so it seems like just the ticket. Let’s try putting our futures in a vector, and replace `join!` with `join_all`. @@ -63,26 +63,26 @@ error[E0308]: mismatched types = help: consider pinning your async block and casting it to a trait object ``` -This might be surprising. After all, none of them return anything, so each -block produces a `Future<Output = ()>`. However, `Future` is a trait, not a -concrete type. The concrete types are the individual data structures generated -by the compiler for async blocks. You can’t put two different hand-written -structs in a `Vec`, and the same thing applies to the different structs -generated by the compiler. +This might be surprising. After all, none of them return anything, so each block +produces a `Future<Output = ()>`. However, `Future` is a trait, not a concrete +type. The concrete types are the individual data structures generated by the +compiler for async blocks. You can’t put two different hand-written structs in a +`Vec`, and the same thing applies to the different structs generated by the +compiler. -To make this work, we need to use _trait objects_, just as we did in [“Returning -Errors from the run function”][dyn]<!-- ignore --> in Chapter 12. (We’ll cover trait objects -in detail in Chapter 18.) Using trait objects lets us treat each of the -anonymous futures produced by these types as the same type, because all of them -implement the `Future` trait. +To make this work, we need to use _trait objects_, just as we did in +[“Returning Errors from the run function”][dyn]<!-- ignore --> in Chapter 12. +(We’ll cover trait objects in detail in Chapter 18.) Using trait objects lets us +treat each of the anonymous futures produced by these types as the same type, +because all of them implement the `Future` trait. > Note: In Chapter 8, we discussed another way to include multiple types in a -> `Vec`: using an enum to represent each of the different types which can -> appear in the vector. We can’t do that here, though. For one thing, we have -> no way to name the different types, because they are anonymous. For another, -> the reason we reached for a vector and `join_all` in the first place was to be -> able to work with a dynamic collection of futures where we don’t know what -> they will all be until runtime. +> `Vec`: using an enum to represent each of the different types which can appear +> in the vector. We can’t do that here, though. For one thing, we have no way to +> name the different types, because they are anonymous. For another, the reason +> we reached for a vector and `join_all` in the first place was to be able to +> work with a dynamic collection of futures where we don’t know what they will +> all be until runtime. We start by wrapping each of the futures in the `vec!` in a `Box::new`, as shown in Listing 17-16. @@ -95,10 +95,10 @@ in Listing 17-16. </Listing> -Unfortunately, this still doesn’t compile. In fact, we have the same basic -error we did before, but we get one for both the second and third `Box::new` -calls, and we also get new errors referring to the `Unpin` trait. We will come -back to the `Unpin` errors in a moment. First, let’s fix the type errors on the +Unfortunately, this still doesn’t compile. In fact, we have the same basic error +we did before, but we get one for both the second and third `Box::new` calls, +and we also get new errors referring to the `Unpin` trait. We will come back to +the `Unpin` errors in a moment. First, let’s fix the type errors on the `Box::new` calls, by explicitly annotating the type of the `futures` variable: <Listing number="17-17" caption="Fixing the rest of the type mismatch errors by using an explicit type declaration" file-name="src/main.rs"> @@ -111,11 +111,11 @@ back to the `Unpin` errors in a moment. First, let’s fix the type errors on th The type we had to write here is a little involved, so let’s walk through it: -- The innermost type is the future itself. We note explicitly that the output of +* The innermost type is the future itself. We note explicitly that the output of the future is the unit type `()` by writing `Future<Output = ()>`. -- Then we annotate the trait with `dyn` to mark it as dynamic. -- The entire trait reference is wrapped in a `Box`. -- Finally, we state explicitly that `futures` is a `Vec` containing these items. +* Then we annotate the trait with `dyn` to mark it as dynamic. +* The entire trait reference is wrapped in a `Box`. +* Finally, we state explicitly that `futures` is a `Vec` containing these items. That already made a big difference. Now when we run the compiler, we only have the errors mentioning `Unpin`. Although there are three of them, notice that @@ -270,9 +270,9 @@ received 'you' Phew! -There’s a bit more we can explore here. For one thing, using `Pin<Box<T>>` -comes with a small amount of extra overhead from putting these futures on the -heap with `Box`—and we’re only doing that to get the types to line up. We don’t +There’s a bit more we can explore here. For one thing, using `Pin<Box<T>>` comes +with a small amount of extra overhead from putting these futures on the heap +with `Box`—and we’re only doing that to get the types to line up. We don’t actually _need_ the heap allocation, after all: these futures are local to this particular function. As noted above, `Pin` is itself a wrapper type, so we can get the benefit of having a single type in the `Vec`—the original reason we @@ -295,8 +295,10 @@ references to the dynamic `Future` type, as in Listing 17-19. We got this far by ignoring the fact that we might have different `Output` types. For example, in Listing 17-20, the anonymous future for `a` implements -`Future<Output = u32>`, the anonymous future for `b` implements `Future<Output = -&str>`, and the anonymous future for `c` implements `Future<Output = bool>`. +`Future<Output = u32>`, the anonymous future for `b` implements +`Future<Output = +&str>`, and the anonymous future for `c` implements +`Future<Output = bool>`. <Listing number="17-20" caption="Three futures with distinct types" file-name="src/main.rs"> @@ -330,7 +332,8 @@ In Listing 17-21, we once again use `trpl::race` to run two futures, `slow` and pauses for some amount of time by calling and awaiting `sleep`, and then prints another message when it finishes. Then we pass both to `trpl::race` and wait for one of them to finish. (The outcome here won’t be too surprising: `fast` wins!) -Unlike when we used `race` back in [“Our First Async Program”][async-program]<!-- +Unlike when we used `race` back in +[“Our First Async Program”][async-program]<!-- ignore -->, we just ignore the `Either` instance it returns here, because all of the interesting behavior happens in the body of the async blocks. @@ -351,19 +354,19 @@ to poll first. Regardless of whether the implementation of race we’re using is fair, though, _one_ of the futures will run up to the first `await` in its body before another task can start. -Recall from [Our First Async Program][async-program]<!-- ignore --> that at each await point, -Rust gives a runtime a chance to pause the task and switch to another one if the -future being awaited isn’t ready. The inverse is also true: Rust _only_ pauses -async blocks and hands control back to a runtime at an await point. Everything -between await points is synchronous. +Recall from [Our First Async Program][async-program]<!-- ignore --> that at each +await point, Rust gives a runtime a chance to pause the task and switch to +another one if the future being awaited isn’t ready. The inverse is also true: +Rust _only_ pauses async blocks and hands control back to a runtime at an await +point. Everything between await points is synchronous. That means if you do a bunch of work in an async block without an await point, that future will block any other futures from making progress. You may sometimes hear this referred to as one future _starving_ other futures. In some cases, that may not be a big deal. However, if you are doing some kind of expensive setup or long-running work, or if you have a future which will keep doing some -particular task indefinitely, you’ll need to think about when and where to -hand control back to the runtime. +particular task indefinitely, you’ll need to think about when and where to hand +control back to the runtime. By the same token, if you have long-running blocking operations, async can be a useful tool for providing ways for different parts of the program to relate to @@ -491,10 +494,10 @@ lot in one millisecond! You can see this for yourself by setting up a little benchmark, such as the one in Listing 17-26. (This isn’t an especially rigorous way to do performance testing, but it suffices to show the difference here.) Here, we skip all the -status printing, pass a one-nanosecond `Duration` to `trpl::sleep`, and let -each future run by itself, with no switching between the futures. Then we run -for 1,000 iterations and see how long the future using `trpl::sleep` takes -compared to the future using `trpl::yield_now`. +status printing, pass a one-nanosecond `Duration` to `trpl::sleep`, and let each +future run by itself, with no switching between the futures. Then we run for +1,000 iterations and see how long the future using `trpl::sleep` takes compared +to the future using `trpl::yield_now`. <Listing number="17-26" caption="Comparing the performance of `sleep` and `yield_now`" file-name="src/main.rs"> @@ -520,9 +523,8 @@ relatively inexpensive, it’s not free! In many cases, trying to break up a compute-bound task might make it significantly slower, so sometimes it’s better for _overall_ performance to let an operation block briefly. You should always measure to see what your code’s actual performance bottlenecks are. The -underlying dynamic is an important one to keep in mind if you _are_ seeing a -lot of work happening in serial that you expected to happen concurrently, -though! +underlying dynamic is an important one to keep in mind if you _are_ seeing a lot +of work happening in serial that you expected to happen concurrently, though! ### Building Our Own Async Abstractions @@ -544,12 +546,12 @@ future. Let’s implement this! To begin, let’s think about the API for `timeout`: -- It needs to be an async function itself so we can await it. -- Its first parameter should be a future to run. We can make it generic to allow +* It needs to be an async function itself so we can await it. +* Its first parameter should be a future to run. We can make it generic to allow it to work with any future. -- Its second parameter will be the maximum time to wait. If we use a `Duration`, +* Its second parameter will be the maximum time to wait. If we use a `Duration`, that will make it easy to pass along to `trpl::sleep`. -- It should return a `Result`. If the future completes successfully, the +* It should return a `Result`. If the future completes successfully, the `Result` will be `Ok` with the value produced by the future. If the timeout elapses first, the `Result` will be `Err` with the duration that the timeout waited for. @@ -599,25 +601,25 @@ Failed after 2 seconds ``` Because futures compose with other futures, you can build really powerful tools -using smaller async building blocks. For example, you can use this same -approach to combine timeouts with retries, and in turn use those with things -such as network calls—one of the examples from the beginning of the chapter! +using smaller async building blocks. For example, you can use this same approach +to combine timeouts with retries, and in turn use those with things such as +network calls—one of the examples from the beginning of the chapter! In practice, you will usually work directly with `async` and `await`, and -secondarily with functions and macros such as `join`, `join_all`, `race`, and -so on. You’ll only need to reach for `pin` now and again to use them with those +secondarily with functions and macros such as `join`, `join_all`, `race`, and so +on. You’ll only need to reach for `pin` now and again to use them with those APIs. -We’ve now seen a number of ways to work with multiple futures at the same -time. Up next, we’ll look at how we can work with multiple futures in a -sequence over time, with _streams_. Here are a couple more things you might want -to consider first, though: +We’ve now seen a number of ways to work with multiple futures at the same time. +Up next, we’ll look at how we can work with multiple futures in a sequence over +time, with _streams_. Here are a couple more things you might want to consider +first, though: -- We used a `Vec` with `join_all` to wait for all of the futures in some group +* We used a `Vec` with `join_all` to wait for all of the futures in some group to finish. How could you use a `Vec` to process a group of futures in sequence, instead? What are the tradeoffs of doing that? -- Take a look at the `futures::stream::FuturesUnordered` type from the `futures` +* Take a look at the `futures::stream::FuturesUnordered` type from the `futures` crate. How would using it be different from using a `Vec`? (Don’t worry about the fact that it is from the `stream` part of the crate; it works just fine with any collection of futures.) diff --git a/src/ch17-04-streams.md b/src/ch17-04-streams.md index e205e77add..e2fdaedf7a 100644 --- a/src/ch17-04-streams.md +++ b/src/ch17-04-streams.md @@ -2,9 +2,9 @@ So far in this chapter, we have mostly stuck to individual futures. The one big exception was the async channel we used. Recall how we used the receiver for our -async channel in the [“Message Passing”][17-02-messages]<!-- ignore --> earlier in the chapter. -The async `recv` method produces a sequence of items over time. This is an -instance of a much more general pattern, often called a _stream_. +async channel in the [“Message Passing”][17-02-messages]<!-- ignore --> earlier +in the chapter. The async `recv` method produces a sequence of items over time. +This is an instance of a much more general pattern, often called a _stream_. A sequence of items is something we’ve seen before, when we looked at the `Iterator` trait in Chapter 13. There are two differences between iterators and @@ -14,8 +14,8 @@ API. When working directly with an `Iterator`, we call its synchronous `next` method. With the `trpl::Receiver` stream in particular, we called an asynchronous `recv` method instead. These APIs otherwise feel very similar. -That similarity isn’t a coincidence. A stream is similar to an asynchronous -form of iteration. Whereas the `trpl::Receiver` specifically waits to receive +That similarity isn’t a coincidence. A stream is similar to an asynchronous form +of iteration. Whereas the `trpl::Receiver` specifically waits to receive messages, though, the general-purpose stream API is much more general: it provides the next item the way `Iterator` does, but asynchronously. The similarity between iterators and streams in Rust means we can actually create a @@ -163,8 +163,8 @@ Message: 'j' ``` We could do this with the regular `Receiver` API, or even the regular `Iterator` -API, though. Let’s add something that requires streams: adding a timeout -which applies to every item in the stream, and a delay on the items we emit. +API, though. Let’s add something that requires streams: adding a timeout which +applies to every item in the stream, and a delay on the items we emit. In Listing 17-34, we start by adding a timeout to the stream with the `timeout` method, which comes from the `StreamExt` trait. Then we update the body of the @@ -309,13 +309,16 @@ particular ordering. Finally, we loop over that combined stream instead of over At this point, neither `messages` nor `intervals` needs to be pinned or mutable, because both will be combined into the single `merged` stream. However, this -call to `merge` does not compile! (Neither does the `next` call in the `while +call to `merge` does not compile! (Neither does the `next` call in the +`while let` loop, but we’ll come back to that after fixing this.) The two streams -have different types. The `messages` stream has the type `Timeout<impl -Stream<Item = String>>`, where `Timeout` is the type which implements `Stream` -for a `timeout` call. Meanwhile, the `intervals` stream has the type `impl -Stream<Item = u32>`. To merge these two streams, we need to transform one of -them to match the other. +have different types. The `messages` stream has the type +`Timeout<impl +Stream<Item = String>>`, where `Timeout` is the type which +implements `Stream` for a `timeout` call. Meanwhile, the `intervals` stream has +the type `impl +Stream<Item = u32>`. To merge these two streams, we need to +transform one of them to match the other. In Listing 17-38, we rework the `intervals` stream, because `messages` is already in the basic format we want and has to handle timeout errors. First, we @@ -338,10 +341,10 @@ through the stream, and pin it so that it’s safe to do so. </Listing> That gets us _almost_ to where we need to be. Everything type checks. If you run -this, though, there will be two problems. First, it will never stop! You’ll -need to stop it with <span class="keystroke">ctrl-c</span>. Second, the -messages from the English alphabet will be buried in the midst of all the -interval counter messages: +this, though, there will be two problems. First, it will never stop! You’ll need +to stop it with <span class="keystroke">ctrl-c</span>. Second, the messages from +the English alphabet will be buried in the midst of all the interval counter +messages: <!-- Not extracting output because changes to this output aren't significant; the changes are likely to be due to the tasks running differently rather than @@ -381,14 +384,14 @@ output, not just one stream or the other. Now when we run the program, it stops after pulling twenty items from the stream, and the intervals don’t overwhelm the messages. We also don’t get `Interval: 100` or `Interval: 200` or so on, but instead get `Interval: 1`, -`Interval: 2`, and so on—even though we have a source stream which _can_ -produce an event every millisecond. That’s because the `throttle` call -produces a new stream, wrapping the original stream, so that the original -stream only gets polled at the throttle rate, not its own “native” rate. We -don’t have a bunch of unhandled interval messages we’re choosing to -ignore. Instead, we never produce those interval messages in the first place! -This is the inherent “laziness” of Rust’s futures at work again, allowing us to -choose our performance characteristics. +`Interval: 2`, and so on—even though we have a source stream which _can_ produce +an event every millisecond. That’s because the `throttle` call produces a new +stream, wrapping the original stream, so that the original stream only gets +polled at the throttle rate, not its own “native” rate. We don’t have a bunch of +unhandled interval messages we’re choosing to ignore. Instead, we never produce +those interval messages in the first place! This is the inherent “laziness” of +Rust’s futures at work again, allowing us to choose our performance +characteristics. <!-- manual-regeneration cd listings/ch17-async-await/listing-17-39 @@ -424,8 +427,8 @@ channel-based streams, the `send` calls could fail when the other side of the channel closes—and that’s just a matter of how the runtime executes the futures which make up the stream. Up until now we have ignored this by calling `unwrap`, but in a well-behaved app, we should explicitly handle the error, at minimum by -ending the loop so we don’t try to send any more messages! Listing 17-40 shows -a simple error strategy: print the issue and then `break` from the loops. As +ending the loop so we don’t try to send any more messages! Listing 17-40 shows a +simple error strategy: print the issue and then `break` from the loops. As usual, the correct way to handle a message send error will vary—just make sure you have a strategy. @@ -437,8 +440,8 @@ you have a strategy. </Listing> -Now that we’ve seen a bunch of async in practice, let’s take a step back and -dig into a few of the details of how `Future`, `Stream`, and the other key -traits which Rust uses to make async work. +Now that we’ve seen a bunch of async in practice, let’s take a step back and dig +into a few of the details of how `Future`, `Stream`, and the other key traits +which Rust uses to make async work. [17-02-messages]: ch17-02-concurrency-with-async.html#message-passing diff --git a/src/ch17-05-traits-for-async.md b/src/ch17-05-traits-for-async.md index 0aed567cdd..54e1d43581 100644 --- a/src/ch17-05-traits-for-async.md +++ b/src/ch17-05-traits-for-async.md @@ -103,10 +103,9 @@ Recall our description (in the [Counting][counting] section) of waiting on initial discussion, we noted that a runtime will pause the future until it’s ready with either `Some(message)` or `None` when the channel closes. With our deeper understanding of `Future` in place, and specifically `Future::poll`, we -can see how that works. The runtime knows the future isn’t ready when it -returns `Poll::Pending`. Conversely, the runtime knows the future is ready and -advances it when `poll` returns `Poll::Ready(Some(message))` or -`Poll::Ready(None)`. +can see how that works. The runtime knows the future isn’t ready when it returns +`Poll::Pending`. Conversely, the runtime knows the future is ready and advances +it when `poll` returns `Poll::Ready(Some(message))` or `Poll::Ready(None)`. The exact details of how a runtime does that are more than we will cover in even this deep dive section. The key here is to see the basic mechanic of futures: a @@ -180,14 +179,14 @@ knows when to check any given future, while still being lazy. The details of how that works are beyond the scope of this chapter, though: you generally only need to worry about it when writing a custom `Future` implementation. -Instead, we’ll focus on the type for `self`. This is the first time we’ve seen -a method where `self` has a type annotation. A type annotation for `self` is +Instead, we’ll focus on the type for `self`. This is the first time we’ve seen a +method where `self` has a type annotation. A type annotation for `self` is similar to type annotations for other function parameters, with two key differences. First, when we specify the type of `self` in this way, we’re telling Rust what type `self` must be to call this method. Second, a type -annotation on `self` can’t be just any type. It’s only allowed to be the type -on which the method is implemented, a reference or smart pointer to that type, -or a `Pin` wrapping a reference to that type. We’ll see more on this syntax in +annotation on `self` can’t be just any type. It’s only allowed to be the type on +which the method is implemented, a reference or smart pointer to that type, or a +`Pin` wrapping a reference to that type. We’ll see more on this syntax in Chapter 18. For now, it’s enough to know that if we want to poll a future (to check whether it is `Pending` or `Ready(Output)`), we need a mutable reference to the type, which is wrapped in a `Pin`. @@ -221,9 +220,10 @@ getting a mutable or immutable reference to it. So far so good: if we get anything wrong about the ownership or references in a given async block, the borrow checker will tell us. When we want to move around -the future that corresponds to that block—like moving it into a `Vec` to pass -to `join_all`, the way we did back in the [“Working With Any Number of -Futures”][any-number-futures]<!-- ignore --> section—things get trickier. +the future that corresponds to that block—like moving it into a `Vec` to pass to +`join_all`, the way we did back in the +[“Working With Any Number of Futures”][any-number-futures]<!-- ignore --> +section—things get trickier. When we move a future—whether by pushing into a data structure to use as an iterator with `join_all`, or returning them from a function—that actually means @@ -262,9 +262,9 @@ In principle, the Rust compiler could try to update every reference to an object every time it gets moved. That would potentially be a lot of performance overhead, especially given there can be a whole web of references that need updating. On the other hand, if we could make sure the data structure in -question _doesn’t move in memory_, we don’t have to update any references. -This is exactly what Rust’s borrow checker requires: you can’t move an item -which has any active references to it using safe code. +question _doesn’t move in memory_, we don’t have to update any references. This +is exactly what Rust’s borrow checker requires: you can’t move an item which has +any active references to it using safe code. `Pin` builds on that to give us the exact guarantee we need. When we _pin_ a value by wrapping a pointer to that value in `Pin`, it can no longer move. Thus, @@ -283,11 +283,11 @@ In fact, the `Box` pointer can still move around freely. Remember: we care about making sure the data ultimately being referenced stays in its place. If a pointer moves around, but the data it points to is in the same place, as in Figure 17-7, there’s no potential problem. (How you would do this with a `Pin` -wrapping a `Box` is more than we’ll get into in this particular discussion, -but it would make for a good exercise! If you look at the docs for the types as -well as the `std::pin` module, you might be able to work out how you would do -that.) The key is that the self-referential type itself cannot move, because it -is still pinned. +wrapping a `Box` is more than we’ll get into in this particular discussion, but +it would make for a good exercise! If you look at the docs for the types as well +as the `std::pin` module, you might be able to work out how you would do that.) +The key is that the self-referential type itself cannot move, because it is +still pinned. <figure> @@ -297,17 +297,17 @@ is still pinned. </figure> -However, most types are perfectly safe to move around, even if they happen to -be behind a `Pin` pointer. We only need to think about pinning when items have +However, most types are perfectly safe to move around, even if they happen to be +behind a `Pin` pointer. We only need to think about pinning when items have internal references. Primitive values such as numbers and booleans don’t have any internal references, so they’re obviously safe. Neither do most types you normally work with in Rust. A `Vec`, for example, doesn’t have any internal references it needs to keep up to date this way, so you can move it around without worrying. If you have a `Pin<Vec<String>>`, you’d have to do everything -via the safe but restrictive APIs provided by `Pin`, even though a -`Vec<String>` is always safe to move if there are no other references to it. We -need a way to tell the compiler that it’s actually just fine to move items -around in cases such as these. For that, we have `Unpin`. +via the safe but restrictive APIs provided by `Pin`, even though a `Vec<String>` +is always safe to move if there are no other references to it. We need a way to +tell the compiler that it’s actually just fine to move items around in cases +such as these. For that, we have `Unpin`. `Unpin` is a marker trait, similar to the `Send` and `Sync` traits we saw in Chapter 16. Recall that marker traits have no functionality of their own. They @@ -369,19 +369,19 @@ idea of how to fix the code! > Note: This combination of `Pin` and `Unpin` allows a whole class of complex > types to be safe in Rust which are otherwise difficult to implement because -> they’re self-referential. Types which require `Pin` show up _most_ commonly -> in async Rust today, but you might—very rarely!—see it in other contexts, too. +> they’re self-referential. Types which require `Pin` show up _most_ commonly in +> async Rust today, but you might—very rarely!—see it in other contexts, too. > -> The specifics of how `Pin` and `Unpin` work, and the rules they’re required -> to uphold, are covered extensively in the API documentation for `std::pin`, so -> if you’d like to understand them more deeply, that’s a great place to start. +> The specifics of how `Pin` and `Unpin` work, and the rules they’re required to +> uphold, are covered extensively in the API documentation for `std::pin`, so if +> you’d like to understand them more deeply, that’s a great place to start. > > If you want to understand how things work “under the hood” in even more > detail, the official [_Asynchronous Programming in Rust_][async-book] book has > you covered: > -> - [Chapter 2: Under the Hood: Executing Futures and Tasks][under-the-hood] -> - [Chapter 4: Pinning][pinning] +> * [Chapter 2: Under the Hood: Executing Futures and Tasks][under-the-hood] +> * [Chapter 4: Pinning][pinning] ### The Stream Trait @@ -415,9 +415,9 @@ trait Stream { ``` The `Stream` trait defines an associated type `Item` for the type of the items -produced by the stream. This is similar to `Iterator`: there may be zero to -many of these, and unlike `Future`, where there is always a single `Output` -(even if it’s the unit type `()`). +produced by the stream. This is similar to `Iterator`: there may be zero to many +of these, and unlike `Future`, where there is always a single `Output` (even if +it’s the unit type `()`). `Stream` also defines a method to get those items. We call it `poll_next`, to make it clear that it polls in the same way `Future::poll` does and produces a @@ -428,8 +428,8 @@ because it needs to signal whether there are more messages, just as an iterator does. Something very similar to this will likely end up standardized as part of Rust’s -standard library. In the meantime, it’s part of the toolkit of most runtimes, -so you can rely on it, and everything we cover below should generally apply! +standard library. In the meantime, it’s part of the toolkit of most runtimes, so +you can rely on it, and everything we cover below should generally apply! In the example we saw in the section on streaming, though, we didn’t use `poll_next` _or_ `Stream`, but instead used `next` and `StreamExt`. We _could_ diff --git a/src/ch17-06-futures-tasks-threads.md b/src/ch17-06-futures-tasks-threads.md index 5fa6629810..99dfbdea47 100644 --- a/src/ch17-06-futures-tasks-threads.md +++ b/src/ch17-06-futures-tasks-threads.md @@ -39,8 +39,8 @@ the `thread::spawn` and `thread::sleep` APIs from the standard library in the If you run this, the output is identical. And notice how little changes here from the perspective of the calling code! What’s more, even though one of our -functions spawned an async task on the runtime and the other spawned an -OS thread, the resulting streams were unaffected by the differences. +functions spawned an async task on the runtime and the other spawned an OS +thread, the resulting streams were unaffected by the differences. Despite the similarities, these two approaches behave very differently, although we might have a hard time measuring it in this very simple example. We could @@ -78,10 +78,10 @@ The fact that futures are richer data structures means they can be composed together more naturally, as we have seen. Tasks then give _additional_ control over futures, allowing you to choose where -and how to group the futures. And it turns out that threads and tasks often -work very well together, because tasks can (at least in some runtimes) be moved -around between threads. We haven’t mentioned it up until now, but under the -hood the `Runtime` we have been using, including the `spawn_blocking` and +and how to group the futures. And it turns out that threads and tasks often work +very well together, because tasks can (at least in some runtimes) be moved +around between threads. We haven’t mentioned it up until now, but under the hood +the `Runtime` we have been using, including the `spawn_blocking` and `spawn_task` functions, is multithreaded by default! Many runtimes use an approach called _work stealing_ to transparently move tasks around between threads based on the current utilization of the threads, with the aim of @@ -90,9 +90,9 @@ threads _and_ tasks, and therefore futures. As a default way of thinking about which to use when: -- If the work is _very parallelizable_, such as processing a bunch of data where +* If the work is _very parallelizable_, such as processing a bunch of data where each part can be processed separately, threads are a better choice. -- If the work is _very concurrent_, such as handling messages from a bunch of +* If the work is _very concurrent_, such as handling messages from a bunch of different sources which may come in a different intervals or different rates, async is a better choice. @@ -113,8 +113,8 @@ We begin by creating an async channel. Then we spawn a thread which takes ownership of the sender side of the channel. Within the thread, we send the numbers 1 through 10, and sleep for a second in between each. Finally, we run a future created with an async block passed to `trpl::run` just as we have -throughout the chapter. In that future, we await those messages, just as in -the other message-passing examples we have seen. +throughout the chapter. In that future, we await those messages, just as in the +other message-passing examples we have seen. To return to the examples we opened the chapter with: you could imagine running a set of video encoding tasks using a dedicated thread, because video encoding diff --git a/src/ch18-01-what-is-oo.md b/src/ch18-01-what-is-oo.md index e370757468..8e522f0ce1 100644 --- a/src/ch18-01-what-is-oo.md +++ b/src/ch18-01-what-is-oo.md @@ -2,11 +2,11 @@ There is no consensus in the programming community about what features a language must have to be considered object-oriented. Rust is influenced by many -programming paradigms, including OOP; for example, we explored the features -that came from functional programming in Chapter 13. Arguably, OOP languages -share certain common characteristics, namely objects, encapsulation, and -inheritance. Let’s look at what each of those characteristics means and whether -Rust supports it. +programming paradigms, including OOP; for example, we explored the features that +came from functional programming in Chapter 13. Arguably, OOP languages share +certain common characteristics, namely objects, encapsulation, and inheritance. +Let’s look at what each of those characteristics means and whether Rust supports +it. ### Objects Contain Data and Behavior @@ -19,10 +19,10 @@ catalog of object-oriented design patterns. It defines OOP this way: > data and the procedures that operate on that data. The procedures are > typically called _methods_ or _operations_. -Using this definition, Rust is object-oriented: structs and enums have data, -and `impl` blocks provide methods on structs and enums. Even though structs and -enums with methods aren’t _called_ objects, they provide the same -functionality, according to the Gang of Four’s definition of objects. +Using this definition, Rust is object-oriented: structs and enums have data, and +`impl` blocks provide methods on structs and enums. Even though structs and +enums with methods aren’t _called_ objects, they provide the same functionality, +according to the Gang of Four’s definition of objects. ### Encapsulation that Hides Implementation Details @@ -36,12 +36,12 @@ change the code that uses the object. We discussed how to control encapsulation in Chapter 7: we can use the `pub` keyword to decide which modules, types, functions, and methods in our code -should be public, and by default everything else is private. For example, we -can define a struct `AveragedCollection` that has a field containing a vector -of `i32` values. The struct can also have a field that contains the average of -the values in the vector, meaning the average doesn’t have to be computed -on demand whenever anyone needs it. In other words, `AveragedCollection` will -cache the calculated average for us. Listing 18-1 has the definition of the +should be public, and by default everything else is private. For example, we can +define a struct `AveragedCollection` that has a field containing a vector of +`i32` values. The struct can also have a field that contains the average of the +values in the vector, meaning the average doesn’t have to be computed on demand +whenever anyone needs it. In other words, `AveragedCollection` will cache the +calculated average for us. Listing 18-1 has the definition of the `AveragedCollection` struct: <Listing number="18-1" file-name="src/lib.rs" caption="An `AveragedCollection` struct that maintains a list of integers and the average of the items in the collection"> @@ -66,17 +66,17 @@ on the struct, as shown in Listing 18-2: </Listing> -The public methods `add`, `remove`, and `average` are the only ways to access -or modify data in an instance of `AveragedCollection`. When an item is added -to `list` using the `add` method or removed using the `remove` method, the +The public methods `add`, `remove`, and `average` are the only ways to access or +modify data in an instance of `AveragedCollection`. When an item is added to +`list` using the `add` method or removed using the `remove` method, the implementations of each call the private `update_average` method that handles updating the `average` field as well. -We leave the `list` and `average` fields private so there is no way for -external code to add or remove items to or from the `list` field directly; -otherwise, the `average` field might become out of sync when the `list` -changes. The `average` method returns the value in the `average` field, -allowing external code to read the `average` but not modify it. +We leave the `list` and `average` fields private so there is no way for external +code to add or remove items to or from the `list` field directly; otherwise, the +`average` field might become out of sync when the `list` changes. The `average` +method returns the value in the `average` field, allowing external code to read +the `average` but not modify it. Because we’ve encapsulated the implementation details of the struct `AveragedCollection`, we can easily change aspects, such as the data structure, @@ -94,55 +94,55 @@ not for different parts of code enables encapsulation of implementation details. ### Inheritance as a Type System and as Code Sharing -_Inheritance_ is a mechanism whereby an object can inherit elements from -another object’s definition, thus gaining the parent object’s data and behavior -without you having to define them again. +_Inheritance_ is a mechanism whereby an object can inherit elements from another +object’s definition, thus gaining the parent object’s data and behavior without +you having to define them again. -If a language must have inheritance to be an object-oriented language, then -Rust is not one. There is no way to define a struct that inherits the parent -struct’s fields and method implementations without using a macro. +If a language must have inheritance to be an object-oriented language, then Rust +is not one. There is no way to define a struct that inherits the parent struct’s +fields and method implementations without using a macro. However, if you’re used to having inheritance in your programming toolbox, you can use other solutions in Rust, depending on your reason for reaching for inheritance in the first place. -You would choose inheritance for two main reasons. One is for reuse of code: -you can implement particular behavior for one type, and inheritance enables you -to reuse that implementation for a different type. You can do this in a limited -way in Rust code using default trait method implementations, which you saw in +You would choose inheritance for two main reasons. One is for reuse of code: you +can implement particular behavior for one type, and inheritance enables you to +reuse that implementation for a different type. You can do this in a limited way +in Rust code using default trait method implementations, which you saw in Listing 10-14 when we added a default implementation of the `summarize` method -on the `Summary` trait. Any type implementing the `Summary` trait would have -the `summarize` method available on it without any further code. This is -similar to a parent class having an implementation of a method and an -inheriting child class also having the implementation of the method. We can -also override the default implementation of the `summarize` method when we -implement the `Summary` trait, which is similar to a child class overriding the -implementation of a method inherited from a parent class. +on the `Summary` trait. Any type implementing the `Summary` trait would have the +`summarize` method available on it without any further code. This is similar to +a parent class having an implementation of a method and an inheriting child +class also having the implementation of the method. We can also override the +default implementation of the `summarize` method when we implement the `Summary` +trait, which is similar to a child class overriding the implementation of a +method inherited from a parent class. The other reason to use inheritance relates to the type system: to enable a -child type to be used in the same places as the parent type. This is also -called _polymorphism_, which means that you can substitute multiple objects for -each other at runtime if they share certain characteristics. +child type to be used in the same places as the parent type. This is also called +_polymorphism_, which means that you can substitute multiple objects for each +other at runtime if they share certain characteristics. > ### Polymorphism > -> To many people, polymorphism is synonymous with inheritance. But it’s -> actually a more general concept that refers to code that can work with data -> of multiple types. For inheritance, those types are generally subclasses. +> To many people, polymorphism is synonymous with inheritance. But it’s actually +> a more general concept that refers to code that can work with data of multiple +> types. For inheritance, those types are generally subclasses. > -> Rust instead uses generics to abstract over different possible types and -> trait bounds to impose constraints on what those types must provide. This is +> Rust instead uses generics to abstract over different possible types and trait +> bounds to impose constraints on what those types must provide. This is > sometimes called _bounded parametric polymorphism_. -Inheritance has recently fallen out of favor as a programming design solution -in many programming languages because it’s often at risk of sharing more code -than necessary. Subclasses shouldn’t always share all characteristics of their -parent class but will do so with inheritance. This can make a program’s design -less flexible. It also introduces the possibility of calling methods on -subclasses that don’t make sense or that cause errors because the methods don’t -apply to the subclass. In addition, some languages will only allow single -inheritance (meaning a subclass can only inherit from one class), further -restricting the flexibility of a program’s design. +Inheritance has recently fallen out of favor as a programming design solution in +many programming languages because it’s often at risk of sharing more code than +necessary. Subclasses shouldn’t always share all characteristics of their parent +class but will do so with inheritance. This can make a program’s design less +flexible. It also introduces the possibility of calling methods on subclasses +that don’t make sense or that cause errors because the methods don’t apply to +the subclass. In addition, some languages will only allow single inheritance +(meaning a subclass can only inherit from one class), further restricting the +flexibility of a program’s design. For these reasons, Rust takes the different approach of using trait objects instead of inheritance. Let’s look at how trait objects enable polymorphism in diff --git a/src/ch18-02-trait-objects.md b/src/ch18-02-trait-objects.md index 99e1e73900..de28f202b5 100644 --- a/src/ch18-02-trait-objects.md +++ b/src/ch18-02-trait-objects.md @@ -1,12 +1,12 @@ ## Using Trait Objects That Allow for Values of Different Types -In Chapter 8, we mentioned that one limitation of vectors is that they can -store elements of only one type. We created a workaround in Listing 8-9 where -we defined a `SpreadsheetCell` enum that had variants to hold integers, floats, -and text. This meant we could store different types of data in each cell and -still have a vector that represented a row of cells. This is a perfectly good -solution when our interchangeable items are a fixed set of types that we know -when our code is compiled. +In Chapter 8, we mentioned that one limitation of vectors is that they can store +elements of only one type. We created a workaround in Listing 8-9 where we +defined a `SpreadsheetCell` enum that had variants to hold integers, floats, and +text. This meant we could store different types of data in each cell and still +have a vector that represented a row of cells. This is a perfectly good solution +when our interchangeable items are a fixed set of types that we know when our +code is compiled. However, sometimes we want our library user to be able to extend the set of types that are valid in a particular situation. To show how we might achieve @@ -15,9 +15,8 @@ through a list of items, calling a `draw` method on each one to draw it to the screen—a common technique for GUI tools. We’ll create a library crate called `gui` that contains the structure of a GUI library. This crate might include some types for people to use, such as `Button` or `TextField`. In addition, -`gui` users will want to create their own types that can be drawn: for -instance, one programmer might add an `Image` and another might add a -`SelectBox`. +`gui` users will want to create their own types that can be drawn: for instance, +one programmer might add an `Image` and another might add a `SelectBox`. We won’t implement a fully fledged GUI library for this example but will show how the pieces would fit together. At the time of writing the library, we can’t @@ -31,10 +30,10 @@ To do this in a language with inheritance, we might define a class named `Component` that has a method named `draw` on it. The other classes, such as `Button`, `Image`, and `SelectBox`, would inherit from `Component` and thus inherit the `draw` method. They could each override the `draw` method to define -their custom behavior, but the framework could treat all of the types as if -they were `Component` instances and call `draw` on them. But because Rust -doesn’t have inheritance, we need another way to structure the `gui` library to -allow users to extend it with new types. +their custom behavior, but the framework could treat all of the types as if they +were `Component` instances and call `draw` on them. But because Rust doesn’t +have inheritance, we need another way to structure the `gui` library to allow +users to extend it with new types. ### Defining a Trait for Common Behavior @@ -45,21 +44,22 @@ implementing our specified trait and a table used to look up trait methods on that type at runtime. We create a trait object by specifying some sort of pointer, such as a `&` reference or a `Box<T>` smart pointer, then the `dyn` keyword, and then specifying the relevant trait. (We’ll talk about the reason -trait objects must use a pointer in Chapter 20 in the section [“Dynamically +trait objects must use a pointer in Chapter 20 in the section +[“Dynamically Sized Types and the `Sized` Trait.”][dynamically-sized]<!-- ignore -->) We can use trait objects in place of a generic or concrete type. Wherever we use a -trait object, Rust’s type system will ensure at compile time that any value -used in that context will implement the trait object’s trait. Consequently, we -don’t need to know all the possible types at compile time. +trait object, Rust’s type system will ensure at compile time that any value used +in that context will implement the trait object’s trait. Consequently, we don’t +need to know all the possible types at compile time. We’ve mentioned that, in Rust, we refrain from calling structs and enums “objects” to distinguish them from other languages’ objects. In a struct or enum, the data in the struct fields and the behavior in `impl` blocks are separated, whereas in other languages, the data and behavior combined into one concept is often labeled an object. However, trait objects _are_ more like -objects in other languages in the sense that they combine data and behavior. -But trait objects differ from traditional objects in that we can’t add data to -a trait object. Trait objects aren’t as generally useful as objects in other +objects in other languages in the sense that they combine data and behavior. But +trait objects differ from traditional objects in that we can’t add data to a +trait object. Trait objects aren’t as generally useful as objects in other languages: their specific purpose is to allow abstraction across common behavior. @@ -74,11 +74,11 @@ Listing 18-3 shows how to define a trait named `Draw` with one method named </Listing> -This syntax should look familiar from our discussions on how to define traits -in Chapter 10. Next comes some new syntax: Listing 18-4 defines a struct named +This syntax should look familiar from our discussions on how to define traits in +Chapter 10. Next comes some new syntax: Listing 18-4 defines a struct named `Screen` that holds a vector named `components`. This vector is of type -`Box<dyn Draw>`, which is a trait object; it’s a stand-in for any type inside -a `Box` that implements the `Draw` trait. +`Box<dyn Draw>`, which is a trait object; it’s a stand-in for any type inside a +`Box` that implements the `Draw` trait. <Listing number="18-4" file-name="src/lib.rs" caption="Definition of the `Screen` struct with a `components` field holding a vector of trait objects that implement the `Draw` trait"> @@ -99,12 +99,11 @@ On the `Screen` struct, we’ll define a method named `run` that will call the </Listing> -This works differently from defining a struct that uses a generic type -parameter with trait bounds. A generic type parameter can only be substituted -with one concrete type at a time, whereas trait objects allow for multiple -concrete types to fill in for the trait object at runtime. For example, we -could have defined the `Screen` struct using a generic type and a trait bound -as in Listing 18-6: +This works differently from defining a struct that uses a generic type parameter +with trait bounds. A generic type parameter can only be substituted with one +concrete type at a time, whereas trait objects allow for multiple concrete types +to fill in for the trait object at runtime. For example, we could have defined +the `Screen` struct using a generic type and a trait bound as in Listing 18-6: <Listing number="18-6" file-name="src/lib.rs" caption="An alternate implementation of the `Screen` struct and its `run` method using generics and trait bounds"> @@ -120,15 +119,15 @@ collections, using generics and trait bounds is preferable because the definitions will be monomorphized at compile time to use the concrete types. On the other hand, with the method using trait objects, one `Screen` instance -can hold a `Vec<T>` that contains a `Box<Button>` as well as a -`Box<TextField>`. Let’s look at how this works, and then we’ll talk about the -runtime performance implications. +can hold a `Vec<T>` that contains a `Box<Button>` as well as a `Box<TextField>`. +Let’s look at how this works, and then we’ll talk about the runtime performance +implications. ### Implementing the Trait Now we’ll add some types that implement the `Draw` trait. We’ll provide the -`Button` type. Again, actually implementing a GUI library is beyond the scope -of this book, so the `draw` method won’t have any useful implementation in its +`Button` type. Again, actually implementing a GUI library is beyond the scope of +this book, so the `draw` method won’t have any useful implementation in its body. To imagine what the implementation might look like, a `Button` struct might have fields for `width`, `height`, and `label`, as shown in Listing 18-7: @@ -142,13 +141,13 @@ might have fields for `width`, `height`, and `label`, as shown in Listing 18-7: The `width`, `height`, and `label` fields on `Button` will differ from the fields on other components; for example, a `TextField` type might have those -same fields plus a `placeholder` field. Each of the types we want to draw on -the screen will implement the `Draw` trait but will use different code in the -`draw` method to define how to draw that particular type, as `Button` has here -(without the actual GUI code, as mentioned). The `Button` type, for instance, -might have an additional `impl` block containing methods related to what -happens when a user clicks the button. These kinds of methods won’t apply to -types like `TextField`. +same fields plus a `placeholder` field. Each of the types we want to draw on the +screen will implement the `Draw` trait but will use different code in the `draw` +method to define how to draw that particular type, as `Button` has here (without +the actual GUI code, as mentioned). The `Button` type, for instance, might have +an additional `impl` block containing methods related to what happens when a +user clicks the button. These kinds of methods won’t apply to types like +`TextField`. If someone using our library decides to implement a `SelectBox` struct that has `width`, `height`, and `options` fields, they implement the `Draw` trait on the @@ -163,8 +162,8 @@ If someone using our library decides to implement a `SelectBox` struct that has </Listing> Our library’s user can now write their `main` function to create a `Screen` -instance. To the `Screen` instance, they can add a `SelectBox` and a `Button` -by putting each in a `Box<T>` to become a trait object. They can then call the +instance. To the `Screen` instance, they can add a `SelectBox` and a `Button` by +putting each in a `Box<T>` to become a trait object. They can then call the `run` method on the `Screen` instance, which will call `draw` on each of the components. Listing 18-9 shows this implementation: @@ -176,27 +175,26 @@ components. Listing 18-9 shows this implementation: </Listing> -When we wrote the library, we didn’t know that someone might add the -`SelectBox` type, but our `Screen` implementation was able to operate on the -new type and draw it because `SelectBox` implements the `Draw` trait, which -means it implements the `draw` method. +When we wrote the library, we didn’t know that someone might add the `SelectBox` +type, but our `Screen` implementation was able to operate on the new type and +draw it because `SelectBox` implements the `Draw` trait, which means it +implements the `draw` method. This concept—of being concerned only with the messages a value responds to -rather than the value’s concrete type—is similar to the concept of _duck -typing_ in dynamically typed languages: if it walks like a duck and quacks -like a duck, then it must be a duck! In the implementation of `run` on `Screen` -in Listing 18-5, `run` doesn’t need to know what the concrete type of each -component is. It doesn’t check whether a component is an instance of a `Button` -or a `SelectBox`, it just calls the `draw` method on the component. By -specifying `Box<dyn Draw>` as the type of the values in the `components` -vector, we’ve defined `Screen` to need values that we can call the `draw` -method on. +rather than the value’s concrete type—is similar to the concept of _duck typing_ +in dynamically typed languages: if it walks like a duck and quacks like a duck, +then it must be a duck! In the implementation of `run` on `Screen` in Listing +18-5, `run` doesn’t need to know what the concrete type of each component is. It +doesn’t check whether a component is an instance of a `Button` or a `SelectBox`, +it just calls the `draw` method on the component. By specifying `Box<dyn Draw>` +as the type of the values in the `components` vector, we’ve defined `Screen` to +need values that we can call the `draw` method on. The advantage of using trait objects and Rust’s type system to write code -similar to code using duck typing is that we never have to check whether a -value implements a particular method at runtime or worry about getting errors -if a value doesn’t implement a method but we call it anyway. Rust won’t compile -our code if the values don’t implement the traits that the trait objects need. +similar to code using duck typing is that we never have to check whether a value +implements a particular method at runtime or worry about getting errors if a +value doesn’t implement a method but we call it anyway. Rust won’t compile our +code if the values don’t implement the traits that the trait objects need. For example, Listing 18-10 shows what happens if we try to create a `Screen` with a `String` as a component: @@ -221,29 +219,30 @@ didn’t mean to pass and so should pass a different type or we should implement ### Trait Objects Perform Dynamic Dispatch -Recall in the [“Performance of Code Using -Generics”][performance-of-code-using-generics]<!-- ignore --> section in -Chapter 10 our discussion on the monomorphization process performed by the -compiler when we use trait bounds on generics: the compiler generates -nongeneric implementations of functions and methods for each concrete type that -we use in place of a generic type parameter. The code that results from -monomorphization is doing _static dispatch_, which is when the compiler knows -what method you’re calling at compile time. This is opposed to _dynamic -dispatch_, which is when the compiler can’t tell at compile time which method -you’re calling. In dynamic dispatch cases, the compiler emits code that at -runtime will figure out which method to call. +Recall in the +[“Performance of Code Using +Generics”][performance-of-code-using-generics]<!-- ignore --> section in Chapter +10 our discussion on the monomorphization process performed by the compiler when +we use trait bounds on generics: the compiler generates nongeneric +implementations of functions and methods for each concrete type that we use in +place of a generic type parameter. The code that results from monomorphization +is doing _static dispatch_, which is when the compiler knows what method you’re +calling at compile time. This is opposed to _dynamic dispatch_, which is when +the compiler can’t tell at compile time which method you’re calling. In dynamic +dispatch cases, the compiler emits code that at runtime will figure out which +method to call. When we use trait objects, Rust must use dynamic dispatch. The compiler doesn’t know all the types that might be used with the code that’s using trait objects, so it doesn’t know which method implemented on which type to call. Instead, at runtime, Rust uses the pointers inside the trait object to know which method to -call. This lookup incurs a runtime cost that doesn’t occur with static -dispatch. Dynamic dispatch also prevents the compiler from choosing to inline a -method’s code, which in turn prevents some optimizations, and Rust has some -rules about where you can and cannot use dynamic dispatch, called [_dyn -compatibility_][dyn-compatibility]. However, we did get extra flexibility in the code -that we wrote in Listing 18-5 and were able to support in Listing 18-9, so it’s -a trade-off to consider. +call. This lookup incurs a runtime cost that doesn’t occur with static dispatch. +Dynamic dispatch also prevents the compiler from choosing to inline a method’s +code, which in turn prevents some optimizations, and Rust has some rules about +where you can and cannot use dynamic dispatch, called +[_dyn compatibility_][dyn-compatibility]. However, we did get extra flexibility +in the code that we wrote in Listing 18-5 and were able to support in Listing +18-9, so it’s a trade-off to consider. [performance-of-code-using-generics]: ch10-01-syntax.html#performance-of-code-using-generics [dynamically-sized]: ch20-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait diff --git a/src/ch18-03-oo-design-patterns.md b/src/ch18-03-oo-design-patterns.md index 8f50a56fdb..86bd010b86 100644 --- a/src/ch18-03-oo-design-patterns.md +++ b/src/ch18-03-oo-design-patterns.md @@ -8,16 +8,15 @@ post struct that has a field to hold its state, which will be a state object from the set "draft", "review", or "published". The state objects share functionality: in Rust, of course, we use structs and -traits rather than objects and inheritance. Each state object is responsible -for its own behavior and for governing when it should change into another -state. The value that holds a state object knows nothing about the different -behavior of the states or when to transition between states. +traits rather than objects and inheritance. Each state object is responsible for +its own behavior and for governing when it should change into another state. The +value that holds a state object knows nothing about the different behavior of +the states or when to transition between states. -The advantage of using the state pattern is that, when the business -requirements of the program change, we won’t need to change the code of the -value holding the state or the code that uses the value. We’ll only need to -update the code inside one of the state objects to change its rules or perhaps -add more state objects. +The advantage of using the state pattern is that, when the business requirements +of the program change, we won’t need to change the code of the value holding the +state or the code that uses the value. We’ll only need to update the code inside +one of the state objects to change its rules or perhaps add more state objects. First, we’re going to implement the state pattern in a more traditional object-oriented way, then we’ll use an approach that’s a bit more natural in @@ -50,34 +49,33 @@ because we haven’t implemented the `blog` crate. We want to allow the user to create a new draft blog post with `Post::new`. We want to allow text to be added to the blog post. If we try to get the post’s -content immediately, before approval, we shouldn’t get any text because the -post is still a draft. We’ve added `assert_eq!` in the code for demonstration +content immediately, before approval, we shouldn’t get any text because the post +is still a draft. We’ve added `assert_eq!` in the code for demonstration purposes. An excellent unit test for this would be to assert that a draft blog post returns an empty string from the `content` method, but we’re not going to write tests for this example. Next, we want to enable a request for a review of the post, and we want `content` to return an empty string while waiting for the review. When the post -receives approval, it should get published, meaning the text of the post will -be returned when `content` is called. +receives approval, it should get published, meaning the text of the post will be +returned when `content` is called. Notice that the only type we’re interacting with from the crate is the `Post` type. This type will use the state pattern and will hold a value that will be one of three state objects representing the various states a post can be in—draft, waiting for review, or published. Changing from one state to another -will be managed internally within the `Post` type. The states change in -response to the methods called by our library’s users on the `Post` instance, -but they don’t have to manage the state changes directly. Also, users can’t -make a mistake with the states, like publishing a post before it’s reviewed. +will be managed internally within the `Post` type. The states change in response +to the methods called by our library’s users on the `Post` instance, but they +don’t have to manage the state changes directly. Also, users can’t make a +mistake with the states, like publishing a post before it’s reviewed. ### Defining `Post` and Creating a New Instance in the Draft State -Let’s get started on the implementation of the library! We know we need a -public `Post` struct that holds some content, so we’ll start with the -definition of the struct and an associated public `new` function to create an -instance of `Post`, as shown in Listing 18-12. We’ll also make a private -`State` trait that will define the behavior that all state objects for a `Post` -must have. +Let’s get started on the implementation of the library! We know we need a public +`Post` struct that holds some content, so we’ll start with the definition of the +struct and an associated public `new` function to create an instance of `Post`, +as shown in Listing 18-12. We’ll also make a private `State` trait that will +define the behavior that all state objects for a `Post` must have. Then `Post` will hold a trait object of `Box<dyn State>` inside an `Option<T>` in a private field named `state` to hold the state object. You’ll see why the @@ -94,15 +92,15 @@ in a private field named `state` to hold the state object. You’ll see why the The `State` trait defines the behavior shared by different post states. The state objects are `Draft`, `PendingReview`, and `Published`, and they will all implement the `State` trait. For now, the trait doesn’t have any methods, and -we’ll start by defining just the `Draft` state because that is the state we -want a post to start in. +we’ll start by defining just the `Draft` state because that is the state we want +a post to start in. When we create a new `Post`, we set its `state` field to a `Some` value that -holds a `Box`. This `Box` points to a new instance of the `Draft` struct. -This ensures whenever we create a new instance of `Post`, it will start out as -a draft. Because the `state` field of `Post` is private, there is no way to -create a `Post` in any other state! In the `Post::new` function, we set the -`content` field to a new, empty `String`. +holds a `Box`. This `Box` points to a new instance of the `Draft` struct. This +ensures whenever we create a new instance of `Post`, it will start out as a +draft. Because the `state` field of `Post` is private, there is no way to create +a `Post` in any other state! In the `Post::new` function, we set the `content` +field to a new, empty `String`. ### Storing the Text of the Post Content @@ -111,7 +109,8 @@ We saw in Listing 18-11 that we want to be able to call a method named blog post. We implement this as a method, rather than exposing the `content` field as `pub`, so that later we can implement a method that will control how the `content` field’s data is read. The `add_text` method is pretty -straightforward, so let’s add the implementation in Listing 18-13 to the `impl +straightforward, so let’s add the implementation in Listing 18-13 to the +`impl Post` block: <Listing number="18-13" file-name="src/lib.rs" caption="Implementing the `add_text` method to add text to a post’s `content`"> @@ -149,8 +148,8 @@ be empty. Listing 18-14 shows this placeholder implementation: </Listing> -With this added `content` method, everything in Listing 18-11 up to line 7 -works as intended. +With this added `content` method, everything in Listing 18-11 up to line 7 works +as intended. ### Requesting a Review of the Post Changes Its State @@ -171,20 +170,20 @@ current state of `Post`, and this second `request_review` method consumes the current state and returns a new state. We add the `request_review` method to the `State` trait; all types that -implement the trait will now need to implement the `request_review` method. -Note that rather than having `self`, `&self`, or `&mut self` as the first -parameter of the method, we have `self: Box<Self>`. This syntax means the -method is only valid when called on a `Box` holding the type. This syntax takes -ownership of `Box<Self>`, invalidating the old state so the state value of the -`Post` can transform into a new state. - -To consume the old state, the `request_review` method needs to take ownership -of the state value. This is where the `Option` in the `state` field of `Post` -comes in: we call the `take` method to take the `Some` value out of the `state` -field and leave a `None` in its place, because Rust doesn’t let us have -unpopulated fields in structs. This lets us move the `state` value out of -`Post` rather than borrowing it. Then we’ll set the post’s `state` value to the -result of this operation. +implement the trait will now need to implement the `request_review` method. Note +that rather than having `self`, `&self`, or `&mut self` as the first parameter +of the method, we have `self: Box<Self>`. This syntax means the method is only +valid when called on a `Box` holding the type. This syntax takes ownership of +`Box<Self>`, invalidating the old state so the state value of the `Post` can +transform into a new state. + +To consume the old state, the `request_review` method needs to take ownership of +the state value. This is where the `Option` in the `state` field of `Post` comes +in: we call the `take` method to take the `Some` value out of the `state` field +and leave a `None` in its place, because Rust doesn’t let us have unpopulated +fields in structs. This lets us move the `state` value out of `Post` rather than +borrowing it. Then we’ll set the post’s `state` value to the result of this +operation. We need to set `state` to `None` temporarily rather than setting it directly with code like `self.state = self.state.request_review();` to get ownership of @@ -213,9 +212,9 @@ Listing 18-11 now works up to line 10! ### Adding `approve` to Change the Behavior of `content` -The `approve` method will be similar to the `request_review` method: it will -set `state` to the value that the current state says it should have when that -state is approved, as shown in Listing 18-16: +The `approve` method will be similar to the `request_review` method: it will set +`state` to the value that the current state says it should have when that state +is approved, as shown in Listing 18-16: <Listing number="18-16" file-name="src/lib.rs" caption="Implementing the `approve` method on `Post` and the `State` trait"> @@ -233,13 +232,13 @@ Similar to the way `request_review` on `PendingReview` works, if we call the return `self`. When we call `approve` on `PendingReview`, it returns a new, boxed instance of the `Published` struct. The `Published` struct implements the `State` trait, and for both the `request_review` method and the `approve` -method, it returns itself, because the post should stay in the `Published` -state in those cases. +method, it returns itself, because the post should stay in the `Published` state +in those cases. -Now we need to update the `content` method on `Post`. We want the value -returned from `content` to depend on the current state of the `Post`, so we’re -going to have the `Post` delegate to a `content` method defined on its `state`, -as shown in Listing 18-17: +Now we need to update the `content` method on `Post`. We want the value returned +from `content` to depend on the current state of the `Post`, so we’re going to +have the `Post` delegate to a `content` method defined on its `state`, as shown +in Listing 18-17: <Listing number="18-17" file-name="src/lib.rs" caption="Updating the `content` method on `Post` to delegate to a `content` method on `State`"> @@ -255,15 +254,17 @@ instance (that is, `self`) as an argument. Then we return the value that’s returned from using the `content` method on the `state` value. We call the `as_ref` method on the `Option` because we want a reference to the -value inside the `Option` rather than ownership of the value. Because `state` -is an `Option<Box<dyn State>>`, when we call `as_ref`, an `Option<&Box<dyn -State>>` is returned. If we didn’t call `as_ref`, we would get an error because -we can’t move `state` out of the borrowed `&self` of the function parameter. +value inside the `Option` rather than ownership of the value. Because `state` is +an `Option<Box<dyn State>>`, when we call `as_ref`, an `Option<&Box<dyn +State>>` +is returned. If we didn’t call `as_ref`, we would get an error because we can’t +move `state` out of the borrowed `&self` of the function parameter. We then call the `unwrap` method, which we know will never panic, because we know the methods on `Post` ensure that `state` will always contain a `Some` value when those methods are done. This is one of the cases we talked about in -the [“Cases In Which You Have More Information Than the +the +[“Cases In Which You Have More Information Than the Compiler”][more-info-than-rustc]<!-- ignore --> section of Chapter 9 when we know that a `None` value is never possible, even though the compiler isn’t able to understand that. @@ -272,8 +273,8 @@ At this point, when we call `content` on the `&Box<dyn State>`, deref coercion will take effect on the `&` and the `Box` so the `content` method will ultimately be called on the type that implements the `State` trait. That means we need to add `content` to the `State` trait definition, and that is where -we’ll put the logic for what content to return depending on which state we -have, as shown in Listing 18-18: +we’ll put the logic for what content to return depending on which state we have, +as shown in Listing 18-18: <Listing number="18-18" file-name="src/lib.rs" caption="Adding the `content` method to the `State` trait"> @@ -284,8 +285,8 @@ have, as shown in Listing 18-18: </Listing> We add a default implementation for the `content` method that returns an empty -string slice. That means we don’t need to implement `content` on the `Draft` -and `PendingReview` structs. The `Published` struct will override the `content` +string slice. That means we don’t need to implement `content` on the `Draft` and +`PendingReview` structs. The `Published` struct will override the `content` method and return the value in `post.content`. Note that we need lifetime annotations on this method, as we discussed in @@ -294,17 +295,17 @@ reference to part of that `post`, so the lifetime of the returned reference is related to the lifetime of the `post` argument. And we’re done—all of Listing 18-11 now works! We’ve implemented the state -pattern with the rules of the blog post workflow. The logic related to the -rules lives in the state objects rather than being scattered throughout `Post`. +pattern with the rules of the blog post workflow. The logic related to the rules +lives in the state objects rather than being scattered throughout `Post`. > #### Why Not An Enum? > > You may have been wondering why we didn’t use an `enum` with the different -> possible post states as variants. That’s certainly a possible solution, try -> it and compare the end results to see which you prefer! One disadvantage of -> using an enum is every place that checks the value of the enum will need a -> `match` expression or similar to handle every possible variant. This could -> get more repetitive than this trait object solution. +> possible post states as variants. That’s certainly a possible solution, try it +> and compare the end results to see which you prefer! One disadvantage of using +> an enum is every place that checks the value of the enum will need a `match` +> expression or similar to handle every possible variant. This could get more +> repetitive than this trait object solution. ### Trade-offs of the State Pattern @@ -331,20 +332,20 @@ The implementation using the state pattern is easy to extend to add more functionality. To see the simplicity of maintaining code that uses the state pattern, try a few of these suggestions: -- Add a `reject` method that changes the post’s state from `PendingReview` back +* Add a `reject` method that changes the post’s state from `PendingReview` back to `Draft`. -- Require two calls to `approve` before the state can be changed to `Published`. -- Allow users to add text content only when a post is in the `Draft` state. +* Require two calls to `approve` before the state can be changed to `Published`. +* Allow users to add text content only when a post is in the `Draft` state. Hint: have the state object responsible for what might change about the content but not responsible for modifying the `Post`. One downside of the state pattern is that, because the states implement the transitions between states, some of the states are coupled to each other. If we add another state between `PendingReview` and `Published`, such as `Scheduled`, -we would have to change the code in `PendingReview` to transition to -`Scheduled` instead. It would be less work if `PendingReview` didn’t need to -change with the addition of a new state, but that would mean switching to -another design pattern. +we would have to change the code in `PendingReview` to transition to `Scheduled` +instead. It would be less work if `PendingReview` didn’t need to change with the +addition of a new state, but that would mean switching to another design +pattern. Another downside is that we’ve duplicated some logic. To eliminate some of the duplication, we might try to make default implementations for the @@ -362,8 +363,8 @@ repetition (see the [“Macros”][macros]<!-- ignore --> section in Chapter 20) By implementing the state pattern exactly as it’s defined for object-oriented languages, we’re not taking as full advantage of Rust’s strengths as we could. -Let’s look at some changes we can make to the `blog` crate that can make -invalid states and transitions into compile time errors. +Let’s look at some changes we can make to the `blog` crate that can make invalid +states and transitions into compile time errors. #### Encoding States and Behavior as Types @@ -386,12 +387,12 @@ Let’s consider the first part of `main` in Listing 18-11: We still enable the creation of new posts in the draft state using `Post::new` and the ability to add text to the post’s content. But instead of having a `content` method on a draft post that returns an empty string, we’ll make it so -draft posts don’t have the `content` method at all. That way, if we try to get -a draft post’s content, we’ll get a compiler error telling us the method -doesn’t exist. As a result, it will be impossible for us to accidentally -display draft post content in production, because that code won’t even compile. -Listing 18-19 shows the definition of a `Post` struct and a `DraftPost` struct, -as well as methods on each: +draft posts don’t have the `content` method at all. That way, if we try to get a +draft post’s content, we’ll get a compiler error telling us the method doesn’t +exist. As a result, it will be impossible for us to accidentally display draft +post content in production, because that code won’t even compile. Listing 18-19 +shows the definition of a `Post` struct and a `DraftPost` struct, as well as +methods on each: <Listing number="18-19" file-name="src/lib.rs" caption="A `Post` with a `content` method and `DraftPost` without a `content` method"> @@ -408,25 +409,25 @@ struct will represent a published post, and it has a `content` method that returns the `content`. We still have a `Post::new` function, but instead of returning an instance of -`Post`, it returns an instance of `DraftPost`. Because `content` is private -and there aren’t any functions that return `Post`, it’s not possible to create -an instance of `Post` right now. +`Post`, it returns an instance of `DraftPost`. Because `content` is private and +there aren’t any functions that return `Post`, it’s not possible to create an +instance of `Post` right now. -The `DraftPost` struct has an `add_text` method, so we can add text to -`content` as before, but note that `DraftPost` does not have a `content` method -defined! So now the program ensures all posts start as draft posts, and draft -posts don’t have their content available for display. Any attempt to get around -these constraints will result in a compiler error. +The `DraftPost` struct has an `add_text` method, so we can add text to `content` +as before, but note that `DraftPost` does not have a `content` method defined! +So now the program ensures all posts start as draft posts, and draft posts don’t +have their content available for display. Any attempt to get around these +constraints will result in a compiler error. #### Implementing Transitions as Transformations into Different Types -So how do we get a published post? We want to enforce the rule that a draft -post has to be reviewed and approved before it can be published. A post in the -pending review state should still not display any content. Let’s implement -these constraints by adding another struct, `PendingReviewPost`, defining the +So how do we get a published post? We want to enforce the rule that a draft post +has to be reviewed and approved before it can be published. A post in the +pending review state should still not display any content. Let’s implement these +constraints by adding another struct, `PendingReviewPost`, defining the `request_review` method on `DraftPost` to return a `PendingReviewPost`, and -defining an `approve` method on `PendingReviewPost` to return a `Post`, as -shown in Listing 18-20: +defining an `approve` method on `PendingReviewPost` to return a `Post`, as shown +in Listing 18-20: <Listing number="18-20" file-name="src/lib.rs" caption="A `PendingReviewPost` that gets created by calling `request_review` on `DraftPost` and an `approve` method that turns a `PendingReviewPost` into a published `Post`"> @@ -441,8 +442,8 @@ consuming the `DraftPost` and `PendingReviewPost` instances and transforming them into a `PendingReviewPost` and a published `Post`, respectively. This way, we won’t have any lingering `DraftPost` instances after we’ve called `request_review` on them, and so forth. The `PendingReviewPost` struct doesn’t -have a `content` method defined on it, so attempting to read its content -results in a compiler error, as with `DraftPost`. Because the only way to get a +have a `content` method defined on it, so attempting to read its content results +in a compiler error, as with `DraftPost`. Because the only way to get a published `Post` instance that does have a `content` method defined is to call the `approve` method on a `PendingReviewPost`, and the only way to get a `PendingReviewPost` is to call the `request_review` method on a `DraftPost`, @@ -450,8 +451,8 @@ we’ve now encoded the blog post workflow into the type system. But we also have to make some small changes to `main`. The `request_review` and `approve` methods return new instances rather than modifying the struct they’re -called on, so we need to add more `let post =` shadowing assignments to save -the returned instances. We also can’t have the assertions about the draft and +called on, so we need to add more `let post =` shadowing assignments to save the +returned instances. We also can’t have the assertions about the draft and pending review posts’ contents be empty strings, nor do we need them: we can’t compile code that tries to use the content of posts in those states any longer. The updated code in `main` is shown in Listing 18-21: @@ -473,18 +474,17 @@ compile time! This ensures that certain bugs, such as display of the content of an unpublished post, will be discovered before they make it to production. Try the tasks suggested at the start of this section on the `blog` crate as it -is after Listing 18-21 to see what you think about the design of this version -of the code. Note that some of the tasks might be completed already in this -design. +is after Listing 18-21 to see what you think about the design of this version of +the code. Note that some of the tasks might be completed already in this design. We’ve seen that even though Rust is capable of implementing object-oriented design patterns, other patterns, such as encoding state into the type system, are also available in Rust. These patterns have different trade-offs. Although -you might be very familiar with object-oriented patterns, rethinking the -problem to take advantage of Rust’s features can provide benefits, such as -preventing some bugs at compile time. Object-oriented patterns won’t always be -the best solution in Rust due to certain features, like ownership, that -object-oriented languages don’t have. +you might be very familiar with object-oriented patterns, rethinking the problem +to take advantage of Rust’s features can provide benefits, such as preventing +some bugs at compile time. Object-oriented patterns won’t always be the best +solution in Rust due to certain features, like ownership, that object-oriented +languages don’t have. ## Summary @@ -494,9 +494,8 @@ object-oriented features in Rust. Dynamic dispatch can give your code some flexibility in exchange for a bit of runtime performance. You can use this flexibility to implement object-oriented patterns that can help your code’s maintainability. Rust also has other features, like ownership, that -object-oriented languages don’t have. An object-oriented pattern won’t always -be the best way to take advantage of Rust’s strengths, but is an available -option. +object-oriented languages don’t have. An object-oriented pattern won’t always be +the best way to take advantage of Rust’s strengths, but is an available option. Next, we’ll look at patterns, which are another of Rust’s features that enable lots of flexibility. We’ve looked at them briefly throughout the book but diff --git a/src/ch19-00-patterns.md b/src/ch19-00-patterns.md index 4574ac0b8e..e9e636485b 100644 --- a/src/ch19-00-patterns.md +++ b/src/ch19-00-patterns.md @@ -2,25 +2,25 @@ _Patterns_ are a special syntax in Rust for matching against the structure of types, both complex and simple. Using patterns in conjunction with `match` -expressions and other constructs gives you more control over a program’s -control flow. A pattern consists of some combination of the following: +expressions and other constructs gives you more control over a program’s control +flow. A pattern consists of some combination of the following: -- Literals -- Destructured arrays, enums, structs, or tuples -- Variables -- Wildcards -- Placeholders +* Literals +* Destructured arrays, enums, structs, or tuples +* Variables +* Wildcards +* Placeholders Some example patterns include `x`, `(a, 3)`, and `Some(Color::Red)`. In the contexts in which patterns are valid, these components describe the shape of data. Our program then matches values against the patterns to determine whether it has the correct shape of data to continue running a particular piece of code. -To use a pattern, we compare it to some value. If the pattern matches the -value, we use the value parts in our code. Recall the `match` expressions in -Chapter 6 that used patterns, such as the coin-sorting machine example. If the -value fits the shape of the pattern, we can use the named pieces. If it -doesn’t, the code associated with the pattern won’t run. +To use a pattern, we compare it to some value. If the pattern matches the value, +we use the value parts in our code. Recall the `match` expressions in Chapter 6 +that used patterns, such as the coin-sorting machine example. If the value fits +the shape of the pattern, we can use the named pieces. If it doesn’t, the code +associated with the pattern won’t run. This chapter is a reference on all things related to patterns. We’ll cover the valid places to use patterns, the difference between refutable and irrefutable diff --git a/src/ch19-01-all-the-places-for-patterns.md b/src/ch19-01-all-the-places-for-patterns.md index 0db908a8a0..fedc85160e 100644 --- a/src/ch19-01-all-the-places-for-patterns.md +++ b/src/ch19-01-all-the-places-for-patterns.md @@ -8,8 +8,8 @@ valid. As discussed in Chapter 6, we use patterns in the arms of `match` expressions. Formally, `match` expressions are defined as the keyword `match`, a value to -match on, and one or more match arms that consist of a pattern and an -expression to run if the value matches that arm’s pattern, like this: +match on, and one or more match arms that consist of a pattern and an expression +to run if the value matches that arm’s pattern, like this: ```text match VALUE { @@ -29,34 +29,36 @@ match x { } ``` -The patterns in this `match` expression are the `None` and `Some(i)` on the -left of each arrow. +The patterns in this `match` expression are the `None` and `Some(i)` on the left +of each arrow. One requirement for `match` expressions is that they need to be _exhaustive_ in -the sense that all possibilities for the value in the `match` expression must -be accounted for. One way to ensure you’ve covered every possibility is to have -a catchall pattern for the last arm: for example, a variable name matching any +the sense that all possibilities for the value in the `match` expression must be +accounted for. One way to ensure you’ve covered every possibility is to have a +catchall pattern for the last arm: for example, a variable name matching any value can never fail and thus covers every remaining case. The particular pattern `_` will match anything, but it never binds to a variable, so it’s often used in the last match arm. The `_` pattern can be -useful when you want to ignore any value not specified, for example. We’ll -cover the `_` pattern in more detail in the [“Ignoring Values in a -Pattern”][ignoring-values-in-a-pattern]<!-- ignore --> section later in this -chapter. +useful when you want to ignore any value not specified, for example. We’ll cover +the `_` pattern in more detail in the +[“Ignoring Values in a Pattern”][ignoring-values-in-a-pattern]<!-- ignore --> +section later in this chapter. ### Conditional `if let` Expressions In Chapter 6 we discussed how to use `if let` expressions mainly as a shorter -way to write the equivalent of a `match` that only matches one case. -Optionally, `if let` can have a corresponding `else` containing code to run if -the pattern in the `if let` doesn’t match. +way to write the equivalent of a `match` that only matches one case. Optionally, +`if let` can have a corresponding `else` containing code to run if the pattern +in the `if let` doesn’t match. Listing 19-1 shows that it’s also possible to mix and match `if let`, `else -if`, and `else if let` expressions. Doing so gives us more flexibility than a -`match` expression in which we can express only one value to compare with the -patterns. Also, Rust doesn't require that the conditions in a series of `if -let`, `else if`, `else if let` arms relate to each other. +if`, +and `else if let` expressions. Doing so gives us more flexibility than a `match` +expression in which we can express only one value to compare with the patterns. +Also, Rust doesn't require that the conditions in a series of `if +let`, +`else if`, `else if let` arms relate to each other. The code in Listing 19-1 determines what color to make your background based on a series of checks for several conditions. For this example, we’ve created @@ -71,24 +73,27 @@ input. </Listing> -If the user specifies a favorite color, that color is used as the background. -If no favorite color is specified and today is Tuesday, the background color is +If the user specifies a favorite color, that color is used as the background. If +no favorite color is specified and today is Tuesday, the background color is green. Otherwise, if the user specifies their age as a string and we can parse it as a number successfully, the color is either purple or orange depending on -the value of the number. If none of these conditions apply, the background -color is blue. +the value of the number. If none of these conditions apply, the background color +is blue. This conditional structure lets us support complex requirements. With the -hardcoded values we have here, this example will print `Using purple as the +hardcoded values we have here, this example will print +`Using purple as the background color`. You can see that `if let` can also introduce new variables which shadow existing variables in the same way that `match` arms can: the line `if let Ok(age) = age` introduces a new `age` variable that contains the value inside the `Ok` variant, -shadowing the existing `age` variable. This means we need to place the `if age > -30` condition within that block: we can’t combine these two conditions into `if -let Ok(age) = age && age > 30`. The new `age` we want to compare to 30 isn’t -valid until the new scope starts with the curly bracket. +shadowing the existing `age` variable. This means we need to place the +`if age > +30` condition within that block: we can’t combine these two conditions +into `if +let Ok(age) = age && age > 30`. The new `age` we want to compare to 30 +isn’t valid until the new scope starts with the curly bracket. The downside of using `if let` expressions is that the compiler doesn’t check for exhaustiveness, whereas with `match` expressions it does. If we omitted the @@ -120,10 +125,10 @@ produces an `Err` once the sender side disconnects. ### `for` Loops -In a `for` loop, the value that directly follows the keyword `for` is a -pattern. For example, in `for x in y` the `x` is the pattern. Listing 19-3 -demonstrates how to use a pattern in a `for` loop to destructure, or break -apart, a tuple as part of the `for` loop. +In a `for` loop, the value that directly follows the keyword `for` is a pattern. +For example, in `for x in y` the `x` is the pattern. Listing 19-3 demonstrates +how to use a pattern in a `for` loop to destructure, or break apart, a tuple as +part of the `for` loop. <Listing number="19-3" caption="Using a pattern in a `for` loop to destructure a tuple"> @@ -139,11 +144,10 @@ The code in Listing 19-3 will print the following: {{#include ../listings/ch19-patterns-and-matching/listing-19-03/output.txt}} ``` -We adapt an iterator using the `enumerate` method so it produces a value and -the index for that value, placed into a tuple. The first value produced is the -tuple `(0, 'a')`. When this value is matched to the pattern `(index, value)`, -`index` will be `0` and `value` will be `'a'`, printing the first line of the -output. +We adapt an iterator using the `enumerate` method so it produces a value and the +index for that value, placed into a tuple. The first value produced is the tuple +`(0, 'a')`. When this value is matched to the pattern `(index, value)`, `index` +will be `0` and `value` will be `'a'`, printing the first line of the output. ### `let` Statements @@ -165,14 +169,14 @@ let PATTERN = EXPRESSION; ``` In statements like `let x = 5;` with a variable name in the `PATTERN` slot, the -variable name is just a particularly simple form of a pattern. Rust compares -the expression against the pattern and assigns any names it finds. So in the -`let x = 5;` example, `x` is a pattern that means “bind what matches here to -the variable `x`.” Because the name `x` is the whole pattern, this pattern +variable name is just a particularly simple form of a pattern. Rust compares the +expression against the pattern and assigns any names it finds. So in the +`let x = 5;` example, `x` is a pattern that means “bind what matches here to the +variable `x`.” Because the name `x` is the whole pattern, this pattern effectively means “bind everything to the variable `x`, whatever the value is.” -To see the pattern matching aspect of `let` more clearly, consider Listing -19-4, which uses a pattern with `let` to destructure a tuple. +To see the pattern matching aspect of `let` more clearly, consider Listing 19-4, +which uses a pattern with `let` to destructure a tuple. <Listing number="19-4" caption="Using a pattern to destructure a tuple and create three variables at once"> @@ -182,13 +186,13 @@ To see the pattern matching aspect of `let` more clearly, consider Listing </Listing> -Here, we match a tuple against a pattern. Rust compares the value `(1, 2, 3)` -to the pattern `(x, y, z)` and sees that the value matches the pattern, so Rust +Here, we match a tuple against a pattern. Rust compares the value `(1, 2, 3)` to +the pattern `(x, y, z)` and sees that the value matches the pattern, so Rust binds `1` to `x`, `2` to `y`, and `3` to `z`. You can think of this tuple pattern as nesting three individual variable patterns inside it. -If the number of elements in the pattern doesn’t match the number of elements -in the tuple, the overall type won’t match and we’ll get a compiler error. For +If the number of elements in the pattern doesn’t match the number of elements in +the tuple, the overall type won’t match and we’ll get a compiler error. For example, Listing 19-5 shows an attempt to destructure a tuple with three elements into two variables, which won’t work. @@ -207,11 +211,11 @@ Attempting to compile this code results in this type error: ``` To fix the error, we could ignore one or more of the values in the tuple using -`_` or `..`, as you’ll see in the [“Ignoring Values in a -Pattern”][ignoring-values-in-a-pattern]<!-- ignore --> section. If the problem -is that we have too many variables in the pattern, the solution is to make the -types match by removing variables so the number of variables equals the number -of elements in the tuple. +`_` or `..`, as you’ll see in the +[“Ignoring Values in a Pattern”][ignoring-values-in-a-pattern]<!-- ignore --> +section. If the problem is that we have too many variables in the pattern, the +solution is to make the types match by removing variables so the number of +variables equals the number of elements in the tuple. ### Function Parameters diff --git a/src/ch19-02-refutability.md b/src/ch19-02-refutability.md index 515358879c..ae780ab8dc 100644 --- a/src/ch19-02-refutability.md +++ b/src/ch19-02-refutability.md @@ -2,25 +2,26 @@ Patterns come in two forms: refutable and irrefutable. Patterns that will match for any possible value passed are _irrefutable_. An example would be `x` in the -statement `let x = 5;` because `x` matches anything and therefore cannot fail -to match. Patterns that can fail to match for some possible value are -_refutable_. An example would be `Some(x)` in the expression `if let Some(x) = -a_value` because if the value in the `a_value` variable is `None` rather than -`Some`, the `Some(x)` pattern will not match. +statement `let x = 5;` because `x` matches anything and therefore cannot fail to +match. Patterns that can fail to match for some possible value are _refutable_. +An example would be `Some(x)` in the expression `if let Some(x) = +a_value` +because if the value in the `a_value` variable is `None` rather than `Some`, the +`Some(x)` pattern will not match. Function parameters, `let` statements, and `for` loops can only accept irrefutable patterns, because the program cannot do anything meaningful when -values don’t match. The `if let` and `while let` expressions accept -refutable and irrefutable patterns, but the compiler warns against -irrefutable patterns because by definition they’re intended to handle possible -failure: the functionality of a conditional is in its ability to perform -differently depending on success or failure. +values don’t match. The `if let` and `while let` expressions accept refutable +and irrefutable patterns, but the compiler warns against irrefutable patterns +because by definition they’re intended to handle possible failure: the +functionality of a conditional is in its ability to perform differently +depending on success or failure. In general, you shouldn’t have to worry about the distinction between refutable and irrefutable patterns; however, you do need to be familiar with the concept -of refutability so you can respond when you see it in an error message. In -those cases, you’ll need to change either the pattern or the construct you’re -using the pattern with, depending on the intended behavior of the code. +of refutability so you can respond when you see it in an error message. In those +cases, you’ll need to change either the pattern or the construct you’re using +the pattern with, depending on the intended behavior of the code. Let’s look at an example of what happens when we try to use a refutable pattern where Rust requires an irrefutable pattern and vice versa. Listing 19-8 shows a @@ -45,14 +46,14 @@ use a refutable pattern where an irrefutable pattern is required: {{#include ../listings/ch19-patterns-and-matching/listing-19-08/output.txt}} ``` -Because we didn’t cover (and couldn’t cover!) every valid value with the -pattern `Some(x)`, Rust rightfully produces a compiler error. +Because we didn’t cover (and couldn’t cover!) every valid value with the pattern +`Some(x)`, Rust rightfully produces a compiler error. If we have a refutable pattern where an irrefutable pattern is needed, we can fix it by changing the code that uses the pattern: instead of using `let`, we -can use `if let`. Then if the pattern doesn’t match, the code will just skip -the code in the curly brackets, giving it a way to continue validly. Listing -19-9 shows how to fix the code in Listing 19-8. +can use `if let`. Then if the pattern doesn’t match, the code will just skip the +code in the curly brackets, giving it a way to continue validly. Listing 19-9 +shows how to fix the code in Listing 19-8. <Listing number="19-9" caption="Using `if let` and a block with refutable patterns instead of `let`"> @@ -62,10 +63,9 @@ the code in the curly brackets, giving it a way to continue validly. Listing </Listing> -We’ve given the code an out! This code is perfectly valid now. However, -if we give `if let` an irrefutable pattern (a pattern that will always -match), such as `x`, as shown in Listing 19-10, the compiler will give a -warning. +We’ve given the code an out! This code is perfectly valid now. However, if we +give `if let` an irrefutable pattern (a pattern that will always match), such as +`x`, as shown in Listing 19-10, the compiler will give a warning. <Listing number="19-10" caption="Attempting to use an irrefutable pattern with `if let`"> @@ -84,10 +84,9 @@ pattern: For this reason, match arms must use refutable patterns, except for the last arm, which should match any remaining values with an irrefutable pattern. Rust -allows us to use an irrefutable pattern in a `match` with only one arm, but -this syntax isn’t particularly useful and could be replaced with a simpler -`let` statement. +allows us to use an irrefutable pattern in a `match` with only one arm, but this +syntax isn’t particularly useful and could be replaced with a simpler `let` +statement. -Now that you know where to use patterns and the difference between refutable -and irrefutable patterns, let’s cover all the syntax we can use to create -patterns. +Now that you know where to use patterns and the difference between refutable and +irrefutable patterns, let’s cover all the syntax we can use to create patterns. diff --git a/src/ch19-03-pattern-syntax.md b/src/ch19-03-pattern-syntax.md index 21c1137d95..379f690d6a 100644 --- a/src/ch19-03-pattern-syntax.md +++ b/src/ch19-03-pattern-syntax.md @@ -12,20 +12,19 @@ following code gives some examples: {{#rustdoc_include ../listings/ch19-patterns-and-matching/no-listing-01-literals/src/main.rs:here}} ``` -This code prints `one` because the value in `x` is 1. This syntax is useful -when you want your code to take an action if it gets a particular concrete -value. +This code prints `one` because the value in `x` is 1. This syntax is useful when +you want your code to take an action if it gets a particular concrete value. ### Matching Named Variables Named variables are irrefutable patterns that match any value, and we’ve used -them many times in the book. However, there is a complication when you use -named variables in `match` expressions. Because `match` starts a new scope, -variables declared as part of a pattern inside the `match` expression will -shadow those with the same name outside the `match` construct, as is the case -with all variables. In Listing 19-11, we declare a variable named `x` with the -value `Some(5)` and a variable `y` with the value `10`. We then create a -`match` expression on the value `x`. Look at the patterns in the match arms and +them many times in the book. However, there is a complication when you use named +variables in `match` expressions. Because `match` starts a new scope, variables +declared as part of a pattern inside the `match` expression will shadow those +with the same name outside the `match` construct, as is the case with all +variables. In Listing 19-11, we declare a variable named `x` with the value +`Some(5)` and a variable `y` with the value `10`. We then create a `match` +expression on the value `x`. Look at the patterns in the match arms and `println!` at the end, and try to figure out what the code will print before running this code or reading further. @@ -37,8 +36,8 @@ running this code or reading further. </Listing> -Let’s walk through what happens when the `match` expression runs. The pattern -in the first match arm doesn’t match the defined value of `x`, so the code +Let’s walk through what happens when the `match` expression runs. The pattern in +the first match arm doesn’t match the defined value of `x`, so the code continues. The pattern in the second match arm introduces a new variable named `y` that @@ -53,7 +52,8 @@ If `x` had been a `None` value instead of `Some(5)`, the patterns in the first two arms wouldn’t have matched, so the value would have matched to the underscore. We didn’t introduce the `x` variable in the pattern of the underscore arm, so the `x` in the expression is still the outer `x` that hasn’t -been shadowed. In this hypothetical case, the `match` would print `Default +been shadowed. In this hypothetical case, the `match` would print +`Default case, x = None`. When the `match` expression is done, its scope ends, and so does the scope of @@ -62,7 +62,8 @@ the inner `y`. The last `println!` produces `at the end: x = Some(5), y = 10`. To create a `match` expression that compares the values of the outer `x` and `y`, rather than introducing a new variable which shadows the existing `y` variable, we would need to use a match guard conditional instead. We’ll talk -about match guards later in the [“Extra Conditionals with Match +about match guards later in the +[“Extra Conditionals with Match Guards”](#extra-conditionals-with-match-guards)<!-- ignore --> section. ### Multiple Patterns @@ -70,8 +71,8 @@ Guards”](#extra-conditionals-with-match-guards)<!-- ignore --> section. In `match` expressions, you can match multiple patterns using the `|` syntax, which is the pattern _or_ operator. For example, in the following code we match the value of `x` against the match arms, the first of which has an _or_ option, -meaning if the value of `x` matches either of the values in that arm, that -arm’s code will run: +meaning if the value of `x` matches either of the values in that arm, that arm’s +code will run: ```rust {{#rustdoc_include ../listings/ch19-patterns-and-matching/no-listing-02-multiple-patterns/src/main.rs:here}} @@ -82,8 +83,8 @@ This code prints `one or two`. ### Matching Ranges of Values with `..=` The `..=` syntax allows us to match to an inclusive range of values. In the -following code, when a pattern matches any of the values within the given -range, that arm will execute: +following code, when a pattern matches any of the values within the given range, +that arm will execute: ```rust {{#rustdoc_include ../listings/ch19-patterns-and-matching/no-listing-03-ranges/src/main.rs:here}} @@ -105,7 +106,8 @@ Here is an example using ranges of `char` values: {{#rustdoc_include ../listings/ch19-patterns-and-matching/no-listing-04-ranges-of-char/src/main.rs:here}} ``` -Rust can tell that `'c'` is within the first pattern’s range and prints `early +Rust can tell that `'c'` is within the first pattern’s range and prints +`early ASCII letter`. ### Destructuring to Break Apart Values @@ -126,17 +128,17 @@ break apart using a pattern with a `let` statement. </Listing> -This code creates the variables `a` and `b` that match the values of the `x` -and `y` fields of the `p` struct. This example shows that the names of the -variables in the pattern don’t have to match the field names of the struct. -However, it’s common to match the variable names to the field names to make it -easier to remember which variables came from which fields. Because of this -common usage, and because writing `let Point { x: x, y: y } = p;` contains a -lot of duplication, Rust has a shorthand for patterns that match struct fields: -you only need to list the name of the struct field, and the variables created -from the pattern will have the same names. Listing 19-13 behaves in the same -way as the code in Listing 19-12, but the variables created in the `let` -pattern are `x` and `y` instead of `a` and `b`. +This code creates the variables `a` and `b` that match the values of the `x` and +`y` fields of the `p` struct. This example shows that the names of the variables +in the pattern don’t have to match the field names of the struct. However, it’s +common to match the variable names to the field names to make it easier to +remember which variables came from which fields. Because of this common usage, +and because writing `let Point { x: x, y: y } = p;` contains a lot of +duplication, Rust has a shorthand for patterns that match struct fields: you +only need to list the name of the struct field, and the variables created from +the pattern will have the same names. Listing 19-13 behaves in the same way as +the code in Listing 19-12, but the variables created in the `let` pattern are +`x` and `y` instead of `a` and `b`. <Listing number="19-13" file-name="src/main.rs" caption="Destructuring struct fields using struct field shorthand"> @@ -146,14 +148,14 @@ pattern are `x` and `y` instead of `a` and `b`. </Listing> -This code creates the variables `x` and `y` that match the `x` and `y` fields -of the `p` variable. The outcome is that the variables `x` and `y` contain the +This code creates the variables `x` and `y` that match the `x` and `y` fields of +the `p` variable. The outcome is that the variables `x` and `y` contain the values from the `p` struct. -We can also destructure with literal values as part of the struct pattern -rather than creating variables for all the fields. Doing so allows us to test -some of the fields for particular values while creating variables to -destructure the other fields. +We can also destructure with literal values as part of the struct pattern rather +than creating variables for all the fields. Doing so allows us to test some of +the fields for particular values while creating variables to destructure the +other fields. In Listing 19-14, we have a `match` expression that separates `Point` values into three cases: points that lie directly on the `x` axis (which is true when @@ -188,8 +190,8 @@ and the `y` axis, this code would only print `On the x axis at 0`. We've destructured enums in this book (for example, Listing 6-5 in Chapter 6), but haven’t yet explicitly discussed that the pattern to destructure an enum corresponds to the way the data stored within the enum is defined. As an -example, in Listing 19-15 we use the `Message` enum from Listing 6-2 and write -a `match` with patterns that will destructure each inner value. +example, in Listing 19-15 we use the `Message` enum from Listing 6-2 and write a +`match` with patterns that will destructure each inner value. <Listing number="19-15" file-name="src/main.rs" caption="Destructuring enum variants that hold different kinds of values"> @@ -209,8 +211,8 @@ and no variables are in that pattern. For struct-like enum variants, such as `Message::Move`, we can use a pattern similar to the pattern we specify to match structs. After the variant name, we place curly brackets and then list the fields with variables so we break apart -the pieces to use in the code for this arm. Here we use the shorthand form as -we did in Listing 19-13. +the pieces to use in the code for this arm. Here we use the shorthand form as we +did in Listing 19-13. For tuple-like enum variants, like `Message::Write` that holds a tuple with one element and `Message::ChangeColor` that holds a tuple with three elements, the @@ -220,10 +222,10 @@ matching. #### Destructuring Nested Structs and Enums -So far, our examples have all been matching structs or enums one level deep, -but matching can work on nested items too! For example, we can refactor the -code in Listing 19-15 to support RGB and HSV colors in the `ChangeColor` -message, as shown in Listing 19-16. +So far, our examples have all been matching structs or enums one level deep, but +matching can work on nested items too! For example, we can refactor the code in +Listing 19-15 to support RGB and HSV colors in the `ChangeColor` message, as +shown in Listing 19-16. <Listing number="19-16" caption="Matching on nested enums"> @@ -235,10 +237,10 @@ message, as shown in Listing 19-16. The pattern of the first arm in the `match` expression matches a `Message::ChangeColor` enum variant that contains a `Color::Rgb` variant; then -the pattern binds to the three inner `i32` values. The pattern of the second -arm also matches a `Message::ChangeColor` enum variant, but the inner enum -matches `Color::Hsv` instead. We can specify these complex conditions in one -`match` expression, even though two enums are involved. +the pattern binds to the three inner `i32` values. The pattern of the second arm +also matches a `Message::ChangeColor` enum variant, but the inner enum matches +`Color::Hsv` instead. We can specify these complex conditions in one `match` +expression, even though two enums are involved. #### Destructuring Structs and Tuples @@ -253,18 +255,18 @@ tuples inside a tuple and destructure all the primitive values out: This code lets us break complex types into their component parts so we can use the values we’re interested in separately. -Destructuring with patterns is a convenient way to use pieces of values, such -as the value from each field in a struct, separately from each other. +Destructuring with patterns is a convenient way to use pieces of values, such as +the value from each field in a struct, separately from each other. ### Ignoring Values in a Pattern -You’ve seen that it’s sometimes useful to ignore values in a pattern, such as -in the last arm of a `match`, to get a catchall that doesn’t actually do -anything but does account for all remaining possible values. There are a few -ways to ignore entire values or parts of values in a pattern: using the `_` -pattern (which you’ve seen), using the `_` pattern within another pattern, -using a name that starts with an underscore, or using `..` to ignore remaining -parts of a value. Let’s explore how and why to use each of these patterns. +You’ve seen that it’s sometimes useful to ignore values in a pattern, such as in +the last arm of a `match`, to get a catchall that doesn’t actually do anything +but does account for all remaining possible values. There are a few ways to +ignore entire values or parts of values in a pattern: using the `_` pattern +(which you’ve seen), using the `_` pattern within another pattern, using a name +that starts with an underscore, or using `..` to ignore remaining parts of a +value. Let’s explore how and why to use each of these patterns. #### Ignoring an Entire Value with `_` @@ -281,16 +283,16 @@ parameters, as shown in Listing 19-17. </Listing> -This code will completely ignore the value `3` passed as the first argument, -and will print `This code only uses the y parameter: 4`. +This code will completely ignore the value `3` passed as the first argument, and +will print `This code only uses the y parameter: 4`. -In most cases when you no longer need a particular function parameter, you -would change the signature so it doesn’t include the unused parameter. Ignoring -a function parameter can be especially useful in cases when, for example, -you're implementing a trait when you need a certain type signature but the -function body in your implementation doesn’t need one of the parameters. You -then avoid getting a compiler warning about unused function parameters, as you -would if you used a name instead. +In most cases when you no longer need a particular function parameter, you would +change the signature so it doesn’t include the unused parameter. Ignoring a +function parameter can be especially useful in cases when, for example, you're +implementing a trait when you need a certain type signature but the function +body in your implementation doesn’t need one of the parameters. You then avoid +getting a compiler warning about unused function parameters, as you would if you +used a name instead. #### Ignoring Parts of a Value with a Nested `_` @@ -316,8 +318,8 @@ when `setting_value` and `new_setting_value` are the `Some` variant. In that case, we print the reason for not changing `setting_value`, and it doesn’t get changed. -In all other cases (if either `setting_value` or `new_setting_value` are -`None`) expressed by the `_` pattern in the second arm, we want to allow +In all other cases (if either `setting_value` or `new_setting_value` are `None`) +expressed by the `_` pattern in the second arm, we want to allow `new_setting_value` to become `setting_value`. We can also use underscores in multiple places within one pattern to ignore @@ -340,10 +342,10 @@ ignored. If you create a variable but don’t use it anywhere, Rust will usually issue a warning because an unused variable could be a bug. However, sometimes it’s useful to be able to create a variable you won’t use yet, such as when you’re -prototyping or just starting a project. In this situation, you can tell Rust -not to warn you about the unused variable by starting the name of the variable -with an underscore. In Listing 19-20, we create two unused variables, but when -we compile this code, we should only get a warning about one of them. +prototyping or just starting a project. In this situation, you can tell Rust not +to warn you about the unused variable by starting the name of the variable with +an underscore. In Listing 19-20, we create two unused variables, but when we +compile this code, we should only get a warning about one of them. <Listing number="19-20" file-name="src/main.rs" caption="Starting a variable name with an underscore to avoid getting unused variable warnings"> @@ -358,8 +360,8 @@ warning about not using `_x`. Note that there is a subtle difference between using only `_` and using a name that starts with an underscore. The syntax `_x` still binds the value to the -variable, whereas `_` doesn’t bind at all. To show a case where this -distinction matters, Listing 19-21 will provide us with an error. +variable, whereas `_` doesn’t bind at all. To show a case where this distinction +matters, Listing 19-21 will provide us with an error. <Listing number="19-21" caption="An unused variable starting with an underscore still binds the value, which might take ownership of the value"> @@ -391,8 +393,8 @@ parts and ignore the rest, avoiding the need to list underscores for each ignored value. The `..` pattern ignores any parts of a value that we haven’t explicitly matched in the rest of the pattern. In Listing 19-23, we have a `Point` struct that holds a coordinate in three-dimensional space. In the -`match` expression, we want to operate only on the `x` coordinate and ignore -the values in the `y` and `z` fields. +`match` expression, we want to operate only on the `x` coordinate and ignore the +values in the `y` and `z` fields. <Listing number="19-23" caption="Ignoring all fields of a `Point` except for `x` by using `..`"> @@ -441,12 +443,12 @@ When we compile this example, we get this error: ``` It’s impossible for Rust to determine how many values in the tuple to ignore -before matching a value with `second` and then how many further values to -ignore thereafter. This code could mean that we want to ignore `2`, bind -`second` to `4`, and then ignore `8`, `16`, and `32`; or that we want to ignore -`2` and `4`, bind `second` to `8`, and then ignore `16` and `32`; and so forth. -The variable name `second` doesn’t mean anything special to Rust, so we get a -compiler error because using `..` in two places like this is ambiguous. +before matching a value with `second` and then how many further values to ignore +thereafter. This code could mean that we want to ignore `2`, bind `second` to +`4`, and then ignore `8`, `16`, and `32`; or that we want to ignore `2` and `4`, +bind `second` to `8`, and then ignore `16` and `32`; and so forth. The variable +name `second` doesn’t mean anything special to Rust, so we get a compiler error +because using `..` in two places like this is ambiguous. ### Extra Conditionals with Match Guards @@ -455,8 +457,8 @@ a `match` arm, that must also match for that arm to be chosen. Match guards are useful for expressing more complex ideas than a pattern alone allows. The condition can use variables created in the pattern. Listing 19-26 shows a -`match` where the first arm has the pattern `Some(x)` and also has a match -guard of `if x % 2 == 0` (which will be true if the number is even). +`match` where the first arm has the pattern `Some(x)` and also has a match guard +of `if x % 2 == 0` (which will be true if the number is even). <Listing number="19-26" caption="Adding a match guard to a pattern"> @@ -468,25 +470,24 @@ guard of `if x % 2 == 0` (which will be true if the number is even). This example will print `The number 4 is even`. When `num` is compared to the pattern in the first arm, it matches, because `Some(4)` matches `Some(x)`. Then -the match guard checks whether the remainder of dividing `x` by 2 is equal to -0, and because it is, the first arm is selected. +the match guard checks whether the remainder of dividing `x` by 2 is equal to 0, +and because it is, the first arm is selected. -If `num` had been `Some(5)` instead, the match guard in the first arm would -have been false because the remainder of 5 divided by 2 is 1, which is not -equal to 0. Rust would then go to the second arm, which would match because the -second arm doesn’t have a match guard and therefore matches any `Some` variant. +If `num` had been `Some(5)` instead, the match guard in the first arm would have +been false because the remainder of 5 divided by 2 is 1, which is not equal +to 0. Rust would then go to the second arm, which would match because the second +arm doesn’t have a match guard and therefore matches any `Some` variant. There is no way to express the `if x % 2 == 0` condition within a pattern, so -the match guard gives us the ability to express this logic. The downside of -this additional expressiveness is that the compiler doesn't try to check for +the match guard gives us the ability to express this logic. The downside of this +additional expressiveness is that the compiler doesn't try to check for exhaustiveness when match guard expressions are involved. In Listing 19-11, we mentioned that we could use match guards to solve our pattern-shadowing problem. Recall that we created a new variable inside the pattern in the `match` expression instead of using the variable outside the -`match`. That new variable meant we couldn’t test against the value of the -outer variable. Listing 19-27 shows how we can use a match guard to fix this -problem. +`match`. That new variable meant we couldn’t test against the value of the outer +variable. Listing 19-27 shows how we can use a match guard to fix this problem. <Listing number="19-27" file-name="src/main.rs" caption="Using a match guard to test for equality with an outer variable"> @@ -511,9 +512,9 @@ we can look for a value that has the same value as the outer `y` by comparing You can also use the _or_ operator `|` in a match guard to specify multiple patterns; the match guard condition will apply to all the patterns. Listing 19-28 shows the precedence when combining a pattern that uses `|` with a match -guard. The important part of this example is that the `if y` match guard -applies to `4`, `5`, _and_ `6`, even though it might look like `if y` only -applies to `6`. +guard. The important part of this example is that the `if y` match guard applies +to `4`, `5`, _and_ `6`, even though it might look like `if y` only applies to +`6`. <Listing number="19-28" caption="Combining multiple patterns with a match guard"> @@ -565,13 +566,14 @@ field, but for this example we’ll use a different name. </Listing> This example will print `Found an id in range: 5`. By specifying `id_variable -@` before the range `3..=7`, we’re capturing whatever value matched the range -while also testing that the value matched the range pattern. +@` +before the range `3..=7`, we’re capturing whatever value matched the range while +also testing that the value matched the range pattern. In the second arm, where we only have a range specified in the pattern, the code associated with the arm doesn’t have a variable that contains the actual value -of the `id` field. The `id` field’s value could have been 10, 11, or 12, but -the code that goes with that pattern doesn’t know which it is. The pattern code +of the `id` field. The `id` field’s value could have been 10, 11, or 12, but the +code that goes with that pattern doesn’t know which it is. The pattern code isn’t able to use the value from the `id` field, because we haven’t saved the `id` value in a variable. diff --git a/src/ch20-00-advanced-features.md b/src/ch20-00-advanced-features.md index 049681b5da..3e612d2af2 100644 --- a/src/ch20-00-advanced-features.md +++ b/src/ch20-00-advanced-features.md @@ -10,13 +10,13 @@ grasp of all the features Rust has to offer. In this chapter, we’ll cover: -- Unsafe Rust: how to opt out of some of Rust’s guarantees and take +* Unsafe Rust: how to opt out of some of Rust’s guarantees and take responsibility for manually upholding those guarantees -- Advanced traits: associated types, default type parameters, fully qualified +* Advanced traits: associated types, default type parameters, fully qualified syntax, supertraits, and the newtype pattern in relation to traits -- Advanced types: more about the newtype pattern, type aliases, the never type, +* Advanced types: more about the newtype pattern, type aliases, the never type, and dynamically sized types -- Advanced functions and closures: function pointers and returning closures -- Macros: ways to define code that defines more code at compile time +* Advanced functions and closures: function pointers and returning closures +* Macros: ways to define code that defines more code at compile time It’s a panoply of Rust features with something for everyone! Let’s dive in! diff --git a/src/ch20-01-unsafe-rust.md b/src/ch20-01-unsafe-rust.md index 3e6147fe97..0b51eb5188 100644 --- a/src/ch20-01-unsafe-rust.md +++ b/src/ch20-01-unsafe-rust.md @@ -5,23 +5,23 @@ enforced at compile time. However, Rust has a second language hidden inside it that doesn’t enforce these memory safety guarantees: it’s called _unsafe Rust_ and works just like regular Rust, but gives us extra superpowers. -Unsafe Rust exists because, by nature, static analysis is conservative. When -the compiler tries to determine whether or not code upholds the guarantees, -it’s better for it to reject some valid programs than to accept some invalid +Unsafe Rust exists because, by nature, static analysis is conservative. When the +compiler tries to determine whether or not code upholds the guarantees, it’s +better for it to reject some valid programs than to accept some invalid programs. Although the code _might_ be okay, if the Rust compiler doesn’t have -enough information to be confident, it will reject the code. In these cases, -you can use unsafe code to tell the compiler, “Trust me, I know what I’m -doing.” Be warned, however, that you use unsafe Rust at your own risk: if you -use unsafe code incorrectly, problems can occur due to memory unsafety, such as -null pointer dereferencing. +enough information to be confident, it will reject the code. In these cases, you +can use unsafe code to tell the compiler, “Trust me, I know what I’m doing.” Be +warned, however, that you use unsafe Rust at your own risk: if you use unsafe +code incorrectly, problems can occur due to memory unsafety, such as null +pointer dereferencing. Another reason Rust has an unsafe alter ego is that the underlying computer hardware is inherently unsafe. If Rust didn’t let you do unsafe operations, you couldn’t do certain tasks. Rust needs to allow you to do low-level systems programming, such as directly interacting with the operating system or even -writing your own operating system. Working with low-level systems programming -is one of the goals of the language. Let’s explore what we can do with unsafe -Rust and how to do it. +writing your own operating system. Working with low-level systems programming is +one of the goals of the language. Let’s explore what we can do with unsafe Rust +and how to do it. ### Unsafe Superpowers @@ -30,17 +30,17 @@ that holds the unsafe code. You can take five actions in unsafe Rust that you can’t in safe Rust, which we call _unsafe superpowers_. Those superpowers include the ability to: -- Dereference a raw pointer -- Call an unsafe function or method -- Access or modify a mutable static variable -- Implement an unsafe trait -- Access fields of a `union` +* Dereference a raw pointer +* Call an unsafe function or method +* Access or modify a mutable static variable +* Implement an unsafe trait +* Access fields of a `union` It’s important to understand that `unsafe` doesn’t turn off the borrow checker or disable any other of Rust’s safety checks: if you use a reference in unsafe code, it will still be checked. The `unsafe` keyword only gives you access to -these five features that are then not checked by the compiler for memory -safety. You’ll still get some degree of safety inside of an unsafe block. +these five features that are then not checked by the compiler for memory safety. +You’ll still get some degree of safety inside of an unsafe block. In addition, `unsafe` does not mean the code inside the block is necessarily dangerous or that it will definitely have memory safety problems: the intent is @@ -68,21 +68,21 @@ some abstractions that provide a safe interface to unsafe code. ### Dereferencing a Raw Pointer In Chapter 4, in the [“Dangling References”][dangling-references]<!-- ignore ---> section, we mentioned that the compiler ensures references are always -valid. Unsafe Rust has two new types called _raw pointers_ that are similar to -references. As with references, raw pointers can be immutable or mutable and -are written as `*const T` and `*mut T`, respectively. The asterisk isn’t the +--> section, we mentioned that the compiler ensures references are always valid. +Unsafe Rust has two new types called _raw pointers_ that are similar to +references. As with references, raw pointers can be immutable or mutable and are +written as `*const T` and `*mut T`, respectively. The asterisk isn’t the dereference operator; it’s part of the type name. In the context of raw -pointers, _immutable_ means that the pointer can’t be directly assigned to -after being dereferenced. +pointers, _immutable_ means that the pointer can’t be directly assigned to after +being dereferenced. Different from references and smart pointers, raw pointers: -- Are allowed to ignore the borrowing rules by having both immutable and - mutable pointers or multiple mutable pointers to the same location -- Aren’t guaranteed to point to valid memory -- Are allowed to be null -- Don’t implement any automatic cleanup +* Are allowed to ignore the borrowing rules by having both immutable and mutable + pointers or multiple mutable pointers to the same location +* Aren’t guaranteed to point to valid memory +* Are allowed to be null +* Don’t implement any automatic cleanup By opting out of having Rust enforce these guarantees, you can give up guaranteed safety in exchange for greater performance or the ability to @@ -103,7 +103,8 @@ raw pointers in safe code; we just can’t dereference raw pointers outside an unsafe block, as you’ll see in a bit. We’ve created raw pointers by using the raw borrow operators: `&raw const num` -creates a `*const i32` immutable raw pointer, and `&raw mut num` creates a `*mut +creates a `*const i32` immutable raw pointer, and `&raw mut num` creates a +`*mut i32` mutable raw pointer. Because we created them directly from a local variable, we know these particular raw pointers are valid, but we can’t make that assumption about just any raw pointer. @@ -144,10 +145,10 @@ Note also that in Listing 20-1 and 20-3, we created `*const i32` and `*mut i32` raw pointers that both pointed to the same memory location, where `num` is stored. If we instead tried to create an immutable and a mutable reference to `num`, the code would not have compiled because Rust’s ownership rules don’t -allow a mutable reference at the same time as any immutable references. With -raw pointers, we can create a mutable pointer and an immutable pointer to the -same location and change data through the mutable pointer, potentially creating -a data race. Be careful! +allow a mutable reference at the same time as any immutable references. With raw +pointers, we can create a mutable pointer and an immutable pointer to the same +location and change data through the mutable pointer, potentially creating a +data race. Be careful! With all of these dangers, why would you ever use raw pointers? One major use case is when interfacing with C code, as you’ll see in the next section, @@ -193,12 +194,12 @@ unsafe operations within an unsafe function, we don’t need to add another #### Creating a Safe Abstraction over Unsafe Code Just because a function contains unsafe code doesn’t mean we need to mark the -entire function as unsafe. In fact, wrapping unsafe code in a safe function is -a common abstraction. As an example, let’s study the `split_at_mut` function -from the standard library, which requires some unsafe code. We’ll explore how -we might implement it. This safe method is defined on mutable slices: it takes -one slice and makes it two by splitting the slice at the index given as an -argument. Listing 20-4 shows how to use `split_at_mut`. +entire function as unsafe. In fact, wrapping unsafe code in a safe function is a +common abstraction. As an example, let’s study the `split_at_mut` function from +the standard library, which requires some unsafe code. We’ll explore how we +might implement it. This safe method is defined on mutable slices: it takes one +slice and makes it two by splitting the slice at the index given as an argument. +Listing 20-4 shows how to use `split_at_mut`. <Listing number="20-4" caption="Using the safe `split_at_mut` function"> @@ -221,15 +222,14 @@ of `i32` values rather than for a generic type `T`. </Listing> -This function first gets the total length of the slice. Then it asserts that -the index given as a parameter is within the slice by checking whether it’s -less than or equal to the length. The assertion means that if we pass an index -that is greater than the length to split the slice at, the function will panic -before it attempts to use that index. +This function first gets the total length of the slice. Then it asserts that the +index given as a parameter is within the slice by checking whether it’s less +than or equal to the length. The assertion means that if we pass an index that +is greater than the length to split the slice at, the function will panic before +it attempts to use that index. -Then we return two mutable slices in a tuple: one from the start of the -original slice to the `mid` index and another from `mid` to the end of the -slice. +Then we return two mutable slices in a tuple: one from the start of the original +slice to the `mid` index and another from `mid` to the end of the slice. When we try to compile the code in Listing 20-5, we’ll get an error. @@ -254,30 +254,30 @@ to unsafe functions to make the implementation of `split_at_mut` work. </Listing> -Recall from [“The Slice Type”][the-slice-type]<!-- ignore --> section in -Chapter 4 that slices are a pointer to some data and the length of the slice. -We use the `len` method to get the length of a slice and the `as_mut_ptr` -method to access the raw pointer of a slice. In this case, because we have a -mutable slice to `i32` values, `as_mut_ptr` returns a raw pointer with the type -`*mut i32`, which we’ve stored in the variable `ptr`. +Recall from [“The Slice Type”][the-slice-type]<!-- ignore --> section in Chapter +4 that slices are a pointer to some data and the length of the slice. We use the +`len` method to get the length of a slice and the `as_mut_ptr` method to access +the raw pointer of a slice. In this case, because we have a mutable slice to +`i32` values, `as_mut_ptr` returns a raw pointer with the type `*mut i32`, which +we’ve stored in the variable `ptr`. We keep the assertion that the `mid` index is within the slice. Then we get to the unsafe code: the `slice::from_raw_parts_mut` function takes a raw pointer and a length, and it creates a slice. We use this function to create a slice -that starts from `ptr` and is `mid` items long. Then we call the `add` -method on `ptr` with `mid` as an argument to get a raw pointer that starts at -`mid`, and we create a slice using that pointer and the remaining number of -items after `mid` as the length. +that starts from `ptr` and is `mid` items long. Then we call the `add` method on +`ptr` with `mid` as an argument to get a raw pointer that starts at `mid`, and +we create a slice using that pointer and the remaining number of items after +`mid` as the length. The function `slice::from_raw_parts_mut` is unsafe because it takes a raw pointer and must trust that this pointer is valid. The `add` method on raw pointers is also unsafe, because it must trust that the offset location is also a valid pointer. Therefore, we had to put an `unsafe` block around our calls to -`slice::from_raw_parts_mut` and `add` so we could call them. By looking at -the code and by adding the assertion that `mid` must be less than or equal to -`len`, we can tell that all the raw pointers used within the `unsafe` block -will be valid pointers to data within the slice. This is an acceptable and -appropriate use of `unsafe`. +`slice::from_raw_parts_mut` and `add` so we could call them. By looking at the +code and by adding the assertion that `mid` must be less than or equal to `len`, +we can tell that all the raw pointers used within the `unsafe` block will be +valid pointers to data within the slice. This is an acceptable and appropriate +use of `unsafe`. Note that we don’t need to mark the resulting `split_at_mut` function as `unsafe`, and we can call this function from safe Rust. We’ve created a safe @@ -285,9 +285,9 @@ abstraction to the unsafe code with an implementation of the function that uses `unsafe` code in a safe way, because it creates only valid pointers from the data this function has access to. -In contrast, the use of `slice::from_raw_parts_mut` in Listing 20-7 would -likely crash when the slice is used. This code takes an arbitrary memory -location and creates a slice 10,000 items long. +In contrast, the use of `slice::from_raw_parts_mut` in Listing 20-7 would likely +crash when the slice is used. This code takes an arbitrary memory location and +creates a slice 10,000 items long. <Listing number="20-7" caption="Creating a slice from an arbitrary memory location"> @@ -364,8 +364,8 @@ responsibility to make sure that promise is kept! > built-in mangling, so it is our responsibility to make sure the name we have > exported is safe to export without mangling. > -> In the following example, we make the `call_from_c` function accessible from -> C code, after it’s compiled to a shared library and linked from C: +> In the following example, we make the `call_from_c` function accessible from C +> code, after it’s compiled to a shared library and linked from C: > > ```rust > #[unsafe(no_mangle)] @@ -383,8 +383,7 @@ support but can be problematic with Rust’s ownership rules. If two threads are accessing the same mutable global variable, it can cause a data race. In Rust, global variables are called _static_ variables. Listing 20-10 shows an -example declaration and use of a static variable with a string slice as a -value. +example declaration and use of a static variable with a string slice as a value. <Listing number="20-10" file-name="src/main.rs" caption="Defining and using an immutable static variable"> @@ -404,8 +403,8 @@ aren’t required to annotate it explicitly. Accessing an immutable static variable is safe. A subtle difference between constants and immutable static variables is that -values in a static variable have a fixed address in memory. Using the value -will always access the same data. Constants, on the other hand, are allowed to +values in a static variable have a fixed address in memory. Using the value will +always access the same data. Constants, on the other hand, are allowed to duplicate their data whenever they’re used. Another difference is that static variables can be mutable. Accessing and modifying mutable static variables is _unsafe_. Listing 20-11 shows how to declare, access, and modify a mutable @@ -444,8 +443,8 @@ that data accessed from different threads is done safely. We can use `unsafe` to implement an unsafe trait. A trait is unsafe when at least one of its methods has some invariant that the compiler can’t verify. We declare that a trait is `unsafe` by adding the `unsafe` keyword before `trait` -and marking the implementation of the trait as `unsafe` too, as shown in -Listing 20-12. +and marking the implementation of the trait as `unsafe` too, as shown in Listing +20-12. <Listing number="20-12" caption="Defining and implementing an unsafe trait"> @@ -461,22 +460,22 @@ the compiler can’t verify. As an example, recall the `Sync` and `Send` marker traits we discussed in the [“Extensible Concurrency with the `Sync` and `Send` Traits”][extensible-concurrency-with-the-sync-and-send-traits]<!-- ignore --> -section in Chapter 16: the compiler implements these traits automatically if -our types are composed entirely of `Send` and `Sync` types. If we implement a -type that contains a type that is not `Send` or `Sync`, such as raw pointers, -and we want to mark that type as `Send` or `Sync`, we must use `unsafe`. Rust -can’t verify that our type upholds the guarantees that it can be safely sent -across threads or accessed from multiple threads; therefore, we need to do -those checks manually and indicate as such with `unsafe`. +section in Chapter 16: the compiler implements these traits automatically if our +types are composed entirely of `Send` and `Sync` types. If we implement a type +that contains a type that is not `Send` or `Sync`, such as raw pointers, and we +want to mark that type as `Send` or `Sync`, we must use `unsafe`. Rust can’t +verify that our type upholds the guarantees that it can be safely sent across +threads or accessed from multiple threads; therefore, we need to do those checks +manually and indicate as such with `unsafe`. ### Accessing Fields of a Union -The final action that works only with `unsafe` is accessing fields of a -_union_. A `union` is similar to a `struct`, but only one declared field is -used in a particular instance at one time. Unions are primarily used to -interface with unions in C code. Accessing union fields is unsafe because Rust -can’t guarantee the type of the data currently being stored in the union -instance. You can learn more about unions in [the Rust Reference][reference]. +The final action that works only with `unsafe` is accessing fields of a _union_. +A `union` is similar to a `struct`, but only one declared field is used in a +particular instance at one time. Unions are primarily used to interface with +unions in C code. Accessing union fields is unsafe because Rust can’t guarantee +the type of the data currently being stored in the union instance. You can learn +more about unions in [the Rust Reference][reference]. ### Using Miri to check unsafe code @@ -490,10 +489,12 @@ understands about how Rust should work. Using Miri requires a nightly build of Rust (which we talk about more in [Appendix G: How Rust is Made and “Nightly Rust”][nightly]). You can install -both a nightly version of Rust and the Miri tool by typing `rustup +nightly -component add miri`. This does not change what version of Rust your project -uses; it only adds the tool to your system so you can use it when you want to. -You can run Miri on a project by typing `cargo +nightly miri run` or `cargo +both a nightly version of Rust and the Miri tool by typing +`rustup +nightly +component add miri`. This does not change what version of Rust +your project uses; it only adds the tool to your system so you can use it when +you want to. You can run Miri on a project by typing `cargo +nightly miri run` +or `cargo +nightly miri test`. For an example of how helpful this can be, consider what happens when we run it diff --git a/src/ch20-03-advanced-traits.md b/src/ch20-03-advanced-traits.md index 3b5b80728b..067ad8ad87 100644 --- a/src/ch20-03-advanced-traits.md +++ b/src/ch20-03-advanced-traits.md @@ -1,6 +1,7 @@ ## Advanced Traits -We first covered traits in the [“Traits: Defining Shared +We first covered traits in the +[“Traits: Defining Shared Behavior”][traits-defining-shared-behavior]<!-- ignore --> section of Chapter 10, but we didn’t discuss the more advanced details. Now that you know more about Rust, we can get into the nitty-gritty. @@ -20,10 +21,9 @@ than features explained in the rest of the book but more commonly than many of the other features discussed in this chapter. One example of a trait with an associated type is the `Iterator` trait that the -standard library provides. The associated type is named `Item` and stands in -for the type of the values the type implementing the `Iterator` trait is -iterating over. The definition of the `Iterator` trait is as shown in Listing -20-13. +standard library provides. The associated type is named `Item` and stands in for +the type of the values the type implementing the `Iterator` trait is iterating +over. The definition of the `Iterator` trait is as shown in Listing 20-13. <Listing number="20-13" caption="The definition of the `Iterator` trait that has an associated type `Item`"> @@ -67,22 +67,22 @@ The difference is that when using generics, as in Listing 20-14, we must annotate the types in each implementation; because we can also implement `Iterator<String> for Counter` or any other type, we could have multiple implementations of `Iterator` for `Counter`. In other words, when a trait has a -generic parameter, it can be implemented for a type multiple times, changing -the concrete types of the generic type parameters each time. When we use the -`next` method on `Counter`, we would have to provide type annotations to -indicate which implementation of `Iterator` we want to use. +generic parameter, it can be implemented for a type multiple times, changing the +concrete types of the generic type parameters each time. When we use the `next` +method on `Counter`, we would have to provide type annotations to indicate which +implementation of `Iterator` we want to use. With associated types, we don’t need to annotate types because we can’t -implement a trait on a type multiple times. In Listing 20-13 with the -definition that uses associated types, we can only choose what the type of -`Item` will be once, because there can only be one `impl Iterator for Counter`. -We don’t have to specify that we want an iterator of `u32` values everywhere -that we call `next` on `Counter`. +implement a trait on a type multiple times. In Listing 20-13 with the definition +that uses associated types, we can only choose what the type of `Item` will be +once, because there can only be one `impl Iterator for Counter`. We don’t have +to specify that we want an iterator of `u32` values everywhere that we call +`next` on `Counter`. Associated types also become part of the trait’s contract: implementors of the trait must provide a type to stand in for the associated type placeholder. -Associated types often have a name that describes how the type will be used, -and documenting the associated type in the API documentation is good practice. +Associated types often have a name that describes how the type will be used, and +documenting the associated type in the API documentation is good practice. ### Default Generic Type Parameters and Operator Overloading @@ -110,8 +110,8 @@ struct: </Listing> -The `add` method adds the `x` values of two `Point` instances and the `y` -values of two `Point` instances to create a new `Point`. The `Add` trait has an +The `add` method adds the `x` values of two `Point` instances and the `y` values +of two `Point` instances to create a new `Point`. The `Add` trait has an associated type named `Output` that determines the type returned from the `add` method. @@ -127,12 +127,11 @@ trait Add<Rhs=Self> { ``` This code should look generally familiar: a trait with one method and an -associated type. The new part is `Rhs=Self`: this syntax is called _default -type parameters_. The `Rhs` generic type parameter (short for “right hand -side”) defines the type of the `rhs` parameter in the `add` method. If we don’t -specify a concrete type for `Rhs` when we implement the `Add` trait, the type -of `Rhs` will default to `Self`, which will be the type we’re implementing -`Add` on. +associated type. The new part is `Rhs=Self`: this syntax is called _default type +parameters_. The `Rhs` generic type parameter (short for “right hand side”) +defines the type of the `rhs` parameter in the `add` method. If we don’t specify +a concrete type for `Rhs` when we implement the `Add` trait, the type of `Rhs` +will default to `Self`, which will be the type we’re implementing `Add` on. When we implemented `Add` for `Point`, we used the default for `Rhs` because we wanted to add two `Point` instances. Let’s look at an example of implementing @@ -141,7 +140,8 @@ default. We have two structs, `Millimeters` and `Meters`, holding values in different units. This thin wrapping of an existing type in another struct is known as the -_newtype pattern_, which we describe in more detail in the [“Using the Newtype +_newtype pattern_, which we describe in more detail in the +[“Using the Newtype Pattern to Implement External Traits on External Types”][newtype]<!-- ignore --> section. We want to add values in millimeters to values in meters and have the implementation of `Add` do the conversion correctly. We can implement `Add` @@ -160,15 +160,15 @@ value of the `Rhs` type parameter instead of using the default of `Self`. You’ll use default type parameters in two main ways: -- To extend a type without breaking existing code -- To allow customization in specific cases most users won’t need +* To extend a type without breaking existing code +* To allow customization in specific cases most users won’t need -The standard library’s `Add` trait is an example of the second purpose: -usually, you’ll add two like types, but the `Add` trait provides the ability to -customize beyond that. Using a default type parameter in the `Add` trait -definition means you don’t have to specify the extra parameter most of the -time. In other words, a bit of implementation boilerplate isn’t needed, making -it easier to use the trait. +The standard library’s `Add` trait is an example of the second purpose: usually, +you’ll add two like types, but the `Add` trait provides the ability to customize +beyond that. Using a default type parameter in the `Add` trait definition means +you don’t have to specify the extra parameter most of the time. In other words, +a bit of implementation boilerplate isn’t needed, making it easier to use the +trait. The first purpose is similar to the second but in reverse: if you want to add a type parameter to an existing trait, you can give it a default to allow @@ -207,8 +207,8 @@ the method that is directly implemented on the type, as shown in Listing 20-18. </Listing> -Running this code will print `*waving arms furiously*`, showing that Rust -called the `fly` method implemented on `Human` directly. +Running this code will print `*waving arms furiously*`, showing that Rust called +the `fly` method implemented on `Human` directly. To call the `fly` methods from either the `Pilot` trait or the `Wizard` trait, we need to use more explicit syntax to specify which `fly` method we mean. @@ -224,8 +224,8 @@ Listing 20-19 demonstrates this syntax. Specifying the trait name before the method name clarifies to Rust which implementation of `fly` we want to call. We could also write -`Human::fly(&person)`, which is equivalent to the `person.fly()` that we used -in Listing 20-19, but this is a bit longer to write if we don’t need to +`Human::fly(&person)`, which is equivalent to the `person.fly()` that we used in +Listing 20-19, but this is a bit longer to write if we don’t need to disambiguate. Running this code prints the following: @@ -242,10 +242,10 @@ However, associated functions that are not methods don’t have a `self` parameter. When there are multiple types or traits that define non-method functions with the same function name, Rust doesn't always know which type you mean unless you use _fully qualified syntax_. For example, in Listing 20-20 we -create a trait for an animal shelter that wants to name all baby dogs _Spot_. -We make an `Animal` trait with an associated non-method function `baby_name`. -The `Animal` trait is implemented for the struct `Dog`, on which we also -provide an associated non-method function `baby_name` directly. +create a trait for an animal shelter that wants to name all baby dogs _Spot_. We +make an `Animal` trait with an associated non-method function `baby_name`. The +`Animal` trait is implemented for the struct `Dog`, on which we also provide an +associated non-method function `baby_name` directly. <Listing number="20-20" file-name="src/main.rs" caption="A trait with an associated function and a type with an associated function of the same name that also implements the trait"> @@ -290,10 +290,10 @@ implementation of `Animal::baby_name` we want. We’ll get this compiler error: {{#include ../listings/ch20-advanced-features/listing-20-21/output.txt}} ``` -To disambiguate and tell Rust that we want to use the implementation of -`Animal` for `Dog` as opposed to the implementation of `Animal` for some other -type, we need to use fully qualified syntax. Listing 20-22 demonstrates how to -use fully qualified syntax. +To disambiguate and tell Rust that we want to use the implementation of `Animal` +for `Dog` as opposed to the implementation of `Animal` for some other type, we +need to use fully qualified syntax. Listing 20-22 demonstrates how to use fully +qualified syntax. <Listing number="20-22" file-name="src/main.rs" caption="Using fully qualified syntax to specify that we want to call the `baby_name` function from the `Animal` trait as implemented on `Dog`"> @@ -320,16 +320,16 @@ In general, fully qualified syntax is defined as follows: For associated functions that aren’t methods, there would not be a `receiver`: there would only be the list of other arguments. You could use fully qualified -syntax everywhere that you call functions or methods. However, you’re allowed -to omit any part of this syntax that Rust can figure out from other information -in the program. You only need to use this more verbose syntax in cases where -there are multiple implementations that use the same name and Rust needs help -to identify which implementation you want to call. +syntax everywhere that you call functions or methods. However, you’re allowed to +omit any part of this syntax that Rust can figure out from other information in +the program. You only need to use this more verbose syntax in cases where there +are multiple implementations that use the same name and Rust needs help to +identify which implementation you want to call. ### Using Supertraits to Require One Trait’s Functionality Within Another Trait -Sometimes, you might write a trait definition that depends on another trait: -for a type to implement the first trait, you want to require that type to also +Sometimes, you might write a trait definition that depends on another trait: for +a type to implement the first trait, you want to require that type to also implement the second trait. You would do this so that your trait definition can make use of the associated items of the second trait. The trait your trait definition is relying on is called a _supertrait_ of your trait. @@ -352,10 +352,10 @@ should print the following: In the implementation of the `outline_print` method, we want to use the `Display` trait’s functionality. Therefore, we need to specify that the `OutlinePrint` trait will work only for types that also implement `Display` and -provide the functionality that `OutlinePrint` needs. We can do that in the -trait definition by specifying `OutlinePrint: Display`. This technique is -similar to adding a trait bound to the trait. Listing 20-23 shows an -implementation of the `OutlinePrint` trait. +provide the functionality that `OutlinePrint` needs. We can do that in the trait +definition by specifying `OutlinePrint: Display`. This technique is similar to +adding a trait bound to the trait. Listing 20-23 shows an implementation of the +`OutlinePrint` trait. <Listing number="20-23" file-name="src/main.rs" caption="Implementing the `OutlinePrint` trait that requires the functionality from `Display`"> @@ -365,12 +365,12 @@ implementation of the `OutlinePrint` trait. </Listing> -Because we’ve specified that `OutlinePrint` requires the `Display` trait, we -can use the `to_string` function that is automatically implemented for any type -that implements `Display`. If we tried to use `to_string` without adding a -colon and specifying the `Display` trait after the trait name, we’d get an -error saying that no method named `to_string` was found for the type `&Self` in -the current scope. +Because we’ve specified that `OutlinePrint` requires the `Display` trait, we can +use the `to_string` function that is automatically implemented for any type that +implements `Display`. If we tried to use `to_string` without adding a colon and +specifying the `Display` trait after the trait name, we’d get an error saying +that no method named `to_string` was found for the type `&Self` in the current +scope. Let’s see what happens when we try to implement `OutlinePrint` on a type that doesn’t implement `Display`, such as the `Point` struct: @@ -400,31 +400,33 @@ To fix this, we implement `Display` on `Point` and satisfy the constraint that </Listing> -Then implementing the `OutlinePrint` trait on `Point` will compile -successfully, and we can call `outline_print` on a `Point` instance to display -it within an outline of asterisks. +Then implementing the `OutlinePrint` trait on `Point` will compile successfully, +and we can call `outline_print` on a `Point` instance to display it within an +outline of asterisks. ### Using the Newtype Pattern to Implement External Traits on External Types -In Chapter 10 in the [“Implementing a Trait on a +In Chapter 10 in the +[“Implementing a Trait on a Type”][implementing-a-trait-on-a-type]<!-- ignore --> section, we mentioned the orphan rule that states we’re only allowed to implement a trait on a type if -either the trait or the type are local to our crate. It’s possible to get -around this restriction using the _newtype pattern_, which involves creating a -new type in a tuple struct. (We covered tuple structs in the [“Using Tuple +either the trait or the type are local to our crate. It’s possible to get around +this restriction using the _newtype pattern_, which involves creating a new type +in a tuple struct. (We covered tuple structs in the +[“Using Tuple Structs without Named Fields to Create Different Types”][tuple-structs]<!-- ignore --> section of Chapter 5.) The tuple struct will have one field and be a thin wrapper around the type we want to implement a trait for. Then the wrapper type is local to our crate, and we can implement the trait on the wrapper. -_Newtype_ is a term that originates from the Haskell programming language. -There is no runtime performance penalty for using this pattern, and the wrapper -type is elided at compile time. +_Newtype_ is a term that originates from the Haskell programming language. There +is no runtime performance penalty for using this pattern, and the wrapper type +is elided at compile time. As an example, let’s say we want to implement `Display` on `Vec<T>`, which the orphan rule prevents us from doing directly because the `Display` trait and the -`Vec<T>` type are defined outside our crate. We can make a `Wrapper` struct -that holds an instance of `Vec<T>`; then we can implement `Display` on -`Wrapper` and use the `Vec<T>` value, as shown in Listing 20-24. +`Vec<T>` type are defined outside our crate. We can make a `Wrapper` struct that +holds an instance of `Vec<T>`; then we can implement `Display` on `Wrapper` and +use the `Vec<T>` value, as shown in Listing 20-24. <Listing number="20-24" file-name="src/main.rs" caption="Creating a `Wrapper` type around `Vec<String>` to implement `Display`"> @@ -440,11 +442,11 @@ tuple. Then we can use the functionality of the `Display` trait on `Wrapper`. The downside of using this technique is that `Wrapper` is a new type, so it doesn’t have the methods of the value it’s holding. We would have to implement -all the methods of `Vec<T>` directly on `Wrapper` such that the methods -delegate to `self.0`, which would allow us to treat `Wrapper` exactly like a -`Vec<T>`. If we wanted the new type to have every method the inner type has, -implementing the `Deref` trait (discussed in Chapter 15 in the [“Treating Smart -Pointers Like Regular References with the `Deref` +all the methods of `Vec<T>` directly on `Wrapper` such that the methods delegate +to `self.0`, which would allow us to treat `Wrapper` exactly like a `Vec<T>`. If +we wanted the new type to have every method the inner type has, implementing the +`Deref` trait (discussed in Chapter 15 in the +[“Treating Smart Pointers Like Regular References with the `Deref` Trait”][smart-pointer-deref]<!-- ignore --> section) on the `Wrapper` to return the inner type would be a solution. If we don’t want the `Wrapper` type to have all the methods of the inner type—for example, to restrict the `Wrapper` type’s diff --git a/src/ch20-04-advanced-types.md b/src/ch20-04-advanced-types.md index 498a3c36b6..fc562d8802 100644 --- a/src/ch20-04-advanced-types.md +++ b/src/ch20-04-advanced-types.md @@ -8,8 +8,8 @@ the `!` type and dynamically sized types. ### Using the Newtype Pattern for Type Safety and Abstraction -> Note: This section assumes you’ve read the earlier section [“Using the -> Newtype Pattern to Implement External Traits on External +> Note: This section assumes you’ve read the earlier section +> [“Using the Newtype Pattern to Implement External Traits on External > Types.”][using-the-newtype-pattern]<!-- ignore --> The newtype pattern is also useful for tasks beyond those we’ve discussed so @@ -17,30 +17,29 @@ far, including statically enforcing that values are never confused and indicating the units of a value. You saw an example of using newtypes to indicate units in Listing 20-16: recall that the `Millimeters` and `Meters` structs wrapped `u32` values in a newtype. If we wrote a function with a -parameter of type `Millimeters`, we couldn’t compile a program that -accidentally tried to call that function with a value of type `Meters` or a -plain `u32`. +parameter of type `Millimeters`, we couldn’t compile a program that accidentally +tried to call that function with a value of type `Meters` or a plain `u32`. -We can also use the newtype pattern to abstract away some implementation -details of a type: the new type can expose a public API that is different from -the API of the private inner type. +We can also use the newtype pattern to abstract away some implementation details +of a type: the new type can expose a public API that is different from the API +of the private inner type. Newtypes can also hide internal implementation. For example, we could provide a `People` type to wrap a `HashMap<i32, String>` that stores a person’s ID associated with their name. Code using `People` would only interact with the public API we provide, such as a method to add a name string to the `People` collection; that code wouldn’t need to know that we assign an `i32` ID to names -internally. The newtype pattern is a lightweight way to achieve encapsulation -to hide implementation details, which we discussed in the [“Encapsulation that -Hides Implementation +internally. The newtype pattern is a lightweight way to achieve encapsulation to +hide implementation details, which we discussed in the +[“Encapsulation that Hides Implementation Details”][encapsulation-that-hides-implementation-details]<!-- ignore --> section of Chapter 18. ### Creating Type Synonyms with Type Aliases Rust provides the ability to declare a _type alias_ to give an existing type -another name. For this we use the `type` keyword. For example, we can create -the alias `Kilometers` to `i32` like so: +another name. For this we use the `type` keyword. For example, we can create the +alias `Kilometers` to `i32` like so: ```rust {{#rustdoc_include ../listings/ch20-advanced-features/no-listing-04-kilometers-alias/src/main.rs:here}} @@ -59,8 +58,8 @@ Because `Kilometers` and `i32` are the same type, we can add values of both types and we can pass `Kilometers` values to functions that take `i32` parameters. However, using this method, we don’t get the type checking benefits that we get from the newtype pattern discussed earlier. In other words, if we -mix up `Kilometers` and `i32` values somewhere, the compiler will not give us -an error. +mix up `Kilometers` and `i32` values somewhere, the compiler will not give us an +error. The main use case for type synonyms is to reduce repetition. For example, we might have a lengthy type like this: @@ -103,8 +102,8 @@ repetition. Consider the `std::io` module in the standard library. I/O operations often return a `Result<T, E>` to handle situations when operations fail to work. This library has a `std::io::Error` struct that represents all possible I/O errors. Many of the functions in `std::io` will be returning -`Result<T, E>` where the `E` is `std::io::Error`, such as these functions in -the `Write` trait: +`Result<T, E>` where the `E` is `std::io::Error`, such as these functions in the +`Write` trait: ```rust,noplayground {{#rustdoc_include ../listings/ch20-advanced-features/no-listing-05-write-trait/src/lib.rs}} @@ -147,8 +146,8 @@ never are called _diverging functions_. We can’t create values of the type `!` so `bar` can never possibly return. But what use is a type you can never create values for? Recall the code from -Listing 2-5, part of the number guessing game; we’ve reproduced a bit of it -here in Listing 20-27. +Listing 2-5, part of the number guessing game; we’ve reproduced a bit of it here +in Listing 20-27. <Listing number="20-27" caption="A `match` with an arm that ends in `continue`"> @@ -158,7 +157,8 @@ here in Listing 20-27. </Listing> -At the time, we skipped over some details in this code. In Chapter 6 in [“The +At the time, we skipped over some details in this code. In Chapter 6 in +[“The `match` Control Flow Operator”][the-match-control-flow-operator]<!-- ignore --> section, we discussed that `match` arms must all return the same type. So, for example, the following code doesn’t work: @@ -167,10 +167,10 @@ example, the following code doesn’t work: {{#rustdoc_include ../listings/ch20-advanced-features/no-listing-08-match-arms-different-types/src/main.rs:here}} ``` -The type of `guess` in this code would have to be an integer _and_ a string, -and Rust requires that `guess` have only one type. So what does `continue` -return? How were we allowed to return a `u32` from one arm and have another arm -that ends with `continue` in Listing 20-27? +The type of `guess` in this code would have to be an integer _and_ a string, and +Rust requires that `guess` have only one type. So what does `continue` return? +How were we allowed to return a `u32` from one arm and have another arm that +ends with `continue` in Listing 20-27? As you might have guessed, `continue` has a `!` value. That is, when Rust computes the type of `guess`, it looks at both match arms, the former with a @@ -192,10 +192,10 @@ this definition: ``` In this code, the same thing happens as in the `match` in Listing 20-27: Rust -sees that `val` has the type `T` and `panic!` has the type `!`, so the result -of the overall `match` expression is `T`. This code works because `panic!` -doesn’t produce a value; it ends the program. In the `None` case, we won’t be -returning a value from `unwrap`, so this code is valid. +sees that `val` has the type `T` and `panic!` has the type `!`, so the result of +the overall `match` expression is `T`. This code works because `panic!` doesn’t +produce a value; it ends the program. In the `None` case, we won’t be returning +a value from `unwrap`, so this code is valid. One final expression that has the type `!` is a `loop`: @@ -204,8 +204,8 @@ One final expression that has the type `!` is a `loop`: ``` Here, the loop never ends, so `!` is the value of the expression. However, this -wouldn’t be true if we included a `break`, because the loop would terminate -when it got to the `break`. +wouldn’t be true if we included a `break`, because the loop would terminate when +it got to the `break`. ### Dynamically Sized Types and the `Sized` Trait @@ -215,11 +215,11 @@ system a little confusing at first: the concept of _dynamically sized types_. Sometimes referred to as _DSTs_ or _unsized types_, these types let us write code using values whose size we can know only at runtime. -Let’s dig into the details of a dynamically sized type called `str`, which -we’ve been using throughout the book. That’s right, not `&str`, but `str` on -its own, is a DST. We can’t know how long the string is until runtime, meaning -we can’t create a variable of type `str`, nor can we take an argument of type -`str`. Consider the following code, which does not work: +Let’s dig into the details of a dynamically sized type called `str`, which we’ve +been using throughout the book. That’s right, not `&str`, but `str` on its own, +is a DST. We can’t know how long the string is until runtime, meaning we can’t +create a variable of type `str`, nor can we take an argument of type `str`. +Consider the following code, which does not work: ```rust,ignore,does_not_compile {{#rustdoc_include ../listings/ch20-advanced-features/no-listing-11-cant-create-str/src/main.rs:here}} @@ -233,15 +233,15 @@ storage and `s2` needs 15. This is why it’s not possible to create a variable holding a dynamically sized type. So what do we do? In this case, you already know the answer: we make the types -of `s1` and `s2` a `&str` rather than a `str`. Recall from the [“String -Slices”][string-slices]<!-- ignore --> section of Chapter 4 that the slice data -structure just stores the starting position and the length of the slice. So -although a `&T` is a single value that stores the memory address of where the -`T` is located, a `&str` is _two_ values: the address of the `str` and its -length. As such, we can know the size of a `&str` value at compile time: it’s -twice the length of a `usize`. That is, we always know the size of a `&str`, no -matter how long the string it refers to is. In general, this is the way in -which dynamically sized types are used in Rust: they have an extra bit of +of `s1` and `s2` a `&str` rather than a `str`. Recall from the +[“String Slices”][string-slices]<!-- ignore --> section of Chapter 4 that the +slice data structure just stores the starting position and the length of the +slice. So although a `&T` is a single value that stores the memory address of +where the `T` is located, a `&str` is _two_ values: the address of the `str` and +its length. As such, we can know the size of a `&str` value at compile time: +it’s twice the length of a `usize`. That is, we always know the size of a +`&str`, no matter how long the string it refers to is. In general, this is the +way in which dynamically sized types are used in Rust: they have an extra bit of metadata that stores the size of the dynamic information. The golden rule of dynamically sized types is that we must always put values of dynamically sized types behind a pointer of some kind. @@ -249,18 +249,19 @@ types behind a pointer of some kind. We can combine `str` with all kinds of pointers: for example, `Box<str>` or `Rc<str>`. In fact, you’ve seen this before but with a different dynamically sized type: traits. Every trait is a dynamically sized type we can refer to by -using the name of the trait. In Chapter 18 in the [“Using Trait Objects That -Allow for Values of Different +using the name of the trait. In Chapter 18 in the +[“Using Trait Objects That Allow for Values of Different Types”][using-trait-objects-that-allow-for-values-of-different-types]<!-- ignore --> section, we mentioned that to use traits as trait objects, we must -put them behind a pointer, such as `&dyn Trait` or `Box<dyn Trait>` (`Rc<dyn +put them behind a pointer, such as `&dyn Trait` or `Box<dyn Trait>` +(`Rc<dyn Trait>` would work too). -To work with DSTs, Rust provides the `Sized` trait to determine whether or not -a type’s size is known at compile time. This trait is automatically implemented -for everything whose size is known at compile time. In addition, Rust -implicitly adds a bound on `Sized` to every generic function. That is, a -generic function definition like this: +To work with DSTs, Rust provides the `Sized` trait to determine whether or not a +type’s size is known at compile time. This trait is automatically implemented +for everything whose size is known at compile time. In addition, Rust implicitly +adds a bound on `Sized` to every generic function. That is, a generic function +definition like this: ```rust,ignore {{#rustdoc_include ../listings/ch20-advanced-features/no-listing-12-generic-fn-definition/src/lib.rs}} diff --git a/src/ch20-05-advanced-functions-and-closures.md b/src/ch20-05-advanced-functions-and-closures.md index 9f04892b0a..8602962a1d 100644 --- a/src/ch20-05-advanced-functions-and-closures.md +++ b/src/ch20-05-advanced-functions-and-closures.md @@ -16,8 +16,8 @@ functions. The syntax for specifying that a parameter is a function pointer is similar to that of closures, as shown in Listing 20-28, where we’ve defined a function `add_one` that adds one to its parameter. The function `do_twice` takes two -parameters: a function pointer to any function that takes an `i32` parameter -and returns an `i32`, and one `i32` value. The `do_twice` function calls the +parameters: a function pointer to any function that takes an `i32` parameter and +returns an `i32`, and one `i32` value. The `do_twice` function calls the function `f` twice, passing it the `arg` value, then adds the two function call results together. The `main` function calls `do_twice` with the arguments `add_one` and `5`. @@ -42,8 +42,8 @@ of the `Fn` traits as a trait bound. Function pointers implement all three of the closure traits (`Fn`, `FnMut`, and `FnOnce`), meaning you can always pass a function pointer as an argument for a function that expects a closure. It’s best to write functions using a generic -type and one of the closure traits so your functions can accept either -functions or closures. +type and one of the closure traits so your functions can accept either functions +or closures. That said, one example of where you would want to only accept `fn` and not closures is when interfacing with external code that doesn’t have closures: C @@ -65,14 +65,14 @@ like this: {{#rustdoc_include ../listings/ch20-advanced-features/no-listing-16-map-function/src/main.rs:here}} ``` -Note that we must use the fully qualified syntax that we talked about earlier -in the [“Advanced Traits”][advanced-traits]<!-- ignore --> section because -there are multiple functions available named `to_string`. Here, we’re using the -`to_string` function defined in the `ToString` trait, which the standard -library has implemented for any type that implements `Display`. +Note that we must use the fully qualified syntax that we talked about earlier in +the [“Advanced Traits”][advanced-traits]<!-- ignore --> section because there +are multiple functions available named `to_string`. Here, we’re using the +`to_string` function defined in the `ToString` trait, which the standard library +has implemented for any type that implements `Display`. -Recall from the [“Enum values”][enum-values]<!-- ignore --> section of Chapter -6 that the name of each enum variant that we define also becomes an initializer +Recall from the [“Enum values”][enum-values]<!-- ignore --> section of Chapter 6 +that the name of each enum variant that we define also becomes an initializer function. We can use these initializer functions as function pointers that implement the closure traits, which means we can specify the initializer functions as arguments for methods that take closures, like so: @@ -92,8 +92,8 @@ Closures are represented by traits, which means you can’t return closures directly. In most cases where you might want to return a trait, you can instead use the concrete type that implements the trait as the return value of the function. However, you can’t do that with closures because they don’t have a -concrete type that is returnable; you’re not allowed to use the function -pointer `fn` as a return type, for example. +concrete type that is returnable; you’re not allowed to use the function pointer +`fn` as a return type, for example. The following code tries to return a closure directly, but it won’t compile: @@ -108,15 +108,16 @@ The compiler error is as follows: ``` The error references the `Sized` trait again! Rust doesn’t know how much space -it will need to store the closure. We saw a solution to this problem earlier. -We can use a trait object: +it will need to store the closure. We saw a solution to this problem earlier. We +can use a trait object: ```rust,noplayground {{#rustdoc_include ../listings/ch20-advanced-features/no-listing-19-returns-closure-trait-object/src/lib.rs}} ``` This code will compile just fine. For more about trait objects, refer to the -section [“Using Trait Objects That Allow for Values of Different +section +[“Using Trait Objects That Allow for Values of Different Types”][using-trait-objects-that-allow-for-values-of-different-types]<!-- ignore --> in Chapter 19. diff --git a/src/ch20-06-macros.md b/src/ch20-06-macros.md index c687c8b614..ef48c5f5c7 100644 --- a/src/ch20-06-macros.md +++ b/src/ch20-06-macros.md @@ -2,13 +2,13 @@ We’ve used macros like `println!` throughout this book, but we haven’t fully explored what a macro is and how it works. The term _macro_ refers to a family -of features in Rust: _declarative_ macros with `macro_rules!` and three kinds -of _procedural_ macros: +of features in Rust: _declarative_ macros with `macro_rules!` and three kinds of +_procedural_ macros: -- Custom `#[derive]` macros that specify code added with the `derive` attribute +* Custom `#[derive]` macros that specify code added with the `derive` attribute used on structs and enums -- Attribute-like macros that define custom attributes usable on any item -- Function-like macros that look like function calls but operate on the tokens +* Attribute-like macros that define custom attributes usable on any item +* Function-like macros that look like function calls but operate on the tokens specified as their argument We’ll talk about each of these in turn, but first, let’s look at why we even @@ -16,23 +16,23 @@ need macros when we already have functions. ### The Difference Between Macros and Functions -Fundamentally, macros are a way of writing code that writes other code, which -is known as _metaprogramming_. In Appendix C, we discuss the `derive` -attribute, which generates an implementation of various traits for you. We’ve -also used the `println!` and `vec!` macros throughout the book. All of these -macros _expand_ to produce more code than the code you’ve written manually. +Fundamentally, macros are a way of writing code that writes other code, which is +known as _metaprogramming_. In Appendix C, we discuss the `derive` attribute, +which generates an implementation of various traits for you. We’ve also used the +`println!` and `vec!` macros throughout the book. All of these macros _expand_ +to produce more code than the code you’ve written manually. Metaprogramming is useful for reducing the amount of code you have to write and -maintain, which is also one of the roles of functions. However, macros have -some additional powers that functions don’t. +maintain, which is also one of the roles of functions. However, macros have some +additional powers that functions don’t. -A function signature must declare the number and type of parameters the -function has. Macros, on the other hand, can take a variable number of -parameters: we can call `println!("hello")` with one argument or -`println!("hello {}", name)` with two arguments. Also, macros are expanded -before the compiler interprets the meaning of the code, so a macro can, for -example, implement a trait on a given type. A function can’t, because it gets -called at runtime and a trait needs to be implemented at compile time. +A function signature must declare the number and type of parameters the function +has. Macros, on the other hand, can take a variable number of parameters: we can +call `println!("hello")` with one argument or `println!("hello {}", name)` with +two arguments. Also, macros are expanded before the compiler interprets the +meaning of the code, so a macro can, for example, implement a trait on a given +type. A function can’t, because it gets called at runtime and a trait needs to +be implemented at compile time. The downside to implementing a macro instead of a function is that macro definitions are more complex than function definitions because you’re writing @@ -91,23 +91,23 @@ The `#[macro_export]` annotation indicates that this macro should be made available whenever the crate in which the macro is defined is brought into scope. Without this annotation, the macro can’t be brought into scope. -We then start the macro definition with `macro_rules!` and the name of the -macro we’re defining _without_ the exclamation mark. The name, in this case -`vec`, is followed by curly brackets denoting the body of the macro definition. +We then start the macro definition with `macro_rules!` and the name of the macro +we’re defining _without_ the exclamation mark. The name, in this case `vec`, is +followed by curly brackets denoting the body of the macro definition. The structure in the `vec!` body is similar to the structure of a `match` -expression. Here we have one arm with the pattern `( $( $x:expr ),* )`, -followed by `=>` and the block of code associated with this pattern. If the -pattern matches, the associated block of code will be emitted. Given that this -is the only pattern in this macro, there is only one valid way to match; any -other pattern will result in an error. More complex macros will have more than -one arm. +expression. Here we have one arm with the pattern `( $( $x:expr ),* )`, followed +by `=>` and the block of code associated with this pattern. If the pattern +matches, the associated block of code will be emitted. Given that this is the +only pattern in this macro, there is only one valid way to match; any other +pattern will result in an error. More complex macros will have more than one +arm. Valid pattern syntax in macro definitions is different than the pattern syntax covered in Chapter 19 because macro patterns are matched against Rust code structure rather than values. Let’s walk through what the pattern pieces in -Listing 20-29 mean; for the full macro pattern syntax, see the [Rust -Reference][ref]. +Listing 20-29 mean; for the full macro pattern syntax, see the +[Rust Reference][ref]. First, we use a set of parentheses to encompass the whole pattern. We use a dollar sign (`$`) to declare a variable in the macro system that will contain @@ -125,11 +125,11 @@ When we call this macro with `vec![1, 2, 3];`, the `$x` pattern matches three times with the three expressions `1`, `2`, and `3`. Now let’s look at the pattern in the body of the code associated with this arm: -`temp_vec.push()` within `$()*` is generated for each part that matches `$()` -in the pattern zero or more times depending on how many times the pattern -matches. The `$x` is replaced with each expression matched. When we call this -macro with `vec![1, 2, 3];`, the code generated that replaces this macro call -will be the following: +`temp_vec.push()` within `$()*` is generated for each part that matches `$()` in +the pattern zero or more times depending on how many times the pattern matches. +The `$x` is replaced with each expression matched. When we call this macro with +`vec![1, 2, 3];`, the code generated that replaces this macro call will be the +following: ```rust,ignore { @@ -158,10 +158,10 @@ macros do. The three kinds of procedural macros are custom derive, attribute-like, and function-like, and all work in a similar fashion. When creating procedural macros, the definitions must reside in their own crate -with a special crate type. This is for complex technical reasons that we hope -to eliminate in the future. In Listing 20-30, we show how to define a -procedural macro, where `some_attribute` is a placeholder for using a specific -macro variety. +with a special crate type. This is for complex technical reasons that we hope to +eliminate in the future. In Listing 20-30, we show how to define a procedural +macro, where `some_attribute` is a placeholder for using a specific macro +variety. <Listing number="20-30" file-name="src/lib.rs" caption="An example of defining a procedural macro"> @@ -190,15 +190,16 @@ other forms different. ### How to Write a Custom `derive` Macro -Let’s create a crate named `hello_macro` that defines a trait named -`HelloMacro` with one associated function named `hello_macro`. Rather than -making our users implement the `HelloMacro` trait for each of their types, -we’ll provide a procedural macro so users can annotate their type with -`#[derive(HelloMacro)]` to get a default implementation of the `hello_macro` -function. The default implementation will print `Hello, Macro! My name is -TypeName!` where `TypeName` is the name of the type on which this trait has -been defined. In other words, we’ll write a crate that enables another -programmer to write code like Listing 20-31 using our crate. +Let’s create a crate named `hello_macro` that defines a trait named `HelloMacro` +with one associated function named `hello_macro`. Rather than making our users +implement the `HelloMacro` trait for each of their types, we’ll provide a +procedural macro so users can annotate their type with `#[derive(HelloMacro)]` +to get a default implementation of the `hello_macro` function. The default +implementation will print `Hello, Macro! My name is +TypeName!` where `TypeName` +is the name of the type on which this trait has been defined. In other words, +we’ll write a crate that enables another programmer to write code like Listing +20-31 using our crate. <Listing number="20-31" file-name="src/main.rs" caption="The code a user of our crate will be able to write when using our procedural macro"> @@ -237,9 +238,9 @@ wanted to use with `hello_macro`; we want to spare them from having to do this work. Additionally, we can’t yet provide the `hello_macro` function with default -implementation that will print the name of the type the trait is implemented -on: Rust doesn’t have reflection capabilities, so it can’t look up the type’s -name at runtime. We need a macro to generate code at compile time. +implementation that will print the name of the type the trait is implemented on: +Rust doesn’t have reflection capabilities, so it can’t look up the type’s name +at runtime. We need a macro to generate code at compile time. The next step is to define the procedural macro. At the time of this writing, procedural macros need to be in their own crate. Eventually, this restriction @@ -255,9 +256,9 @@ $ cargo new hello_macro_derive --lib Our two crates are tightly related, so we create the procedural macro crate within the directory of our `hello_macro` crate. If we change the trait definition in `hello_macro`, we’ll have to change the implementation of the -procedural macro in `hello_macro_derive` as well. The two crates will need to -be published separately, and programmers using these crates will need to add -both as dependencies and bring them both into scope. We could instead have the +procedural macro in `hello_macro_derive` as well. The two crates will need to be +published separately, and programmers using these crates will need to add both +as dependencies and bring them both into scope. We could instead have the `hello_macro` crate use `hello_macro_derive` as a dependency and re-export the procedural macro code. However, the way we’ve structured the project makes it possible for programmers to use `hello_macro` even if they don’t want the @@ -292,21 +293,20 @@ Notice that we’ve split the code into the `hello_macro_derive` function, which is responsible for parsing the `TokenStream`, and the `impl_hello_macro` function, which is responsible for transforming the syntax tree: this makes writing a procedural macro more convenient. The code in the outer function -(`hello_macro_derive` in this case) will be the same for almost every -procedural macro crate you see or create. The code you specify in the body of -the inner function (`impl_hello_macro` in this case) will be different -depending on your procedural macro’s purpose. +(`hello_macro_derive` in this case) will be the same for almost every procedural +macro crate you see or create. The code you specify in the body of the inner +function (`impl_hello_macro` in this case) will be different depending on your +procedural macro’s purpose. We’ve introduced three new crates: `proc_macro`, [`syn`], and [`quote`]. The `proc_macro` crate comes with Rust, so we didn’t need to add that to the dependencies in _Cargo.toml_. The `proc_macro` crate is the compiler’s API that allows us to read and manipulate Rust code from our code. -The `syn` crate parses Rust code from a string into a data structure that we -can perform operations on. The `quote` crate turns `syn` data structures back -into Rust code. These crates make it much simpler to parse any sort of Rust -code we might want to handle: writing a full parser for Rust code is no simple -task. +The `syn` crate parses Rust code from a string into a data structure that we can +perform operations on. The `quote` crate turns `syn` data structures back into +Rust code. These crates make it much simpler to parse any sort of Rust code we +might want to handle: writing a full parser for Rust code is no simple task. The `hello_macro_derive` function will be called when a user of our library specifies `#[derive(HelloMacro)]` on a type. This is possible because we’ve @@ -347,15 +347,14 @@ DeriveInput { The fields of this struct show that the Rust code we’ve parsed is a unit struct with the `ident` (identifier, meaning the name) of `Pancakes`. There are more -fields on this struct for describing all sorts of Rust code; check the [`syn` -documentation for `DeriveInput`][syn-docs] for more information. +fields on this struct for describing all sorts of Rust code; check the +[`syn` documentation for `DeriveInput`][syn-docs] for more information. Soon we’ll define the `impl_hello_macro` function, which is where we’ll build -the new Rust code we want to include. But before we do, note that the output -for our derive macro is also a `TokenStream`. The returned `TokenStream` is -added to the code that our crate users write, so when they compile their crate, -they’ll get the extra functionality that we provide in the modified -`TokenStream`. +the new Rust code we want to include. But before we do, note that the output for +our derive macro is also a `TokenStream`. The returned `TokenStream` is added to +the code that our crate users write, so when they compile their crate, they’ll +get the extra functionality that we provide in the modified `TokenStream`. You might have noticed that we’re calling `unwrap` to cause the `hello_macro_derive` function to panic if the call to the `syn::parse` function @@ -378,12 +377,12 @@ into a `DeriveInput` instance, let’s generate the code that implements the </Listing> We get an `Ident` struct instance containing the name (identifier) of the -annotated type using `ast.ident`. The struct in Listing 20-33 shows that when -we run the `impl_hello_macro` function on the code in Listing 20-31, the -`ident` we get will have the `ident` field with a value of `"Pancakes"`. Thus, -the `name` variable in Listing 20-34 will contain an `Ident` struct instance -that, when printed, will be the string `"Pancakes"`, the name of the struct in -Listing 20-31. +annotated type using `ast.ident`. The struct in Listing 20-33 shows that when we +run the `impl_hello_macro` function on the code in Listing 20-31, the `ident` we +get will have the `ident` field with a value of `"Pancakes"`. Thus, the `name` +variable in Listing 20-34 will contain an `Ident` struct instance that, when +printed, will be the string `"Pancakes"`, the name of the struct in Listing +20-31. The `quote!` macro lets us define the Rust code that we want to return. The compiler expects something different to the direct result of the `quote!` @@ -402,32 +401,32 @@ trait implementation has the one function `hello_macro`, whose body contains the functionality we want to provide: printing `Hello, Macro! My name is` and then the name of the annotated type. -The `stringify!` macro used here is built into Rust. It takes a Rust -expression, such as `1 + 2`, and at compile time turns the expression into a -string literal, such as `"1 + 2"`. This is different than `format!` or -`println!`, macros which evaluate the expression and then turn the result into -a `String`. There is a possibility that the `#name` input might be an -expression to print literally, so we use `stringify!`. Using `stringify!` also -saves an allocation by converting `#name` to a string literal at compile time. +The `stringify!` macro used here is built into Rust. It takes a Rust expression, +such as `1 + 2`, and at compile time turns the expression into a string literal, +such as `"1 + 2"`. This is different than `format!` or `println!`, macros which +evaluate the expression and then turn the result into a `String`. There is a +possibility that the `#name` input might be an expression to print literally, so +we use `stringify!`. Using `stringify!` also saves an allocation by converting +`#name` to a string literal at compile time. At this point, `cargo build` should complete successfully in both `hello_macro` and `hello_macro_derive`. Let’s hook up these crates to the code in Listing -20-31 to see the procedural macro in action! Create a new binary project in -your _projects_ directory using `cargo new pancakes`. We need to add -`hello_macro` and `hello_macro_derive` as dependencies in the `pancakes` -crate’s _Cargo.toml_. If you’re publishing your versions of `hello_macro` and -`hello_macro_derive` to [crates.io](https://crates.io/), they would be regular -dependencies; if not, you can specify them as `path` dependencies as follows: +20-31 to see the procedural macro in action! Create a new binary project in your +_projects_ directory using `cargo new pancakes`. We need to add `hello_macro` +and `hello_macro_derive` as dependencies in the `pancakes` crate’s _Cargo.toml_. +If you’re publishing your versions of `hello_macro` and `hello_macro_derive` to +[crates.io](https://crates.io/), they would be regular dependencies; if not, you +can specify them as `path` dependencies as follows: ```toml {{#include ../listings/ch20-advanced-features/no-listing-21-pancakes/pancakes/Cargo.toml:7:9}} ``` -Put the code in Listing 20-31 into _src/main.rs_, and run `cargo run`: it -should print `Hello, Macro! My name is Pancakes!` The implementation of the -`HelloMacro` trait from the procedural macro was included without the -`pancakes` crate needing to implement it; the `#[derive(HelloMacro)]` added the -trait implementation. +Put the code in Listing 20-31 into _src/main.rs_, and run `cargo run`: it should +print `Hello, Macro! My name is Pancakes!` The implementation of the +`HelloMacro` trait from the procedural macro was included without the `pancakes` +crate needing to implement it; the `#[derive(HelloMacro)]` added the trait +implementation. Next, let’s explore how the other kinds of procedural macros differ from custom derive macros. @@ -456,8 +455,8 @@ pub fn route(attr: TokenStream, item: TokenStream) -> TokenStream { Here, we have two parameters of type `TokenStream`. The first is for the contents of the attribute: the `GET, "/"` part. The second is the body of the -item the attribute is attached to: in this case, `fn index() {}` and the rest -of the function’s body. +item the attribute is attached to: in this case, `fn index() {}` and the rest of +the function’s body. Other than that, attribute-like macros work the same way as custom derive macros: you create a crate with the `proc-macro` crate type and implement a @@ -495,14 +494,13 @@ generate. ## Summary Whew! Now you have some Rust features in your toolbox that you likely won’t use -often, but you’ll know they’re available in very particular circumstances. -We’ve introduced several complex topics so that when you encounter them in -error message suggestions or in other peoples’ code, you’ll be able to -recognize these concepts and syntax. Use this chapter as a reference to guide -you to solutions. - -Next, we’ll put everything we’ve discussed throughout the book into practice -and do one more project! +often, but you’ll know they’re available in very particular circumstances. We’ve +introduced several complex topics so that when you encounter them in error +message suggestions or in other peoples’ code, you’ll be able to recognize these +concepts and syntax. Use this chapter as a reference to guide you to solutions. + +Next, we’ll put everything we’ve discussed throughout the book into practice and +do one more project! [ref]: ../reference/macros-by-example.html [tlborm]: https://veykril.github.io/tlborm/ diff --git a/src/ch21-01-single-threaded.md b/src/ch21-01-single-threaded.md index 2c35b25b59..a6c40b9daa 100644 --- a/src/ch21-01-single-threaded.md +++ b/src/ch21-01-single-threaded.md @@ -2,12 +2,12 @@ We’ll start by getting a single-threaded web server working. Before we begin, let’s look at a quick overview of the protocols involved in building web -servers. The details of these protocols are beyond the scope of this book, but -a brief overview will give you the information you need. +servers. The details of these protocols are beyond the scope of this book, but a +brief overview will give you the information you need. -The two main protocols involved in web servers are _Hypertext Transfer -Protocol_ _(HTTP)_ and _Transmission Control Protocol_ _(TCP)_. Both protocols -are _request-response_ protocols, meaning a _client_ initiates requests and a +The two main protocols involved in web servers are _Hypertext Transfer Protocol_ +_(HTTP)_ and _Transmission Control Protocol_ _(TCP)_. Both protocols are +_request-response_ protocols, meaning a _client_ initiates requests and a _server_ listens to the requests and provides a response to the client. The contents of those requests and responses are defined by the protocols. @@ -51,19 +51,19 @@ our server is unlikely to conflict with any other web server you might have running on your machine, and 7878 is _rust_ typed on a telephone. The `bind` function in this scenario works like the `new` function in that it -will return a new `TcpListener` instance. The function is called `bind` -because, in networking, connecting to a port to listen to is known as “binding -to a port.” - -The `bind` function returns a `Result<T, E>`, which indicates that it’s -possible for binding to fail. For example, connecting to port 80 requires -administrator privileges (nonadministrators can listen only on ports higher -than 1023), so if we tried to connect to port 80 without being an -administrator, binding wouldn’t work. Binding also wouldn’t work, for example, -if we ran two instances of our program and so had two programs listening to the -same port. Because we’re writing a basic server just for learning purposes, we -won’t worry about handling these kinds of errors; instead, we use `unwrap` to -stop the program if errors happen. +will return a new `TcpListener` instance. The function is called `bind` because, +in networking, connecting to a port to listen to is known as “binding to a +port.” + +The `bind` function returns a `Result<T, E>`, which indicates that it’s possible +for binding to fail. For example, connecting to port 80 requires administrator +privileges (nonadministrators can listen only on ports higher than 1023), so if +we tried to connect to port 80 without being an administrator, binding wouldn’t +work. Binding also wouldn’t work, for example, if we ran two instances of our +program and so had two programs listening to the same port. Because we’re +writing a basic server just for learning purposes, we won’t worry about handling +these kinds of errors; instead, we use `unwrap` to stop the program if errors +happen. The `incoming` method on `TcpListener` returns an iterator that gives us a sequence of streams (more specifically, streams of type `TcpStream`). A single @@ -80,18 +80,17 @@ our program if the stream has any errors; if there aren’t any errors, the program prints a message. We’ll add more functionality for the success case in the next listing. The reason we might receive errors from the `incoming` method when a client connects to the server is that we’re not actually iterating over -connections. Instead, we’re iterating over _connection attempts_. The -connection might not be successful for a number of reasons, many of them -operating system specific. For example, many operating systems have a limit to -the number of simultaneous open connections they can support; new connection -attempts beyond that number will produce an error until some of the open -connections are closed. +connections. Instead, we’re iterating over _connection attempts_. The connection +might not be successful for a number of reasons, many of them operating system +specific. For example, many operating systems have a limit to the number of +simultaneous open connections they can support; new connection attempts beyond +that number will produce an error until some of the open connections are closed. Let’s try running this code! Invoke `cargo run` in the terminal and then load -_127.0.0.1:7878_ in a web browser. The browser should show an error message -like “Connection reset,” because the server isn’t currently sending back any -data. But when you look at your terminal, you should see several messages that -were printed when the browser connected to the server! +_127.0.0.1:7878_ in a web browser. The browser should show an error message like +“Connection reset,” because the server isn’t currently sending back any data. +But when you look at your terminal, you should see several messages that were +printed when the browser connected to the server! ```text Running `target/debug/hello` @@ -107,10 +106,10 @@ browser tab. It could also be that the browser is trying to connect to the server multiple times because the server isn’t responding with any data. When `stream` goes out -of scope and is dropped at the end of the loop, the connection is closed as -part of the `drop` implementation. Browsers sometimes deal with closed -connections by retrying, because the problem might be temporary. The important -factor is that we’ve successfully gotten a handle to a TCP connection! +of scope and is dropped at the end of the loop, the connection is closed as part +of the `drop` implementation. Browsers sometimes deal with closed connections by +retrying, because the problem might be temporary. The important factor is that +we’ve successfully gotten a handle to a TCP connection! Remember to stop the program by pressing <kbd>ctrl</kbd>-<kbd>c</kbd> when you’re done running a particular version of the code. Then restart the program @@ -134,38 +133,39 @@ look like Listing 21-2. </Listing> -We bring `std::io::prelude` and `std::io::BufReader` into scope to get access -to traits and types that let us read from and write to the stream. In the `for` +We bring `std::io::prelude` and `std::io::BufReader` into scope to get access to +traits and types that let us read from and write to the stream. In the `for` loop in the `main` function, instead of printing a message that says we made a connection, we now call the new `handle_connection` function and pass the `stream` to it. In the `handle_connection` function, we create a new `BufReader` instance that -wraps a reference to the `stream`. The `BufReader` adds buffering by managing calls -to the `std::io::Read` trait methods for us. +wraps a reference to the `stream`. The `BufReader` adds buffering by managing +calls to the `std::io::Read` trait methods for us. We create a variable named `http_request` to collect the lines of the request -the browser sends to our server. We indicate that we want to collect these -lines in a vector by adding the `Vec<_>` type annotation. +the browser sends to our server. We indicate that we want to collect these lines +in a vector by adding the `Vec<_>` type annotation. `BufReader` implements the `std::io::BufRead` trait, which provides the `lines` -method. The `lines` method returns an iterator of `Result<String, -std::io::Error>` by splitting the stream of data whenever it sees a newline -byte. To get each `String`, we map and `unwrap` each `Result`. The `Result` -might be an error if the data isn’t valid UTF-8 or if there was a problem -reading from the stream. Again, a production program should handle these errors -more gracefully, but we’re choosing to stop the program in the error case for -simplicity. - -The browser signals the end of an HTTP request by sending two newline -characters in a row, so to get one request from the stream, we take lines until -we get a line that is the empty string. Once we’ve collected the lines into the -vector, we’re printing them out using pretty debug formatting so we can take a -look at the instructions the web browser is sending to our server. +method. The `lines` method returns an iterator of +`Result<String, +std::io::Error>` by splitting the stream of data whenever it sees +a newline byte. To get each `String`, we map and `unwrap` each `Result`. The +`Result` might be an error if the data isn’t valid UTF-8 or if there was a +problem reading from the stream. Again, a production program should handle these +errors more gracefully, but we’re choosing to stop the program in the error case +for simplicity. + +The browser signals the end of an HTTP request by sending two newline characters +in a row, so to get one request from the stream, we take lines until we get a +line that is the empty string. Once we’ve collected the lines into the vector, +we’re printing them out using pretty debug formatting so we can take a look at +the instructions the web browser is sending to our server. Let’s try this code! Start the program and make a request in a web browser -again. Note that we’ll still get an error page in the browser, but our -program’s output in the terminal will now look similar to this: +again. Note that we’ll still get an error page in the browser, but our program’s +output in the terminal will now look similar to this: ```console $ cargo run @@ -191,11 +191,11 @@ Request: [ ``` Depending on your browser, you might get slightly different output. Now that -we’re printing the request data, we can see why we get multiple connections -from one browser request by looking at the path after `GET` in the first line -of the request. If the repeated connections are all requesting _/_, we know the -browser is trying to fetch _/_ repeatedly because it’s not getting a response -from our program. +we’re printing the request data, we can see why we get multiple connections from +one browser request by looking at the path after `GET` in the first line of the +request. If the repeated connections are all requesting _/_, we know the browser +is trying to fetch _/_ repeatedly because it’s not getting a response from our +program. Let’s break down this request data to understand what the browser is asking of our program. @@ -224,27 +224,27 @@ uses the term URI, so we can just mentally substitute URL for URI here. The last part is the HTTP version the client uses, and then the request line ends in a _CRLF sequence_. (CRLF stands for _carriage return_ and _line feed_, -which are terms from the typewriter days!) The CRLF sequence can also be -written as `\r\n`, where `\r` is a carriage return and `\n` is a line feed. The -CRLF sequence separates the request line from the rest of the request data. -Note that when the CRLF is printed, we see a new line start rather than `\r\n`. +which are terms from the typewriter days!) The CRLF sequence can also be written +as `\r\n`, where `\r` is a carriage return and `\n` is a line feed. The CRLF +sequence separates the request line from the rest of the request data. Note that +when the CRLF is printed, we see a new line start rather than `\r\n`. -Looking at the request line data we received from running our program so far, -we see that `GET` is the method, _/_ is the request URI, and `HTTP/1.1` is the +Looking at the request line data we received from running our program so far, we +see that `GET` is the method, _/_ is the request URI, and `HTTP/1.1` is the version. After the request line, the remaining lines starting from `Host:` onward are headers. `GET` requests have no body. -Try making a request from a different browser or asking for a different -address, such as _127.0.0.1:7878/test_, to see how the request data changes. +Try making a request from a different browser or asking for a different address, +such as _127.0.0.1:7878/test_, to see how the request data changes. Now that we know what the browser is asking for, let’s send back some data! ### Writing a Response -We’re going to implement sending data in response to a client request. -Responses have the following format: +We’re going to implement sending data in response to a client request. Responses +have the following format: ```text HTTP-Version Status-Code Reason-Phrase CRLF @@ -253,8 +253,8 @@ message-body ``` The first line is a _status line_ that contains the HTTP version used in the -response, a numeric status code that summarizes the result of the request, and -a reason phrase that provides a text description of the status code. After the +response, a numeric status code that summarizes the result of the request, and a +reason phrase that provides a text description of the status code. After the CRLF sequence are any headers, another CRLF sequence, and the body of the response. @@ -267,9 +267,8 @@ HTTP/1.1 200 OK\r\n\r\n The status code 200 is the standard success response. The text is a tiny successful HTTP response. Let’s write this to the stream as our response to a -successful request! From the `handle_connection` function, remove the -`println!` that was printing the request data and replace it with the code in -Listing 21-3. +successful request! From the `handle_connection` function, remove the `println!` +that was printing the request data and replace it with the code in Listing 21-3. <Listing number="21-3" file-name="src/main.rs" caption="Writing a tiny successful HTTP response to the stream"> @@ -336,20 +335,19 @@ should see your HTML rendered! Currently, we’re ignoring the request data in `http_request` and just sending back the contents of the HTML file unconditionally. That means if you try requesting _127.0.0.1:7878/something-else_ in your browser, you’ll still get -back this same HTML response. At the moment, our server is very limited and -does not do what most web servers do. We want to customize our responses -depending on the request and only send back the HTML file for a well-formed -request to _/_. +back this same HTML response. At the moment, our server is very limited and does +not do what most web servers do. We want to customize our responses depending on +the request and only send back the HTML file for a well-formed request to _/_. ### Validating the Request and Selectively Responding Right now, our web server will return the HTML in the file no matter what the client requested. Let’s add functionality to check that the browser is -requesting _/_ before returning the HTML file and return an error if the -browser requests anything else. For this we need to modify `handle_connection`, -as shown in Listing 21-6. This new code checks the content of the request -received against what we know a request for _/_ looks like and adds `if` and -`else` blocks to treat requests differently. +requesting _/_ before returning the HTML file and return an error if the browser +requests anything else. For this we need to modify `handle_connection`, as shown +in Listing 21-6. This new code checks the content of the request received +against what we know a request for _/_ looks like and adds `if` and `else` +blocks to treat requests differently. <Listing number="21-6" file-name="src/main.rs" caption="Handling requests to */* differently from other requests"> @@ -370,9 +368,9 @@ Next, we check the `request_line` to see if it equals the request line of a GET request to the _/_ path. If it does, the `if` block returns the contents of our HTML file. -If the `request_line` does _not_ equal the GET request to the _/_ path, it -means we’ve received some other request. We’ll add code to the `else` block in -a moment to respond to all other requests. +If the `request_line` does _not_ equal the GET request to the _/_ path, it means +we’ve received some other request. We’ll add code to the `else` block in a +moment to respond to all other requests. Run this code now and request _127.0.0.1:7878_; you should get the HTML in _hello.html_. If you make any other request, such as @@ -380,8 +378,8 @@ _127.0.0.1:7878/something-else_, you’ll get a connection error like those you saw when running the code in Listing 21-1 and Listing 21-2. Now let’s add the code in Listing 21-7 to the `else` block to return a response -with the status code 404, which signals that the content for the request was -not found. We’ll also return some HTML for a page to render in the browser +with the status code 404, which signals that the content for the request was not +found. We’ll also return some HTML for a page to render in the browser indicating the response to the end user. <Listing number="21-7" file-name="src/main.rs" caption="Responding with status code 404 and an error page if anything other than */* was requested"> @@ -394,9 +392,9 @@ indicating the response to the end user. Here, our response has a status line with status code 404 and the reason phrase `NOT FOUND`. The body of the response will be the HTML in the file _404.html_. -You’ll need to create a _404.html_ file next to _hello.html_ for the error -page; again feel free to use any HTML you want or use the example HTML in -Listing 21-8. +You’ll need to create a _404.html_ file next to _hello.html_ for the error page; +again feel free to use any HTML you want or use the example HTML in Listing +21-8. <Listing number="21-8" file-name="404.html" caption="Sample content for the page to send back with any 404 response"> @@ -416,10 +414,10 @@ At the moment the `if` and `else` blocks have a lot of repetition: they’re bot reading files and writing the contents of the files to the stream. The only differences are the status line and the filename. Let’s make the code more concise by pulling out those differences into separate `if` and `else` lines -that will assign the values of the status line and the filename to variables; -we can then use those variables unconditionally in the code to read the file -and write the response. Listing 21-9 shows the resulting code after replacing -the large `if` and `else` blocks. +that will assign the values of the status line and the filename to variables; we +can then use those variables unconditionally in the code to read the file and +write the response. Listing 21-9 shows the resulting code after replacing the +large `if` and `else` blocks. <Listing number="21-9" file-name="src/main.rs" caption="Refactoring the `if` and `else` blocks to contain only the code that differs between the two cases"> @@ -429,17 +427,17 @@ the large `if` and `else` blocks. </Listing> -Now the `if` and `else` blocks only return the appropriate values for the -status line and filename in a tuple; we then use destructuring to assign these -two values to `status_line` and `filename` using a pattern in the `let` -statement, as discussed in Chapter 19. +Now the `if` and `else` blocks only return the appropriate values for the status +line and filename in a tuple; we then use destructuring to assign these two +values to `status_line` and `filename` using a pattern in the `let` statement, +as discussed in Chapter 19. The previously duplicated code is now outside the `if` and `else` blocks and -uses the `status_line` and `filename` variables. This makes it easier to see -the difference between the two cases, and it means we have only one place to -update the code if we want to change how the file reading and response writing -work. The behavior of the code in Listing 21-9 will be the same as that in -Listing 21-7. +uses the `status_line` and `filename` variables. This makes it easier to see the +difference between the two cases, and it means we have only one place to update +the code if we want to change how the file reading and response writing work. +The behavior of the code in Listing 21-9 will be the same as that in Listing +21-7. Awesome! We now have a simple web server in approximately 40 lines of Rust code that responds to one request with a page of content and responds to all other diff --git a/src/ch21-02-multithreaded.md b/src/ch21-02-multithreaded.md index c2e7865ab5..3a490a4909 100644 --- a/src/ch21-02-multithreaded.md +++ b/src/ch21-02-multithreaded.md @@ -37,13 +37,13 @@ You can see how primitive our server is: real libraries would handle the recognition of multiple requests in a much less verbose way! Start the server using `cargo run`. Then open two browser windows: one for -_http://127.0.0.1:7878/_ and the other for _http://127.0.0.1:7878/sleep_. If -you enter the _/_ URI a few times, as before, you’ll see it respond quickly. -But if you enter _/sleep_ and then load _/_, you’ll see that _/_ waits until -`sleep` has slept for its full 5 seconds before loading. +_http://127.0.0.1:7878/_ and the other for _http://127.0.0.1:7878/sleep_. If you +enter the _/_ URI a few times, as before, you’ll see it respond quickly. But if +you enter _/sleep_ and then load _/_, you’ll see that _/_ waits until `sleep` +has slept for its full 5 seconds before loading. -There are multiple techniques we could use to avoid requests backing up behind -a slow request, including using async as we did Chapter 17; the one we’ll +There are multiple techniques we could use to avoid requests backing up behind a +slow request, including using async as we did Chapter 17; the one we’ll implement is a thread pool. ### Improving Throughput with a Thread Pool @@ -59,29 +59,29 @@ increasing the throughput of your server. We’ll limit the number of threads in the pool to a small number to protect us from Denial of Service (DoS) attacks; if we had our program create a new thread -for each request as it came in, someone making 10 million requests to our -server could create havoc by using up all our server’s resources and grinding -the processing of requests to a halt. +for each request as it came in, someone making 10 million requests to our server +could create havoc by using up all our server’s resources and grinding the +processing of requests to a halt. Rather than spawning unlimited threads, then, we’ll have a fixed number of threads waiting in the pool. Requests that come in are sent to the pool for processing. The pool will maintain a queue of incoming requests. Each of the threads in the pool will pop off a request from this queue, handle the request, and then ask the queue for another request. With this design, we can process up -to `N` requests concurrently, where `N` is the number of threads. If each -thread is responding to a long-running request, subsequent requests can still -back up in the queue, but we’ve increased the number of long-running requests -we can handle before reaching that point. +to `N` requests concurrently, where `N` is the number of threads. If each thread +is responding to a long-running request, subsequent requests can still back up +in the queue, but we’ve increased the number of long-running requests we can +handle before reaching that point. This technique is just one of many ways to improve the throughput of a web server. Other options you might explore are the _fork/join model_, the _single-threaded async I/O model_, or the _multi-threaded async I/O model_. If -you’re interested in this topic, you can read more about other solutions and -try to implement them; with a low-level language like Rust, all of these -options are possible. +you’re interested in this topic, you can read more about other solutions and try +to implement them; with a low-level language like Rust, all of these options are +possible. -Before we begin implementing a thread pool, let’s talk about what using the -pool should look like. When you’re trying to design code, writing the client +Before we begin implementing a thread pool, let’s talk about what using the pool +should look like. When you’re trying to design code, writing the client interface first can help guide your design. Write the API of the code so it’s structured in the way you want to call it; then implement the functionality within that structure rather than implementing the functionality and then @@ -103,9 +103,9 @@ First, let’s explore how our code might look if it did create a new thread for every connection. As mentioned earlier, this isn’t our final plan due to the problems with potentially spawning an unlimited number of threads, but it is a starting point to get a working multithreaded server first. Then we’ll add the -thread pool as an improvement, and contrasting the two solutions will be -easier. Listing 21-11 shows the changes to make to `main` to spawn a new thread -to handle each stream within the `for` loop. +thread pool as an improvement, and contrasting the two solutions will be easier. +Listing 21-11 shows the changes to make to `main` to spawn a new thread to +handle each stream within the `for` loop. <Listing number="21-11" file-name="src/main.rs" caption="Spawning a new thread for each stream"> @@ -133,9 +133,9 @@ pool and think about how things would look different or the same with async. #### Creating a Finite Number of Threads We want our thread pool to work in a similar, familiar way so switching from -threads to a thread pool doesn’t require large changes to the code that uses -our API. Listing 21-12 shows the hypothetical interface for a `ThreadPool` -struct we want to use instead of `thread::spawn`. +threads to a thread pool doesn’t require large changes to the code that uses our +API. Listing 21-12 shows the hypothetical interface for a `ThreadPool` struct we +want to use instead of `thread::spawn`. <Listing number="21-12" file-name="src/main.rs" caption="Our ideal `ThreadPool` interface"> @@ -148,9 +148,9 @@ struct we want to use instead of `thread::spawn`. We use `ThreadPool::new` to create a new thread pool with a configurable number of threads, in this case four. Then, in the `for` loop, `pool.execute` has a similar interface as `thread::spawn` in that it takes a closure the pool should -run for each stream. We need to implement `pool.execute` so it takes the -closure and gives it to a thread in the pool to run. This code won’t yet -compile, but we’ll try so the compiler can guide us in how to fix it. +run for each stream. We need to implement `pool.execute` so it takes the closure +and gives it to a thread in the pool to run. This code won’t yet compile, but +we’ll try so the compiler can guide us in how to fix it. <!-- Old headings. Do not remove or links may break. --> @@ -166,13 +166,12 @@ error we get: {{#include ../listings/ch21-web-server/listing-21-12/output.txt}} ``` -Great! This error tells us we need a `ThreadPool` type or module, so we’ll -build one now. Our `ThreadPool` implementation will be independent of the kind -of work our web server is doing. So, let’s switch the `hello` crate from a -binary crate to a library crate to hold our `ThreadPool` implementation. After -we change to a library crate, we could also use the separate thread pool -library for any work we want to do using a thread pool, not just for serving -web requests. +Great! This error tells us we need a `ThreadPool` type or module, so we’ll build +one now. Our `ThreadPool` implementation will be independent of the kind of work +our web server is doing. So, let’s switch the `hello` crate from a binary crate +to a library crate to hold our `ThreadPool` implementation. After we change to a +library crate, we could also use the separate thread pool library for any work +we want to do using a thread pool, not just for serving web requests. Create a _src/lib.rs_ that contains the following, which is the simplest definition of a `ThreadPool` struct that we can have for now: @@ -185,8 +184,8 @@ definition of a `ThreadPool` struct that we can have for now: </Listing> -Then edit _main.rs_ file to bring `ThreadPool` into scope from the library -crate by adding the following code to the top of _src/main.rs_: +Then edit _main.rs_ file to bring `ThreadPool` into scope from the library crate +by adding the following code to the top of _src/main.rs_: <Listing file-name="src/main.rs"> @@ -204,10 +203,9 @@ we need to address: ``` This error indicates that next we need to create an associated function named -`new` for `ThreadPool`. We also know that `new` needs to have one parameter -that can accept `4` as an argument and should return a `ThreadPool` instance. -Let’s implement the simplest `new` function that will have those -characteristics: +`new` for `ThreadPool`. We also know that `new` needs to have one parameter that +can accept `4` as an argument and should return a `ThreadPool` instance. Let’s +implement the simplest `new` function that will have those characteristics: <Listing file-name="src/lib.rs"> @@ -218,9 +216,9 @@ characteristics: </Listing> We chose `usize` as the type of the `size` parameter, because we know that a -negative number of threads doesn’t make any sense. We also know we’ll use this -4 as the number of elements in a collection of threads, which is what the -`usize` type is for, as discussed in the [“Integer Types”][integer-types]<!-- +negative number of threads doesn’t make any sense. We also know we’ll use this 4 +as the number of elements in a collection of threads, which is what the `usize` +type is for, as discussed in the [“Integer Types”][integer-types]<!-- ignore --> section of Chapter 3. Let’s check the code again: @@ -230,20 +228,22 @@ Let’s check the code again: ``` Now the error occurs because we don’t have an `execute` method on `ThreadPool`. -Recall from the [“Creating a Finite Number of +Recall from the +[“Creating a Finite Number of Threads”](#creating-a-finite-number-of-threads)<!-- ignore --> section that we decided our thread pool should have an interface similar to `thread::spawn`. In addition, we’ll implement the `execute` function so it takes the closure it’s given and gives it to an idle thread in the pool to run. We’ll define the `execute` method on `ThreadPool` to take a closure as a -parameter. Recall from the [“Moving Captured Values Out of the Closure and the +parameter. Recall from the +[“Moving Captured Values Out of the Closure and the `Fn` Traits”][fn-traits]<!-- ignore --> section in Chapter 13 that we can take -closures as parameters with three different traits: `Fn`, `FnMut`, and -`FnOnce`. We need to decide which kind of closure to use here. We know we’ll -end up doing something similar to the standard library `thread::spawn` -implementation, so we can look at what bounds the signature of `thread::spawn` -has on its parameter. The documentation shows us the following: +closures as parameters with three different traits: `Fn`, `FnMut`, and `FnOnce`. +We need to decide which kind of closure to use here. We know we’ll end up doing +something similar to the standard library `thread::spawn` implementation, so we +can look at what bounds the signature of `thread::spawn` has on its parameter. +The documentation shows us the following: ```rust,ignore pub fn spawn<F, T>(f: F) -> JoinHandle<T> @@ -321,11 +321,11 @@ zero by using the `assert!` macro, as shown in Listing 21-13. </Listing> -We’ve also added some documentation for our `ThreadPool` with doc comments. -Note that we followed good documentation practices by adding a section that -calls out the situations in which our function can panic, as discussed in -Chapter 14. Try running `cargo doc --open` and clicking the `ThreadPool` struct -to see what the generated docs for `new` look like! +We’ve also added some documentation for our `ThreadPool` with doc comments. Note +that we followed good documentation practices by adding a section that calls out +the situations in which our function can panic, as discussed in Chapter 14. Try +running `cargo doc --open` and clicking the `ThreadPool` struct to see what the +generated docs for `new` look like! Instead of adding the `assert!` macro as we’ve done here, we could change `new` into `build` and return a `Result` like we did with `Config::build` in the I/O @@ -340,8 +340,8 @@ pub fn build(size: usize) -> Result<ThreadPool, PoolCreationError> { #### Creating Space to Store the Threads -Now that we have a way to know we have a valid number of threads to store in -the pool, we can create those threads and store them in the `ThreadPool` struct +Now that we have a way to know we have a valid number of threads to store in the +pool, we can create those threads and store them in the `ThreadPool` struct before returning the struct. But how do we “store” a thread? Let’s take another look at the `thread::spawn` signature: @@ -358,8 +358,8 @@ closure returns. Let’s try using `JoinHandle` too and see what happens. In our case, the closures we’re passing to the thread pool will handle the connection and not return anything, so `T` will be the unit type `()`. -The code in Listing 21-14 will compile but doesn’t create any threads yet. -We’ve changed the definition of `ThreadPool` to hold a vector of +The code in Listing 21-14 will compile but doesn’t create any threads yet. We’ve +changed the definition of `ThreadPool` to hold a vector of `thread::JoinHandle<()>` instances, initialized the vector with a capacity of `size`, set up a `for` loop that will run some code to create the threads, and returned a `ThreadPool` instance containing them. @@ -372,16 +372,15 @@ returned a `ThreadPool` instance containing them. </Listing> -We’ve brought `std::thread` into scope in the library crate, because we’re -using `thread::JoinHandle` as the type of the items in the vector in -`ThreadPool`. +We’ve brought `std::thread` into scope in the library crate, because we’re using +`thread::JoinHandle` as the type of the items in the vector in `ThreadPool`. Once a valid size is received, our `ThreadPool` creates a new vector that can hold `size` items. The `with_capacity` function performs the same task as `Vec::new` but with an important difference: it preallocates space in the vector. Because we know we need to store `size` elements in the vector, doing -this allocation up front is slightly more efficient than using `Vec::new`, -which resizes itself as elements are inserted. +this allocation up front is slightly more efficient than using `Vec::new`, which +resizes itself as elements are inserted. When you run `cargo check` again, it should succeed. @@ -389,27 +388,26 @@ When you run `cargo check` again, it should succeed. We left a comment in the `for` loop in Listing 21-14 regarding the creation of threads. Here, we’ll look at how we actually create threads. The standard -library provides `thread::spawn` as a way to create threads, and -`thread::spawn` expects to get some code the thread should run as soon as the -thread is created. However, in our case, we want to create the threads and have -them _wait_ for code that we’ll send later. The standard library’s -implementation of threads doesn’t include any way to do that; we have to -implement it manually. +library provides `thread::spawn` as a way to create threads, and `thread::spawn` +expects to get some code the thread should run as soon as the thread is created. +However, in our case, we want to create the threads and have them _wait_ for +code that we’ll send later. The standard library’s implementation of threads +doesn’t include any way to do that; we have to implement it manually. We’ll implement this behavior by introducing a new data structure between the -`ThreadPool` and the threads that will manage this new behavior. We’ll call -this data structure _Worker_, which is a common term in pooling -implementations. The Worker picks up code that needs to be run and runs the -code in the Worker’s thread. Think of people working in the kitchen at a -restaurant: the workers wait until orders come in from customers, and then -they’re responsible for taking those orders and fulfilling them. +`ThreadPool` and the threads that will manage this new behavior. We’ll call this +data structure _Worker_, which is a common term in pooling implementations. The +Worker picks up code that needs to be run and runs the code in the Worker’s +thread. Think of people working in the kitchen at a restaurant: the workers wait +until orders come in from customers, and then they’re responsible for taking +those orders and fulfilling them. Instead of storing a vector of `JoinHandle<()>` instances in the thread pool, we’ll store instances of the `Worker` struct. Each `Worker` will store a single `JoinHandle<()>` instance. Then we’ll implement a method on `Worker` that will take a closure of code to run and send it to the already running thread for -execution. We’ll also give each worker an `id` so we can distinguish between -the different workers in the pool when logging or debugging. +execution. We’ll also give each worker an `id` so we can distinguish between the +different workers in the pool when logging or debugging. Here is the new process that will happen when we create a `ThreadPool`. We’ll implement the code that sends the closure to the thread after we have `Worker` @@ -438,20 +436,20 @@ Ready? Here is Listing 21-15 with one way to make the preceding modifications. We’ve changed the name of the field on `ThreadPool` from `threads` to `workers` because it’s now holding `Worker` instances instead of `JoinHandle<()>` -instances. We use the counter in the `for` loop as an argument to -`Worker::new`, and we store each new `Worker` in the vector named `workers`. +instances. We use the counter in the `for` loop as an argument to `Worker::new`, +and we store each new `Worker` in the vector named `workers`. External code (like our server in _src/main.rs_) doesn’t need to know the -implementation details regarding using a `Worker` struct within `ThreadPool`, -so we make the `Worker` struct and its `new` function private. The -`Worker::new` function uses the `id` we give it and stores a `JoinHandle<()>` -instance that is created by spawning a new thread using an empty closure. +implementation details regarding using a `Worker` struct within `ThreadPool`, so +we make the `Worker` struct and its `new` function private. The `Worker::new` +function uses the `id` we give it and stores a `JoinHandle<()>` instance that is +created by spawning a new thread using an empty closure. > Note: If the operating system can’t create a thread because there aren’t -> enough system resources, `thread::spawn` will panic. That will cause our -> whole server to panic, even though the creation of some threads might -> succeed. For simplicity’s sake, this behavior is fine, but in a production -> thread pool implementation, you’d likely want to use +> enough system resources, `thread::spawn` will panic. That will cause our whole +> server to panic, even though the creation of some threads might succeed. For +> simplicity’s sake, this behavior is fine, but in a production thread pool +> implementation, you’d likely want to use > [`std::thread::Builder`][builder]<!-- ignore --> and its > [`spawn`][builder-spawn]<!-- ignore --> method that returns `Result` instead. @@ -471,8 +469,8 @@ a queue held in the `ThreadPool` and send that code to its thread to run. The channels we learned about in Chapter 16—a simple way to communicate between two threads—would be perfect for this use case. We’ll use a channel to function -as the queue of jobs, and `execute` will send a job from the `ThreadPool` to -the `Worker` instances, which will send the job to its thread. Here is the plan: +as the queue of jobs, and `execute` will send a job from the `ThreadPool` to the +`Worker` instances, which will send the job to its thread. Here is the plan: 1. The `ThreadPool` will create a channel and hold on to the sender. 2. Each `Worker` will hold on to the receiver. @@ -483,10 +481,10 @@ the `Worker` instances, which will send the job to its thread. Here is the plan: 5. In its thread, the `Worker` will loop over its receiver and execute the closures of any jobs it receives. -Let’s start by creating a channel in `ThreadPool::new` and holding the sender -in the `ThreadPool` instance, as shown in Listing 21-16. The `Job` struct -doesn’t hold anything for now but will be the type of item we’re sending down -the channel. +Let’s start by creating a channel in `ThreadPool::new` and holding the sender in +the `ThreadPool` instance, as shown in Listing 21-16. The `Job` struct doesn’t +hold anything for now but will be the type of item we’re sending down the +channel. <Listing number="21-16" file-name="src/lib.rs" caption="Modifying `ThreadPool` to store the sender of a channel that transmits `Job` instances"> @@ -521,12 +519,12 @@ When we try to check this code, we get this error: {{#include ../listings/ch21-web-server/listing-21-17/output.txt}} ``` -The code is trying to pass `receiver` to multiple `Worker` instances. This -won’t work, as you’ll recall from Chapter 16: the channel implementation that -Rust provides is multiple _producer_, single _consumer_. This means we can’t -just clone the consuming end of the channel to fix this code. We also don’t -want to send a message multiple times to multiple consumers; we want one list -of messages with multiple workers such that each message gets processed once. +The code is trying to pass `receiver` to multiple `Worker` instances. This won’t +work, as you’ll recall from Chapter 16: the channel implementation that Rust +provides is multiple _producer_, single _consumer_. This means we can’t just +clone the consuming end of the channel to fix this code. We also don’t want to +send a message multiple times to multiple consumers; we want one list of +messages with multiple workers such that each message gets processed once. Additionally, taking a job off the channel queue involves mutating the `receiver`, so the threads need a safe way to share and modify `receiver`; @@ -556,10 +554,11 @@ With these changes, the code compiles! We’re getting there! Let’s finally implement the `execute` method on `ThreadPool`. We’ll also change `Job` from a struct to a type alias for a trait object that holds the type of -closure that `execute` receives. As discussed in the [“Creating Type Synonyms +closure that `execute` receives. As discussed in the +[“Creating Type Synonyms with Type Aliases”][creating-type-synonyms-with-type-aliases]<!-- ignore --> -section of Chapter 20, type aliases allow us to make long types shorter for -ease of use. Look at Listing 21-19. +section of Chapter 20, type aliases allow us to make long types shorter for ease +of use. Look at Listing 21-19. <Listing number="21-19" file-name="src/lib.rs" caption="Creating a `Job` type alias for a `Box` that holds each closure and then sending the job down the channel"> @@ -573,10 +572,10 @@ After creating a new `Job` instance using the closure we get in `execute`, we send that job down the sending end of the channel. We’re calling `unwrap` on `send` for the case that sending fails. This might happen if, for example, we stop all our threads from executing, meaning the receiving end has stopped -receiving new messages. At the moment, we can’t stop our threads from -executing: our threads continue executing as long as the pool exists. The -reason we use `unwrap` is that we know the failure case won’t happen, but the -compiler doesn’t know that. +receiving new messages. At the moment, we can’t stop our threads from executing: +our threads continue executing as long as the pool exists. The reason we use +`unwrap` is that we know the failure case won’t happen, but the compiler doesn’t +know that. But we’re not quite done yet! In the worker, our closure being passed to `thread::spawn` still only _references_ the receiving end of the channel. @@ -602,8 +601,8 @@ you. If we get the lock on the mutex, we call `recv` to receive a `Job` from the channel. A final `unwrap` moves past any errors here as well, which might occur -if the thread holding the sender has shut down, similar to how the `send` -method returns `Err` if the receiver shuts down. +if the thread holding the sender has shut down, similar to how the `send` method +returns `Err` if the receiver shuts down. The call to `recv` blocks, so if there is no job yet, the current thread will wait until a job becomes available. The `Mutex<T>` ensures that only one @@ -690,18 +689,19 @@ processed. The reason is somewhat subtle: the `Mutex` struct has no public `unlock` method because the ownership of the lock is based on the lifetime of the `MutexGuard<T>` within the `LockResult<MutexGuard<T>>` that the `lock` method returns. At compile time, the borrow checker can then enforce the rule -that a resource guarded by a `Mutex` cannot be accessed unless we hold the -lock. However, this implementation can also result in the lock being held -longer than intended if we aren’t mindful of the lifetime of the -`MutexGuard<T>`. - -The code in Listing 21-20 that uses `let job = -receiver.lock().unwrap().recv().unwrap();` works because with `let`, any -temporary values used in the expression on the right hand side of the equals +that a resource guarded by a `Mutex` cannot be accessed unless we hold the lock. +However, this implementation can also result in the lock being held longer than +intended if we aren’t mindful of the lifetime of the `MutexGuard<T>`. + +The code in Listing 21-20 that uses +`let job = +receiver.lock().unwrap().recv().unwrap();` works because with `let`, +any temporary values used in the expression on the right hand side of the equals sign are immediately dropped when the `let` statement ends. However, `while -let` (and `if let` and `match`) does not drop temporary values until the end of -the associated block. In Listing 21-21, the lock remains held for the duration -of the call to `job()`, meaning other workers cannot receive jobs. +let` +(and `if let` and `match`) does not drop temporary values until the end of the +associated block. In Listing 21-21, the lock remains held for the duration of +the call to `job()`, meaning other workers cannot receive jobs. [creating-type-synonyms-with-type-aliases]: ch20-04-advanced-types.html#creating-type-synonyms-with-type-aliases [integer-types]: ch03-02-data-types.html#integer-types diff --git a/src/ch21-03-graceful-shutdown-and-cleanup.md b/src/ch21-03-graceful-shutdown-and-cleanup.md index 36e85f32b3..2c5b0dcd37 100644 --- a/src/ch21-03-graceful-shutdown-and-cleanup.md +++ b/src/ch21-03-graceful-shutdown-and-cleanup.md @@ -34,12 +34,12 @@ quite work yet. </Listing> -First, we loop through each of the thread pool `workers`. We use `&mut` for -this because `self` is a mutable reference, and we also need to be able to -mutate `worker`. For each worker, we print a message saying that this -particular worker is shutting down, and then we call `join` on that worker’s -thread. If the call to `join` fails, we use `unwrap` to make Rust panic and go -into an ungraceful shutdown. +First, we loop through each of the thread pool `workers`. We use `&mut` for this +because `self` is a mutable reference, and we also need to be able to mutate +`worker`. For each worker, we print a message saying that this particular worker +is shutting down, and then we call `join` on that worker’s thread. If the call +to `join` fails, we use `unwrap` to make Rust panic and go into an ungraceful +shutdown. Here is the error we get when we compile this code: @@ -47,16 +47,15 @@ Here is the error we get when we compile this code: {{#include ../listings/ch21-web-server/listing-21-22/output.txt}} ``` -The error tells us we can’t call `join` because we only have a mutable borrow -of each `worker` and `join` takes ownership of its argument. To solve this -issue, we need to move the thread out of the `Worker` instance that owns -`thread` so `join` can consume the thread. We did this in Listing 17-15: if -`Worker` holds an `Option<thread::JoinHandle<()>>` instead, we can call the -`take` method on the `Option` to move the value out of the `Some` variant and -leave a `None` variant in its place. In other words, a `Worker` that is running -will have a `Some` variant in `thread`, and when we want to clean up a -`Worker`, we’ll replace `Some` with `None` so the `Worker` doesn’t have a -thread to run. +The error tells us we can’t call `join` because we only have a mutable borrow of +each `worker` and `join` takes ownership of its argument. To solve this issue, +we need to move the thread out of the `Worker` instance that owns `thread` so +`join` can consume the thread. We did this in Listing 17-15: if `Worker` holds +an `Option<thread::JoinHandle<()>>` instead, we can call the `take` method on +the `Option` to move the value out of the `Some` variant and leave a `None` +variant in its place. In other words, a `Worker` that is running will have a +`Some` variant in `thread`, and when we want to clean up a `Worker`, we’ll +replace `Some` with `None` so the `Worker` doesn’t have a thread to run. So we know we want to update the definition of `Worker` like this: @@ -76,8 +75,8 @@ Checking this code, we get two errors: ``` Let’s address the second error, which points to the code at the end of -`Worker::new`; we need to wrap the `thread` value in `Some` when we create a -new `Worker`. Make the following changes to fix this error: +`Worker::new`; we need to wrap the `thread` value in `Some` when we create a new +`Worker`. Make the following changes to fix this error: <Listing file-name="src/lib.rs"> @@ -102,8 +101,8 @@ The following changes will do so: As discussed in Chapter 18, the `take` method on `Option` takes the `Some` variant out and leaves `None` in its place. We’re using `if let` to destructure the `Some` and get the thread; then we call `join` on the thread. If a worker’s -thread is already `None`, we know that worker has already had its thread -cleaned up, so nothing happens in that case. +thread is already `None`, we know that worker has already had its thread cleaned +up, so nothing happens in that case. ### Signaling to the Threads to Stop Listening for Jobs @@ -111,9 +110,9 @@ With all the changes we’ve made, our code compiles without any warnings. However, the bad news is this code doesn’t function the way we want it to yet. The key is the logic in the closures run by the threads of the `Worker` instances: at the moment, we call `join`, but that won’t shut down the threads -because they `loop` forever looking for jobs. If we try to drop our -`ThreadPool` with our current implementation of `drop`, the main thread will -block forever waiting for the first thread to finish. +because they `loop` forever looking for jobs. If we try to drop our `ThreadPool` +with our current implementation of `drop`, the main thread will block forever +waiting for the first thread to finish. To fix this problem, we’ll need a change in the `ThreadPool` `drop` implementation and then a change in the `Worker` loop. @@ -161,9 +160,9 @@ You wouldn’t want a real-world web server to shut down after serving only two requests. This code just demonstrates that the graceful shutdown and cleanup is in working order. -The `take` method is defined in the `Iterator` trait and limits the iteration -to the first two items at most. The `ThreadPool` will go out of scope at the -end of `main`, and the `drop` implementation will run. +The `take` method is defined in the `Iterator` trait and limits the iteration to +the first two items at most. The `ThreadPool` will go out of scope at the end of +`main`, and the `drop` implementation will run. Start the server with `cargo run`, and make three requests. The third request should error, and in your terminal you should see output similar to this: @@ -201,17 +200,17 @@ You might see a different ordering of workers and messages printed. We can see how this code works from the messages: workers 0 and 3 got the first two requests. The server stopped accepting connections after the second connection, and the `Drop` implementation on `ThreadPool` starts executing before worker 3 -even starts its job. Dropping the `sender` disconnects all the workers and -tells them to shut down. The workers each print a message when they disconnect, -and then the thread pool calls `join` to wait for each worker thread to finish. +even starts its job. Dropping the `sender` disconnects all the workers and tells +them to shut down. The workers each print a message when they disconnect, and +then the thread pool calls `join` to wait for each worker thread to finish. Notice one interesting aspect of this particular execution: the `ThreadPool` dropped the `sender`, and before any worker received an error, we tried to join worker 0. Worker 0 had not yet gotten an error from `recv`, so the main thread -blocked waiting for worker 0 to finish. In the meantime, worker 3 received a -job and then all threads received an error. When worker 0 finished, the main -thread waited for the rest of the workers to finish. At that point, they had -all exited their loops and stopped. +blocked waiting for worker 0 to finish. In the meantime, worker 3 received a job +and then all threads received an error. When worker 0 finished, the main thread +waited for the rest of the workers to finish. At that point, they had all exited +their loops and stopped. Congrats! We’ve now completed our project; we have a basic web server that uses a thread pool to respond asynchronously. We’re able to perform a graceful @@ -238,11 +237,11 @@ Here’s the full code for reference: We could do more here! If you want to continue enhancing this project, here are some ideas: -- Add more documentation to `ThreadPool` and its public methods. -- Add tests of the library’s functionality. -- Change calls to `unwrap` to more robust error handling. -- Use `ThreadPool` to perform some task other than serving web requests. -- Find a thread pool crate on [crates.io](https://crates.io/) and implement a +* Add more documentation to `ThreadPool` and its public methods. +* Add tests of the library’s functionality. +* Change calls to `unwrap` to more robust error handling. +* Use `ThreadPool` to perform some task other than serving web requests. +* Find a thread pool crate on [crates.io](https://crates.io/) and implement a similar web server using the crate instead. Then compare its API and robustness to the thread pool we implemented. diff --git a/src/foreword.md b/src/foreword.md index f108b65a67..9be699078e 100644 --- a/src/foreword.md +++ b/src/foreword.md @@ -7,18 +7,18 @@ domains than you did before. Take, for example, “systems-level” work that deals with low-level details of memory management, data representation, and concurrency. Traditionally, this -realm of programming is seen as arcane, accessible only to a select few who -have devoted the necessary years learning to avoid its infamous pitfalls. And -even those who practice it do so with caution, lest their code be open to -exploits, crashes, or corruption. +realm of programming is seen as arcane, accessible only to a select few who have +devoted the necessary years learning to avoid its infamous pitfalls. And even +those who practice it do so with caution, lest their code be open to exploits, +crashes, or corruption. Rust breaks down these barriers by eliminating the old pitfalls and providing a friendly, polished set of tools to help you along the way. Programmers who need to “dip down” into lower-level control can do so with Rust, without taking on -the customary risk of crashes or security holes, and without having to learn -the fine points of a fickle toolchain. Better yet, the language is designed to -guide you naturally towards reliable code that is efficient in terms of speed -and memory usage. +the customary risk of crashes or security holes, and without having to learn the +fine points of a fickle toolchain. Better yet, the language is designed to guide +you naturally towards reliable code that is efficient in terms of speed and +memory usage. Programmers who are already working with low-level code can use Rust to raise their ambitions. For example, introducing parallelism in Rust is a relatively @@ -28,10 +28,10 @@ that you won’t accidentally introduce crashes or vulnerabilities. But Rust isn’t limited to low-level systems programming. It’s expressive and ergonomic enough to make CLI apps, web servers, and many other kinds of code -quite pleasant to write — you’ll find simple examples of both later in the -book. Working with Rust allows you to build skills that transfer from one -domain to another; you can learn Rust by writing a web app, then apply those -same skills to target your Raspberry Pi. +quite pleasant to write — you’ll find simple examples of both later in the book. +Working with Rust allows you to build skills that transfer from one domain to +another; you can learn Rust by writing a web app, then apply those same skills +to target your Raspberry Pi. This book fully embraces the potential of Rust to empower its users. It’s a friendly and approachable text intended to help you level up not just your diff --git a/src/title-page.md b/src/title-page.md index 7301493201..e58122e1a0 100644 --- a/src/title-page.md +++ b/src/title-page.md @@ -9,13 +9,14 @@ to install or update Rust. The HTML format is available online at [https://doc.rust-lang.org/stable/book/](https://doc.rust-lang.org/stable/book/) -and offline with installations of Rust made with `rustup`; run `rustup doc +and offline with installations of Rust made with `rustup`; run +`rustup doc --book` to open. Several community [translations] are also available. -This text is available in [paperback and ebook format from No Starch -Press][nsprust]. +This text is available in +[paperback and ebook format from No Starch Press][nsprust]. [install]: ch01-01-installation.html [editions]: appendix-05-editions.html diff --git a/style-guide.md b/style-guide.md index 04dc805ca5..92ee776feb 100644 --- a/style-guide.md +++ b/style-guide.md @@ -2,33 +2,40 @@ ## Prose -- Prefer title case for chapter/section headings, ex: `## Generating a Secret +* Prefer title case for chapter/section headings, ex: + `## Generating a Secret Number` rather than `## Generating a secret number`. -- Prefer italics over single quotes when calling out a term, ex: `is an - *associated function* of` rather than `is an ‘associated function’ of`. -- When talking about a method in prose, DO NOT include the parentheses, ex: +* Prefer italics over single quotes when calling out a term, ex: + `is an + *associated function* of` rather than + `is an ‘associated function’ of`. +* When talking about a method in prose, DO NOT include the parentheses, ex: `read_line` rather than `read_line()`. -- Hard wrap at 80 chars -- Prefer not mixing code and not-code in one word, ex: ``Remember when we wrote - `use std::io`?`` rather than ``Remember when we `use`d `std::io`?`` +* Hard wrap at 80 chars +* Prefer not mixing code and not-code in one word, ex: + ``Remember when we wrote + `use std::io`?`` rather than + ``Remember when we `use`d `std::io`?`` ## Code -- Add the file name before markdown blocks to make it clear which file we're +* Add the file name before markdown blocks to make it clear which file we're talking about, when applicable. -- When making changes to code, make it clear which parts of the code changed - and which stayed the same... not sure how to do this yet -- Split up long lines as appropriate to keep them under 80 chars if possible -- Use `bash` syntax highlighting for command line output code blocks +* When making changes to code, make it clear which parts of the code changed and + which stayed the same... not sure how to do this yet +* Split up long lines as appropriate to keep them under 80 chars if possible +* Use `bash` syntax highlighting for command line output code blocks ## Links Once all the scripts are done: -- If a link shouldn't be printed, mark it to be ignored - - This includes all "Chapter XX" intra-book links, which _should_ be links - for the HTML version -- Make intra-book links and stdlib API doc links relative so they work whether +* If a link shouldn't be printed, mark it to be ignored + * This includes all "Chapter XX" intra-book links, which _should_ be links for + the HTML version +* Make intra-book links and stdlib API doc links relative so they work whether the book is read offline or on docs.rust-lang.org -- Use markdown links and keep in mind that they will be changed into `text at - *url*` in print, so word them in a way that it reads well in that format +* Use markdown links and keep in mind that they will be changed into + `text at + *url*` in print, so word them in a way that it reads well in that + format