The book says, "Let's make a new binary project with Cargo called
rectangles," but the listing output files use `structs` as the name.
Change this to `rectangles` to align with the book's text.
The compiler has gotten sufficiently smart regarding const evaluation to
catch many trivial instances of array-index-out-of-bounds at compile
time. However, the compiler can't read peoples' minds... yet.
I was trying to reuse too much; we don't want the `take(2)` to be part
of the final code, but we do want it to be in the listing for the
example where we shut down gracefully after 2 requests.
The reasons we needed empty `fn main() {}`s were twofold:
- Avoid confusing people when they click the "expand" button on the code
listing and see the auto-main wrapping
- Avoid failing doctests when running `mdbook test` that don't work when
rustdoc wraps a code listing in main
I think I have a solution that mostly solves these cases.
I don't know why this didn't occur to me before. Here's my current
thinking in case these assumptions turn out to be wrong:
There are a [few things that tell mdbook to disable the
main-wrapping][mdbook], and I hadn't noticed one of them until now: if
you annotate a code block with `noplayground`, it won't add a `main`
around it (and it also won't have the "play" button in the upper right
that runs the block and inserts the result into the page).
So instead of putting an empty `fn main() {}` at the bottom of
src/lib.rs files that doesn't make sense, annotate those listings with
`noplayground`. I don't think anyone will miss the play button anyway
because:
- The play button doesn't run tests, so there wasn't any output for
these examples anyway
- If an example doesn't compile, we have it marked `ignore` so that it
doesn't make the tests fail, and `ignore` also disables the play button,
so there isn't a way to see compiler errors either
In most of these cases, `mdbook test` that runs these as doctests will
still wrap these in main, but the tests still pass.
There are some cases, mostly around modules and using `crate::` that
won't pass as doctests when wrapped in main. For those, I've annotated
them with the [undocumented][] [`test_harness`][] attribute that apparently
I was using at some point and then [stopped using][] and now I've
decided to use again, but maybe send in a PR to rust-lang/rust to
change the name to `no_main` and document it or something. In any case,
that shouldn't affect readers at all.
[mdbook]: d0deee90b0/src/renderer/html_handlebars/hbs_renderer.rs (L805-L808)
[undocumented]: https://github.com/rust-lang/rust/issues/42288#issuecomment-309661382
[`test_harness`]: 220352781c/src/librustdoc/doctest.rs (L252)
[stopped using]: https://github.com/rust-lang/book/pull/1233#discussion_r175515585
There are a few output files that have more than one test binaries; make
their hashes be the same because I'm too tired to figure out how to
capture and replace multiple different hashes.
In chapter 20 on writing a web server, one of the listings includes `.take(2)` on the TCP stream iterator to only handle 2 requests and then quit. This is as an example to demonstrate that the web server shuts down gracefully. However, this has slipped into the final code listing, which I don't think should include this arbitrary restriction. This pull request amends this error.
We were cheating due to Copy, and that made this example awkward.
Returning a reference is probably better here anyway, and it makes
everything flow a bit nicer.
Fixes#1761