From 698f8c2f01ea549d77d7dc3338a12e04c11057b9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:02:58 +0200 Subject: Adding upstream version 1.64.0+dfsg1. Signed-off-by: Daniel Baumann --- .../listing-02-01/Cargo.lock | 6 ++ .../listing-02-01/Cargo.toml | 8 ++ .../listing-02-01/src/main.rs | 31 ++++++ .../listing-02-02/Cargo.lock | 83 +++++++++++++++ .../listing-02-02/Cargo.toml | 9 ++ .../listing-02-02/src/main.rs | 15 +++ .../listing-02-03/Cargo.lock | 83 +++++++++++++++ .../listing-02-03/Cargo.toml | 9 ++ .../listing-02-03/src/main.rs | 28 +++++ .../listing-02-04/Cargo.lock | 83 +++++++++++++++ .../listing-02-04/Cargo.toml | 9 ++ .../listing-02-04/output.txt | 20 ++++ .../listing-02-04/src/main.rs | 32 ++++++ .../listing-02-05/Cargo.lock | 83 +++++++++++++++ .../listing-02-05/Cargo.toml | 9 ++ .../listing-02-05/src/main.rs | 45 ++++++++ .../listing-02-06/Cargo.lock | 83 +++++++++++++++ .../listing-02-06/Cargo.toml | 9 ++ .../listing-02-06/src/main.rs | 35 +++++++ .../no-listing-01-cargo-new/Cargo.lock | 7 ++ .../no-listing-01-cargo-new/Cargo.toml | 8 ++ .../no-listing-01-cargo-new/output.txt | 5 + .../no-listing-01-cargo-new/src/main.rs | 3 + .../no-listing-02-without-expect/Cargo.lock | 6 ++ .../no-listing-02-without-expect/Cargo.toml | 8 ++ .../no-listing-02-without-expect/output.txt | 13 +++ .../no-listing-02-without-expect/src/main.rs | 13 +++ .../Cargo.lock | 83 +++++++++++++++ .../Cargo.toml | 9 ++ .../src/main.rs | 33 ++++++ .../no-listing-04-looping/Cargo.lock | 83 +++++++++++++++ .../no-listing-04-looping/Cargo.toml | 9 ++ .../no-listing-04-looping/src/main.rs | 40 ++++++++ .../no-listing-05-quitting/Cargo.lock | 83 +++++++++++++++ .../no-listing-05-quitting/Cargo.toml | 9 ++ .../no-listing-05-quitting/src/main.rs | 38 +++++++ .../listing-03-01/Cargo.lock | 6 ++ .../listing-03-01/Cargo.toml | 6 ++ .../listing-03-01/src/main.rs | 3 + .../listing-03-02/Cargo.lock | 6 ++ .../listing-03-02/Cargo.toml | 6 ++ .../listing-03-02/output.txt | 5 + .../listing-03-02/src/main.rs | 6 ++ .../listing-03-03/Cargo.lock | 6 ++ .../listing-03-03/Cargo.toml | 6 ++ .../listing-03-03/src/main.rs | 11 ++ .../listing-03-04/Cargo.lock | 6 ++ .../listing-03-04/Cargo.toml | 6 ++ .../listing-03-04/output.txt | 9 ++ .../listing-03-04/src/main.rs | 10 ++ .../listing-03-05/Cargo.lock | 6 ++ .../listing-03-05/Cargo.toml | 6 ++ .../listing-03-05/src/main.rs | 7 ++ .../Cargo.lock | 6 ++ .../Cargo.toml | 6 ++ .../output.txt | 16 +++ .../src/main.rs | 6 ++ .../no-listing-02-adding-mut/Cargo.lock | 6 ++ .../no-listing-02-adding-mut/Cargo.toml | 6 ++ .../no-listing-02-adding-mut/output.txt | 6 ++ .../no-listing-02-adding-mut/src/main.rs | 6 ++ .../no-listing-03-shadowing/Cargo.lock | 6 ++ .../no-listing-03-shadowing/Cargo.toml | 6 ++ .../no-listing-03-shadowing/output.txt | 6 ++ .../no-listing-03-shadowing/src/main.rs | 12 +++ .../Cargo.lock | 6 ++ .../Cargo.toml | 6 ++ .../src/main.rs | 6 ++ .../no-listing-05-mut-cant-change-types/Cargo.lock | 6 ++ .../no-listing-05-mut-cant-change-types/Cargo.toml | 6 ++ .../no-listing-05-mut-cant-change-types/output.txt | 12 +++ .../src/main.rs | 6 ++ .../no-listing-06-floating-point/Cargo.lock | 6 ++ .../no-listing-06-floating-point/Cargo.toml | 6 ++ .../no-listing-06-floating-point/src/main.rs | 5 + .../no-listing-07-numeric-operations/Cargo.lock | 6 ++ .../no-listing-07-numeric-operations/Cargo.toml | 6 ++ .../no-listing-07-numeric-operations/src/main.rs | 17 ++++ .../no-listing-08-boolean/Cargo.lock | 6 ++ .../no-listing-08-boolean/Cargo.toml | 6 ++ .../no-listing-08-boolean/src/main.rs | 5 + .../no-listing-09-char/Cargo.lock | 6 ++ .../no-listing-09-char/Cargo.toml | 6 ++ .../no-listing-09-char/src/main.rs | 5 + .../no-listing-10-tuples/Cargo.lock | 6 ++ .../no-listing-10-tuples/Cargo.toml | 6 ++ .../no-listing-10-tuples/src/main.rs | 3 + .../no-listing-11-destructuring-tuples/Cargo.lock | 6 ++ .../no-listing-11-destructuring-tuples/Cargo.toml | 6 ++ .../no-listing-11-destructuring-tuples/src/main.rs | 7 ++ .../no-listing-12-tuple-indexing/Cargo.lock | 6 ++ .../no-listing-12-tuple-indexing/Cargo.toml | 6 ++ .../no-listing-12-tuple-indexing/src/main.rs | 9 ++ .../no-listing-13-arrays/Cargo.lock | 6 ++ .../no-listing-13-arrays/Cargo.toml | 6 ++ .../no-listing-13-arrays/src/main.rs | 3 + .../no-listing-14-array-indexing/Cargo.lock | 6 ++ .../no-listing-14-array-indexing/Cargo.toml | 6 ++ .../no-listing-14-array-indexing/src/main.rs | 6 ++ .../no-listing-15-invalid-array-access/Cargo.lock | 6 ++ .../no-listing-15-invalid-array-access/Cargo.toml | 6 ++ .../no-listing-15-invalid-array-access/src/main.rs | 22 ++++ .../no-listing-16-functions/Cargo.lock | 6 ++ .../no-listing-16-functions/Cargo.toml | 6 ++ .../no-listing-16-functions/output.txt | 6 ++ .../no-listing-16-functions/src/main.rs | 9 ++ .../Cargo.lock | 6 ++ .../Cargo.toml | 6 ++ .../output.txt | 5 + .../src/main.rs | 7 ++ .../Cargo.lock | 6 ++ .../Cargo.toml | 6 ++ .../output.txt | 5 + .../src/main.rs | 7 ++ .../Cargo.lock | 4 + .../Cargo.toml | 6 ++ .../output.txt | 34 +++++++ .../rustfmt-ignore | 1 + .../src/main.rs | 3 + .../Cargo.lock | 6 ++ .../Cargo.toml | 6 ++ .../src/main.rs | 8 ++ .../Cargo.lock | 6 ++ .../Cargo.toml | 6 ++ .../output.txt | 5 + .../src/main.rs | 9 ++ .../Cargo.lock | 6 ++ .../Cargo.toml | 6 ++ .../src/main.rs | 9 ++ .../Cargo.lock | 6 ++ .../Cargo.toml | 6 ++ .../output.txt | 14 +++ .../src/main.rs | 9 ++ .../no-listing-24-comments-end-of-line/Cargo.lock | 6 ++ .../no-listing-24-comments-end-of-line/Cargo.toml | 6 ++ .../no-listing-24-comments-end-of-line/src/main.rs | 3 + .../no-listing-25-comments-above-line/Cargo.lock | 6 ++ .../no-listing-25-comments-above-line/Cargo.toml | 6 ++ .../no-listing-25-comments-above-line/src/main.rs | 4 + .../no-listing-26-if-true/Cargo.lock | 6 ++ .../no-listing-26-if-true/Cargo.toml | 6 ++ .../no-listing-26-if-true/output.txt | 5 + .../no-listing-26-if-true/src/main.rs | 9 ++ .../no-listing-27-if-false/Cargo.lock | 6 ++ .../no-listing-27-if-false/Cargo.toml | 6 ++ .../no-listing-27-if-false/output.txt | 5 + .../no-listing-27-if-false/src/main.rs | 11 ++ .../Cargo.lock | 6 ++ .../Cargo.toml | 6 ++ .../output.txt | 10 ++ .../src/main.rs | 7 ++ .../no-listing-29-if-not-equal-0/Cargo.lock | 6 ++ .../no-listing-29-if-not-equal-0/Cargo.toml | 6 ++ .../no-listing-29-if-not-equal-0/src/main.rs | 7 ++ .../no-listing-30-else-if/Cargo.lock | 6 ++ .../no-listing-30-else-if/Cargo.toml | 6 ++ .../no-listing-30-else-if/output.txt | 5 + .../no-listing-30-else-if/src/main.rs | 13 +++ .../Cargo.lock | 6 ++ .../Cargo.toml | 6 ++ .../output.txt | 12 +++ .../src/main.rs | 7 ++ .../no-listing-32-5-loop-labels/Cargo.lock | 5 + .../no-listing-32-5-loop-labels/Cargo.toml | 6 ++ .../no-listing-32-5-loop-labels/output.txt | 13 +++ .../no-listing-32-5-loop-labels/src/main.rs | 21 ++++ .../no-listing-32-loop/Cargo.lock | 6 ++ .../no-listing-32-loop/Cargo.toml | 6 ++ .../no-listing-32-loop/src/main.rs | 5 + .../Cargo.lock | 6 ++ .../Cargo.toml | 6 ++ .../src/main.rs | 13 +++ .../no-listing-34-for-range/Cargo.lock | 6 ++ .../no-listing-34-for-range/Cargo.toml | 6 ++ .../no-listing-34-for-range/src/main.rs | 6 ++ .../output-only-01-no-type-annotations/Cargo.lock | 6 ++ .../output-only-01-no-type-annotations/Cargo.toml | 8 ++ .../output-only-01-no-type-annotations/output.txt | 10 ++ .../output-only-01-no-type-annotations/src/main.rs | 3 + .../listing-04-01/Cargo.lock | 4 + .../listing-04-01/Cargo.toml | 6 ++ .../listing-04-01/rustfmt-ignore | 3 + .../listing-04-01/src/main.rs | 9 ++ .../listing-04-02/Cargo.lock | 6 ++ .../listing-04-02/Cargo.toml | 6 ++ .../listing-04-02/src/main.rs | 6 ++ .../listing-04-03/Cargo.lock | 4 + .../listing-04-03/Cargo.toml | 6 ++ .../listing-04-03/rustfmt-ignore | 3 + .../listing-04-03/src/main.rs | 23 +++++ .../listing-04-04/Cargo.lock | 4 + .../listing-04-04/Cargo.toml | 6 ++ .../listing-04-04/rustfmt-ignore | 3 + .../listing-04-04/src/main.rs | 29 ++++++ .../listing-04-05/Cargo.lock | 6 ++ .../listing-04-05/Cargo.toml | 6 ++ .../listing-04-05/src/main.rs | 13 +++ .../listing-04-06/Cargo.lock | 6 ++ .../listing-04-06/Cargo.toml | 6 ++ .../listing-04-06/output.txt | 12 +++ .../listing-04-06/src/main.rs | 9 ++ .../listing-04-07/Cargo.lock | 6 ++ .../listing-04-07/Cargo.toml | 6 ++ .../listing-04-07/src/main.rs | 21 ++++ .../listing-04-08/Cargo.lock | 6 ++ .../listing-04-08/Cargo.toml | 6 ++ .../listing-04-08/src/main.rs | 24 +++++ .../listing-04-09/Cargo.lock | 6 ++ .../listing-04-09/Cargo.toml | 6 ++ .../listing-04-09/src/main.rs | 36 +++++++ .../no-listing-01-can-mutate-string/Cargo.lock | 6 ++ .../no-listing-01-can-mutate-string/Cargo.toml | 6 ++ .../no-listing-01-can-mutate-string/src/main.rs | 9 ++ .../no-listing-02-string-scope/Cargo.lock | 4 + .../no-listing-02-string-scope/Cargo.toml | 6 ++ .../no-listing-02-string-scope/rustfmt-ignore | 3 + .../no-listing-02-string-scope/src/main.rs | 10 ++ .../no-listing-03-string-move/Cargo.lock | 6 ++ .../no-listing-03-string-move/Cargo.toml | 6 ++ .../no-listing-03-string-move/src/main.rs | 6 ++ .../no-listing-04-cant-use-after-move/Cargo.lock | 6 ++ .../no-listing-04-cant-use-after-move/Cargo.toml | 6 ++ .../no-listing-04-cant-use-after-move/output.txt | 17 ++++ .../no-listing-04-cant-use-after-move/src/main.rs | 8 ++ .../no-listing-05-clone/Cargo.lock | 6 ++ .../no-listing-05-clone/Cargo.toml | 6 ++ .../no-listing-05-clone/src/main.rs | 8 ++ .../no-listing-06-copy/Cargo.lock | 6 ++ .../no-listing-06-copy/Cargo.toml | 6 ++ .../no-listing-06-copy/src/main.rs | 8 ++ .../no-listing-07-reference/Cargo.lock | 6 ++ .../no-listing-07-reference/Cargo.toml | 6 ++ .../no-listing-07-reference/src/main.rs | 15 +++ .../Cargo.lock | 4 + .../Cargo.toml | 6 ++ .../rustfmt-ignore | 3 + .../src/main.rs | 14 +++ .../no-listing-09-fixes-listing-04-06/Cargo.lock | 6 ++ .../no-listing-09-fixes-listing-04-06/Cargo.toml | 6 ++ .../no-listing-09-fixes-listing-04-06/src/main.rs | 9 ++ .../Cargo.lock | 6 ++ .../Cargo.toml | 6 ++ .../output.txt | 15 +++ .../src/main.rs | 10 ++ .../Cargo.lock | 6 ++ .../Cargo.toml | 6 ++ .../src/main.rs | 11 ++ .../Cargo.lock | 6 ++ .../Cargo.toml | 6 ++ .../output.txt | 16 +++ .../src/main.rs | 11 ++ .../no-listing-13-reference-scope-ends/Cargo.lock | 6 ++ .../no-listing-13-reference-scope-ends/Cargo.toml | 6 ++ .../no-listing-13-reference-scope-ends/src/main.rs | 13 +++ .../no-listing-14-dangling-reference/Cargo.lock | 6 ++ .../no-listing-14-dangling-reference/Cargo.toml | 6 ++ .../no-listing-14-dangling-reference/output.txt | 16 +++ .../no-listing-14-dangling-reference/src/main.rs | 9 ++ .../Cargo.lock | 4 + .../Cargo.toml | 6 ++ .../rustfmt-ignore | 3 + .../src/main.rs | 13 +++ .../no-listing-16-no-dangle/Cargo.lock | 6 ++ .../no-listing-16-no-dangle/Cargo.toml | 6 ++ .../no-listing-16-no-dangle/src/main.rs | 11 ++ .../no-listing-17-slice/Cargo.lock | 6 ++ .../no-listing-17-slice/Cargo.toml | 6 ++ .../no-listing-17-slice/src/main.rs | 8 ++ .../no-listing-18-first-word-slice/Cargo.lock | 6 ++ .../no-listing-18-first-word-slice/Cargo.toml | 6 ++ .../no-listing-18-first-word-slice/src/main.rs | 15 +++ .../no-listing-19-slice-error/Cargo.lock | 6 ++ .../no-listing-19-slice-error/Cargo.toml | 6 ++ .../no-listing-19-slice-error/output.txt | 16 +++ .../no-listing-19-slice-error/src/main.rs | 23 +++++ .../listing-05-01/Cargo.lock | 6 ++ .../listing-05-01/Cargo.toml | 6 ++ .../listing-05-01/src/main.rs | 10 ++ .../listing-05-02/Cargo.lock | 6 ++ .../listing-05-02/Cargo.toml | 6 ++ .../listing-05-02/src/main.rs | 17 ++++ .../listing-05-03/Cargo.lock | 6 ++ .../listing-05-03/Cargo.toml | 6 ++ .../listing-05-03/src/main.rs | 19 ++++ .../listing-05-04/Cargo.lock | 6 ++ .../listing-05-04/Cargo.toml | 6 ++ .../listing-05-04/src/main.rs | 24 +++++ .../listing-05-05/Cargo.lock | 6 ++ .../listing-05-05/Cargo.toml | 6 ++ .../listing-05-05/src/main.rs | 24 +++++ .../listing-05-06/Cargo.lock | 6 ++ .../listing-05-06/Cargo.toml | 6 ++ .../listing-05-06/src/main.rs | 28 +++++ .../listing-05-07/Cargo.lock | 6 ++ .../listing-05-07/Cargo.toml | 6 ++ .../listing-05-07/src/main.rs | 26 +++++ .../listing-05-08/Cargo.lock | 5 + .../listing-05-08/Cargo.toml | 6 ++ .../listing-05-08/output.txt | 5 + .../listing-05-08/src/main.rs | 17 ++++ .../listing-05-09/Cargo.lock | 5 + .../listing-05-09/Cargo.toml | 6 ++ .../listing-05-09/src/main.rs | 12 +++ .../listing-05-10/Cargo.lock | 5 + .../listing-05-10/Cargo.toml | 6 ++ .../listing-05-10/src/main.rs | 20 ++++ .../listing-05-11/Cargo.lock | 5 + .../listing-05-11/Cargo.toml | 6 ++ .../listing-05-11/output.txt | 14 +++ .../listing-05-11/src/main.rs | 13 +++ .../listing-05-12/Cargo.lock | 5 + .../listing-05-12/Cargo.toml | 6 ++ .../listing-05-12/output.txt | 5 + .../listing-05-12/src/main.rs | 14 +++ .../listing-05-13/Cargo.lock | 5 + .../listing-05-13/Cargo.toml | 6 ++ .../listing-05-13/src/main.rs | 23 +++++ .../listing-05-14/Cargo.lock | 5 + .../listing-05-14/Cargo.toml | 6 ++ .../listing-05-14/src/main.rs | 17 ++++ .../listing-05-15/Cargo.lock | 5 + .../listing-05-15/Cargo.toml | 6 ++ .../listing-05-15/src/main.rs | 35 +++++++ .../listing-05-16/Cargo.lock | 5 + .../listing-05-16/Cargo.toml | 6 ++ .../listing-05-16/src/main.rs | 37 +++++++ .../no-listing-01-tuple-structs/Cargo.lock | 6 ++ .../no-listing-01-tuple-structs/Cargo.toml | 6 ++ .../no-listing-01-tuple-structs/src/main.rs | 7 ++ .../no-listing-02-reference-in-struct/Cargo.lock | 6 ++ .../no-listing-02-reference-in-struct/Cargo.toml | 8 ++ .../no-listing-02-reference-in-struct/output.txt | 31 ++++++ .../no-listing-02-reference-in-struct/src/main.rs | 15 +++ .../no-listing-03-associated-functions/Cargo.lock | 5 + .../no-listing-03-associated-functions/Cargo.toml | 6 ++ .../no-listing-03-associated-functions/src/main.rs | 20 ++++ .../no-listing-04-unit-like-structs/Cargo.lock | 7 ++ .../no-listing-04-unit-like-structs/Cargo.toml | 6 ++ .../no-listing-04-unit-like-structs/src/main.rs | 5 + .../no-listing-05-dbg-macro/Cargo.lock | 5 + .../no-listing-05-dbg-macro/Cargo.toml | 6 ++ .../no-listing-05-dbg-macro/output.txt | 9 ++ .../no-listing-05-dbg-macro/src/main.rs | 15 +++ .../Cargo.lock | 5 + .../Cargo.toml | 6 ++ .../src/main.rs | 24 +++++ .../output-only-01-debug/Cargo.lock | 5 + .../output-only-01-debug/Cargo.toml | 6 ++ .../output-only-01-debug/output.txt | 18 ++++ .../output-only-01-debug/src/main.rs | 13 +++ .../output-only-02-pretty-debug/Cargo.lock | 5 + .../output-only-02-pretty-debug/Cargo.toml | 6 ++ .../output-only-02-pretty-debug/output.txt | 8 ++ .../output-only-02-pretty-debug/src/main.rs | 14 +++ .../listing-06-01/Cargo.lock | 6 ++ .../listing-06-01/Cargo.toml | 6 ++ .../listing-06-01/src/main.rs | 23 +++++ .../listing-06-02/Cargo.lock | 6 ++ .../listing-06-02/Cargo.toml | 6 ++ .../listing-06-02/src/main.rs | 10 ++ .../listing-06-03/Cargo.lock | 6 ++ .../listing-06-03/Cargo.toml | 6 ++ .../listing-06-03/src/main.rs | 19 ++++ .../listing-06-04/Cargo.lock | 6 ++ .../listing-06-04/Cargo.toml | 6 ++ .../listing-06-04/src/main.rs | 17 ++++ .../listing-06-05/Cargo.lock | 6 ++ .../listing-06-05/Cargo.toml | 6 ++ .../listing-06-05/src/main.rs | 18 ++++ .../listing-06-06/Cargo.lock | 6 ++ .../listing-06-06/Cargo.toml | 6 ++ .../listing-06-06/src/main.rs | 9 ++ .../no-listing-01-defining-enums/Cargo.lock | 6 ++ .../no-listing-01-defining-enums/Cargo.toml | 6 ++ .../no-listing-01-defining-enums/src/main.rs | 22 ++++ .../no-listing-02-enum-with-data/Cargo.lock | 6 ++ .../no-listing-02-enum-with-data/Cargo.toml | 6 ++ .../no-listing-02-enum-with-data/src/main.rs | 12 +++ .../Cargo.lock | 6 ++ .../Cargo.toml | 6 ++ .../src/main.rs | 12 +++ .../Cargo.lock | 6 ++ .../Cargo.toml | 6 ++ .../src/main.rs | 11 ++ .../no-listing-05-methods-on-enums/Cargo.lock | 6 ++ .../no-listing-05-methods-on-enums/Cargo.toml | 6 ++ .../no-listing-05-methods-on-enums/src/main.rs | 19 ++++ .../no-listing-06-option-examples/Cargo.lock | 6 ++ .../no-listing-06-option-examples/Cargo.toml | 6 ++ .../no-listing-06-option-examples/src/main.rs | 8 ++ .../Cargo.lock | 6 ++ .../Cargo.toml | 6 ++ .../output.txt | 22 ++++ .../src/main.rs | 8 ++ .../Cargo.lock | 6 ++ .../Cargo.toml | 6 ++ .../src/main.rs | 22 ++++ .../no-listing-09-variable-in-pattern/Cargo.lock | 6 ++ .../no-listing-09-variable-in-pattern/Cargo.toml | 6 ++ .../no-listing-09-variable-in-pattern/src/main.rs | 31 ++++++ .../no-listing-10-non-exhaustive-match/Cargo.lock | 6 ++ .../no-listing-10-non-exhaustive-match/Cargo.toml | 6 ++ .../no-listing-10-non-exhaustive-match/output.txt | 18 ++++ .../no-listing-10-non-exhaustive-match/src/main.rs | 13 +++ .../no-listing-12-if-let/Cargo.lock | 6 ++ .../no-listing-12-if-let/Cargo.toml | 6 ++ .../no-listing-12-if-let/src/main.rs | 8 ++ .../Cargo.lock | 6 ++ .../Cargo.toml | 6 ++ .../src/main.rs | 24 +++++ .../Cargo.lock | 6 ++ .../Cargo.toml | 6 ++ .../src/main.rs | 25 +++++ .../no-listing-15-binding-catchall/Cargo.lock | 6 ++ .../no-listing-15-binding-catchall/Cargo.toml | 6 ++ .../no-listing-15-binding-catchall/src/main.rs | 14 +++ .../no-listing-16-underscore-catchall/Cargo.lock | 6 ++ .../no-listing-16-underscore-catchall/Cargo.toml | 6 ++ .../no-listing-16-underscore-catchall/src/main.rs | 14 +++ .../no-listing-17-underscore-unit/Cargo.lock | 6 ++ .../no-listing-17-underscore-unit/Cargo.toml | 6 ++ .../no-listing-17-underscore-unit/src/main.rs | 13 +++ .../listing-07-01/Cargo.lock | 6 ++ .../listing-07-01/Cargo.toml | 6 ++ .../listing-07-01/src/lib.rs | 15 +++ .../listing-07-03/Cargo.lock | 6 ++ .../listing-07-03/Cargo.toml | 6 ++ .../listing-07-03/output.txt | 28 +++++ .../listing-07-03/src/lib.rs | 13 +++ .../listing-07-05/Cargo.lock | 6 ++ .../listing-07-05/Cargo.toml | 6 ++ .../listing-07-05/output.txt | 28 +++++ .../listing-07-05/src/lib.rs | 13 +++ .../listing-07-07/Cargo.lock | 6 ++ .../listing-07-07/Cargo.toml | 6 ++ .../listing-07-07/src/lib.rs | 13 +++ .../listing-07-08/Cargo.lock | 6 ++ .../listing-07-08/Cargo.toml | 6 ++ .../listing-07-08/src/lib.rs | 10 ++ .../listing-07-09/Cargo.lock | 6 ++ .../listing-07-09/Cargo.toml | 6 ++ .../listing-07-09/src/lib.rs | 27 +++++ .../listing-07-10/Cargo.lock | 6 ++ .../listing-07-10/Cargo.toml | 6 ++ .../listing-07-10/src/lib.rs | 11 ++ .../listing-07-11/Cargo.lock | 6 ++ .../listing-07-11/Cargo.toml | 6 ++ .../listing-07-11/src/lib.rs | 11 ++ .../listing-07-12/Cargo.lock | 6 ++ .../listing-07-12/Cargo.toml | 6 ++ .../listing-07-12/output.txt | 19 ++++ .../listing-07-12/src/lib.rs | 13 +++ .../listing-07-13/Cargo.lock | 6 ++ .../listing-07-13/Cargo.toml | 6 ++ .../listing-07-13/src/lib.rs | 11 ++ .../listing-07-14/Cargo.lock | 6 ++ .../listing-07-14/Cargo.toml | 6 ++ .../listing-07-14/src/main.rs | 6 ++ .../listing-07-15/Cargo.lock | 6 ++ .../listing-07-15/Cargo.toml | 6 ++ .../listing-07-15/src/lib.rs | 18 ++++ .../listing-07-16/Cargo.lock | 6 ++ .../listing-07-16/Cargo.toml | 6 ++ .../listing-07-16/src/lib.rs | 18 ++++ .../listing-07-17/Cargo.lock | 6 ++ .../listing-07-17/Cargo.toml | 6 ++ .../listing-07-17/src/lib.rs | 11 ++ .../listing-07-18/Cargo.lock | 83 +++++++++++++++ .../listing-07-18/Cargo.toml | 7 ++ .../listing-07-18/src/main.rs | 32 ++++++ .../listing-07-19/Cargo.lock | 6 ++ .../listing-07-19/Cargo.toml | 6 ++ .../listing-07-19/src/lib.rs | 2 + .../listing-07-20/Cargo.lock | 6 ++ .../listing-07-20/Cargo.toml | 6 ++ .../listing-07-20/src/lib.rs | 1 + .../listing-07-21-and-22/Cargo.lock | 6 ++ .../listing-07-21-and-22/Cargo.toml | 6 ++ .../listing-07-21-and-22/src/front_of_house.rs | 3 + .../listing-07-21-and-22/src/lib.rs | 7 ++ .../no-listing-01-use-std-unnested/Cargo.lock | 83 +++++++++++++++ .../no-listing-01-use-std-unnested/Cargo.toml | 9 ++ .../no-listing-01-use-std-unnested/src/main.rs | 31 ++++++ .../no-listing-02-extracting-hosting/Cargo.lock | 6 ++ .../no-listing-02-extracting-hosting/Cargo.toml | 6 ++ .../src/front_of_house.rs | 1 + .../src/front_of_house/hosting.rs | 1 + .../no-listing-02-extracting-hosting/src/lib.rs | 7 ++ .../quick-reference-example/Cargo.lock | 7 ++ .../quick-reference-example/Cargo.toml | 6 ++ .../quick-reference-example/output.txt | 5 + .../quick-reference-example/src/garden.rs | 1 + .../src/garden/vegetables.rs | 2 + .../quick-reference-example/src/main.rs | 8 ++ .../listing-08-01/Cargo.lock | 6 ++ .../listing-08-01/Cargo.toml | 6 ++ .../listing-08-01/src/main.rs | 5 + .../listing-08-02/Cargo.lock | 6 ++ .../listing-08-02/Cargo.toml | 6 ++ .../listing-08-02/src/main.rs | 5 + .../listing-08-03/Cargo.lock | 6 ++ .../listing-08-03/Cargo.toml | 6 ++ .../listing-08-03/src/main.rs | 10 ++ .../listing-08-04/Cargo.lock | 6 ++ .../listing-08-04/Cargo.toml | 6 ++ .../listing-08-04/src/main.rs | 14 +++ .../listing-08-05/Cargo.lock | 6 ++ .../listing-08-05/Cargo.toml | 6 ++ .../listing-08-05/src/main.rs | 8 ++ .../listing-08-06/Cargo.lock | 6 ++ .../listing-08-06/Cargo.toml | 6 ++ .../listing-08-06/output.txt | 16 +++ .../listing-08-06/src/main.rs | 11 ++ .../listing-08-07/Cargo.lock | 6 ++ .../listing-08-07/Cargo.toml | 6 ++ .../listing-08-07/src/main.rs | 8 ++ .../listing-08-08/Cargo.lock | 6 ++ .../listing-08-08/Cargo.toml | 6 ++ .../listing-08-08/src/main.rs | 8 ++ .../listing-08-09/Cargo.lock | 6 ++ .../listing-08-09/Cargo.toml | 6 ++ .../listing-08-09/src/main.rs | 15 +++ .../listing-08-10/Cargo.lock | 6 ++ .../listing-08-10/Cargo.toml | 6 ++ .../listing-08-10/src/main.rs | 9 ++ .../listing-08-11/Cargo.lock | 6 ++ .../listing-08-11/Cargo.toml | 6 ++ .../listing-08-11/src/main.rs | 5 + .../listing-08-12/Cargo.lock | 6 ++ .../listing-08-12/Cargo.toml | 6 ++ .../listing-08-12/src/main.rs | 10 ++ .../listing-08-13/Cargo.lock | 6 ++ .../listing-08-13/Cargo.toml | 6 ++ .../listing-08-13/src/main.rs | 5 + .../listing-08-14/Cargo.lock | 6 ++ .../listing-08-14/Cargo.toml | 6 ++ .../listing-08-14/src/main.rs | 19 ++++ .../listing-08-15/Cargo.lock | 6 ++ .../listing-08-15/Cargo.toml | 6 ++ .../listing-08-15/src/main.rs | 6 ++ .../listing-08-16/Cargo.lock | 6 ++ .../listing-08-16/Cargo.toml | 6 ++ .../listing-08-16/src/main.rs | 8 ++ .../listing-08-17/Cargo.lock | 6 ++ .../listing-08-17/Cargo.toml | 6 ++ .../listing-08-17/src/main.rs | 6 ++ .../listing-08-18/Cargo.lock | 6 ++ .../listing-08-18/Cargo.toml | 6 ++ .../listing-08-18/src/main.rs | 7 ++ .../listing-08-19/Cargo.lock | 6 ++ .../listing-08-19/Cargo.toml | 6 ++ .../listing-08-19/output.txt | 20 ++++ .../listing-08-19/src/main.rs | 6 ++ .../listing-08-20/Cargo.lock | 6 ++ .../listing-08-20/Cargo.toml | 6 ++ .../listing-08-20/src/main.rs | 10 ++ .../listing-08-21/Cargo.lock | 6 ++ .../listing-08-21/Cargo.toml | 6 ++ .../listing-08-21/src/main.rs | 13 +++ .../listing-08-22/Cargo.lock | 6 ++ .../listing-08-22/Cargo.toml | 6 ++ .../listing-08-22/src/main.rs | 13 +++ .../listing-08-23/Cargo.lock | 6 ++ .../listing-08-23/Cargo.toml | 6 ++ .../listing-08-23/src/main.rs | 12 +++ .../listing-08-24/Cargo.lock | 6 ++ .../listing-08-24/Cargo.toml | 6 ++ .../listing-08-24/src/main.rs | 13 +++ .../listing-08-25/Cargo.lock | 6 ++ .../listing-08-25/Cargo.toml | 6 ++ .../listing-08-25/src/main.rs | 16 +++ .../Cargo.lock | 6 ++ .../Cargo.toml | 6 ++ .../src/main.rs | 9 ++ .../no-listing-02-format/Cargo.lock | 6 ++ .../no-listing-02-format/Cargo.toml | 6 ++ .../no-listing-02-format/src/main.rs | 9 ++ .../no-listing-03-iterate-over-hashmap/Cargo.lock | 6 ++ .../no-listing-03-iterate-over-hashmap/Cargo.toml | 6 ++ .../no-listing-03-iterate-over-hashmap/src/main.rs | 14 +++ .../output-only-01-not-char-boundary/Cargo.lock | 6 ++ .../output-only-01-not-char-boundary/Cargo.toml | 6 ++ .../output-only-01-not-char-boundary/output.txt | 6 ++ .../output-only-01-not-char-boundary/src/main.rs | 5 + .../ch09-error-handling/listing-09-01/Cargo.lock | 6 ++ .../ch09-error-handling/listing-09-01/Cargo.toml | 6 ++ .../ch09-error-handling/listing-09-01/output.txt | 6 ++ .../ch09-error-handling/listing-09-01/src/main.rs | 5 + .../ch09-error-handling/listing-09-03/Cargo.lock | 6 ++ .../ch09-error-handling/listing-09-03/Cargo.toml | 6 ++ .../ch09-error-handling/listing-09-03/src/main.rs | 5 + .../ch09-error-handling/listing-09-04/Cargo.lock | 6 ++ .../ch09-error-handling/listing-09-04/Cargo.toml | 6 ++ .../ch09-error-handling/listing-09-04/output.txt | 6 ++ .../ch09-error-handling/listing-09-04/src/main.rs | 10 ++ .../ch09-error-handling/listing-09-05/Cargo.lock | 6 ++ .../ch09-error-handling/listing-09-05/Cargo.toml | 6 ++ .../ch09-error-handling/listing-09-05/src/main.rs | 19 ++++ .../ch09-error-handling/listing-09-06/Cargo.lock | 6 ++ .../ch09-error-handling/listing-09-06/Cargo.toml | 6 ++ .../ch09-error-handling/listing-09-06/src/main.rs | 24 +++++ .../ch09-error-handling/listing-09-07/Cargo.lock | 6 ++ .../ch09-error-handling/listing-09-07/Cargo.toml | 6 ++ .../ch09-error-handling/listing-09-07/src/main.rs | 16 +++ .../ch09-error-handling/listing-09-08/Cargo.lock | 6 ++ .../ch09-error-handling/listing-09-08/Cargo.toml | 6 ++ .../ch09-error-handling/listing-09-08/src/main.rs | 17 ++++ .../ch09-error-handling/listing-09-09/Cargo.lock | 6 ++ .../ch09-error-handling/listing-09-09/Cargo.toml | 6 ++ .../ch09-error-handling/listing-09-09/src/main.rs | 12 +++ .../ch09-error-handling/listing-09-10/Cargo.lock | 6 ++ .../ch09-error-handling/listing-09-10/Cargo.toml | 6 ++ .../ch09-error-handling/listing-09-10/output.txt | 15 +++ .../ch09-error-handling/listing-09-10/src/main.rs | 5 + .../ch09-error-handling/listing-09-11/Cargo.lock | 7 ++ .../ch09-error-handling/listing-09-11/Cargo.toml | 6 ++ .../ch09-error-handling/listing-09-11/src/main.rs | 15 +++ .../ch09-error-handling/listing-09-12/Cargo.lock | 6 ++ .../ch09-error-handling/listing-09-12/Cargo.toml | 6 ++ .../ch09-error-handling/listing-09-12/src/main.rs | 8 ++ .../ch09-error-handling/listing-09-13/Cargo.lock | 83 +++++++++++++++ .../ch09-error-handling/listing-09-13/Cargo.toml | 7 ++ .../ch09-error-handling/listing-09-13/src/main.rs | 55 ++++++++++ .../no-listing-01-panic/Cargo.lock | 6 ++ .../no-listing-01-panic/Cargo.toml | 6 ++ .../no-listing-01-panic/output.txt | 6 ++ .../no-listing-01-panic/src/main.rs | 3 + .../no-listing-04-unwrap/Cargo.lock | 6 ++ .../no-listing-04-unwrap/Cargo.toml | 6 ++ .../no-listing-04-unwrap/src/main.rs | 5 + .../no-listing-05-expect/Cargo.lock | 6 ++ .../no-listing-05-expect/Cargo.toml | 6 ++ .../no-listing-05-expect/src/main.rs | 6 ++ .../no-listing-08-unwrap-that-cant-fail/Cargo.lock | 6 ++ .../no-listing-08-unwrap-that-cant-fail/Cargo.toml | 6 ++ .../src/main.rs | 9 ++ .../no-listing-09-guess-out-of-range/Cargo.lock | 83 +++++++++++++++ .../no-listing-09-guess-out-of-range/Cargo.toml | 7 ++ .../no-listing-09-guess-out-of-range/src/main.rs | 47 +++++++++ .../listing-10-01/Cargo.lock | 6 ++ .../listing-10-01/Cargo.toml | 6 ++ .../listing-10-01/src/main.rs | 18 ++++ .../listing-10-02/Cargo.lock | 6 ++ .../listing-10-02/Cargo.toml | 6 ++ .../listing-10-02/src/main.rs | 25 +++++ .../listing-10-03/Cargo.lock | 6 ++ .../listing-10-03/Cargo.toml | 6 ++ .../listing-10-03/src/main.rs | 31 ++++++ .../listing-10-04/Cargo.lock | 6 ++ .../listing-10-04/Cargo.toml | 6 ++ .../listing-10-04/src/main.rs | 43 ++++++++ .../listing-10-05/Cargo.lock | 6 ++ .../listing-10-05/Cargo.toml | 6 ++ .../listing-10-05/output.txt | 17 ++++ .../listing-10-05/src/main.rs | 23 +++++ .../listing-10-06/Cargo.lock | 6 ++ .../listing-10-06/Cargo.toml | 6 ++ .../listing-10-06/src/main.rs | 9 ++ .../listing-10-07/Cargo.lock | 6 ++ .../listing-10-07/Cargo.toml | 6 ++ .../listing-10-07/output.txt | 10 ++ .../listing-10-07/src/main.rs | 8 ++ .../listing-10-08/Cargo.lock | 6 ++ .../listing-10-08/Cargo.toml | 6 ++ .../listing-10-08/src/main.rs | 10 ++ .../listing-10-09/Cargo.lock | 6 ++ .../listing-10-09/Cargo.toml | 6 ++ .../listing-10-09/src/main.rs | 16 +++ .../listing-10-10/Cargo.lock | 6 ++ .../listing-10-10/Cargo.toml | 6 ++ .../listing-10-10/src/main.rs | 24 +++++ .../listing-10-11/Cargo.lock | 6 ++ .../listing-10-11/Cargo.toml | 6 ++ .../listing-10-11/src/main.rs | 22 ++++ .../listing-10-12/Cargo.lock | 7 ++ .../listing-10-12/Cargo.toml | 6 ++ .../listing-10-12/src/lib.rs | 3 + .../listing-10-13/Cargo.lock | 7 ++ .../listing-10-13/Cargo.toml | 6 ++ .../listing-10-13/src/lib.rs | 31 ++++++ .../listing-10-14/Cargo.lock | 7 ++ .../listing-10-14/Cargo.toml | 6 ++ .../listing-10-14/src/lib.rs | 29 ++++++ .../listing-10-15/Cargo.lock | 6 ++ .../listing-10-15/Cargo.toml | 6 ++ .../listing-10-15/src/lib.rs | 22 ++++ .../listing-10-16/Cargo.lock | 6 ++ .../listing-10-16/Cargo.toml | 6 ++ .../listing-10-16/output.txt | 15 +++ .../listing-10-16/src/main.rs | 10 ++ .../listing-10-17/Cargo.lock | 4 + .../listing-10-17/Cargo.toml | 6 ++ .../listing-10-17/rustfmt-ignore | 3 + .../listing-10-17/src/main.rs | 10 ++ .../listing-10-18/Cargo.lock | 4 + .../listing-10-18/Cargo.toml | 6 ++ .../listing-10-18/rustfmt-ignore | 3 + .../listing-10-18/src/main.rs | 8 ++ .../listing-10-19/Cargo.lock | 6 ++ .../listing-10-19/Cargo.toml | 6 ++ .../listing-10-19/src/main.rs | 7 ++ .../listing-10-20/Cargo.lock | 6 ++ .../listing-10-20/Cargo.toml | 6 ++ .../listing-10-20/output.txt | 16 +++ .../listing-10-20/src/main.rs | 17 ++++ .../listing-10-21/Cargo.lock | 6 ++ .../listing-10-21/Cargo.toml | 6 ++ .../listing-10-21/src/main.rs | 17 ++++ .../listing-10-22/Cargo.lock | 6 ++ .../listing-10-22/Cargo.toml | 6 ++ .../listing-10-22/src/main.rs | 19 ++++ .../listing-10-23/Cargo.lock | 6 ++ .../listing-10-23/Cargo.toml | 6 ++ .../listing-10-23/output.txt | 14 +++ .../listing-10-23/src/main.rs | 19 ++++ .../listing-10-24/Cargo.lock | 6 ++ .../listing-10-24/Cargo.toml | 6 ++ .../listing-10-24/src/main.rs | 11 ++ .../listing-10-25/Cargo.lock | 6 ++ .../listing-10-25/Cargo.toml | 6 ++ .../listing-10-25/src/main.rs | 29 ++++++ .../no-listing-01-calling-trait-method/Cargo.lock | 7 ++ .../no-listing-01-calling-trait-method/Cargo.toml | 6 ++ .../no-listing-01-calling-trait-method/src/lib.rs | 29 ++++++ .../no-listing-01-calling-trait-method/src/main.rs | 14 +++ .../no-listing-02-calling-default-impl/Cargo.lock | 7 ++ .../no-listing-02-calling-default-impl/Cargo.toml | 6 ++ .../no-listing-02-calling-default-impl/src/lib.rs | 27 +++++ .../no-listing-02-calling-default-impl/src/main.rs | 17 ++++ .../Cargo.lock | 7 ++ .../Cargo.toml | 6 ++ .../src/lib.rs | 24 +++++ .../src/main.rs | 16 +++ .../no-listing-04-traits-as-parameters/Cargo.lock | 7 ++ .../no-listing-04-traits-as-parameters/Cargo.toml | 6 ++ .../no-listing-04-traits-as-parameters/src/lib.rs | 35 +++++++ .../no-listing-05-returning-impl-trait/Cargo.lock | 7 ++ .../no-listing-05-returning-impl-trait/Cargo.toml | 6 ++ .../no-listing-05-returning-impl-trait/src/lib.rs | 42 ++++++++ .../Cargo.lock | 7 ++ .../Cargo.toml | 6 ++ .../src/lib.rs | 56 ++++++++++ .../no-listing-07-where-clause/Cargo.toml | 8 ++ .../no-listing-07-where-clause/src/lib.rs | 9 ++ .../Cargo.lock | 6 ++ .../Cargo.toml | 6 ++ .../src/main.rs | 13 +++ .../no-listing-09-unrelated-lifetime/Cargo.lock | 6 ++ .../no-listing-09-unrelated-lifetime/Cargo.toml | 6 ++ .../no-listing-09-unrelated-lifetime/output.txt | 10 ++ .../no-listing-09-unrelated-lifetime/src/main.rs | 14 +++ .../no-listing-10-lifetimes-on-methods/Cargo.lock | 6 ++ .../no-listing-10-lifetimes-on-methods/Cargo.toml | 6 ++ .../no-listing-10-lifetimes-on-methods/src/main.rs | 28 +++++ .../Cargo.lock | 6 ++ .../Cargo.toml | 6 ++ .../src/main.rs | 31 ++++++ .../listing-11-01/Cargo.lock | 6 ++ .../listing-11-01/Cargo.toml | 6 ++ .../listing-11-01/output.txt | 16 +++ .../listing-11-01/src/lib.rs | 8 ++ .../listing-11-03/Cargo.lock | 6 ++ .../listing-11-03/Cargo.toml | 6 ++ .../listing-11-03/output.txt | 22 ++++ .../listing-11-03/src/lib.rs | 14 +++ .../listing-11-05/Cargo.lock | 6 ++ .../listing-11-05/Cargo.toml | 6 ++ .../listing-11-05/src/lib.rs | 13 +++ .../listing-11-06/Cargo.lock | 6 ++ .../listing-11-06/Cargo.toml | 6 ++ .../listing-11-06/output.txt | 16 +++ .../listing-11-06/src/lib.rs | 32 ++++++ .../listing-11-07/Cargo.lock | 6 ++ .../listing-11-07/Cargo.toml | 6 ++ .../listing-11-07/output.txt | 16 +++ .../listing-11-07/src/lib.rs | 13 +++ .../listing-11-08/Cargo.lock | 6 ++ .../listing-11-08/Cargo.toml | 6 ++ .../listing-11-08/output.txt | 16 +++ .../listing-11-08/src/lib.rs | 24 +++++ .../listing-11-09/Cargo.lock | 6 ++ .../listing-11-09/Cargo.toml | 6 ++ .../listing-11-09/src/lib.rs | 36 +++++++ .../listing-11-10/Cargo.lock | 6 ++ .../listing-11-10/Cargo.toml | 6 ++ .../listing-11-10/output.txt | 25 +++++ .../listing-11-10/src/lib.rs | 21 ++++ .../listing-11-11/Cargo.lock | 6 ++ .../listing-11-11/Cargo.toml | 6 ++ .../listing-11-11/output.txt | 18 ++++ .../listing-11-11/src/lib.rs | 23 +++++ .../listing-11-12/Cargo.lock | 6 ++ .../listing-11-12/Cargo.toml | 6 ++ .../listing-11-12/src/lib.rs | 17 ++++ .../listing-11-13/Cargo.lock | 6 ++ .../listing-11-13/Cargo.toml | 6 ++ .../listing-11-13/output.txt | 23 +++++ .../listing-11-13/src/lib.rs | 17 ++++ .../listing-11-13/tests/integration_test.rs | 6 ++ .../no-listing-01-changing-test-name/Cargo.lock | 6 ++ .../no-listing-01-changing-test-name/Cargo.toml | 6 ++ .../no-listing-01-changing-test-name/output.txt | 16 +++ .../no-listing-01-changing-test-name/src/lib.rs | 7 ++ .../Cargo.lock | 6 ++ .../Cargo.toml | 6 ++ .../output.txt | 17 ++++ .../src/lib.rs | 49 +++++++++ .../no-listing-03-introducing-a-bug/Cargo.lock | 6 ++ .../no-listing-03-introducing-a-bug/Cargo.toml | 6 ++ .../no-listing-03-introducing-a-bug/output.txt | 22 ++++ .../no-listing-03-introducing-a-bug/src/lib.rs | 47 +++++++++ .../no-listing-04-bug-in-add-two/Cargo.lock | 6 ++ .../no-listing-04-bug-in-add-two/Cargo.toml | 6 ++ .../no-listing-04-bug-in-add-two/output.txt | 23 +++++ .../no-listing-04-bug-in-add-two/src/lib.rs | 15 +++ .../no-listing-05-greeter/Cargo.lock | 6 ++ .../no-listing-05-greeter/Cargo.toml | 6 ++ .../no-listing-05-greeter/src/lib.rs | 14 +++ .../no-listing-06-greeter-with-bug/Cargo.lock | 6 ++ .../no-listing-06-greeter-with-bug/Cargo.toml | 6 ++ .../no-listing-06-greeter-with-bug/output.txt | 21 ++++ .../no-listing-06-greeter-with-bug/src/lib.rs | 16 +++ .../Cargo.lock | 6 ++ .../Cargo.toml | 6 ++ .../output.txt | 21 ++++ .../src/lib.rs | 20 ++++ .../no-listing-08-guess-with-bug/Cargo.lock | 6 ++ .../no-listing-08-guess-with-bug/Cargo.toml | 6 ++ .../no-listing-08-guess-with-bug/output.txt | 19 ++++ .../no-listing-08-guess-with-bug/src/lib.rs | 27 +++++ .../Cargo.lock | 6 ++ .../Cargo.toml | 6 ++ .../output.txt | 23 +++++ .../src/lib.rs | 34 +++++++ .../no-listing-10-result-in-tests/Cargo.lock | 6 ++ .../no-listing-10-result-in-tests/Cargo.toml | 6 ++ .../no-listing-10-result-in-tests/src/lib.rs | 11 ++ .../no-listing-11-ignore-a-test/Cargo.lock | 6 ++ .../no-listing-11-ignore-a-test/Cargo.toml | 6 ++ .../no-listing-11-ignore-a-test/output.txt | 17 ++++ .../no-listing-11-ignore-a-test/src/lib.rs | 10 ++ .../Cargo.lock | 6 ++ .../Cargo.toml | 6 ++ .../output.txt | 29 ++++++ .../src/lib.rs | 17 ++++ .../tests/common.rs | 3 + .../tests/integration_test.rs | 6 ++ .../Cargo.lock | 6 ++ .../Cargo.toml | 6 ++ .../src/lib.rs | 17 ++++ .../tests/common/mod.rs | 3 + .../tests/integration_test.rs | 9 ++ .../output-only-01-show-output/Cargo.lock | 6 ++ .../output-only-01-show-output/Cargo.toml | 6 ++ .../output-only-01-show-output/output.txt | 34 +++++++ .../output-only-01-show-output/src/lib.rs | 21 ++++ .../output-only-02-single-test/Cargo.lock | 6 ++ .../output-only-02-single-test/Cargo.toml | 6 ++ .../output-only-02-single-test/output.txt | 10 ++ .../output-only-02-single-test/src/lib.rs | 23 +++++ .../output-only-03-multiple-tests/Cargo.lock | 6 ++ .../output-only-03-multiple-tests/Cargo.toml | 6 ++ .../output-only-03-multiple-tests/output.txt | 11 ++ .../output-only-03-multiple-tests/src/lib.rs | 23 +++++ .../output-only-04-running-ignored/Cargo.lock | 6 ++ .../output-only-04-running-ignored/Cargo.toml | 6 ++ .../output-only-04-running-ignored/output.txt | 16 +++ .../output-only-04-running-ignored/src/lib.rs | 12 +++ .../output-only-05-single-integration/Cargo.lock | 6 ++ .../output-only-05-single-integration/Cargo.toml | 6 ++ .../output-only-05-single-integration/output.txt | 10 ++ .../output-only-05-single-integration/src/lib.rs | 17 ++++ .../tests/integration_test.rs | 6 ++ .../ch12-an-io-project/listing-12-01/Cargo.lock | 6 ++ .../ch12-an-io-project/listing-12-01/Cargo.toml | 6 ++ .../ch12-an-io-project/listing-12-01/output.txt | 7 ++ .../ch12-an-io-project/listing-12-01/src/main.rs | 6 ++ .../ch12-an-io-project/listing-12-02/Cargo.lock | 6 ++ .../ch12-an-io-project/listing-12-02/Cargo.toml | 6 ++ .../ch12-an-io-project/listing-12-02/output.txt | 6 ++ .../ch12-an-io-project/listing-12-02/src/main.rs | 11 ++ .../ch12-an-io-project/listing-12-03/Cargo.lock | 6 ++ .../ch12-an-io-project/listing-12-03/Cargo.toml | 6 ++ .../ch12-an-io-project/listing-12-03/poem.txt | 9 ++ .../ch12-an-io-project/listing-12-03/src/main.rs | 11 ++ .../ch12-an-io-project/listing-12-04/Cargo.lock | 6 ++ .../ch12-an-io-project/listing-12-04/Cargo.toml | 6 ++ .../ch12-an-io-project/listing-12-04/output.txt | 17 ++++ .../ch12-an-io-project/listing-12-04/poem.txt | 9 ++ .../ch12-an-io-project/listing-12-04/src/main.rs | 22 ++++ .../ch12-an-io-project/listing-12-05/Cargo.lock | 6 ++ .../ch12-an-io-project/listing-12-05/Cargo.toml | 6 ++ .../ch12-an-io-project/listing-12-05/poem.txt | 9 ++ .../ch12-an-io-project/listing-12-05/src/main.rs | 29 ++++++ .../ch12-an-io-project/listing-12-06/Cargo.lock | 6 ++ .../ch12-an-io-project/listing-12-06/Cargo.toml | 6 ++ .../ch12-an-io-project/listing-12-06/poem.txt | 9 ++ .../ch12-an-io-project/listing-12-06/src/main.rs | 34 +++++++ .../ch12-an-io-project/listing-12-07/Cargo.lock | 6 ++ .../ch12-an-io-project/listing-12-07/Cargo.toml | 6 ++ .../ch12-an-io-project/listing-12-07/output.txt | 6 ++ .../ch12-an-io-project/listing-12-07/poem.txt | 9 ++ .../ch12-an-io-project/listing-12-07/src/main.rs | 40 ++++++++ .../ch12-an-io-project/listing-12-08/Cargo.lock | 6 ++ .../ch12-an-io-project/listing-12-08/Cargo.toml | 6 ++ .../ch12-an-io-project/listing-12-08/output.txt | 6 ++ .../ch12-an-io-project/listing-12-08/poem.txt | 9 ++ .../ch12-an-io-project/listing-12-08/src/main.rs | 38 +++++++ .../ch12-an-io-project/listing-12-09/Cargo.lock | 6 ++ .../ch12-an-io-project/listing-12-09/Cargo.toml | 6 ++ .../ch12-an-io-project/listing-12-09/poem.txt | 9 ++ .../ch12-an-io-project/listing-12-09/src/main.rs | 36 +++++++ .../ch12-an-io-project/listing-12-10/Cargo.lock | 6 ++ .../ch12-an-io-project/listing-12-10/Cargo.toml | 6 ++ .../ch12-an-io-project/listing-12-10/output.txt | 5 + .../ch12-an-io-project/listing-12-10/poem.txt | 9 ++ .../ch12-an-io-project/listing-12-10/src/main.rs | 42 ++++++++ .../ch12-an-io-project/listing-12-11/Cargo.lock | 6 ++ .../ch12-an-io-project/listing-12-11/Cargo.toml | 6 ++ .../ch12-an-io-project/listing-12-11/poem.txt | 9 ++ .../ch12-an-io-project/listing-12-11/src/main.rs | 50 +++++++++ .../ch12-an-io-project/listing-12-12/Cargo.lock | 6 ++ .../ch12-an-io-project/listing-12-12/Cargo.toml | 6 ++ .../ch12-an-io-project/listing-12-12/output.txt | 27 +++++ .../ch12-an-io-project/listing-12-12/poem.txt | 9 ++ .../ch12-an-io-project/listing-12-12/src/main.rs | 51 ++++++++++ .../ch12-an-io-project/listing-12-13/Cargo.lock | 6 ++ .../ch12-an-io-project/listing-12-13/Cargo.toml | 6 ++ .../ch12-an-io-project/listing-12-13/poem.txt | 9 ++ .../ch12-an-io-project/listing-12-13/src/lib.rs | 36 +++++++ .../ch12-an-io-project/listing-12-13/src/main.rs | 19 ++++ .../ch12-an-io-project/listing-12-14/Cargo.lock | 6 ++ .../ch12-an-io-project/listing-12-14/Cargo.toml | 6 ++ .../ch12-an-io-project/listing-12-14/poem.txt | 9 ++ .../ch12-an-io-project/listing-12-14/src/lib.rs | 28 +++++ .../ch12-an-io-project/listing-12-14/src/main.rs | 29 ++++++ .../ch12-an-io-project/listing-12-15/Cargo.lock | 6 ++ .../ch12-an-io-project/listing-12-15/Cargo.toml | 6 ++ .../ch12-an-io-project/listing-12-15/poem.txt | 9 ++ .../ch12-an-io-project/listing-12-15/src/lib.rs | 44 ++++++++ .../ch12-an-io-project/listing-12-15/src/main.rs | 18 ++++ .../ch12-an-io-project/listing-12-16/Cargo.lock | 6 ++ .../ch12-an-io-project/listing-12-16/Cargo.toml | 6 ++ .../ch12-an-io-project/listing-12-16/output.txt | 23 +++++ .../ch12-an-io-project/listing-12-16/poem.txt | 9 ++ .../ch12-an-io-project/listing-12-16/src/lib.rs | 48 +++++++++ .../ch12-an-io-project/listing-12-16/src/main.rs | 18 ++++ .../ch12-an-io-project/listing-12-17/Cargo.lock | 6 ++ .../ch12-an-io-project/listing-12-17/Cargo.toml | 6 ++ .../ch12-an-io-project/listing-12-17/poem.txt | 9 ++ .../ch12-an-io-project/listing-12-17/src/lib.rs | 50 +++++++++ .../ch12-an-io-project/listing-12-17/src/main.rs | 18 ++++ .../ch12-an-io-project/listing-12-18/Cargo.lock | 6 ++ .../ch12-an-io-project/listing-12-18/Cargo.toml | 6 ++ .../ch12-an-io-project/listing-12-18/poem.txt | 9 ++ .../ch12-an-io-project/listing-12-18/src/lib.rs | 52 ++++++++++ .../ch12-an-io-project/listing-12-18/src/main.rs | 18 ++++ .../ch12-an-io-project/listing-12-19/Cargo.lock | 6 ++ .../ch12-an-io-project/listing-12-19/Cargo.toml | 6 ++ .../ch12-an-io-project/listing-12-19/output.txt | 22 ++++ .../ch12-an-io-project/listing-12-19/poem.txt | 9 ++ .../ch12-an-io-project/listing-12-19/src/lib.rs | 58 +++++++++++ .../ch12-an-io-project/listing-12-19/src/main.rs | 18 ++++ .../ch12-an-io-project/listing-12-20/Cargo.lock | 6 ++ .../ch12-an-io-project/listing-12-20/Cargo.toml | 6 ++ .../ch12-an-io-project/listing-12-20/poem.txt | 9 ++ .../ch12-an-io-project/listing-12-20/src/lib.rs | 76 ++++++++++++++ .../ch12-an-io-project/listing-12-20/src/main.rs | 18 ++++ .../ch12-an-io-project/listing-12-21/Cargo.lock | 6 ++ .../ch12-an-io-project/listing-12-21/Cargo.toml | 6 ++ .../ch12-an-io-project/listing-12-21/output.txt | 23 +++++ .../ch12-an-io-project/listing-12-21/poem.txt | 9 ++ .../ch12-an-io-project/listing-12-21/src/lib.rs | 92 +++++++++++++++++ .../ch12-an-io-project/listing-12-21/src/main.rs | 18 ++++ .../ch12-an-io-project/listing-12-22/Cargo.lock | 6 ++ .../ch12-an-io-project/listing-12-22/Cargo.toml | 6 ++ .../ch12-an-io-project/listing-12-22/poem.txt | 9 ++ .../ch12-an-io-project/listing-12-22/src/lib.rs | 101 ++++++++++++++++++ .../ch12-an-io-project/listing-12-22/src/main.rs | 18 ++++ .../ch12-an-io-project/listing-12-23/Cargo.lock | 6 ++ .../ch12-an-io-project/listing-12-23/Cargo.toml | 6 ++ .../ch12-an-io-project/listing-12-23/output.txt | 6 ++ .../ch12-an-io-project/listing-12-23/poem.txt | 9 ++ .../ch12-an-io-project/listing-12-23/src/lib.rs | 110 ++++++++++++++++++++ .../ch12-an-io-project/listing-12-23/src/main.rs | 18 ++++ .../ch12-an-io-project/listing-12-24/Cargo.lock | 6 ++ .../ch12-an-io-project/listing-12-24/Cargo.toml | 6 ++ .../ch12-an-io-project/listing-12-24/poem.txt | 9 ++ .../ch12-an-io-project/listing-12-24/src/lib.rs | 104 +++++++++++++++++++ .../ch12-an-io-project/listing-12-24/src/main.rs | 20 ++++ .../Cargo.lock | 6 ++ .../Cargo.toml | 6 ++ .../no-listing-01-handling-errors-in-main/poem.txt | 9 ++ .../src/main.rs | 53 ++++++++++ .../no-listing-02-using-search-in-run/Cargo.lock | 6 ++ .../no-listing-02-using-search-in-run/Cargo.toml | 6 ++ .../no-listing-02-using-search-in-run/output.txt | 5 + .../no-listing-02-using-search-in-run/poem.txt | 9 ++ .../no-listing-02-using-search-in-run/src/lib.rs | 60 +++++++++++ .../no-listing-02-using-search-in-run/src/main.rs | 18 ++++ .../output-only-01-with-args/Cargo.lock | 6 ++ .../output-only-01-with-args/Cargo.toml | 6 ++ .../output-only-01-with-args/output.txt | 9 ++ .../output-only-01-with-args/src/main.rs | 6 ++ .../output-only-02-missing-lifetimes/Cargo.lock | 6 ++ .../output-only-02-missing-lifetimes/Cargo.toml | 6 ++ .../output-only-02-missing-lifetimes/output.txt | 16 +++ .../output-only-02-missing-lifetimes/poem.txt | 9 ++ .../output-only-02-missing-lifetimes/src/lib.rs | 48 +++++++++ .../output-only-02-missing-lifetimes/src/main.rs | 18 ++++ .../output-only-03-multiple-matches/Cargo.lock | 6 ++ .../output-only-03-multiple-matches/Cargo.toml | 6 ++ .../output-only-03-multiple-matches/output.txt | 7 ++ .../output-only-03-multiple-matches/poem.txt | 9 ++ .../output-only-03-multiple-matches/src/lib.rs | 60 +++++++++++ .../output-only-03-multiple-matches/src/main.rs | 18 ++++ .../output-only-04-no-matches/Cargo.lock | 6 ++ .../output-only-04-no-matches/Cargo.toml | 6 ++ .../output-only-04-no-matches/output.txt | 4 + .../output-only-04-no-matches/poem.txt | 9 ++ .../output-only-04-no-matches/src/lib.rs | 60 +++++++++++ .../output-only-04-no-matches/src/main.rs | 18 ++++ .../listing-12-23-reproduced/Cargo.lock | 6 ++ .../listing-12-23-reproduced/Cargo.toml | 6 ++ .../listing-12-23-reproduced/poem.txt | 9 ++ .../listing-12-23-reproduced/src/lib.rs | 106 +++++++++++++++++++ .../listing-12-23-reproduced/src/main.rs | 18 ++++ .../listing-12-24-reproduced/Cargo.lock | 6 ++ .../listing-12-24-reproduced/Cargo.toml | 6 ++ .../listing-12-24-reproduced/poem.txt | 9 ++ .../listing-12-24-reproduced/src/lib.rs | 104 +++++++++++++++++++ .../listing-12-24-reproduced/src/main.rs | 24 +++++ .../listing-13-01/Cargo.lock | 7 ++ .../listing-13-01/Cargo.toml | 6 ++ .../listing-13-01/output.txt | 6 ++ .../listing-13-01/src/main.rs | 52 ++++++++++ .../listing-13-02/Cargo.lock | 6 ++ .../listing-13-02/Cargo.toml | 6 ++ .../listing-13-02/src/main.rs | 33 ++++++ .../listing-13-03/Cargo.lock | 6 ++ .../listing-13-03/Cargo.toml | 6 ++ .../listing-13-03/output.txt | 12 +++ .../listing-13-03/src/main.rs | 8 ++ .../listing-13-04/Cargo.lock | 6 ++ .../listing-13-04/Cargo.toml | 6 ++ .../listing-13-04/output.txt | 8 ++ .../listing-13-04/src/main.rs | 10 ++ .../listing-13-05/Cargo.lock | 6 ++ .../listing-13-05/Cargo.toml | 6 ++ .../listing-13-05/output.txt | 6 ++ .../listing-13-05/src/main.rs | 9 ++ .../listing-13-06/Cargo.toml | 8 ++ .../listing-13-06/src/main.rs | 10 ++ .../listing-13-07/.rustfmt.toml | 2 + .../listing-13-07/Cargo.toml | 6 ++ .../listing-13-07/output.txt | 18 ++++ .../listing-13-07/src/main.rs | 16 +++ .../listing-13-08/.rustfmt.toml | 2 + .../listing-13-08/Cargo.toml | 8 ++ .../listing-13-08/output.txt | 18 ++++ .../listing-13-08/src/main.rs | 22 ++++ .../listing-13-09/.rustfmt.toml | 2 + .../listing-13-09/Cargo.lock | 6 ++ .../listing-13-09/Cargo.toml | 6 ++ .../listing-13-09/src/main.rs | 20 ++++ .../listing-13-10/Cargo.lock | 6 ++ .../listing-13-10/Cargo.toml | 6 ++ .../listing-13-10/src/main.rs | 7 ++ .../listing-13-11/Cargo.lock | 6 ++ .../listing-13-11/Cargo.toml | 6 ++ .../listing-13-11/src/main.rs | 11 ++ .../listing-13-12/Cargo.lock | 6 ++ .../listing-13-12/Cargo.toml | 6 ++ .../listing-13-12/src/lib.rs | 16 +++ .../listing-13-13/Cargo.lock | 6 ++ .../listing-13-13/Cargo.toml | 6 ++ .../listing-13-13/src/lib.rs | 15 +++ .../listing-13-14/Cargo.lock | 6 ++ .../listing-13-14/Cargo.toml | 6 ++ .../listing-13-14/output.txt | 14 +++ .../listing-13-14/src/main.rs | 7 ++ .../listing-13-15/Cargo.lock | 6 ++ .../listing-13-15/Cargo.toml | 6 ++ .../listing-13-15/src/main.rs | 9 ++ .../listing-13-16/Cargo.lock | 6 ++ .../listing-13-16/Cargo.toml | 6 ++ .../listing-13-16/src/lib.rs | 48 +++++++++ .../listing-13-18/Cargo.lock | 6 ++ .../listing-13-18/Cargo.toml | 6 ++ .../listing-13-18/poem.txt | 9 ++ .../listing-13-18/src/lib.rs | 104 +++++++++++++++++++ .../listing-13-18/src/main.rs | 22 ++++ .../listing-13-19/Cargo.lock | 6 ++ .../listing-13-19/Cargo.toml | 6 ++ .../listing-13-19/poem.txt | 9 ++ .../listing-13-19/src/lib.rs | 109 ++++++++++++++++++++ .../listing-13-19/src/main.rs | 16 +++ .../listing-13-20/Cargo.lock | 6 ++ .../listing-13-20/Cargo.toml | 6 ++ .../listing-13-20/poem.txt | 9 ++ .../listing-13-20/src/lib.rs | 113 +++++++++++++++++++++ .../listing-13-20/src/main.rs | 16 +++ .../listing-13-22/Cargo.lock | 6 ++ .../listing-13-22/Cargo.toml | 6 ++ .../listing-13-22/poem.txt | 9 ++ .../listing-13-22/src/lib.rs | 108 ++++++++++++++++++++ .../listing-13-22/src/main.rs | 16 +++ .../ch14-more-about-cargo/listing-14-01/Cargo.lock | 6 ++ .../ch14-more-about-cargo/listing-14-01/Cargo.toml | 6 ++ .../ch14-more-about-cargo/listing-14-01/src/lib.rs | 13 +++ .../ch14-more-about-cargo/listing-14-02/Cargo.lock | 6 ++ .../ch14-more-about-cargo/listing-14-02/Cargo.toml | 6 ++ .../ch14-more-about-cargo/listing-14-02/src/lib.rs | 21 ++++ .../ch14-more-about-cargo/listing-14-03/Cargo.lock | 6 ++ .../ch14-more-about-cargo/listing-14-03/Cargo.toml | 6 ++ .../ch14-more-about-cargo/listing-14-03/src/lib.rs | 34 +++++++ .../ch14-more-about-cargo/listing-14-04/Cargo.lock | 6 ++ .../ch14-more-about-cargo/listing-14-04/Cargo.toml | 6 ++ .../ch14-more-about-cargo/listing-14-04/src/lib.rs | 29 ++++++ .../listing-14-04/src/main.rs | 8 ++ .../ch14-more-about-cargo/listing-14-05/Cargo.lock | 6 ++ .../ch14-more-about-cargo/listing-14-05/Cargo.toml | 6 ++ .../ch14-more-about-cargo/listing-14-05/src/lib.rs | 41 ++++++++ .../ch14-more-about-cargo/listing-14-06/Cargo.lock | 6 ++ .../ch14-more-about-cargo/listing-14-06/Cargo.toml | 6 ++ .../ch14-more-about-cargo/listing-14-06/src/lib.rs | 33 ++++++ .../listing-14-06/src/main.rs | 13 +++ .../listing-14-07/add/Cargo.lock | 13 +++ .../listing-14-07/add/Cargo.toml | 6 ++ .../listing-14-07/add/add_one/Cargo.toml | 6 ++ .../listing-14-07/add/add_one/src/lib.rs | 3 + .../listing-14-07/add/adder/Cargo.toml | 8 ++ .../listing-14-07/add/adder/src/main.rs | 6 ++ .../add/Cargo.lock | 6 ++ .../add/Cargo.toml | 5 + .../add/adder/Cargo.toml | 6 ++ .../add/adder/src/main.rs | 3 + .../add/Cargo.lock | 13 +++ .../add/Cargo.toml | 6 ++ .../add/add_one/Cargo.toml | 6 ++ .../add/add_one/src/lib.rs | 3 + .../add/adder/Cargo.toml | 7 ++ .../add/adder/src/main.rs | 3 + .../add/Cargo.lock | 90 ++++++++++++++++ .../add/Cargo.toml | 6 ++ .../add/add_one/Cargo.toml | 7 ++ .../add/add_one/src/lib.rs | 5 + .../add/adder/Cargo.toml | 8 ++ .../add/adder/src/main.rs | 10 ++ .../add/Cargo.lock | 13 +++ .../add/Cargo.toml | 6 ++ .../add/add_one/Cargo.toml | 6 ++ .../add/add_one/src/lib.rs | 13 +++ .../add/adder/Cargo.toml | 8 ++ .../add/adder/src/main.rs | 10 ++ .../output-only-01-adder-crate/add/Cargo.lock | 5 + .../output-only-01-adder-crate/add/Cargo.toml | 5 + .../output-only-01-adder-crate/add/rustfmt-ignore | 2 + .../output-only-02-add-one/add/Cargo.lock | 13 +++ .../output-only-02-add-one/add/Cargo.toml | 6 ++ .../output-only-02-add-one/add/add_one/Cargo.toml | 8 ++ .../output-only-02-add-one/add/add_one/src/lib.rs | 8 ++ .../output-only-02-add-one/add/adder/Cargo.toml | 8 ++ .../output-only-02-add-one/add/adder/src/main.rs | 3 + .../output-only-03-use-rand/add/Cargo.lock | 90 ++++++++++++++++ .../output-only-03-use-rand/add/Cargo.toml | 6 ++ .../output-only-03-use-rand/add/add_one/Cargo.toml | 7 ++ .../output-only-03-use-rand/add/add_one/src/lib.rs | 5 + .../output-only-03-use-rand/add/adder/Cargo.toml | 8 ++ .../output-only-03-use-rand/add/adder/src/main.rs | 11 ++ .../ch15-smart-pointers/listing-15-01/Cargo.lock | 6 ++ .../ch15-smart-pointers/listing-15-01/Cargo.toml | 6 ++ .../ch15-smart-pointers/listing-15-01/src/main.rs | 4 + .../ch15-smart-pointers/listing-15-02/Cargo.lock | 6 ++ .../ch15-smart-pointers/listing-15-02/Cargo.toml | 6 ++ .../ch15-smart-pointers/listing-15-02/src/main.rs | 8 ++ .../ch15-smart-pointers/listing-15-03/Cargo.lock | 6 ++ .../ch15-smart-pointers/listing-15-03/Cargo.toml | 6 ++ .../ch15-smart-pointers/listing-15-03/output.txt | 27 +++++ .../ch15-smart-pointers/listing-15-03/src/main.rs | 12 +++ .../ch15-smart-pointers/listing-15-05/Cargo.lock | 6 ++ .../ch15-smart-pointers/listing-15-05/Cargo.toml | 6 ++ .../ch15-smart-pointers/listing-15-05/src/main.rs | 10 ++ .../ch15-smart-pointers/listing-15-06/Cargo.lock | 6 ++ .../ch15-smart-pointers/listing-15-06/Cargo.toml | 6 ++ .../ch15-smart-pointers/listing-15-06/src/main.rs | 7 ++ .../ch15-smart-pointers/listing-15-07/Cargo.lock | 6 ++ .../ch15-smart-pointers/listing-15-07/Cargo.toml | 6 ++ .../ch15-smart-pointers/listing-15-07/src/main.rs | 7 ++ .../ch15-smart-pointers/listing-15-08/Cargo.lock | 6 ++ .../ch15-smart-pointers/listing-15-08/Cargo.toml | 6 ++ .../ch15-smart-pointers/listing-15-08/src/main.rs | 11 ++ .../ch15-smart-pointers/listing-15-09/Cargo.lock | 6 ++ .../ch15-smart-pointers/listing-15-09/Cargo.toml | 6 ++ .../ch15-smart-pointers/listing-15-09/output.txt | 10 ++ .../ch15-smart-pointers/listing-15-09/src/main.rs | 17 ++++ .../ch15-smart-pointers/listing-15-10/Cargo.lock | 6 ++ .../ch15-smart-pointers/listing-15-10/Cargo.toml | 6 ++ .../ch15-smart-pointers/listing-15-10/src/main.rs | 27 +++++ .../ch15-smart-pointers/listing-15-11/Cargo.lock | 6 ++ .../ch15-smart-pointers/listing-15-11/Cargo.toml | 6 ++ .../ch15-smart-pointers/listing-15-11/src/main.rs | 7 ++ .../ch15-smart-pointers/listing-15-12/Cargo.lock | 6 ++ .../ch15-smart-pointers/listing-15-12/Cargo.toml | 6 ++ .../ch15-smart-pointers/listing-15-12/src/main.rs | 28 +++++ .../ch15-smart-pointers/listing-15-13/Cargo.lock | 6 ++ .../ch15-smart-pointers/listing-15-13/Cargo.toml | 6 ++ .../ch15-smart-pointers/listing-15-13/src/main.rs | 28 +++++ .../ch15-smart-pointers/listing-15-14/Cargo.lock | 6 ++ .../ch15-smart-pointers/listing-15-14/Cargo.toml | 6 ++ .../ch15-smart-pointers/listing-15-14/output.txt | 7 ++ .../ch15-smart-pointers/listing-15-14/src/main.rs | 19 ++++ .../ch15-smart-pointers/listing-15-15/Cargo.lock | 6 ++ .../ch15-smart-pointers/listing-15-15/Cargo.toml | 6 ++ .../ch15-smart-pointers/listing-15-15/output.txt | 13 +++ .../ch15-smart-pointers/listing-15-15/src/main.rs | 20 ++++ .../ch15-smart-pointers/listing-15-16/Cargo.lock | 6 ++ .../ch15-smart-pointers/listing-15-16/Cargo.toml | 6 ++ .../ch15-smart-pointers/listing-15-16/output.txt | 7 ++ .../ch15-smart-pointers/listing-15-16/src/main.rs | 20 ++++ .../ch15-smart-pointers/listing-15-17/Cargo.lock | 6 ++ .../ch15-smart-pointers/listing-15-17/Cargo.toml | 6 ++ .../ch15-smart-pointers/listing-15-17/output.txt | 14 +++ .../ch15-smart-pointers/listing-15-17/src/main.rs | 12 +++ .../ch15-smart-pointers/listing-15-18/Cargo.lock | 6 ++ .../ch15-smart-pointers/listing-15-18/Cargo.toml | 6 ++ .../ch15-smart-pointers/listing-15-18/src/main.rs | 13 +++ .../ch15-smart-pointers/listing-15-19/Cargo.lock | 6 ++ .../ch15-smart-pointers/listing-15-19/Cargo.toml | 6 ++ .../ch15-smart-pointers/listing-15-19/output.txt | 8 ++ .../ch15-smart-pointers/listing-15-19/src/main.rs | 21 ++++ .../ch15-smart-pointers/listing-15-20/Cargo.lock | 6 ++ .../ch15-smart-pointers/listing-15-20/Cargo.toml | 6 ++ .../ch15-smart-pointers/listing-15-20/src/lib.rs | 38 +++++++ .../ch15-smart-pointers/listing-15-21/Cargo.lock | 6 ++ .../ch15-smart-pointers/listing-15-21/Cargo.toml | 6 ++ .../ch15-smart-pointers/listing-15-21/output.txt | 14 +++ .../ch15-smart-pointers/listing-15-21/src/lib.rs | 73 +++++++++++++ .../ch15-smart-pointers/listing-15-22/Cargo.lock | 6 ++ .../ch15-smart-pointers/listing-15-22/Cargo.toml | 6 ++ .../ch15-smart-pointers/listing-15-22/src/lib.rs | 78 ++++++++++++++ .../ch15-smart-pointers/listing-15-23/Cargo.lock | 6 ++ .../ch15-smart-pointers/listing-15-23/Cargo.toml | 6 ++ .../ch15-smart-pointers/listing-15-23/output.txt | 21 ++++ .../ch15-smart-pointers/listing-15-23/src/lib.rs | 78 ++++++++++++++ .../ch15-smart-pointers/listing-15-24/Cargo.lock | 6 ++ .../ch15-smart-pointers/listing-15-24/Cargo.toml | 6 ++ .../ch15-smart-pointers/listing-15-24/output.txt | 7 ++ .../ch15-smart-pointers/listing-15-24/src/main.rs | 24 +++++ .../ch15-smart-pointers/listing-15-25/Cargo.lock | 6 ++ .../ch15-smart-pointers/listing-15-25/Cargo.toml | 6 ++ .../ch15-smart-pointers/listing-15-25/src/main.rs | 20 ++++ .../ch15-smart-pointers/listing-15-26/Cargo.lock | 6 ++ .../ch15-smart-pointers/listing-15-26/Cargo.toml | 6 ++ .../ch15-smart-pointers/listing-15-26/output.txt | 11 ++ .../ch15-smart-pointers/listing-15-26/src/main.rs | 44 ++++++++ .../ch15-smart-pointers/listing-15-27/Cargo.lock | 6 ++ .../ch15-smart-pointers/listing-15-27/Cargo.toml | 6 ++ .../ch15-smart-pointers/listing-15-27/src/main.rs | 24 +++++ .../ch15-smart-pointers/listing-15-28/Cargo.lock | 6 ++ .../ch15-smart-pointers/listing-15-28/Cargo.toml | 6 ++ .../ch15-smart-pointers/listing-15-28/src/main.rs | 33 ++++++ .../ch15-smart-pointers/listing-15-29/Cargo.lock | 6 ++ .../ch15-smart-pointers/listing-15-29/Cargo.toml | 6 ++ .../ch15-smart-pointers/listing-15-29/src/main.rs | 54 ++++++++++ .../Cargo.lock | 6 ++ .../Cargo.toml | 6 ++ .../output.txt | 12 +++ .../src/main.rs | 4 + .../Cargo.lock | 6 ++ .../Cargo.toml | 6 ++ .../output.txt | 23 +++++ .../src/main.rs | 7 ++ .../listing-16-01/Cargo.lock | 6 ++ .../listing-16-01/Cargo.toml | 6 ++ .../listing-16-01/src/main.rs | 16 +++ .../listing-16-02/Cargo.lock | 6 ++ .../listing-16-02/Cargo.toml | 6 ++ .../listing-16-02/src/main.rs | 18 ++++ .../listing-16-03/Cargo.lock | 6 ++ .../listing-16-03/Cargo.toml | 6 ++ .../listing-16-03/output.txt | 25 +++++ .../listing-16-03/src/main.rs | 11 ++ .../listing-16-04/Cargo.lock | 6 ++ .../listing-16-04/Cargo.toml | 6 ++ .../listing-16-04/src/main.rs | 13 +++ .../listing-16-05/Cargo.lock | 6 ++ .../listing-16-05/Cargo.toml | 6 ++ .../listing-16-05/src/main.rs | 11 ++ .../listing-16-06/Cargo.lock | 6 ++ .../listing-16-06/Cargo.toml | 6 ++ .../listing-16-06/src/main.rs | 5 + .../listing-16-07/Cargo.lock | 6 ++ .../listing-16-07/Cargo.toml | 6 ++ .../listing-16-07/src/main.rs | 11 ++ .../listing-16-08/Cargo.lock | 6 ++ .../listing-16-08/Cargo.toml | 6 ++ .../listing-16-08/src/main.rs | 14 +++ .../listing-16-09/Cargo.lock | 6 ++ .../listing-16-09/Cargo.toml | 6 ++ .../listing-16-09/output.txt | 16 +++ .../listing-16-09/src/main.rs | 15 +++ .../listing-16-10/Cargo.lock | 6 ++ .../listing-16-10/Cargo.toml | 6 ++ .../listing-16-10/src/main.rs | 25 +++++ .../listing-16-11/Cargo.lock | 6 ++ .../listing-16-11/Cargo.toml | 6 ++ .../listing-16-11/src/main.rs | 46 +++++++++ .../listing-16-12/Cargo.lock | 6 ++ .../listing-16-12/Cargo.toml | 6 ++ .../listing-16-12/src/main.rs | 12 +++ .../listing-16-13/Cargo.lock | 6 ++ .../listing-16-13/Cargo.toml | 6 ++ .../listing-16-13/output.txt | 15 +++ .../listing-16-13/src/main.rs | 22 ++++ .../listing-16-14/Cargo.lock | 6 ++ .../listing-16-14/Cargo.toml | 6 ++ .../listing-16-14/output.txt | 21 ++++ .../listing-16-14/src/main.rs | 24 +++++ .../listing-16-15/Cargo.lock | 6 ++ .../listing-16-15/Cargo.toml | 6 ++ .../listing-16-15/src/main.rs | 23 +++++ .../no-listing-01-join-too-early/Cargo.lock | 6 ++ .../no-listing-01-join-too-early/Cargo.toml | 6 ++ .../no-listing-01-join-too-early/src/main.rs | 18 ++++ .../Cargo.lock | 6 ++ .../Cargo.toml | 6 ++ .../src/main.rs | 27 +++++ .../output-only-01-move-drop/Cargo.lock | 6 ++ .../output-only-01-move-drop/Cargo.toml | 6 ++ .../output-only-01-move-drop/output.txt | 18 ++++ .../output-only-01-move-drop/src/main.rs | 13 +++ .../listings/ch17-oop/listing-17-01/Cargo.lock | 6 ++ .../listings/ch17-oop/listing-17-01/Cargo.toml | 6 ++ .../listings/ch17-oop/listing-17-01/src/lib.rs | 4 + .../listings/ch17-oop/listing-17-02/Cargo.lock | 6 ++ .../listings/ch17-oop/listing-17-02/Cargo.toml | 6 ++ .../listings/ch17-oop/listing-17-02/src/lib.rs | 33 ++++++ .../listings/ch17-oop/listing-17-03/Cargo.lock | 6 ++ .../listings/ch17-oop/listing-17-03/Cargo.toml | 6 ++ .../listings/ch17-oop/listing-17-03/src/lib.rs | 3 + .../listings/ch17-oop/listing-17-04/Cargo.lock | 6 ++ .../listings/ch17-oop/listing-17-04/Cargo.toml | 6 ++ .../listings/ch17-oop/listing-17-04/src/lib.rs | 9 ++ .../listings/ch17-oop/listing-17-05/Cargo.lock | 6 ++ .../listings/ch17-oop/listing-17-05/Cargo.toml | 6 ++ .../listings/ch17-oop/listing-17-05/src/lib.rs | 17 ++++ .../listings/ch17-oop/listing-17-06/Cargo.lock | 6 ++ .../listings/ch17-oop/listing-17-06/Cargo.toml | 6 ++ .../listings/ch17-oop/listing-17-06/src/lib.rs | 20 ++++ .../listings/ch17-oop/listing-17-07/Cargo.lock | 6 ++ .../listings/ch17-oop/listing-17-07/Cargo.toml | 6 ++ .../listings/ch17-oop/listing-17-07/src/lib.rs | 29 ++++++ .../listings/ch17-oop/listing-17-08/Cargo.lock | 6 ++ .../listings/ch17-oop/listing-17-08/Cargo.toml | 6 ++ .../listings/ch17-oop/listing-17-08/src/lib.rs | 27 +++++ .../listings/ch17-oop/listing-17-08/src/main.rs | 17 ++++ .../listings/ch17-oop/listing-17-09/Cargo.lock | 6 ++ .../listings/ch17-oop/listing-17-09/Cargo.toml | 6 ++ .../listings/ch17-oop/listing-17-09/src/lib.rs | 27 +++++ .../listings/ch17-oop/listing-17-09/src/main.rs | 40 ++++++++ .../listings/ch17-oop/listing-17-10/Cargo.lock | 6 ++ .../listings/ch17-oop/listing-17-10/Cargo.toml | 6 ++ .../listings/ch17-oop/listing-17-10/output.txt | 13 +++ .../listings/ch17-oop/listing-17-10/src/lib.rs | 27 +++++ .../listings/ch17-oop/listing-17-10/src/main.rs | 9 ++ .../listings/ch17-oop/listing-17-11/Cargo.lock | 6 ++ .../listings/ch17-oop/listing-17-11/Cargo.toml | 6 ++ .../listings/ch17-oop/listing-17-11/src/main.rs | 20 ++++ .../listings/ch17-oop/listing-17-12/Cargo.lock | 6 ++ .../listings/ch17-oop/listing-17-12/Cargo.toml | 6 ++ .../listings/ch17-oop/listing-17-12/src/lib.rs | 19 ++++ .../listings/ch17-oop/listing-17-12/src/main.rs | 14 +++ .../listings/ch17-oop/listing-17-13/Cargo.lock | 6 ++ .../listings/ch17-oop/listing-17-13/Cargo.toml | 6 ++ .../listings/ch17-oop/listing-17-13/src/lib.rs | 28 +++++ .../listings/ch17-oop/listing-17-13/src/main.rs | 14 +++ .../listings/ch17-oop/listing-17-14/Cargo.lock | 6 ++ .../listings/ch17-oop/listing-17-14/Cargo.toml | 6 ++ .../listings/ch17-oop/listing-17-14/src/lib.rs | 32 ++++++ .../listings/ch17-oop/listing-17-14/src/main.rs | 14 +++ .../listings/ch17-oop/listing-17-15/Cargo.lock | 6 ++ .../listings/ch17-oop/listing-17-15/Cargo.toml | 6 ++ .../listings/ch17-oop/listing-17-15/src/lib.rs | 52 ++++++++++ .../listings/ch17-oop/listing-17-15/src/main.rs | 14 +++ .../listings/ch17-oop/listing-17-16/Cargo.lock | 6 ++ .../listings/ch17-oop/listing-17-16/Cargo.toml | 6 ++ .../listings/ch17-oop/listing-17-16/src/lib.rs | 85 ++++++++++++++++ .../listings/ch17-oop/listing-17-16/src/main.rs | 14 +++ .../listings/ch17-oop/listing-17-17/Cargo.lock | 6 ++ .../listings/ch17-oop/listing-17-17/Cargo.toml | 6 ++ .../listings/ch17-oop/listing-17-17/src/lib.rs | 82 +++++++++++++++ .../listings/ch17-oop/listing-17-17/src/main.rs | 14 +++ .../listings/ch17-oop/listing-17-18/Cargo.lock | 6 ++ .../listings/ch17-oop/listing-17-18/Cargo.toml | 6 ++ .../listings/ch17-oop/listing-17-18/src/lib.rs | 94 +++++++++++++++++ .../listings/ch17-oop/listing-17-18/src/main.rs | 14 +++ .../listings/ch17-oop/listing-17-19/Cargo.lock | 6 ++ .../listings/ch17-oop/listing-17-19/Cargo.toml | 6 ++ .../listings/ch17-oop/listing-17-19/src/lib.rs | 25 +++++ .../listings/ch17-oop/listing-17-20/Cargo.lock | 6 ++ .../listings/ch17-oop/listing-17-20/Cargo.toml | 6 ++ .../listings/ch17-oop/listing-17-20/src/lib.rs | 48 +++++++++ .../listings/ch17-oop/listing-17-21/Cargo.lock | 6 ++ .../listings/ch17-oop/listing-17-21/Cargo.toml | 6 ++ .../listings/ch17-oop/listing-17-21/src/lib.rs | 43 ++++++++ .../listings/ch17-oop/listing-17-21/src/main.rs | 13 +++ .../listing-18-01/Cargo.lock | 6 ++ .../listing-18-01/Cargo.toml | 6 ++ .../listing-18-01/src/main.rs | 19 ++++ .../listing-18-02/Cargo.lock | 6 ++ .../listing-18-02/Cargo.toml | 6 ++ .../listing-18-02/src/main.rs | 13 +++ .../listing-18-03/Cargo.lock | 6 ++ .../listing-18-03/Cargo.toml | 6 ++ .../listing-18-03/output.txt | 7 ++ .../listing-18-03/src/main.rs | 9 ++ .../listing-18-04/Cargo.lock | 6 ++ .../listing-18-04/Cargo.toml | 6 ++ .../listing-18-04/src/main.rs | 5 + .../listing-18-05/Cargo.lock | 6 ++ .../listing-18-05/Cargo.toml | 6 ++ .../listing-18-05/output.txt | 15 +++ .../listing-18-05/src/main.rs | 5 + .../listing-18-06/Cargo.lock | 6 ++ .../listing-18-06/Cargo.toml | 6 ++ .../listing-18-06/src/main.rs | 7 ++ .../listing-18-07/Cargo.lock | 6 ++ .../listing-18-07/Cargo.toml | 6 ++ .../listing-18-07/src/main.rs | 8 ++ .../listing-18-08/Cargo.lock | 6 ++ .../listing-18-08/Cargo.toml | 6 ++ .../listing-18-08/output.txt | 19 ++++ .../listing-18-08/src/main.rs | 6 ++ .../listing-18-09/Cargo.lock | 6 ++ .../listing-18-09/Cargo.toml | 6 ++ .../listing-18-09/src/main.rs | 8 ++ .../listing-18-10/Cargo.lock | 6 ++ .../listing-18-10/Cargo.toml | 6 ++ .../listing-18-10/output.txt | 16 +++ .../listing-18-10/src/main.rs | 7 ++ .../listing-18-11/Cargo.lock | 6 ++ .../listing-18-11/Cargo.toml | 6 ++ .../listing-18-11/src/main.rs | 14 +++ .../listing-18-12/Cargo.lock | 6 ++ .../listing-18-12/Cargo.toml | 6 ++ .../listing-18-12/src/main.rs | 12 +++ .../listing-18-13/Cargo.lock | 6 ++ .../listing-18-13/Cargo.toml | 6 ++ .../listing-18-13/src/main.rs | 12 +++ .../listing-18-14/Cargo.lock | 6 ++ .../listing-18-14/Cargo.toml | 6 ++ .../listing-18-14/src/main.rs | 16 +++ .../listing-18-15/Cargo.lock | 6 ++ .../listing-18-15/Cargo.toml | 6 ++ .../listing-18-15/src/main.rs | 27 +++++ .../listing-18-16/Cargo.lock | 6 ++ .../listing-18-16/Cargo.toml | 6 ++ .../listing-18-16/src/main.rs | 27 +++++ .../listing-18-17/Cargo.lock | 6 ++ .../listing-18-17/Cargo.toml | 6 ++ .../listing-18-17/src/main.rs | 7 ++ .../listing-18-18/Cargo.lock | 6 ++ .../listing-18-18/Cargo.toml | 6 ++ .../listing-18-18/src/main.rs | 17 ++++ .../listing-18-19/Cargo.lock | 6 ++ .../listing-18-19/Cargo.toml | 6 ++ .../listing-18-19/src/main.rs | 11 ++ .../listing-18-20/Cargo.lock | 6 ++ .../listing-18-20/Cargo.toml | 6 ++ .../listing-18-20/src/main.rs | 4 + .../listing-18-21/Cargo.lock | 6 ++ .../listing-18-21/Cargo.toml | 6 ++ .../listing-18-21/src/main.rs | 11 ++ .../listing-18-22/Cargo.lock | 6 ++ .../listing-18-22/Cargo.toml | 6 ++ .../listing-18-22/src/main.rs | 11 ++ .../listing-18-23/Cargo.lock | 6 ++ .../listing-18-23/Cargo.toml | 6 ++ .../listing-18-23/src/main.rs | 15 +++ .../listing-18-24/Cargo.lock | 6 ++ .../listing-18-24/Cargo.toml | 6 ++ .../listing-18-24/src/main.rs | 9 ++ .../listing-18-25/Cargo.lock | 4 + .../listing-18-25/Cargo.toml | 6 ++ .../listing-18-25/output.txt | 11 ++ .../listing-18-25/rustfmt-ignore | 1 + .../listing-18-25/src/main.rs | 9 ++ .../listing-18-26/Cargo.lock | 6 ++ .../listing-18-26/Cargo.toml | 6 ++ .../listing-18-26/src/main.rs | 11 ++ .../listing-18-27/Cargo.lock | 6 ++ .../listing-18-27/Cargo.toml | 6 ++ .../listing-18-27/src/main.rs | 12 +++ .../listing-18-28/Cargo.lock | 6 ++ .../listing-18-28/Cargo.toml | 6 ++ .../listing-18-28/src/main.rs | 11 ++ .../listing-18-29/Cargo.lock | 6 ++ .../listing-18-29/Cargo.toml | 6 ++ .../listing-18-29/src/main.rs | 19 ++++ .../no-listing-01-literals/Cargo.lock | 6 ++ .../no-listing-01-literals/Cargo.toml | 6 ++ .../no-listing-01-literals/src/main.rs | 12 +++ .../no-listing-02-multiple-patterns/Cargo.lock | 6 ++ .../no-listing-02-multiple-patterns/Cargo.toml | 6 ++ .../no-listing-02-multiple-patterns/src/main.rs | 11 ++ .../no-listing-03-ranges/Cargo.lock | 6 ++ .../no-listing-03-ranges/Cargo.toml | 6 ++ .../no-listing-03-ranges/src/main.rs | 10 ++ .../no-listing-04-ranges-of-char/Cargo.lock | 6 ++ .../no-listing-04-ranges-of-char/Cargo.toml | 6 ++ .../no-listing-04-ranges-of-char/src/main.rs | 11 ++ .../Cargo.lock | 6 ++ .../Cargo.toml | 6 ++ .../src/main.rs | 10 ++ .../listing-19-01/Cargo.lock | 6 ++ .../listing-19-01/Cargo.toml | 6 ++ .../listing-19-01/src/main.rs | 8 ++ .../listing-19-02/Cargo.lock | 6 ++ .../listing-19-02/Cargo.toml | 6 ++ .../listing-19-02/src/main.rs | 6 ++ .../listing-19-03/Cargo.lock | 6 ++ .../listing-19-03/Cargo.toml | 6 ++ .../listing-19-03/src/main.rs | 13 +++ .../listing-19-04/Cargo.lock | 6 ++ .../listing-19-04/Cargo.toml | 6 ++ .../listing-19-04/src/main.rs | 12 +++ .../listing-19-05/Cargo.lock | 6 ++ .../listing-19-05/Cargo.toml | 6 ++ .../listing-19-05/output.txt | 17 ++++ .../listing-19-05/src/main.rs | 14 +++ .../listing-19-06/Cargo.lock | 6 ++ .../listing-19-06/Cargo.toml | 6 ++ .../listing-19-06/src/main.rs | 22 ++++ .../listing-19-07/Cargo.lock | 6 ++ .../listing-19-07/Cargo.toml | 6 ++ .../listing-19-07/src/main.rs | 10 ++ .../listing-19-08/Cargo.lock | 6 ++ .../listing-19-08/Cargo.toml | 6 ++ .../listing-19-08/src/main.rs | 9 ++ .../listing-19-09/Cargo.lock | 6 ++ .../listing-19-09/Cargo.toml | 6 ++ .../listing-19-09/src/main.rs | 5 + .../listing-19-10/Cargo.lock | 6 ++ .../listing-19-10/Cargo.toml | 6 ++ .../listing-19-10/src/main.rs | 15 +++ .../listing-19-11/Cargo.lock | 6 ++ .../listing-19-11/Cargo.toml | 6 ++ .../listing-19-11/src/main.rs | 9 ++ .../listing-19-12/Cargo.lock | 6 ++ .../listing-19-12/Cargo.toml | 6 ++ .../listing-19-12/src/lib.rs | 5 + .../listing-19-13/Cargo.lock | 6 ++ .../listing-19-13/Cargo.toml | 6 ++ .../listing-19-13/src/lib.rs | 3 + .../listing-19-14/Cargo.lock | 6 ++ .../listing-19-14/Cargo.toml | 6 ++ .../listing-19-14/src/main.rs | 25 +++++ .../listing-19-15/Cargo.lock | 6 ++ .../listing-19-15/Cargo.toml | 6 ++ .../listing-19-15/src/lib.rs | 12 +++ .../listing-19-16/Cargo.lock | 6 ++ .../listing-19-16/Cargo.toml | 6 ++ .../listing-19-16/src/main.rs | 31 ++++++ .../listing-19-17/Cargo.lock | 6 ++ .../listing-19-17/Cargo.toml | 6 ++ .../listing-19-17/src/main.rs | 34 +++++++ .../listing-19-18/Cargo.lock | 6 ++ .../listing-19-18/Cargo.toml | 6 ++ .../listing-19-18/output.txt | 7 ++ .../listing-19-18/src/main.rs | 36 +++++++ .../listing-19-19/Cargo.lock | 6 ++ .../listing-19-19/Cargo.toml | 6 ++ .../listing-19-19/output.txt | 5 + .../listing-19-19/src/main.rs | 21 ++++ .../listing-19-20/Cargo.lock | 6 ++ .../listing-19-20/Cargo.toml | 6 ++ .../listing-19-20/output.txt | 12 +++ .../listing-19-20/src/main.rs | 23 +++++ .../listing-19-21/Cargo.lock | 6 ++ .../listing-19-21/Cargo.toml | 6 ++ .../listing-19-21/output.txt | 5 + .../listing-19-21/src/main.rs | 23 +++++ .../listing-19-22/Cargo.lock | 6 ++ .../listing-19-22/Cargo.toml | 6 ++ .../listing-19-22/src/main.rs | 17 ++++ .../listing-19-23/Cargo.lock | 6 ++ .../listing-19-23/Cargo.toml | 6 ++ .../listing-19-23/src/main.rs | 14 +++ .../listing-19-24/Cargo.lock | 6 ++ .../listing-19-24/Cargo.toml | 6 ++ .../listing-19-24/src/main.rs | 16 +++ .../listing-19-25/Cargo.lock | 6 ++ .../listing-19-25/Cargo.toml | 6 ++ .../listing-19-25/src/main.rs | 18 ++++ .../listing-19-27/Cargo.lock | 6 ++ .../listing-19-27/Cargo.toml | 6 ++ .../listing-19-27/src/main.rs | 13 +++ .../listing-19-28/Cargo.lock | 6 ++ .../listing-19-28/Cargo.toml | 6 ++ .../listing-19-28/src/lib.rs | 12 +++ .../listing-19-30/Cargo.lock | 6 ++ .../listing-19-30/Cargo.toml | 6 ++ .../listing-19-30/src/main.rs | 9 ++ .../listing-19-31/hello_macro/Cargo.lock | 6 ++ .../listing-19-31/hello_macro/Cargo.toml | 6 ++ .../hello_macro/hello_macro_derive/Cargo.lock | 46 +++++++++ .../hello_macro/hello_macro_derive/Cargo.toml | 11 ++ .../hello_macro/hello_macro_derive/src/lib.rs | 13 +++ .../listing-19-31/hello_macro/src/lib.rs | 3 + .../listing-19-31/hello_macro/src/main.rs | 13 +++ .../listing-19-33/hello_macro/Cargo.lock | 6 ++ .../listing-19-33/hello_macro/Cargo.toml | 6 ++ .../hello_macro/hello_macro_derive/Cargo.lock | 46 +++++++++ .../hello_macro/hello_macro_derive/Cargo.toml | 11 ++ .../hello_macro/hello_macro_derive/src/lib.rs | 27 +++++ .../listing-19-33/hello_macro/src/lib.rs | 3 + .../listing-19-33/hello_macro/src/main.rs | 13 +++ .../no-listing-01-unsafe-fn/Cargo.lock | 6 ++ .../no-listing-01-unsafe-fn/Cargo.toml | 6 ++ .../no-listing-01-unsafe-fn/src/main.rs | 9 ++ .../Cargo.lock | 6 ++ .../Cargo.toml | 6 ++ .../output.txt | 18 ++++ .../src/main.rs | 27 +++++ .../Cargo.lock | 6 ++ .../Cargo.toml | 6 ++ .../src/main.rs | 33 ++++++ .../no-listing-04-kilometers-alias/Cargo.lock | 6 ++ .../no-listing-04-kilometers-alias/Cargo.toml | 6 ++ .../no-listing-04-kilometers-alias/src/main.rs | 12 +++ .../no-listing-05-write-trait/Cargo.lock | 6 ++ .../no-listing-05-write-trait/Cargo.toml | 6 ++ .../no-listing-05-write-trait/src/lib.rs | 10 ++ .../no-listing-06-result-alias/Cargo.lock | 6 ++ .../no-listing-06-result-alias/Cargo.toml | 6 ++ .../no-listing-06-result-alias/src/lib.rs | 15 +++ .../no-listing-07-never-type/Cargo.lock | 6 ++ .../no-listing-07-never-type/Cargo.toml | 6 ++ .../no-listing-07-never-type/src/lib.rs | 8 ++ .../Cargo.lock | 6 ++ .../Cargo.toml | 6 ++ .../src/main.rs | 9 ++ .../no-listing-09-unwrap-definition/Cargo.lock | 6 ++ .../no-listing-09-unwrap-definition/Cargo.toml | 6 ++ .../no-listing-09-unwrap-definition/src/lib.rs | 17 ++++ .../no-listing-10-loop-returns-never/Cargo.lock | 6 ++ .../no-listing-10-loop-returns-never/Cargo.toml | 6 ++ .../no-listing-10-loop-returns-never/src/main.rs | 9 ++ .../no-listing-11-cant-create-str/Cargo.lock | 6 ++ .../no-listing-11-cant-create-str/Cargo.toml | 6 ++ .../no-listing-11-cant-create-str/src/main.rs | 6 ++ .../no-listing-12-generic-fn-definition/Cargo.lock | 6 ++ .../no-listing-12-generic-fn-definition/Cargo.toml | 6 ++ .../no-listing-12-generic-fn-definition/src/lib.rs | 3 + .../Cargo.lock | 6 ++ .../Cargo.toml | 6 ++ .../src/lib.rs | 3 + .../no-listing-14-generic-maybe-sized/Cargo.lock | 6 ++ .../no-listing-14-generic-maybe-sized/Cargo.toml | 6 ++ .../no-listing-14-generic-maybe-sized/src/lib.rs | 3 + .../no-listing-15-map-closure/Cargo.lock | 6 ++ .../no-listing-15-map-closure/Cargo.toml | 6 ++ .../no-listing-15-map-closure/src/main.rs | 7 ++ .../no-listing-16-map-function/Cargo.lock | 6 ++ .../no-listing-16-map-function/Cargo.toml | 6 ++ .../no-listing-16-map-function/src/main.rs | 7 ++ .../no-listing-17-map-initializer/Cargo.lock | 6 ++ .../no-listing-17-map-initializer/Cargo.toml | 6 ++ .../no-listing-17-map-initializer/src/main.rs | 10 ++ .../no-listing-18-returns-closure/Cargo.lock | 6 ++ .../no-listing-18-returns-closure/Cargo.toml | 6 ++ .../no-listing-18-returns-closure/output.txt | 16 +++ .../no-listing-18-returns-closure/src/lib.rs | 3 + .../Cargo.lock | 6 ++ .../Cargo.toml | 6 ++ .../src/lib.rs | 3 + .../hello_macro/Cargo.lock | 6 ++ .../hello_macro/Cargo.toml | 6 ++ .../hello_macro/src/lib.rs | 3 + .../pancakes/Cargo.lock | 13 +++ .../pancakes/Cargo.toml | 7 ++ .../pancakes/src/main.rs | 13 +++ .../no-listing-21-pancakes/hello_macro/Cargo.lock | 6 ++ .../no-listing-21-pancakes/hello_macro/Cargo.toml | 6 ++ .../hello_macro/hello_macro_derive/Cargo.lock | 46 +++++++++ .../hello_macro/hello_macro_derive/Cargo.toml | 11 ++ .../hello_macro/hello_macro_derive/src/lib.rs | 25 +++++ .../no-listing-21-pancakes/hello_macro/src/lib.rs | 3 + .../no-listing-21-pancakes/hello_macro/src/main.rs | 13 +++ .../no-listing-21-pancakes/pancakes/Cargo.lock | 58 +++++++++++ .../no-listing-21-pancakes/pancakes/Cargo.toml | 8 ++ .../no-listing-21-pancakes/pancakes/src/main.rs | 9 ++ .../no-listing-22-iterator-on-counter/Cargo.lock | 6 ++ .../no-listing-22-iterator-on-counter/Cargo.toml | 6 ++ .../no-listing-22-iterator-on-counter/src/lib.rs | 25 +++++ .../output-only-01-missing-unsafe/Cargo.lock | 6 ++ .../output-only-01-missing-unsafe/Cargo.toml | 6 ++ .../output-only-01-missing-unsafe/output.txt | 12 +++ .../output-only-01-missing-unsafe/src/main.rs | 7 ++ .../ch20-web-server/listing-20-01/Cargo.lock | 6 ++ .../ch20-web-server/listing-20-01/Cargo.toml | 6 ++ .../ch20-web-server/listing-20-01/src/main.rs | 11 ++ .../ch20-web-server/listing-20-02/Cargo.lock | 6 ++ .../ch20-web-server/listing-20-02/Cargo.toml | 6 ++ .../ch20-web-server/listing-20-02/src/main.rs | 25 +++++ .../ch20-web-server/listing-20-03/Cargo.lock | 6 ++ .../ch20-web-server/listing-20-03/Cargo.toml | 6 ++ .../ch20-web-server/listing-20-03/src/main.rs | 29 ++++++ .../ch20-web-server/listing-20-05/Cargo.lock | 6 ++ .../ch20-web-server/listing-20-05/Cargo.toml | 6 ++ .../ch20-web-server/listing-20-05/hello.html | 11 ++ .../ch20-web-server/listing-20-05/src/main.rs | 38 +++++++ .../ch20-web-server/listing-20-06/Cargo.lock | 6 ++ .../ch20-web-server/listing-20-06/Cargo.toml | 6 ++ .../ch20-web-server/listing-20-06/hello.html | 11 ++ .../ch20-web-server/listing-20-06/src/main.rs | 37 +++++++ .../ch20-web-server/listing-20-07/404.html | 11 ++ .../ch20-web-server/listing-20-07/Cargo.lock | 6 ++ .../ch20-web-server/listing-20-07/Cargo.toml | 6 ++ .../ch20-web-server/listing-20-07/hello.html | 11 ++ .../ch20-web-server/listing-20-07/src/main.rs | 45 ++++++++ .../ch20-web-server/listing-20-09/404.html | 11 ++ .../ch20-web-server/listing-20-09/Cargo.lock | 6 ++ .../ch20-web-server/listing-20-09/Cargo.toml | 6 ++ .../ch20-web-server/listing-20-09/hello.html | 11 ++ .../ch20-web-server/listing-20-09/src/main.rs | 40 ++++++++ .../ch20-web-server/listing-20-10/404.html | 11 ++ .../ch20-web-server/listing-20-10/Cargo.lock | 6 ++ .../ch20-web-server/listing-20-10/Cargo.toml | 6 ++ .../ch20-web-server/listing-20-10/hello.html | 11 ++ .../ch20-web-server/listing-20-10/src/main.rs | 52 ++++++++++ .../ch20-web-server/listing-20-11/404.html | 11 ++ .../ch20-web-server/listing-20-11/Cargo.lock | 6 ++ .../ch20-web-server/listing-20-11/Cargo.toml | 6 ++ .../ch20-web-server/listing-20-11/hello.html | 11 ++ .../ch20-web-server/listing-20-11/src/main.rs | 43 ++++++++ .../ch20-web-server/listing-20-12/404.html | 11 ++ .../ch20-web-server/listing-20-12/Cargo.lock | 6 ++ .../ch20-web-server/listing-20-12/Cargo.toml | 6 ++ .../ch20-web-server/listing-20-12/hello.html | 11 ++ .../ch20-web-server/listing-20-12/output.txt | 10 ++ .../ch20-web-server/listing-20-12/src/main.rs | 44 ++++++++ .../ch20-web-server/listing-20-13/404.html | 11 ++ .../ch20-web-server/listing-20-13/Cargo.lock | 6 ++ .../ch20-web-server/listing-20-13/Cargo.toml | 6 ++ .../ch20-web-server/listing-20-13/hello.html | 11 ++ .../ch20-web-server/listing-20-13/src/lib.rs | 27 +++++ .../ch20-web-server/listing-20-13/src/main.rs | 43 ++++++++ .../ch20-web-server/listing-20-14/404.html | 11 ++ .../ch20-web-server/listing-20-14/Cargo.lock | 6 ++ .../ch20-web-server/listing-20-14/Cargo.toml | 6 ++ .../ch20-web-server/listing-20-14/hello.html | 11 ++ .../ch20-web-server/listing-20-14/src/lib.rs | 40 ++++++++ .../ch20-web-server/listing-20-14/src/main.rs | 43 ++++++++ .../ch20-web-server/listing-20-15/404.html | 11 ++ .../ch20-web-server/listing-20-15/Cargo.lock | 6 ++ .../ch20-web-server/listing-20-15/Cargo.toml | 6 ++ .../ch20-web-server/listing-20-15/hello.html | 11 ++ .../ch20-web-server/listing-20-15/src/lib.rs | 53 ++++++++++ .../ch20-web-server/listing-20-15/src/main.rs | 43 ++++++++ .../ch20-web-server/listing-20-16/404.html | 11 ++ .../ch20-web-server/listing-20-16/Cargo.lock | 6 ++ .../ch20-web-server/listing-20-16/Cargo.toml | 6 ++ .../ch20-web-server/listing-20-16/hello.html | 11 ++ .../ch20-web-server/listing-20-16/src/lib.rs | 58 +++++++++++ .../ch20-web-server/listing-20-16/src/main.rs | 43 ++++++++ .../ch20-web-server/listing-20-17/404.html | 11 ++ .../ch20-web-server/listing-20-17/Cargo.lock | 6 ++ .../ch20-web-server/listing-20-17/Cargo.toml | 6 ++ .../ch20-web-server/listing-20-17/hello.html | 11 ++ .../ch20-web-server/listing-20-17/output.txt | 13 +++ .../ch20-web-server/listing-20-17/src/lib.rs | 65 ++++++++++++ .../ch20-web-server/listing-20-17/src/main.rs | 43 ++++++++ .../ch20-web-server/listing-20-18/404.html | 11 ++ .../ch20-web-server/listing-20-18/Cargo.lock | 6 ++ .../ch20-web-server/listing-20-18/Cargo.toml | 6 ++ .../ch20-web-server/listing-20-18/hello.html | 11 ++ .../ch20-web-server/listing-20-18/src/lib.rs | 76 ++++++++++++++ .../ch20-web-server/listing-20-18/src/main.rs | 43 ++++++++ .../ch20-web-server/listing-20-19/404.html | 11 ++ .../ch20-web-server/listing-20-19/Cargo.lock | 6 ++ .../ch20-web-server/listing-20-19/Cargo.toml | 6 ++ .../ch20-web-server/listing-20-19/hello.html | 11 ++ .../ch20-web-server/listing-20-19/src/lib.rs | 69 +++++++++++++ .../ch20-web-server/listing-20-19/src/main.rs | 43 ++++++++ .../ch20-web-server/listing-20-20/404.html | 11 ++ .../ch20-web-server/listing-20-20/Cargo.lock | 6 ++ .../ch20-web-server/listing-20-20/Cargo.toml | 6 ++ .../ch20-web-server/listing-20-20/hello.html | 11 ++ .../ch20-web-server/listing-20-20/src/lib.rs | 68 +++++++++++++ .../ch20-web-server/listing-20-20/src/main.rs | 43 ++++++++ .../ch20-web-server/listing-20-21/404.html | 11 ++ .../ch20-web-server/listing-20-21/Cargo.lock | 6 ++ .../ch20-web-server/listing-20-21/Cargo.toml | 6 ++ .../ch20-web-server/listing-20-21/hello.html | 11 ++ .../ch20-web-server/listing-20-21/src/lib.rs | 67 ++++++++++++ .../ch20-web-server/listing-20-21/src/main.rs | 43 ++++++++ .../ch20-web-server/listing-20-22/404.html | 11 ++ .../ch20-web-server/listing-20-22/Cargo.lock | 6 ++ .../ch20-web-server/listing-20-22/Cargo.toml | 6 ++ .../ch20-web-server/listing-20-22/hello.html | 11 ++ .../ch20-web-server/listing-20-22/output.txt | 14 +++ .../ch20-web-server/listing-20-22/src/lib.rs | 76 ++++++++++++++ .../ch20-web-server/listing-20-22/src/main.rs | 43 ++++++++ .../ch20-web-server/listing-20-23/404.html | 11 ++ .../ch20-web-server/listing-20-23/Cargo.lock | 6 ++ .../ch20-web-server/listing-20-23/Cargo.toml | 6 ++ .../ch20-web-server/listing-20-23/hello.html | 11 ++ .../ch20-web-server/listing-20-23/src/lib.rs | 95 +++++++++++++++++ .../ch20-web-server/listing-20-23/src/main.rs | 45 ++++++++ .../ch20-web-server/listing-20-24/404.html | 11 ++ .../ch20-web-server/listing-20-24/Cargo.lock | 6 ++ .../ch20-web-server/listing-20-24/Cargo.toml | 6 ++ .../ch20-web-server/listing-20-24/hello.html | 11 ++ .../ch20-web-server/listing-20-24/src/lib.rs | 92 +++++++++++++++++ .../ch20-web-server/listing-20-24/src/main.rs | 45 ++++++++ .../ch20-web-server/listing-20-25/404.html | 11 ++ .../ch20-web-server/listing-20-25/Cargo.lock | 6 ++ .../ch20-web-server/listing-20-25/Cargo.toml | 6 ++ .../ch20-web-server/listing-20-25/hello.html | 11 ++ .../ch20-web-server/listing-20-25/src/lib.rs | 92 +++++++++++++++++ .../ch20-web-server/listing-20-25/src/main.rs | 53 ++++++++++ .../404.html | 11 ++ .../Cargo.lock | 6 ++ .../Cargo.toml | 6 ++ .../hello.html | 11 ++ .../output.txt | 10 ++ .../src/lib.rs | 1 + .../src/main.rs | 45 ++++++++ .../no-listing-02-impl-threadpool-new/404.html | 11 ++ .../no-listing-02-impl-threadpool-new/Cargo.lock | 6 ++ .../no-listing-02-impl-threadpool-new/Cargo.toml | 6 ++ .../no-listing-02-impl-threadpool-new/hello.html | 11 ++ .../no-listing-02-impl-threadpool-new/output.txt | 10 ++ .../no-listing-02-impl-threadpool-new/src/lib.rs | 7 ++ .../no-listing-02-impl-threadpool-new/src/main.rs | 43 ++++++++ .../no-listing-03-define-execute/404.html | 11 ++ .../no-listing-03-define-execute/Cargo.lock | 6 ++ .../no-listing-03-define-execute/Cargo.toml | 6 ++ .../no-listing-03-define-execute/hello.html | 11 ++ .../no-listing-03-define-execute/output.txt | 3 + .../no-listing-03-define-execute/src/lib.rs | 18 ++++ .../no-listing-03-define-execute/src/main.rs | 43 ++++++++ .../404.html | 11 ++ .../Cargo.lock | 6 ++ .../Cargo.toml | 6 ++ .../hello.html | 11 ++ .../output.txt | 24 +++++ .../src/lib.rs | 76 ++++++++++++++ .../src/main.rs | 43 ++++++++ .../no-listing-05-fix-worker-new/404.html | 11 ++ .../no-listing-05-fix-worker-new/Cargo.lock | 6 ++ .../no-listing-05-fix-worker-new/Cargo.toml | 6 ++ .../no-listing-05-fix-worker-new/hello.html | 11 ++ .../no-listing-05-fix-worker-new/src/lib.rs | 83 +++++++++++++++ .../no-listing-05-fix-worker-new/src/main.rs | 43 ++++++++ .../no-listing-06-fix-threadpool-drop/404.html | 11 ++ .../no-listing-06-fix-threadpool-drop/Cargo.lock | 6 ++ .../no-listing-06-fix-threadpool-drop/Cargo.toml | 6 ++ .../no-listing-06-fix-threadpool-drop/hello.html | 11 ++ .../no-listing-06-fix-threadpool-drop/src/lib.rs | 81 +++++++++++++++ .../no-listing-06-fix-threadpool-drop/src/main.rs | 45 ++++++++ .../no-listing-07-final-code/404.html | 11 ++ .../no-listing-07-final-code/Cargo.lock | 6 ++ .../no-listing-07-final-code/Cargo.toml | 6 ++ .../no-listing-07-final-code/hello.html | 11 ++ .../no-listing-07-final-code/src/lib.rs | 92 +++++++++++++++++ .../no-listing-07-final-code/src/main.rs | 51 ++++++++++ 1869 files changed, 23238 insertions(+) create mode 100644 src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-01/Cargo.lock create mode 100644 src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-01/Cargo.toml create mode 100644 src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-01/src/main.rs create mode 100644 src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-02/Cargo.lock create mode 100644 src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-02/Cargo.toml create mode 100644 src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-02/src/main.rs create mode 100644 src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-03/Cargo.lock create mode 100644 src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-03/Cargo.toml create mode 100644 src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-03/src/main.rs create mode 100644 src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-04/Cargo.lock create mode 100644 src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-04/Cargo.toml create mode 100644 src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-04/output.txt create mode 100644 src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-04/src/main.rs create mode 100644 src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-05/Cargo.lock create mode 100644 src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-05/Cargo.toml create mode 100644 src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-05/src/main.rs create mode 100644 src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-06/Cargo.lock create mode 100644 src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-06/Cargo.toml create mode 100644 src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-06/src/main.rs create mode 100644 src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-01-cargo-new/Cargo.lock create mode 100644 src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-01-cargo-new/Cargo.toml create mode 100644 src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-01-cargo-new/output.txt create mode 100644 src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-01-cargo-new/src/main.rs create mode 100644 src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-02-without-expect/Cargo.lock create mode 100644 src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-02-without-expect/Cargo.toml create mode 100644 src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-02-without-expect/output.txt create mode 100644 src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-02-without-expect/src/main.rs create mode 100644 src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-03-convert-string-to-number/Cargo.lock create mode 100644 src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-03-convert-string-to-number/Cargo.toml create mode 100644 src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-03-convert-string-to-number/src/main.rs create mode 100644 src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-04-looping/Cargo.lock create mode 100644 src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-04-looping/Cargo.toml create mode 100644 src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-04-looping/src/main.rs create mode 100644 src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-05-quitting/Cargo.lock create mode 100644 src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-05-quitting/Cargo.toml create mode 100644 src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-05-quitting/src/main.rs create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/listing-03-01/Cargo.lock create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/listing-03-01/Cargo.toml create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/listing-03-01/src/main.rs create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/listing-03-02/Cargo.lock create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/listing-03-02/Cargo.toml create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/listing-03-02/output.txt create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/listing-03-02/src/main.rs create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/listing-03-03/Cargo.lock create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/listing-03-03/Cargo.toml create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/listing-03-03/src/main.rs create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/listing-03-04/Cargo.lock create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/listing-03-04/Cargo.toml create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/listing-03-04/output.txt create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/listing-03-04/src/main.rs create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/listing-03-05/Cargo.lock create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/listing-03-05/Cargo.toml create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/listing-03-05/src/main.rs create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-01-variables-are-immutable/Cargo.lock create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-01-variables-are-immutable/Cargo.toml create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-01-variables-are-immutable/output.txt create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-01-variables-are-immutable/src/main.rs create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-02-adding-mut/Cargo.lock create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-02-adding-mut/Cargo.toml create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-02-adding-mut/output.txt create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-02-adding-mut/src/main.rs create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-03-shadowing/Cargo.lock create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-03-shadowing/Cargo.toml create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-03-shadowing/output.txt create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-03-shadowing/src/main.rs create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-04-shadowing-can-change-types/Cargo.lock create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-04-shadowing-can-change-types/Cargo.toml create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-04-shadowing-can-change-types/src/main.rs create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-05-mut-cant-change-types/Cargo.lock create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-05-mut-cant-change-types/Cargo.toml create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-05-mut-cant-change-types/output.txt create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-05-mut-cant-change-types/src/main.rs create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-06-floating-point/Cargo.lock create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-06-floating-point/Cargo.toml create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-06-floating-point/src/main.rs create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-07-numeric-operations/Cargo.lock create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-07-numeric-operations/Cargo.toml create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-07-numeric-operations/src/main.rs create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-08-boolean/Cargo.lock create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-08-boolean/Cargo.toml create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-08-boolean/src/main.rs create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-09-char/Cargo.lock create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-09-char/Cargo.toml create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-09-char/src/main.rs create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-10-tuples/Cargo.lock create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-10-tuples/Cargo.toml create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-10-tuples/src/main.rs create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-11-destructuring-tuples/Cargo.lock create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-11-destructuring-tuples/Cargo.toml create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-11-destructuring-tuples/src/main.rs create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-12-tuple-indexing/Cargo.lock create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-12-tuple-indexing/Cargo.toml create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-12-tuple-indexing/src/main.rs create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-13-arrays/Cargo.lock create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-13-arrays/Cargo.toml create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-13-arrays/src/main.rs create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-14-array-indexing/Cargo.lock create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-14-array-indexing/Cargo.toml create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-14-array-indexing/src/main.rs create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-15-invalid-array-access/Cargo.lock create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-15-invalid-array-access/Cargo.toml create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-15-invalid-array-access/src/main.rs create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-16-functions/Cargo.lock create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-16-functions/Cargo.toml create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-16-functions/output.txt create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-16-functions/src/main.rs create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-17-functions-with-parameters/Cargo.lock create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-17-functions-with-parameters/Cargo.toml create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-17-functions-with-parameters/output.txt create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-17-functions-with-parameters/src/main.rs create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-18-functions-with-multiple-parameters/Cargo.lock create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-18-functions-with-multiple-parameters/Cargo.toml create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-18-functions-with-multiple-parameters/output.txt create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-18-functions-with-multiple-parameters/src/main.rs create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-19-statements-vs-expressions/Cargo.lock create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-19-statements-vs-expressions/Cargo.toml create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-19-statements-vs-expressions/output.txt create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-19-statements-vs-expressions/rustfmt-ignore create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-19-statements-vs-expressions/src/main.rs create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-20-blocks-are-expressions/Cargo.lock create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-20-blocks-are-expressions/Cargo.toml create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-20-blocks-are-expressions/src/main.rs create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-21-function-return-values/Cargo.lock create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-21-function-return-values/Cargo.toml create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-21-function-return-values/output.txt create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-21-function-return-values/src/main.rs create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-22-function-parameter-and-return/Cargo.lock create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-22-function-parameter-and-return/Cargo.toml create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-22-function-parameter-and-return/src/main.rs create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-23-statements-dont-return-values/Cargo.lock create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-23-statements-dont-return-values/Cargo.toml create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-23-statements-dont-return-values/output.txt create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-23-statements-dont-return-values/src/main.rs create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-24-comments-end-of-line/Cargo.lock create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-24-comments-end-of-line/Cargo.toml create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-24-comments-end-of-line/src/main.rs create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-25-comments-above-line/Cargo.lock create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-25-comments-above-line/Cargo.toml create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-25-comments-above-line/src/main.rs create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-26-if-true/Cargo.lock create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-26-if-true/Cargo.toml create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-26-if-true/output.txt create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-26-if-true/src/main.rs create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-27-if-false/Cargo.lock create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-27-if-false/Cargo.toml create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-27-if-false/output.txt create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-27-if-false/src/main.rs create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-28-if-condition-must-be-bool/Cargo.lock create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-28-if-condition-must-be-bool/Cargo.toml create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-28-if-condition-must-be-bool/output.txt create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-28-if-condition-must-be-bool/src/main.rs create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-29-if-not-equal-0/Cargo.lock create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-29-if-not-equal-0/Cargo.toml create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-29-if-not-equal-0/src/main.rs create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-30-else-if/Cargo.lock create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-30-else-if/Cargo.toml create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-30-else-if/output.txt create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-30-else-if/src/main.rs create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-31-arms-must-return-same-type/Cargo.lock create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-31-arms-must-return-same-type/Cargo.toml create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-31-arms-must-return-same-type/output.txt create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-31-arms-must-return-same-type/src/main.rs create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-32-5-loop-labels/Cargo.lock create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-32-5-loop-labels/Cargo.toml create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-32-5-loop-labels/output.txt create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-32-5-loop-labels/src/main.rs create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-32-loop/Cargo.lock create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-32-loop/Cargo.toml create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-32-loop/src/main.rs create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-33-return-value-from-loop/Cargo.lock create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-33-return-value-from-loop/Cargo.toml create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-33-return-value-from-loop/src/main.rs create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-34-for-range/Cargo.lock create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-34-for-range/Cargo.toml create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/no-listing-34-for-range/src/main.rs create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/output-only-01-no-type-annotations/Cargo.lock create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/output-only-01-no-type-annotations/Cargo.toml create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/output-only-01-no-type-annotations/output.txt create mode 100644 src/doc/book/listings/ch03-common-programming-concepts/output-only-01-no-type-annotations/src/main.rs create mode 100644 src/doc/book/listings/ch04-understanding-ownership/listing-04-01/Cargo.lock create mode 100644 src/doc/book/listings/ch04-understanding-ownership/listing-04-01/Cargo.toml create mode 100644 src/doc/book/listings/ch04-understanding-ownership/listing-04-01/rustfmt-ignore create mode 100644 src/doc/book/listings/ch04-understanding-ownership/listing-04-01/src/main.rs create mode 100644 src/doc/book/listings/ch04-understanding-ownership/listing-04-02/Cargo.lock create mode 100644 src/doc/book/listings/ch04-understanding-ownership/listing-04-02/Cargo.toml create mode 100644 src/doc/book/listings/ch04-understanding-ownership/listing-04-02/src/main.rs create mode 100644 src/doc/book/listings/ch04-understanding-ownership/listing-04-03/Cargo.lock create mode 100644 src/doc/book/listings/ch04-understanding-ownership/listing-04-03/Cargo.toml create mode 100644 src/doc/book/listings/ch04-understanding-ownership/listing-04-03/rustfmt-ignore create mode 100644 src/doc/book/listings/ch04-understanding-ownership/listing-04-03/src/main.rs create mode 100644 src/doc/book/listings/ch04-understanding-ownership/listing-04-04/Cargo.lock create mode 100644 src/doc/book/listings/ch04-understanding-ownership/listing-04-04/Cargo.toml create mode 100644 src/doc/book/listings/ch04-understanding-ownership/listing-04-04/rustfmt-ignore create mode 100644 src/doc/book/listings/ch04-understanding-ownership/listing-04-04/src/main.rs create mode 100644 src/doc/book/listings/ch04-understanding-ownership/listing-04-05/Cargo.lock create mode 100644 src/doc/book/listings/ch04-understanding-ownership/listing-04-05/Cargo.toml create mode 100644 src/doc/book/listings/ch04-understanding-ownership/listing-04-05/src/main.rs create mode 100644 src/doc/book/listings/ch04-understanding-ownership/listing-04-06/Cargo.lock create mode 100644 src/doc/book/listings/ch04-understanding-ownership/listing-04-06/Cargo.toml create mode 100644 src/doc/book/listings/ch04-understanding-ownership/listing-04-06/output.txt create mode 100644 src/doc/book/listings/ch04-understanding-ownership/listing-04-06/src/main.rs create mode 100644 src/doc/book/listings/ch04-understanding-ownership/listing-04-07/Cargo.lock create mode 100644 src/doc/book/listings/ch04-understanding-ownership/listing-04-07/Cargo.toml create mode 100644 src/doc/book/listings/ch04-understanding-ownership/listing-04-07/src/main.rs create mode 100644 src/doc/book/listings/ch04-understanding-ownership/listing-04-08/Cargo.lock create mode 100644 src/doc/book/listings/ch04-understanding-ownership/listing-04-08/Cargo.toml create mode 100644 src/doc/book/listings/ch04-understanding-ownership/listing-04-08/src/main.rs create mode 100644 src/doc/book/listings/ch04-understanding-ownership/listing-04-09/Cargo.lock create mode 100644 src/doc/book/listings/ch04-understanding-ownership/listing-04-09/Cargo.toml create mode 100644 src/doc/book/listings/ch04-understanding-ownership/listing-04-09/src/main.rs create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-01-can-mutate-string/Cargo.lock create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-01-can-mutate-string/Cargo.toml create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-01-can-mutate-string/src/main.rs create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-02-string-scope/Cargo.lock create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-02-string-scope/Cargo.toml create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-02-string-scope/rustfmt-ignore create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-02-string-scope/src/main.rs create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-03-string-move/Cargo.lock create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-03-string-move/Cargo.toml create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-03-string-move/src/main.rs create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-04-cant-use-after-move/Cargo.lock create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-04-cant-use-after-move/Cargo.toml create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-04-cant-use-after-move/output.txt create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-04-cant-use-after-move/src/main.rs create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-05-clone/Cargo.lock create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-05-clone/Cargo.toml create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-05-clone/src/main.rs create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-06-copy/Cargo.lock create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-06-copy/Cargo.toml create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-06-copy/src/main.rs create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-07-reference/Cargo.lock create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-07-reference/Cargo.toml create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-07-reference/src/main.rs create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-08-reference-with-annotations/Cargo.lock create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-08-reference-with-annotations/Cargo.toml create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-08-reference-with-annotations/rustfmt-ignore create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-08-reference-with-annotations/src/main.rs create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-09-fixes-listing-04-06/Cargo.lock create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-09-fixes-listing-04-06/Cargo.toml create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-09-fixes-listing-04-06/src/main.rs create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-10-multiple-mut-not-allowed/Cargo.lock create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-10-multiple-mut-not-allowed/Cargo.toml create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-10-multiple-mut-not-allowed/output.txt create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-10-multiple-mut-not-allowed/src/main.rs create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-11-muts-in-separate-scopes/Cargo.lock create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-11-muts-in-separate-scopes/Cargo.toml create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-11-muts-in-separate-scopes/src/main.rs create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-12-immutable-and-mutable-not-allowed/Cargo.lock create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-12-immutable-and-mutable-not-allowed/Cargo.toml create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-12-immutable-and-mutable-not-allowed/output.txt create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-12-immutable-and-mutable-not-allowed/src/main.rs create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-13-reference-scope-ends/Cargo.lock create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-13-reference-scope-ends/Cargo.toml create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-13-reference-scope-ends/src/main.rs create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-14-dangling-reference/Cargo.lock create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-14-dangling-reference/Cargo.toml create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-14-dangling-reference/output.txt create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-14-dangling-reference/src/main.rs create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-15-dangling-reference-annotated/Cargo.lock create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-15-dangling-reference-annotated/Cargo.toml create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-15-dangling-reference-annotated/rustfmt-ignore create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-15-dangling-reference-annotated/src/main.rs create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-16-no-dangle/Cargo.lock create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-16-no-dangle/Cargo.toml create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-16-no-dangle/src/main.rs create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-17-slice/Cargo.lock create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-17-slice/Cargo.toml create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-17-slice/src/main.rs create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-18-first-word-slice/Cargo.lock create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-18-first-word-slice/Cargo.toml create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-18-first-word-slice/src/main.rs create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-19-slice-error/Cargo.lock create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-19-slice-error/Cargo.toml create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-19-slice-error/output.txt create mode 100644 src/doc/book/listings/ch04-understanding-ownership/no-listing-19-slice-error/src/main.rs create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-01/Cargo.lock create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-01/Cargo.toml create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-01/src/main.rs create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-02/Cargo.lock create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-02/Cargo.toml create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-02/src/main.rs create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-03/Cargo.lock create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-03/Cargo.toml create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-03/src/main.rs create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-04/Cargo.lock create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-04/Cargo.toml create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-04/src/main.rs create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-05/Cargo.lock create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-05/Cargo.toml create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-05/src/main.rs create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-06/Cargo.lock create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-06/Cargo.toml create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-06/src/main.rs create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-07/Cargo.lock create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-07/Cargo.toml create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-07/src/main.rs create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-08/Cargo.lock create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-08/Cargo.toml create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-08/output.txt create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-08/src/main.rs create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-09/Cargo.lock create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-09/Cargo.toml create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-09/src/main.rs create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-10/Cargo.lock create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-10/Cargo.toml create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-10/src/main.rs create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-11/Cargo.lock create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-11/Cargo.toml create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-11/output.txt create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-11/src/main.rs create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-12/Cargo.lock create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-12/Cargo.toml create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-12/output.txt create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-12/src/main.rs create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-13/Cargo.lock create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-13/Cargo.toml create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-13/src/main.rs create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-14/Cargo.lock create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-14/Cargo.toml create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-14/src/main.rs create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-15/Cargo.lock create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-15/Cargo.toml create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-15/src/main.rs create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-16/Cargo.lock create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-16/Cargo.toml create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-16/src/main.rs create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-01-tuple-structs/Cargo.lock create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-01-tuple-structs/Cargo.toml create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-01-tuple-structs/src/main.rs create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-02-reference-in-struct/Cargo.lock create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-02-reference-in-struct/Cargo.toml create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-02-reference-in-struct/output.txt create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-02-reference-in-struct/src/main.rs create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-03-associated-functions/Cargo.lock create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-03-associated-functions/Cargo.toml create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-03-associated-functions/src/main.rs create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-04-unit-like-structs/Cargo.lock create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-04-unit-like-structs/Cargo.toml create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-04-unit-like-structs/src/main.rs create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-05-dbg-macro/Cargo.lock create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-05-dbg-macro/Cargo.toml create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-05-dbg-macro/output.txt create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-05-dbg-macro/src/main.rs create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-06-method-field-interaction/Cargo.lock create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-06-method-field-interaction/Cargo.toml create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-06-method-field-interaction/src/main.rs create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/output-only-01-debug/Cargo.lock create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/output-only-01-debug/Cargo.toml create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/output-only-01-debug/output.txt create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/output-only-01-debug/src/main.rs create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/output-only-02-pretty-debug/Cargo.lock create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/output-only-02-pretty-debug/Cargo.toml create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/output-only-02-pretty-debug/output.txt create mode 100644 src/doc/book/listings/ch05-using-structs-to-structure-related-data/output-only-02-pretty-debug/src/main.rs create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-01/Cargo.lock create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-01/Cargo.toml create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-01/src/main.rs create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-02/Cargo.lock create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-02/Cargo.toml create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-02/src/main.rs create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-03/Cargo.lock create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-03/Cargo.toml create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-03/src/main.rs create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-04/Cargo.lock create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-04/Cargo.toml create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-04/src/main.rs create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-05/Cargo.lock create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-05/Cargo.toml create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-05/src/main.rs create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-06/Cargo.lock create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-06/Cargo.toml create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-06/src/main.rs create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-01-defining-enums/Cargo.lock create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-01-defining-enums/Cargo.toml create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-01-defining-enums/src/main.rs create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-02-enum-with-data/Cargo.lock create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-02-enum-with-data/Cargo.toml create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-02-enum-with-data/src/main.rs create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-03-variants-with-different-data/Cargo.lock create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-03-variants-with-different-data/Cargo.toml create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-03-variants-with-different-data/src/main.rs create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-04-structs-similar-to-message-enum/Cargo.lock create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-04-structs-similar-to-message-enum/Cargo.toml create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-04-structs-similar-to-message-enum/src/main.rs create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-05-methods-on-enums/Cargo.lock create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-05-methods-on-enums/Cargo.toml create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-05-methods-on-enums/src/main.rs create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-06-option-examples/Cargo.lock create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-06-option-examples/Cargo.toml create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-06-option-examples/src/main.rs create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-07-cant-use-option-directly/Cargo.lock create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-07-cant-use-option-directly/Cargo.toml create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-07-cant-use-option-directly/output.txt create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-07-cant-use-option-directly/src/main.rs create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-08-match-arm-multiple-lines/Cargo.lock create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-08-match-arm-multiple-lines/Cargo.toml create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-08-match-arm-multiple-lines/src/main.rs create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-09-variable-in-pattern/Cargo.lock create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-09-variable-in-pattern/Cargo.toml create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-09-variable-in-pattern/src/main.rs create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-10-non-exhaustive-match/Cargo.lock create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-10-non-exhaustive-match/Cargo.toml create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-10-non-exhaustive-match/output.txt create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-10-non-exhaustive-match/src/main.rs create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-12-if-let/Cargo.lock create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-12-if-let/Cargo.toml create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-12-if-let/src/main.rs create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-13-count-and-announce-match/Cargo.lock create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-13-count-and-announce-match/Cargo.toml create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-13-count-and-announce-match/src/main.rs create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-14-count-and-announce-if-let-else/Cargo.lock create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-14-count-and-announce-if-let-else/Cargo.toml create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-14-count-and-announce-if-let-else/src/main.rs create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-15-binding-catchall/Cargo.lock create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-15-binding-catchall/Cargo.toml create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-15-binding-catchall/src/main.rs create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-16-underscore-catchall/Cargo.lock create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-16-underscore-catchall/Cargo.toml create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-16-underscore-catchall/src/main.rs create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-17-underscore-unit/Cargo.lock create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-17-underscore-unit/Cargo.toml create mode 100644 src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-17-underscore-unit/src/main.rs create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-01/Cargo.lock create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-01/Cargo.toml create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-01/src/lib.rs create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-03/Cargo.lock create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-03/Cargo.toml create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-03/output.txt create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-03/src/lib.rs create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-05/Cargo.lock create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-05/Cargo.toml create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-05/output.txt create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-05/src/lib.rs create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-07/Cargo.lock create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-07/Cargo.toml create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-07/src/lib.rs create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-08/Cargo.lock create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-08/Cargo.toml create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-08/src/lib.rs create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-09/Cargo.lock create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-09/Cargo.toml create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-09/src/lib.rs create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-10/Cargo.lock create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-10/Cargo.toml create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-10/src/lib.rs create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-11/Cargo.lock create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-11/Cargo.toml create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-11/src/lib.rs create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-12/Cargo.lock create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-12/Cargo.toml create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-12/output.txt create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-12/src/lib.rs create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-13/Cargo.lock create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-13/Cargo.toml create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-13/src/lib.rs create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-14/Cargo.lock create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-14/Cargo.toml create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-14/src/main.rs create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-15/Cargo.lock create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-15/Cargo.toml create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-15/src/lib.rs create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-16/Cargo.lock create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-16/Cargo.toml create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-16/src/lib.rs create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-17/Cargo.lock create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-17/Cargo.toml create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-17/src/lib.rs create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-18/Cargo.lock create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-18/Cargo.toml create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-18/src/main.rs create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-19/Cargo.lock create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-19/Cargo.toml create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-19/src/lib.rs create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-20/Cargo.lock create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-20/Cargo.toml create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-20/src/lib.rs create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-21-and-22/Cargo.lock create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-21-and-22/Cargo.toml create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-21-and-22/src/front_of_house.rs create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/listing-07-21-and-22/src/lib.rs create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/no-listing-01-use-std-unnested/Cargo.lock create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/no-listing-01-use-std-unnested/Cargo.toml create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/no-listing-01-use-std-unnested/src/main.rs create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/no-listing-02-extracting-hosting/Cargo.lock create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/no-listing-02-extracting-hosting/Cargo.toml create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/no-listing-02-extracting-hosting/src/front_of_house.rs create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/no-listing-02-extracting-hosting/src/front_of_house/hosting.rs create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/no-listing-02-extracting-hosting/src/lib.rs create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/quick-reference-example/Cargo.lock create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/quick-reference-example/Cargo.toml create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/quick-reference-example/output.txt create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/quick-reference-example/src/garden.rs create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/quick-reference-example/src/garden/vegetables.rs create mode 100644 src/doc/book/listings/ch07-managing-growing-projects/quick-reference-example/src/main.rs create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-01/Cargo.lock create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-01/Cargo.toml create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-01/src/main.rs create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-02/Cargo.lock create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-02/Cargo.toml create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-02/src/main.rs create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-03/Cargo.lock create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-03/Cargo.toml create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-03/src/main.rs create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-04/Cargo.lock create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-04/Cargo.toml create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-04/src/main.rs create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-05/Cargo.lock create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-05/Cargo.toml create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-05/src/main.rs create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-06/Cargo.lock create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-06/Cargo.toml create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-06/output.txt create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-06/src/main.rs create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-07/Cargo.lock create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-07/Cargo.toml create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-07/src/main.rs create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-08/Cargo.lock create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-08/Cargo.toml create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-08/src/main.rs create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-09/Cargo.lock create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-09/Cargo.toml create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-09/src/main.rs create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-10/Cargo.lock create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-10/Cargo.toml create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-10/src/main.rs create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-11/Cargo.lock create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-11/Cargo.toml create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-11/src/main.rs create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-12/Cargo.lock create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-12/Cargo.toml create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-12/src/main.rs create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-13/Cargo.lock create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-13/Cargo.toml create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-13/src/main.rs create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-14/Cargo.lock create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-14/Cargo.toml create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-14/src/main.rs create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-15/Cargo.lock create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-15/Cargo.toml create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-15/src/main.rs create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-16/Cargo.lock create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-16/Cargo.toml create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-16/src/main.rs create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-17/Cargo.lock create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-17/Cargo.toml create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-17/src/main.rs create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-18/Cargo.lock create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-18/Cargo.toml create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-18/src/main.rs create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-19/Cargo.lock create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-19/Cargo.toml create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-19/output.txt create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-19/src/main.rs create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-20/Cargo.lock create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-20/Cargo.toml create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-20/src/main.rs create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-21/Cargo.lock create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-21/Cargo.toml create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-21/src/main.rs create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-22/Cargo.lock create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-22/Cargo.toml create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-22/src/main.rs create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-23/Cargo.lock create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-23/Cargo.toml create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-23/src/main.rs create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-24/Cargo.lock create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-24/Cargo.toml create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-24/src/main.rs create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-25/Cargo.lock create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-25/Cargo.toml create mode 100644 src/doc/book/listings/ch08-common-collections/listing-08-25/src/main.rs create mode 100644 src/doc/book/listings/ch08-common-collections/no-listing-01-concat-multiple-strings/Cargo.lock create mode 100644 src/doc/book/listings/ch08-common-collections/no-listing-01-concat-multiple-strings/Cargo.toml create mode 100644 src/doc/book/listings/ch08-common-collections/no-listing-01-concat-multiple-strings/src/main.rs create mode 100644 src/doc/book/listings/ch08-common-collections/no-listing-02-format/Cargo.lock create mode 100644 src/doc/book/listings/ch08-common-collections/no-listing-02-format/Cargo.toml create mode 100644 src/doc/book/listings/ch08-common-collections/no-listing-02-format/src/main.rs create mode 100644 src/doc/book/listings/ch08-common-collections/no-listing-03-iterate-over-hashmap/Cargo.lock create mode 100644 src/doc/book/listings/ch08-common-collections/no-listing-03-iterate-over-hashmap/Cargo.toml create mode 100644 src/doc/book/listings/ch08-common-collections/no-listing-03-iterate-over-hashmap/src/main.rs create mode 100644 src/doc/book/listings/ch08-common-collections/output-only-01-not-char-boundary/Cargo.lock create mode 100644 src/doc/book/listings/ch08-common-collections/output-only-01-not-char-boundary/Cargo.toml create mode 100644 src/doc/book/listings/ch08-common-collections/output-only-01-not-char-boundary/output.txt create mode 100644 src/doc/book/listings/ch08-common-collections/output-only-01-not-char-boundary/src/main.rs create mode 100644 src/doc/book/listings/ch09-error-handling/listing-09-01/Cargo.lock create mode 100644 src/doc/book/listings/ch09-error-handling/listing-09-01/Cargo.toml create mode 100644 src/doc/book/listings/ch09-error-handling/listing-09-01/output.txt create mode 100644 src/doc/book/listings/ch09-error-handling/listing-09-01/src/main.rs create mode 100644 src/doc/book/listings/ch09-error-handling/listing-09-03/Cargo.lock create mode 100644 src/doc/book/listings/ch09-error-handling/listing-09-03/Cargo.toml create mode 100644 src/doc/book/listings/ch09-error-handling/listing-09-03/src/main.rs create mode 100644 src/doc/book/listings/ch09-error-handling/listing-09-04/Cargo.lock create mode 100644 src/doc/book/listings/ch09-error-handling/listing-09-04/Cargo.toml create mode 100644 src/doc/book/listings/ch09-error-handling/listing-09-04/output.txt create mode 100644 src/doc/book/listings/ch09-error-handling/listing-09-04/src/main.rs create mode 100644 src/doc/book/listings/ch09-error-handling/listing-09-05/Cargo.lock create mode 100644 src/doc/book/listings/ch09-error-handling/listing-09-05/Cargo.toml create mode 100644 src/doc/book/listings/ch09-error-handling/listing-09-05/src/main.rs create mode 100644 src/doc/book/listings/ch09-error-handling/listing-09-06/Cargo.lock create mode 100644 src/doc/book/listings/ch09-error-handling/listing-09-06/Cargo.toml create mode 100644 src/doc/book/listings/ch09-error-handling/listing-09-06/src/main.rs create mode 100644 src/doc/book/listings/ch09-error-handling/listing-09-07/Cargo.lock create mode 100644 src/doc/book/listings/ch09-error-handling/listing-09-07/Cargo.toml create mode 100644 src/doc/book/listings/ch09-error-handling/listing-09-07/src/main.rs create mode 100644 src/doc/book/listings/ch09-error-handling/listing-09-08/Cargo.lock create mode 100644 src/doc/book/listings/ch09-error-handling/listing-09-08/Cargo.toml create mode 100644 src/doc/book/listings/ch09-error-handling/listing-09-08/src/main.rs create mode 100644 src/doc/book/listings/ch09-error-handling/listing-09-09/Cargo.lock create mode 100644 src/doc/book/listings/ch09-error-handling/listing-09-09/Cargo.toml create mode 100644 src/doc/book/listings/ch09-error-handling/listing-09-09/src/main.rs create mode 100644 src/doc/book/listings/ch09-error-handling/listing-09-10/Cargo.lock create mode 100644 src/doc/book/listings/ch09-error-handling/listing-09-10/Cargo.toml create mode 100644 src/doc/book/listings/ch09-error-handling/listing-09-10/output.txt create mode 100644 src/doc/book/listings/ch09-error-handling/listing-09-10/src/main.rs create mode 100644 src/doc/book/listings/ch09-error-handling/listing-09-11/Cargo.lock create mode 100644 src/doc/book/listings/ch09-error-handling/listing-09-11/Cargo.toml create mode 100644 src/doc/book/listings/ch09-error-handling/listing-09-11/src/main.rs create mode 100644 src/doc/book/listings/ch09-error-handling/listing-09-12/Cargo.lock create mode 100644 src/doc/book/listings/ch09-error-handling/listing-09-12/Cargo.toml create mode 100644 src/doc/book/listings/ch09-error-handling/listing-09-12/src/main.rs create mode 100644 src/doc/book/listings/ch09-error-handling/listing-09-13/Cargo.lock create mode 100644 src/doc/book/listings/ch09-error-handling/listing-09-13/Cargo.toml create mode 100644 src/doc/book/listings/ch09-error-handling/listing-09-13/src/main.rs create mode 100644 src/doc/book/listings/ch09-error-handling/no-listing-01-panic/Cargo.lock create mode 100644 src/doc/book/listings/ch09-error-handling/no-listing-01-panic/Cargo.toml create mode 100644 src/doc/book/listings/ch09-error-handling/no-listing-01-panic/output.txt create mode 100644 src/doc/book/listings/ch09-error-handling/no-listing-01-panic/src/main.rs create mode 100644 src/doc/book/listings/ch09-error-handling/no-listing-04-unwrap/Cargo.lock create mode 100644 src/doc/book/listings/ch09-error-handling/no-listing-04-unwrap/Cargo.toml create mode 100644 src/doc/book/listings/ch09-error-handling/no-listing-04-unwrap/src/main.rs create mode 100644 src/doc/book/listings/ch09-error-handling/no-listing-05-expect/Cargo.lock create mode 100644 src/doc/book/listings/ch09-error-handling/no-listing-05-expect/Cargo.toml create mode 100644 src/doc/book/listings/ch09-error-handling/no-listing-05-expect/src/main.rs create mode 100644 src/doc/book/listings/ch09-error-handling/no-listing-08-unwrap-that-cant-fail/Cargo.lock create mode 100644 src/doc/book/listings/ch09-error-handling/no-listing-08-unwrap-that-cant-fail/Cargo.toml create mode 100644 src/doc/book/listings/ch09-error-handling/no-listing-08-unwrap-that-cant-fail/src/main.rs create mode 100644 src/doc/book/listings/ch09-error-handling/no-listing-09-guess-out-of-range/Cargo.lock create mode 100644 src/doc/book/listings/ch09-error-handling/no-listing-09-guess-out-of-range/Cargo.toml create mode 100644 src/doc/book/listings/ch09-error-handling/no-listing-09-guess-out-of-range/src/main.rs create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-01/Cargo.lock create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-01/Cargo.toml create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-01/src/main.rs create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-02/Cargo.lock create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-02/Cargo.toml create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-02/src/main.rs create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-03/Cargo.lock create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-03/Cargo.toml create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-03/src/main.rs create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-04/Cargo.lock create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-04/Cargo.toml create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-04/src/main.rs create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-05/Cargo.lock create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-05/Cargo.toml create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-05/output.txt create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-05/src/main.rs create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-06/Cargo.lock create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-06/Cargo.toml create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-06/src/main.rs create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-07/Cargo.lock create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-07/Cargo.toml create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-07/output.txt create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-07/src/main.rs create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-08/Cargo.lock create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-08/Cargo.toml create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-08/src/main.rs create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-09/Cargo.lock create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-09/Cargo.toml create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-09/src/main.rs create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-10/Cargo.lock create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-10/Cargo.toml create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-10/src/main.rs create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-11/Cargo.lock create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-11/Cargo.toml create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-11/src/main.rs create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-12/Cargo.lock create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-12/Cargo.toml create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-12/src/lib.rs create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-13/Cargo.lock create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-13/Cargo.toml create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-13/src/lib.rs create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-14/Cargo.lock create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-14/Cargo.toml create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-14/src/lib.rs create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-15/Cargo.lock create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-15/Cargo.toml create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-15/src/lib.rs create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-16/Cargo.lock create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-16/Cargo.toml create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-16/output.txt create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-16/src/main.rs create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-17/Cargo.lock create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-17/Cargo.toml create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-17/rustfmt-ignore create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-17/src/main.rs create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-18/Cargo.lock create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-18/Cargo.toml create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-18/rustfmt-ignore create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-18/src/main.rs create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-19/Cargo.lock create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-19/Cargo.toml create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-19/src/main.rs create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-20/Cargo.lock create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-20/Cargo.toml create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-20/output.txt create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-20/src/main.rs create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-21/Cargo.lock create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-21/Cargo.toml create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-21/src/main.rs create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-22/Cargo.lock create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-22/Cargo.toml create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-22/src/main.rs create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-23/Cargo.lock create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-23/Cargo.toml create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-23/output.txt create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-23/src/main.rs create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-24/Cargo.lock create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-24/Cargo.toml create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-24/src/main.rs create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-25/Cargo.lock create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-25/Cargo.toml create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-25/src/main.rs create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-01-calling-trait-method/Cargo.lock create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-01-calling-trait-method/Cargo.toml create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-01-calling-trait-method/src/lib.rs create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-01-calling-trait-method/src/main.rs create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-02-calling-default-impl/Cargo.lock create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-02-calling-default-impl/Cargo.toml create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-02-calling-default-impl/src/lib.rs create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-02-calling-default-impl/src/main.rs create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-03-default-impl-calls-other-methods/Cargo.lock create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-03-default-impl-calls-other-methods/Cargo.toml create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-03-default-impl-calls-other-methods/src/lib.rs create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-03-default-impl-calls-other-methods/src/main.rs create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-04-traits-as-parameters/Cargo.lock create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-04-traits-as-parameters/Cargo.toml create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-04-traits-as-parameters/src/lib.rs create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-05-returning-impl-trait/Cargo.lock create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-05-returning-impl-trait/Cargo.toml create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-05-returning-impl-trait/src/lib.rs create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-06-impl-trait-returns-one-type/Cargo.lock create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-06-impl-trait-returns-one-type/Cargo.toml create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-06-impl-trait-returns-one-type/src/lib.rs create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-07-where-clause/Cargo.toml create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-07-where-clause/src/lib.rs create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-08-only-one-reference-with-lifetime/Cargo.lock create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-08-only-one-reference-with-lifetime/Cargo.toml create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-08-only-one-reference-with-lifetime/src/main.rs create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-09-unrelated-lifetime/Cargo.lock create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-09-unrelated-lifetime/Cargo.toml create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-09-unrelated-lifetime/output.txt create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-09-unrelated-lifetime/src/main.rs create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-10-lifetimes-on-methods/Cargo.lock create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-10-lifetimes-on-methods/Cargo.toml create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-10-lifetimes-on-methods/src/main.rs create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-11-generics-traits-and-lifetimes/Cargo.lock create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-11-generics-traits-and-lifetimes/Cargo.toml create mode 100644 src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-11-generics-traits-and-lifetimes/src/main.rs create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/listing-11-01/Cargo.lock create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/listing-11-01/Cargo.toml create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/listing-11-01/output.txt create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/listing-11-01/src/lib.rs create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/listing-11-03/Cargo.lock create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/listing-11-03/Cargo.toml create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/listing-11-03/output.txt create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/listing-11-03/src/lib.rs create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/listing-11-05/Cargo.lock create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/listing-11-05/Cargo.toml create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/listing-11-05/src/lib.rs create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/listing-11-06/Cargo.lock create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/listing-11-06/Cargo.toml create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/listing-11-06/output.txt create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/listing-11-06/src/lib.rs create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/listing-11-07/Cargo.lock create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/listing-11-07/Cargo.toml create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/listing-11-07/output.txt create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/listing-11-07/src/lib.rs create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/listing-11-08/Cargo.lock create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/listing-11-08/Cargo.toml create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/listing-11-08/output.txt create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/listing-11-08/src/lib.rs create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/listing-11-09/Cargo.lock create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/listing-11-09/Cargo.toml create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/listing-11-09/src/lib.rs create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/listing-11-10/Cargo.lock create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/listing-11-10/Cargo.toml create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/listing-11-10/output.txt create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/listing-11-10/src/lib.rs create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/listing-11-11/Cargo.lock create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/listing-11-11/Cargo.toml create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/listing-11-11/output.txt create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/listing-11-11/src/lib.rs create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/listing-11-12/Cargo.lock create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/listing-11-12/Cargo.toml create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/listing-11-12/src/lib.rs create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/listing-11-13/Cargo.lock create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/listing-11-13/Cargo.toml create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/listing-11-13/output.txt create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/listing-11-13/src/lib.rs create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/listing-11-13/tests/integration_test.rs create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/no-listing-01-changing-test-name/Cargo.lock create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/no-listing-01-changing-test-name/Cargo.toml create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/no-listing-01-changing-test-name/output.txt create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/no-listing-01-changing-test-name/src/lib.rs create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/no-listing-02-adding-another-rectangle-test/Cargo.lock create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/no-listing-02-adding-another-rectangle-test/Cargo.toml create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/no-listing-02-adding-another-rectangle-test/output.txt create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/no-listing-02-adding-another-rectangle-test/src/lib.rs create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/no-listing-03-introducing-a-bug/Cargo.lock create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/no-listing-03-introducing-a-bug/Cargo.toml create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/no-listing-03-introducing-a-bug/output.txt create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/no-listing-03-introducing-a-bug/src/lib.rs create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/no-listing-04-bug-in-add-two/Cargo.lock create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/no-listing-04-bug-in-add-two/Cargo.toml create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/no-listing-04-bug-in-add-two/output.txt create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/no-listing-04-bug-in-add-two/src/lib.rs create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/no-listing-05-greeter/Cargo.lock create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/no-listing-05-greeter/Cargo.toml create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/no-listing-05-greeter/src/lib.rs create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/no-listing-06-greeter-with-bug/Cargo.lock create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/no-listing-06-greeter-with-bug/Cargo.toml create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/no-listing-06-greeter-with-bug/output.txt create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/no-listing-06-greeter-with-bug/src/lib.rs create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/no-listing-07-custom-failure-message/Cargo.lock create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/no-listing-07-custom-failure-message/Cargo.toml create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/no-listing-07-custom-failure-message/output.txt create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/no-listing-07-custom-failure-message/src/lib.rs create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/no-listing-08-guess-with-bug/Cargo.lock create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/no-listing-08-guess-with-bug/Cargo.toml create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/no-listing-08-guess-with-bug/output.txt create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/no-listing-08-guess-with-bug/src/lib.rs create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/no-listing-09-guess-with-panic-msg-bug/Cargo.lock create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/no-listing-09-guess-with-panic-msg-bug/Cargo.toml create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/no-listing-09-guess-with-panic-msg-bug/output.txt create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/no-listing-09-guess-with-panic-msg-bug/src/lib.rs create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/no-listing-10-result-in-tests/Cargo.lock create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/no-listing-10-result-in-tests/Cargo.toml create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/no-listing-10-result-in-tests/src/lib.rs create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/no-listing-11-ignore-a-test/Cargo.lock create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/no-listing-11-ignore-a-test/Cargo.toml create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/no-listing-11-ignore-a-test/output.txt create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/no-listing-11-ignore-a-test/src/lib.rs create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/no-listing-12-shared-test-code-problem/Cargo.lock create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/no-listing-12-shared-test-code-problem/Cargo.toml create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/no-listing-12-shared-test-code-problem/output.txt create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/no-listing-12-shared-test-code-problem/src/lib.rs create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/no-listing-12-shared-test-code-problem/tests/common.rs create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/no-listing-12-shared-test-code-problem/tests/integration_test.rs create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/no-listing-13-fix-shared-test-code-problem/Cargo.lock create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/no-listing-13-fix-shared-test-code-problem/Cargo.toml create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/no-listing-13-fix-shared-test-code-problem/src/lib.rs create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/no-listing-13-fix-shared-test-code-problem/tests/common/mod.rs create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/no-listing-13-fix-shared-test-code-problem/tests/integration_test.rs create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/output-only-01-show-output/Cargo.lock create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/output-only-01-show-output/Cargo.toml create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/output-only-01-show-output/output.txt create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/output-only-01-show-output/src/lib.rs create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/output-only-02-single-test/Cargo.lock create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/output-only-02-single-test/Cargo.toml create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/output-only-02-single-test/output.txt create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/output-only-02-single-test/src/lib.rs create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/output-only-03-multiple-tests/Cargo.lock create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/output-only-03-multiple-tests/Cargo.toml create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/output-only-03-multiple-tests/output.txt create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/output-only-03-multiple-tests/src/lib.rs create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/output-only-04-running-ignored/Cargo.lock create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/output-only-04-running-ignored/Cargo.toml create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/output-only-04-running-ignored/output.txt create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/output-only-04-running-ignored/src/lib.rs create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/output-only-05-single-integration/Cargo.lock create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/output-only-05-single-integration/Cargo.toml create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/output-only-05-single-integration/output.txt create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/output-only-05-single-integration/src/lib.rs create mode 100644 src/doc/book/listings/ch11-writing-automated-tests/output-only-05-single-integration/tests/integration_test.rs create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-01/Cargo.lock create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-01/Cargo.toml create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-01/output.txt create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-01/src/main.rs create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-02/Cargo.lock create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-02/Cargo.toml create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-02/output.txt create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-02/src/main.rs create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-03/Cargo.lock create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-03/Cargo.toml create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-03/poem.txt create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-03/src/main.rs create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-04/Cargo.lock create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-04/Cargo.toml create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-04/output.txt create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-04/poem.txt create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-04/src/main.rs create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-05/Cargo.lock create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-05/Cargo.toml create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-05/poem.txt create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-05/src/main.rs create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-06/Cargo.lock create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-06/Cargo.toml create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-06/poem.txt create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-06/src/main.rs create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-07/Cargo.lock create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-07/Cargo.toml create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-07/output.txt create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-07/poem.txt create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-07/src/main.rs create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-08/Cargo.lock create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-08/Cargo.toml create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-08/output.txt create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-08/poem.txt create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-08/src/main.rs create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-09/Cargo.lock create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-09/Cargo.toml create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-09/poem.txt create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-09/src/main.rs create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-10/Cargo.lock create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-10/Cargo.toml create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-10/output.txt create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-10/poem.txt create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-10/src/main.rs create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-11/Cargo.lock create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-11/Cargo.toml create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-11/poem.txt create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-11/src/main.rs create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-12/Cargo.lock create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-12/Cargo.toml create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-12/output.txt create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-12/poem.txt create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-12/src/main.rs create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-13/Cargo.lock create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-13/Cargo.toml create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-13/poem.txt create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-13/src/lib.rs create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-13/src/main.rs create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-14/Cargo.lock create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-14/Cargo.toml create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-14/poem.txt create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-14/src/lib.rs create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-14/src/main.rs create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-15/Cargo.lock create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-15/Cargo.toml create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-15/poem.txt create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-15/src/lib.rs create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-15/src/main.rs create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-16/Cargo.lock create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-16/Cargo.toml create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-16/output.txt create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-16/poem.txt create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-16/src/lib.rs create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-16/src/main.rs create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-17/Cargo.lock create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-17/Cargo.toml create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-17/poem.txt create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-17/src/lib.rs create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-17/src/main.rs create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-18/Cargo.lock create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-18/Cargo.toml create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-18/poem.txt create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-18/src/lib.rs create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-18/src/main.rs create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-19/Cargo.lock create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-19/Cargo.toml create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-19/output.txt create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-19/poem.txt create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-19/src/lib.rs create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-19/src/main.rs create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-20/Cargo.lock create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-20/Cargo.toml create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-20/poem.txt create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-20/src/lib.rs create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-20/src/main.rs create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-21/Cargo.lock create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-21/Cargo.toml create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-21/output.txt create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-21/poem.txt create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-21/src/lib.rs create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-21/src/main.rs create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-22/Cargo.lock create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-22/Cargo.toml create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-22/poem.txt create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-22/src/lib.rs create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-22/src/main.rs create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-23/Cargo.lock create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-23/Cargo.toml create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-23/output.txt create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-23/poem.txt create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-23/src/lib.rs create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-23/src/main.rs create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-24/Cargo.lock create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-24/Cargo.toml create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-24/poem.txt create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-24/src/lib.rs create mode 100644 src/doc/book/listings/ch12-an-io-project/listing-12-24/src/main.rs create mode 100644 src/doc/book/listings/ch12-an-io-project/no-listing-01-handling-errors-in-main/Cargo.lock create mode 100644 src/doc/book/listings/ch12-an-io-project/no-listing-01-handling-errors-in-main/Cargo.toml create mode 100644 src/doc/book/listings/ch12-an-io-project/no-listing-01-handling-errors-in-main/poem.txt create mode 100644 src/doc/book/listings/ch12-an-io-project/no-listing-01-handling-errors-in-main/src/main.rs create mode 100644 src/doc/book/listings/ch12-an-io-project/no-listing-02-using-search-in-run/Cargo.lock create mode 100644 src/doc/book/listings/ch12-an-io-project/no-listing-02-using-search-in-run/Cargo.toml create mode 100644 src/doc/book/listings/ch12-an-io-project/no-listing-02-using-search-in-run/output.txt create mode 100644 src/doc/book/listings/ch12-an-io-project/no-listing-02-using-search-in-run/poem.txt create mode 100644 src/doc/book/listings/ch12-an-io-project/no-listing-02-using-search-in-run/src/lib.rs create mode 100644 src/doc/book/listings/ch12-an-io-project/no-listing-02-using-search-in-run/src/main.rs create mode 100644 src/doc/book/listings/ch12-an-io-project/output-only-01-with-args/Cargo.lock create mode 100644 src/doc/book/listings/ch12-an-io-project/output-only-01-with-args/Cargo.toml create mode 100644 src/doc/book/listings/ch12-an-io-project/output-only-01-with-args/output.txt create mode 100644 src/doc/book/listings/ch12-an-io-project/output-only-01-with-args/src/main.rs create mode 100644 src/doc/book/listings/ch12-an-io-project/output-only-02-missing-lifetimes/Cargo.lock create mode 100644 src/doc/book/listings/ch12-an-io-project/output-only-02-missing-lifetimes/Cargo.toml create mode 100644 src/doc/book/listings/ch12-an-io-project/output-only-02-missing-lifetimes/output.txt create mode 100644 src/doc/book/listings/ch12-an-io-project/output-only-02-missing-lifetimes/poem.txt create mode 100644 src/doc/book/listings/ch12-an-io-project/output-only-02-missing-lifetimes/src/lib.rs create mode 100644 src/doc/book/listings/ch12-an-io-project/output-only-02-missing-lifetimes/src/main.rs create mode 100644 src/doc/book/listings/ch12-an-io-project/output-only-03-multiple-matches/Cargo.lock create mode 100644 src/doc/book/listings/ch12-an-io-project/output-only-03-multiple-matches/Cargo.toml create mode 100644 src/doc/book/listings/ch12-an-io-project/output-only-03-multiple-matches/output.txt create mode 100644 src/doc/book/listings/ch12-an-io-project/output-only-03-multiple-matches/poem.txt create mode 100644 src/doc/book/listings/ch12-an-io-project/output-only-03-multiple-matches/src/lib.rs create mode 100644 src/doc/book/listings/ch12-an-io-project/output-only-03-multiple-matches/src/main.rs create mode 100644 src/doc/book/listings/ch12-an-io-project/output-only-04-no-matches/Cargo.lock create mode 100644 src/doc/book/listings/ch12-an-io-project/output-only-04-no-matches/Cargo.toml create mode 100644 src/doc/book/listings/ch12-an-io-project/output-only-04-no-matches/output.txt create mode 100644 src/doc/book/listings/ch12-an-io-project/output-only-04-no-matches/poem.txt create mode 100644 src/doc/book/listings/ch12-an-io-project/output-only-04-no-matches/src/lib.rs create mode 100644 src/doc/book/listings/ch12-an-io-project/output-only-04-no-matches/src/main.rs create mode 100644 src/doc/book/listings/ch13-functional-features/listing-12-23-reproduced/Cargo.lock create mode 100644 src/doc/book/listings/ch13-functional-features/listing-12-23-reproduced/Cargo.toml create mode 100644 src/doc/book/listings/ch13-functional-features/listing-12-23-reproduced/poem.txt create mode 100644 src/doc/book/listings/ch13-functional-features/listing-12-23-reproduced/src/lib.rs create mode 100644 src/doc/book/listings/ch13-functional-features/listing-12-23-reproduced/src/main.rs create mode 100644 src/doc/book/listings/ch13-functional-features/listing-12-24-reproduced/Cargo.lock create mode 100644 src/doc/book/listings/ch13-functional-features/listing-12-24-reproduced/Cargo.toml create mode 100644 src/doc/book/listings/ch13-functional-features/listing-12-24-reproduced/poem.txt create mode 100644 src/doc/book/listings/ch13-functional-features/listing-12-24-reproduced/src/lib.rs create mode 100644 src/doc/book/listings/ch13-functional-features/listing-12-24-reproduced/src/main.rs create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-01/Cargo.lock create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-01/Cargo.toml create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-01/output.txt create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-01/src/main.rs create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-02/Cargo.lock create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-02/Cargo.toml create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-02/src/main.rs create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-03/Cargo.lock create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-03/Cargo.toml create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-03/output.txt create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-03/src/main.rs create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-04/Cargo.lock create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-04/Cargo.toml create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-04/output.txt create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-04/src/main.rs create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-05/Cargo.lock create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-05/Cargo.toml create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-05/output.txt create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-05/src/main.rs create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-06/Cargo.toml create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-06/src/main.rs create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-07/.rustfmt.toml create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-07/Cargo.toml create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-07/output.txt create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-07/src/main.rs create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-08/.rustfmt.toml create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-08/Cargo.toml create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-08/output.txt create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-08/src/main.rs create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-09/.rustfmt.toml create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-09/Cargo.lock create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-09/Cargo.toml create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-09/src/main.rs create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-10/Cargo.lock create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-10/Cargo.toml create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-10/src/main.rs create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-11/Cargo.lock create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-11/Cargo.toml create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-11/src/main.rs create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-12/Cargo.lock create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-12/Cargo.toml create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-12/src/lib.rs create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-13/Cargo.lock create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-13/Cargo.toml create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-13/src/lib.rs create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-14/Cargo.lock create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-14/Cargo.toml create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-14/output.txt create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-14/src/main.rs create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-15/Cargo.lock create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-15/Cargo.toml create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-15/src/main.rs create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-16/Cargo.lock create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-16/Cargo.toml create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-16/src/lib.rs create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-18/Cargo.lock create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-18/Cargo.toml create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-18/poem.txt create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-18/src/lib.rs create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-18/src/main.rs create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-19/Cargo.lock create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-19/Cargo.toml create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-19/poem.txt create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-19/src/lib.rs create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-19/src/main.rs create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-20/Cargo.lock create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-20/Cargo.toml create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-20/poem.txt create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-20/src/lib.rs create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-20/src/main.rs create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-22/Cargo.lock create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-22/Cargo.toml create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-22/poem.txt create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-22/src/lib.rs create mode 100644 src/doc/book/listings/ch13-functional-features/listing-13-22/src/main.rs create mode 100644 src/doc/book/listings/ch14-more-about-cargo/listing-14-01/Cargo.lock create mode 100644 src/doc/book/listings/ch14-more-about-cargo/listing-14-01/Cargo.toml create mode 100644 src/doc/book/listings/ch14-more-about-cargo/listing-14-01/src/lib.rs create mode 100644 src/doc/book/listings/ch14-more-about-cargo/listing-14-02/Cargo.lock create mode 100644 src/doc/book/listings/ch14-more-about-cargo/listing-14-02/Cargo.toml create mode 100644 src/doc/book/listings/ch14-more-about-cargo/listing-14-02/src/lib.rs create mode 100644 src/doc/book/listings/ch14-more-about-cargo/listing-14-03/Cargo.lock create mode 100644 src/doc/book/listings/ch14-more-about-cargo/listing-14-03/Cargo.toml create mode 100644 src/doc/book/listings/ch14-more-about-cargo/listing-14-03/src/lib.rs create mode 100644 src/doc/book/listings/ch14-more-about-cargo/listing-14-04/Cargo.lock create mode 100644 src/doc/book/listings/ch14-more-about-cargo/listing-14-04/Cargo.toml create mode 100644 src/doc/book/listings/ch14-more-about-cargo/listing-14-04/src/lib.rs create mode 100644 src/doc/book/listings/ch14-more-about-cargo/listing-14-04/src/main.rs create mode 100644 src/doc/book/listings/ch14-more-about-cargo/listing-14-05/Cargo.lock create mode 100644 src/doc/book/listings/ch14-more-about-cargo/listing-14-05/Cargo.toml create mode 100644 src/doc/book/listings/ch14-more-about-cargo/listing-14-05/src/lib.rs create mode 100644 src/doc/book/listings/ch14-more-about-cargo/listing-14-06/Cargo.lock create mode 100644 src/doc/book/listings/ch14-more-about-cargo/listing-14-06/Cargo.toml create mode 100644 src/doc/book/listings/ch14-more-about-cargo/listing-14-06/src/lib.rs create mode 100644 src/doc/book/listings/ch14-more-about-cargo/listing-14-06/src/main.rs create mode 100644 src/doc/book/listings/ch14-more-about-cargo/listing-14-07/add/Cargo.lock create mode 100644 src/doc/book/listings/ch14-more-about-cargo/listing-14-07/add/Cargo.toml create mode 100644 src/doc/book/listings/ch14-more-about-cargo/listing-14-07/add/add_one/Cargo.toml create mode 100644 src/doc/book/listings/ch14-more-about-cargo/listing-14-07/add/add_one/src/lib.rs create mode 100644 src/doc/book/listings/ch14-more-about-cargo/listing-14-07/add/adder/Cargo.toml create mode 100644 src/doc/book/listings/ch14-more-about-cargo/listing-14-07/add/adder/src/main.rs create mode 100644 src/doc/book/listings/ch14-more-about-cargo/no-listing-01-workspace-with-adder-crate/add/Cargo.lock create mode 100644 src/doc/book/listings/ch14-more-about-cargo/no-listing-01-workspace-with-adder-crate/add/Cargo.toml create mode 100644 src/doc/book/listings/ch14-more-about-cargo/no-listing-01-workspace-with-adder-crate/add/adder/Cargo.toml create mode 100644 src/doc/book/listings/ch14-more-about-cargo/no-listing-01-workspace-with-adder-crate/add/adder/src/main.rs create mode 100644 src/doc/book/listings/ch14-more-about-cargo/no-listing-02-workspace-with-two-crates/add/Cargo.lock create mode 100644 src/doc/book/listings/ch14-more-about-cargo/no-listing-02-workspace-with-two-crates/add/Cargo.toml create mode 100644 src/doc/book/listings/ch14-more-about-cargo/no-listing-02-workspace-with-two-crates/add/add_one/Cargo.toml create mode 100644 src/doc/book/listings/ch14-more-about-cargo/no-listing-02-workspace-with-two-crates/add/add_one/src/lib.rs create mode 100644 src/doc/book/listings/ch14-more-about-cargo/no-listing-02-workspace-with-two-crates/add/adder/Cargo.toml create mode 100644 src/doc/book/listings/ch14-more-about-cargo/no-listing-02-workspace-with-two-crates/add/adder/src/main.rs create mode 100644 src/doc/book/listings/ch14-more-about-cargo/no-listing-03-workspace-with-external-dependency/add/Cargo.lock create mode 100644 src/doc/book/listings/ch14-more-about-cargo/no-listing-03-workspace-with-external-dependency/add/Cargo.toml create mode 100644 src/doc/book/listings/ch14-more-about-cargo/no-listing-03-workspace-with-external-dependency/add/add_one/Cargo.toml create mode 100644 src/doc/book/listings/ch14-more-about-cargo/no-listing-03-workspace-with-external-dependency/add/add_one/src/lib.rs create mode 100644 src/doc/book/listings/ch14-more-about-cargo/no-listing-03-workspace-with-external-dependency/add/adder/Cargo.toml create mode 100644 src/doc/book/listings/ch14-more-about-cargo/no-listing-03-workspace-with-external-dependency/add/adder/src/main.rs create mode 100644 src/doc/book/listings/ch14-more-about-cargo/no-listing-04-workspace-with-tests/add/Cargo.lock create mode 100644 src/doc/book/listings/ch14-more-about-cargo/no-listing-04-workspace-with-tests/add/Cargo.toml create mode 100644 src/doc/book/listings/ch14-more-about-cargo/no-listing-04-workspace-with-tests/add/add_one/Cargo.toml create mode 100644 src/doc/book/listings/ch14-more-about-cargo/no-listing-04-workspace-with-tests/add/add_one/src/lib.rs create mode 100644 src/doc/book/listings/ch14-more-about-cargo/no-listing-04-workspace-with-tests/add/adder/Cargo.toml create mode 100644 src/doc/book/listings/ch14-more-about-cargo/no-listing-04-workspace-with-tests/add/adder/src/main.rs create mode 100644 src/doc/book/listings/ch14-more-about-cargo/output-only-01-adder-crate/add/Cargo.lock create mode 100644 src/doc/book/listings/ch14-more-about-cargo/output-only-01-adder-crate/add/Cargo.toml create mode 100644 src/doc/book/listings/ch14-more-about-cargo/output-only-01-adder-crate/add/rustfmt-ignore create mode 100644 src/doc/book/listings/ch14-more-about-cargo/output-only-02-add-one/add/Cargo.lock create mode 100644 src/doc/book/listings/ch14-more-about-cargo/output-only-02-add-one/add/Cargo.toml create mode 100644 src/doc/book/listings/ch14-more-about-cargo/output-only-02-add-one/add/add_one/Cargo.toml create mode 100644 src/doc/book/listings/ch14-more-about-cargo/output-only-02-add-one/add/add_one/src/lib.rs create mode 100644 src/doc/book/listings/ch14-more-about-cargo/output-only-02-add-one/add/adder/Cargo.toml create mode 100644 src/doc/book/listings/ch14-more-about-cargo/output-only-02-add-one/add/adder/src/main.rs create mode 100644 src/doc/book/listings/ch14-more-about-cargo/output-only-03-use-rand/add/Cargo.lock create mode 100644 src/doc/book/listings/ch14-more-about-cargo/output-only-03-use-rand/add/Cargo.toml create mode 100644 src/doc/book/listings/ch14-more-about-cargo/output-only-03-use-rand/add/add_one/Cargo.toml create mode 100644 src/doc/book/listings/ch14-more-about-cargo/output-only-03-use-rand/add/add_one/src/lib.rs create mode 100644 src/doc/book/listings/ch14-more-about-cargo/output-only-03-use-rand/add/adder/Cargo.toml create mode 100644 src/doc/book/listings/ch14-more-about-cargo/output-only-03-use-rand/add/adder/src/main.rs create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-01/Cargo.lock create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-01/Cargo.toml create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-01/src/main.rs create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-02/Cargo.lock create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-02/Cargo.toml create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-02/src/main.rs create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-03/Cargo.lock create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-03/Cargo.toml create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-03/output.txt create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-03/src/main.rs create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-05/Cargo.lock create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-05/Cargo.toml create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-05/src/main.rs create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-06/Cargo.lock create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-06/Cargo.toml create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-06/src/main.rs create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-07/Cargo.lock create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-07/Cargo.toml create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-07/src/main.rs create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-08/Cargo.lock create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-08/Cargo.toml create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-08/src/main.rs create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-09/Cargo.lock create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-09/Cargo.toml create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-09/output.txt create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-09/src/main.rs create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-10/Cargo.lock create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-10/Cargo.toml create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-10/src/main.rs create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-11/Cargo.lock create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-11/Cargo.toml create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-11/src/main.rs create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-12/Cargo.lock create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-12/Cargo.toml create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-12/src/main.rs create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-13/Cargo.lock create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-13/Cargo.toml create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-13/src/main.rs create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-14/Cargo.lock create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-14/Cargo.toml create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-14/output.txt create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-14/src/main.rs create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-15/Cargo.lock create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-15/Cargo.toml create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-15/output.txt create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-15/src/main.rs create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-16/Cargo.lock create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-16/Cargo.toml create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-16/output.txt create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-16/src/main.rs create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-17/Cargo.lock create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-17/Cargo.toml create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-17/output.txt create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-17/src/main.rs create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-18/Cargo.lock create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-18/Cargo.toml create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-18/src/main.rs create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-19/Cargo.lock create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-19/Cargo.toml create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-19/output.txt create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-19/src/main.rs create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-20/Cargo.lock create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-20/Cargo.toml create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-20/src/lib.rs create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-21/Cargo.lock create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-21/Cargo.toml create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-21/output.txt create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-21/src/lib.rs create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-22/Cargo.lock create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-22/Cargo.toml create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-22/src/lib.rs create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-23/Cargo.lock create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-23/Cargo.toml create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-23/output.txt create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-23/src/lib.rs create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-24/Cargo.lock create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-24/Cargo.toml create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-24/output.txt create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-24/src/main.rs create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-25/Cargo.lock create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-25/Cargo.toml create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-25/src/main.rs create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-26/Cargo.lock create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-26/Cargo.toml create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-26/output.txt create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-26/src/main.rs create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-27/Cargo.lock create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-27/Cargo.toml create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-27/src/main.rs create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-28/Cargo.lock create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-28/Cargo.toml create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-28/src/main.rs create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-29/Cargo.lock create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-29/Cargo.toml create mode 100644 src/doc/book/listings/ch15-smart-pointers/listing-15-29/src/main.rs create mode 100644 src/doc/book/listings/ch15-smart-pointers/no-listing-01-cant-borrow-immutable-as-mutable/Cargo.lock create mode 100644 src/doc/book/listings/ch15-smart-pointers/no-listing-01-cant-borrow-immutable-as-mutable/Cargo.toml create mode 100644 src/doc/book/listings/ch15-smart-pointers/no-listing-01-cant-borrow-immutable-as-mutable/output.txt create mode 100644 src/doc/book/listings/ch15-smart-pointers/no-listing-01-cant-borrow-immutable-as-mutable/src/main.rs create mode 100644 src/doc/book/listings/ch15-smart-pointers/output-only-01-comparing-to-reference/Cargo.lock create mode 100644 src/doc/book/listings/ch15-smart-pointers/output-only-01-comparing-to-reference/Cargo.toml create mode 100644 src/doc/book/listings/ch15-smart-pointers/output-only-01-comparing-to-reference/output.txt create mode 100644 src/doc/book/listings/ch15-smart-pointers/output-only-01-comparing-to-reference/src/main.rs create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/listing-16-01/Cargo.lock create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/listing-16-01/Cargo.toml create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/listing-16-01/src/main.rs create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/listing-16-02/Cargo.lock create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/listing-16-02/Cargo.toml create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/listing-16-02/src/main.rs create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/listing-16-03/Cargo.lock create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/listing-16-03/Cargo.toml create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/listing-16-03/output.txt create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/listing-16-03/src/main.rs create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/listing-16-04/Cargo.lock create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/listing-16-04/Cargo.toml create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/listing-16-04/src/main.rs create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/listing-16-05/Cargo.lock create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/listing-16-05/Cargo.toml create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/listing-16-05/src/main.rs create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/listing-16-06/Cargo.lock create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/listing-16-06/Cargo.toml create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/listing-16-06/src/main.rs create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/listing-16-07/Cargo.lock create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/listing-16-07/Cargo.toml create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/listing-16-07/src/main.rs create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/listing-16-08/Cargo.lock create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/listing-16-08/Cargo.toml create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/listing-16-08/src/main.rs create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/listing-16-09/Cargo.lock create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/listing-16-09/Cargo.toml create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/listing-16-09/output.txt create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/listing-16-09/src/main.rs create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/listing-16-10/Cargo.lock create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/listing-16-10/Cargo.toml create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/listing-16-10/src/main.rs create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/listing-16-11/Cargo.lock create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/listing-16-11/Cargo.toml create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/listing-16-11/src/main.rs create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/listing-16-12/Cargo.lock create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/listing-16-12/Cargo.toml create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/listing-16-12/src/main.rs create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/listing-16-13/Cargo.lock create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/listing-16-13/Cargo.toml create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/listing-16-13/output.txt create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/listing-16-13/src/main.rs create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/listing-16-14/Cargo.lock create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/listing-16-14/Cargo.toml create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/listing-16-14/output.txt create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/listing-16-14/src/main.rs create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/listing-16-15/Cargo.lock create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/listing-16-15/Cargo.toml create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/listing-16-15/src/main.rs create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/no-listing-01-join-too-early/Cargo.lock create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/no-listing-01-join-too-early/Cargo.toml create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/no-listing-01-join-too-early/src/main.rs create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/no-listing-02-no-loop-to-understand-error/Cargo.lock create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/no-listing-02-no-loop-to-understand-error/Cargo.toml create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/no-listing-02-no-loop-to-understand-error/src/main.rs create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/output-only-01-move-drop/Cargo.lock create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/output-only-01-move-drop/Cargo.toml create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/output-only-01-move-drop/output.txt create mode 100644 src/doc/book/listings/ch16-fearless-concurrency/output-only-01-move-drop/src/main.rs create mode 100644 src/doc/book/listings/ch17-oop/listing-17-01/Cargo.lock create mode 100644 src/doc/book/listings/ch17-oop/listing-17-01/Cargo.toml create mode 100644 src/doc/book/listings/ch17-oop/listing-17-01/src/lib.rs create mode 100644 src/doc/book/listings/ch17-oop/listing-17-02/Cargo.lock create mode 100644 src/doc/book/listings/ch17-oop/listing-17-02/Cargo.toml create mode 100644 src/doc/book/listings/ch17-oop/listing-17-02/src/lib.rs create mode 100644 src/doc/book/listings/ch17-oop/listing-17-03/Cargo.lock create mode 100644 src/doc/book/listings/ch17-oop/listing-17-03/Cargo.toml create mode 100644 src/doc/book/listings/ch17-oop/listing-17-03/src/lib.rs create mode 100644 src/doc/book/listings/ch17-oop/listing-17-04/Cargo.lock create mode 100644 src/doc/book/listings/ch17-oop/listing-17-04/Cargo.toml create mode 100644 src/doc/book/listings/ch17-oop/listing-17-04/src/lib.rs create mode 100644 src/doc/book/listings/ch17-oop/listing-17-05/Cargo.lock create mode 100644 src/doc/book/listings/ch17-oop/listing-17-05/Cargo.toml create mode 100644 src/doc/book/listings/ch17-oop/listing-17-05/src/lib.rs create mode 100644 src/doc/book/listings/ch17-oop/listing-17-06/Cargo.lock create mode 100644 src/doc/book/listings/ch17-oop/listing-17-06/Cargo.toml create mode 100644 src/doc/book/listings/ch17-oop/listing-17-06/src/lib.rs create mode 100644 src/doc/book/listings/ch17-oop/listing-17-07/Cargo.lock create mode 100644 src/doc/book/listings/ch17-oop/listing-17-07/Cargo.toml create mode 100644 src/doc/book/listings/ch17-oop/listing-17-07/src/lib.rs create mode 100644 src/doc/book/listings/ch17-oop/listing-17-08/Cargo.lock create mode 100644 src/doc/book/listings/ch17-oop/listing-17-08/Cargo.toml create mode 100644 src/doc/book/listings/ch17-oop/listing-17-08/src/lib.rs create mode 100644 src/doc/book/listings/ch17-oop/listing-17-08/src/main.rs create mode 100644 src/doc/book/listings/ch17-oop/listing-17-09/Cargo.lock create mode 100644 src/doc/book/listings/ch17-oop/listing-17-09/Cargo.toml create mode 100644 src/doc/book/listings/ch17-oop/listing-17-09/src/lib.rs create mode 100644 src/doc/book/listings/ch17-oop/listing-17-09/src/main.rs create mode 100644 src/doc/book/listings/ch17-oop/listing-17-10/Cargo.lock create mode 100644 src/doc/book/listings/ch17-oop/listing-17-10/Cargo.toml create mode 100644 src/doc/book/listings/ch17-oop/listing-17-10/output.txt create mode 100644 src/doc/book/listings/ch17-oop/listing-17-10/src/lib.rs create mode 100644 src/doc/book/listings/ch17-oop/listing-17-10/src/main.rs create mode 100644 src/doc/book/listings/ch17-oop/listing-17-11/Cargo.lock create mode 100644 src/doc/book/listings/ch17-oop/listing-17-11/Cargo.toml create mode 100644 src/doc/book/listings/ch17-oop/listing-17-11/src/main.rs create mode 100644 src/doc/book/listings/ch17-oop/listing-17-12/Cargo.lock create mode 100644 src/doc/book/listings/ch17-oop/listing-17-12/Cargo.toml create mode 100644 src/doc/book/listings/ch17-oop/listing-17-12/src/lib.rs create mode 100644 src/doc/book/listings/ch17-oop/listing-17-12/src/main.rs create mode 100644 src/doc/book/listings/ch17-oop/listing-17-13/Cargo.lock create mode 100644 src/doc/book/listings/ch17-oop/listing-17-13/Cargo.toml create mode 100644 src/doc/book/listings/ch17-oop/listing-17-13/src/lib.rs create mode 100644 src/doc/book/listings/ch17-oop/listing-17-13/src/main.rs create mode 100644 src/doc/book/listings/ch17-oop/listing-17-14/Cargo.lock create mode 100644 src/doc/book/listings/ch17-oop/listing-17-14/Cargo.toml create mode 100644 src/doc/book/listings/ch17-oop/listing-17-14/src/lib.rs create mode 100644 src/doc/book/listings/ch17-oop/listing-17-14/src/main.rs create mode 100644 src/doc/book/listings/ch17-oop/listing-17-15/Cargo.lock create mode 100644 src/doc/book/listings/ch17-oop/listing-17-15/Cargo.toml create mode 100644 src/doc/book/listings/ch17-oop/listing-17-15/src/lib.rs create mode 100644 src/doc/book/listings/ch17-oop/listing-17-15/src/main.rs create mode 100644 src/doc/book/listings/ch17-oop/listing-17-16/Cargo.lock create mode 100644 src/doc/book/listings/ch17-oop/listing-17-16/Cargo.toml create mode 100644 src/doc/book/listings/ch17-oop/listing-17-16/src/lib.rs create mode 100644 src/doc/book/listings/ch17-oop/listing-17-16/src/main.rs create mode 100644 src/doc/book/listings/ch17-oop/listing-17-17/Cargo.lock create mode 100644 src/doc/book/listings/ch17-oop/listing-17-17/Cargo.toml create mode 100644 src/doc/book/listings/ch17-oop/listing-17-17/src/lib.rs create mode 100644 src/doc/book/listings/ch17-oop/listing-17-17/src/main.rs create mode 100644 src/doc/book/listings/ch17-oop/listing-17-18/Cargo.lock create mode 100644 src/doc/book/listings/ch17-oop/listing-17-18/Cargo.toml create mode 100644 src/doc/book/listings/ch17-oop/listing-17-18/src/lib.rs create mode 100644 src/doc/book/listings/ch17-oop/listing-17-18/src/main.rs create mode 100644 src/doc/book/listings/ch17-oop/listing-17-19/Cargo.lock create mode 100644 src/doc/book/listings/ch17-oop/listing-17-19/Cargo.toml create mode 100644 src/doc/book/listings/ch17-oop/listing-17-19/src/lib.rs create mode 100644 src/doc/book/listings/ch17-oop/listing-17-20/Cargo.lock create mode 100644 src/doc/book/listings/ch17-oop/listing-17-20/Cargo.toml create mode 100644 src/doc/book/listings/ch17-oop/listing-17-20/src/lib.rs create mode 100644 src/doc/book/listings/ch17-oop/listing-17-21/Cargo.lock create mode 100644 src/doc/book/listings/ch17-oop/listing-17-21/Cargo.toml create mode 100644 src/doc/book/listings/ch17-oop/listing-17-21/src/lib.rs create mode 100644 src/doc/book/listings/ch17-oop/listing-17-21/src/main.rs create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-01/Cargo.lock create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-01/Cargo.toml create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-01/src/main.rs create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-02/Cargo.lock create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-02/Cargo.toml create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-02/src/main.rs create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-03/Cargo.lock create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-03/Cargo.toml create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-03/output.txt create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-03/src/main.rs create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-04/Cargo.lock create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-04/Cargo.toml create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-04/src/main.rs create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-05/Cargo.lock create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-05/Cargo.toml create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-05/output.txt create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-05/src/main.rs create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-06/Cargo.lock create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-06/Cargo.toml create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-06/src/main.rs create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-07/Cargo.lock create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-07/Cargo.toml create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-07/src/main.rs create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-08/Cargo.lock create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-08/Cargo.toml create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-08/output.txt create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-08/src/main.rs create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-09/Cargo.lock create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-09/Cargo.toml create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-09/src/main.rs create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-10/Cargo.lock create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-10/Cargo.toml create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-10/output.txt create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-10/src/main.rs create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-11/Cargo.lock create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-11/Cargo.toml create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-11/src/main.rs create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-12/Cargo.lock create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-12/Cargo.toml create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-12/src/main.rs create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-13/Cargo.lock create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-13/Cargo.toml create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-13/src/main.rs create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-14/Cargo.lock create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-14/Cargo.toml create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-14/src/main.rs create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-15/Cargo.lock create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-15/Cargo.toml create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-15/src/main.rs create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-16/Cargo.lock create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-16/Cargo.toml create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-16/src/main.rs create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-17/Cargo.lock create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-17/Cargo.toml create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-17/src/main.rs create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-18/Cargo.lock create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-18/Cargo.toml create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-18/src/main.rs create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-19/Cargo.lock create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-19/Cargo.toml create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-19/src/main.rs create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-20/Cargo.lock create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-20/Cargo.toml create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-20/src/main.rs create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-21/Cargo.lock create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-21/Cargo.toml create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-21/src/main.rs create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-22/Cargo.lock create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-22/Cargo.toml create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-22/src/main.rs create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-23/Cargo.lock create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-23/Cargo.toml create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-23/src/main.rs create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-24/Cargo.lock create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-24/Cargo.toml create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-24/src/main.rs create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-25/Cargo.lock create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-25/Cargo.toml create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-25/output.txt create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-25/rustfmt-ignore create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-25/src/main.rs create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-26/Cargo.lock create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-26/Cargo.toml create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-26/src/main.rs create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-27/Cargo.lock create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-27/Cargo.toml create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-27/src/main.rs create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-28/Cargo.lock create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-28/Cargo.toml create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-28/src/main.rs create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-29/Cargo.lock create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-29/Cargo.toml create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/listing-18-29/src/main.rs create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/no-listing-01-literals/Cargo.lock create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/no-listing-01-literals/Cargo.toml create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/no-listing-01-literals/src/main.rs create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/no-listing-02-multiple-patterns/Cargo.lock create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/no-listing-02-multiple-patterns/Cargo.toml create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/no-listing-02-multiple-patterns/src/main.rs create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/no-listing-03-ranges/Cargo.lock create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/no-listing-03-ranges/Cargo.toml create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/no-listing-03-ranges/src/main.rs create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/no-listing-04-ranges-of-char/Cargo.lock create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/no-listing-04-ranges-of-char/Cargo.toml create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/no-listing-04-ranges-of-char/src/main.rs create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/no-listing-05-destructuring-structs-and-tuples/Cargo.lock create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/no-listing-05-destructuring-structs-and-tuples/Cargo.toml create mode 100644 src/doc/book/listings/ch18-patterns-and-matching/no-listing-05-destructuring-structs-and-tuples/src/main.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-01/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-01/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-01/src/main.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-02/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-02/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-02/src/main.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-03/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-03/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-03/src/main.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-04/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-04/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-04/src/main.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-05/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-05/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-05/output.txt create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-05/src/main.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-06/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-06/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-06/src/main.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-07/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-07/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-07/src/main.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-08/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-08/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-08/src/main.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-09/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-09/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-09/src/main.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-10/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-10/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-10/src/main.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-11/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-11/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-11/src/main.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-12/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-12/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-12/src/lib.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-13/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-13/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-13/src/lib.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-14/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-14/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-14/src/main.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-15/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-15/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-15/src/lib.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-16/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-16/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-16/src/main.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-17/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-17/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-17/src/main.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-18/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-18/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-18/output.txt create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-18/src/main.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-19/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-19/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-19/output.txt create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-19/src/main.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-20/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-20/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-20/output.txt create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-20/src/main.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-21/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-21/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-21/output.txt create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-21/src/main.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-22/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-22/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-22/src/main.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-23/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-23/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-23/src/main.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-24/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-24/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-24/src/main.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-25/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-25/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-25/src/main.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-27/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-27/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-27/src/main.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-28/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-28/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-28/src/lib.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-30/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-30/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-30/src/main.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-31/hello_macro/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-31/hello_macro/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-31/hello_macro/hello_macro_derive/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-31/hello_macro/hello_macro_derive/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-31/hello_macro/hello_macro_derive/src/lib.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-31/hello_macro/src/lib.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-31/hello_macro/src/main.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-33/hello_macro/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-33/hello_macro/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-33/hello_macro/hello_macro_derive/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-33/hello_macro/hello_macro_derive/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-33/hello_macro/hello_macro_derive/src/lib.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-33/hello_macro/src/lib.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/listing-19-33/hello_macro/src/main.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-01-unsafe-fn/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-01-unsafe-fn/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-01-unsafe-fn/src/main.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-02-impl-outlineprint-for-point/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-02-impl-outlineprint-for-point/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-02-impl-outlineprint-for-point/output.txt create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-02-impl-outlineprint-for-point/src/main.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-03-impl-display-for-point/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-03-impl-display-for-point/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-03-impl-display-for-point/src/main.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-04-kilometers-alias/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-04-kilometers-alias/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-04-kilometers-alias/src/main.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-05-write-trait/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-05-write-trait/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-05-write-trait/src/lib.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-06-result-alias/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-06-result-alias/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-06-result-alias/src/lib.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-07-never-type/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-07-never-type/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-07-never-type/src/lib.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-08-match-arms-different-types/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-08-match-arms-different-types/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-08-match-arms-different-types/src/main.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-09-unwrap-definition/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-09-unwrap-definition/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-09-unwrap-definition/src/lib.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-10-loop-returns-never/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-10-loop-returns-never/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-10-loop-returns-never/src/main.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-11-cant-create-str/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-11-cant-create-str/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-11-cant-create-str/src/main.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-12-generic-fn-definition/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-12-generic-fn-definition/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-12-generic-fn-definition/src/lib.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-13-generic-implicit-sized-bound/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-13-generic-implicit-sized-bound/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-13-generic-implicit-sized-bound/src/lib.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-14-generic-maybe-sized/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-14-generic-maybe-sized/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-14-generic-maybe-sized/src/lib.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-15-map-closure/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-15-map-closure/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-15-map-closure/src/main.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-16-map-function/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-16-map-function/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-16-map-function/src/main.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-17-map-initializer/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-17-map-initializer/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-17-map-initializer/src/main.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-18-returns-closure/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-18-returns-closure/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-18-returns-closure/output.txt create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-18-returns-closure/src/lib.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-19-returns-closure-trait-object/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-19-returns-closure-trait-object/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-19-returns-closure-trait-object/src/lib.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-20-impl-hellomacro-for-pancakes/hello_macro/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-20-impl-hellomacro-for-pancakes/hello_macro/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-20-impl-hellomacro-for-pancakes/hello_macro/src/lib.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-20-impl-hellomacro-for-pancakes/pancakes/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-20-impl-hellomacro-for-pancakes/pancakes/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-20-impl-hellomacro-for-pancakes/pancakes/src/main.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-21-pancakes/hello_macro/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-21-pancakes/hello_macro/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-21-pancakes/hello_macro/hello_macro_derive/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-21-pancakes/hello_macro/hello_macro_derive/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-21-pancakes/hello_macro/hello_macro_derive/src/lib.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-21-pancakes/hello_macro/src/lib.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-21-pancakes/hello_macro/src/main.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-21-pancakes/pancakes/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-21-pancakes/pancakes/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-21-pancakes/pancakes/src/main.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-22-iterator-on-counter/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-22-iterator-on-counter/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/no-listing-22-iterator-on-counter/src/lib.rs create mode 100644 src/doc/book/listings/ch19-advanced-features/output-only-01-missing-unsafe/Cargo.lock create mode 100644 src/doc/book/listings/ch19-advanced-features/output-only-01-missing-unsafe/Cargo.toml create mode 100644 src/doc/book/listings/ch19-advanced-features/output-only-01-missing-unsafe/output.txt create mode 100644 src/doc/book/listings/ch19-advanced-features/output-only-01-missing-unsafe/src/main.rs create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-01/Cargo.lock create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-01/Cargo.toml create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-01/src/main.rs create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-02/Cargo.lock create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-02/Cargo.toml create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-02/src/main.rs create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-03/Cargo.lock create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-03/Cargo.toml create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-03/src/main.rs create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-05/Cargo.lock create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-05/Cargo.toml create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-05/hello.html create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-05/src/main.rs create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-06/Cargo.lock create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-06/Cargo.toml create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-06/hello.html create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-06/src/main.rs create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-07/404.html create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-07/Cargo.lock create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-07/Cargo.toml create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-07/hello.html create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-07/src/main.rs create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-09/404.html create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-09/Cargo.lock create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-09/Cargo.toml create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-09/hello.html create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-09/src/main.rs create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-10/404.html create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-10/Cargo.lock create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-10/Cargo.toml create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-10/hello.html create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-10/src/main.rs create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-11/404.html create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-11/Cargo.lock create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-11/Cargo.toml create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-11/hello.html create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-11/src/main.rs create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-12/404.html create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-12/Cargo.lock create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-12/Cargo.toml create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-12/hello.html create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-12/output.txt create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-12/src/main.rs create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-13/404.html create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-13/Cargo.lock create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-13/Cargo.toml create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-13/hello.html create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-13/src/lib.rs create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-13/src/main.rs create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-14/404.html create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-14/Cargo.lock create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-14/Cargo.toml create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-14/hello.html create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-14/src/lib.rs create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-14/src/main.rs create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-15/404.html create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-15/Cargo.lock create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-15/Cargo.toml create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-15/hello.html create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-15/src/lib.rs create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-15/src/main.rs create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-16/404.html create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-16/Cargo.lock create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-16/Cargo.toml create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-16/hello.html create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-16/src/lib.rs create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-16/src/main.rs create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-17/404.html create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-17/Cargo.lock create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-17/Cargo.toml create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-17/hello.html create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-17/output.txt create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-17/src/lib.rs create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-17/src/main.rs create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-18/404.html create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-18/Cargo.lock create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-18/Cargo.toml create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-18/hello.html create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-18/src/lib.rs create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-18/src/main.rs create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-19/404.html create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-19/Cargo.lock create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-19/Cargo.toml create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-19/hello.html create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-19/src/lib.rs create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-19/src/main.rs create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-20/404.html create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-20/Cargo.lock create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-20/Cargo.toml create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-20/hello.html create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-20/src/lib.rs create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-20/src/main.rs create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-21/404.html create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-21/Cargo.lock create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-21/Cargo.toml create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-21/hello.html create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-21/src/lib.rs create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-21/src/main.rs create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-22/404.html create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-22/Cargo.lock create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-22/Cargo.toml create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-22/hello.html create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-22/output.txt create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-22/src/lib.rs create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-22/src/main.rs create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-23/404.html create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-23/Cargo.lock create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-23/Cargo.toml create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-23/hello.html create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-23/src/lib.rs create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-23/src/main.rs create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-24/404.html create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-24/Cargo.lock create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-24/Cargo.toml create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-24/hello.html create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-24/src/lib.rs create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-24/src/main.rs create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-25/404.html create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-25/Cargo.lock create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-25/Cargo.toml create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-25/hello.html create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-25/src/lib.rs create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-25/src/main.rs create mode 100644 src/doc/book/listings/ch20-web-server/no-listing-01-define-threadpool-struct/404.html create mode 100644 src/doc/book/listings/ch20-web-server/no-listing-01-define-threadpool-struct/Cargo.lock create mode 100644 src/doc/book/listings/ch20-web-server/no-listing-01-define-threadpool-struct/Cargo.toml create mode 100644 src/doc/book/listings/ch20-web-server/no-listing-01-define-threadpool-struct/hello.html create mode 100644 src/doc/book/listings/ch20-web-server/no-listing-01-define-threadpool-struct/output.txt create mode 100644 src/doc/book/listings/ch20-web-server/no-listing-01-define-threadpool-struct/src/lib.rs create mode 100644 src/doc/book/listings/ch20-web-server/no-listing-01-define-threadpool-struct/src/main.rs create mode 100644 src/doc/book/listings/ch20-web-server/no-listing-02-impl-threadpool-new/404.html create mode 100644 src/doc/book/listings/ch20-web-server/no-listing-02-impl-threadpool-new/Cargo.lock create mode 100644 src/doc/book/listings/ch20-web-server/no-listing-02-impl-threadpool-new/Cargo.toml create mode 100644 src/doc/book/listings/ch20-web-server/no-listing-02-impl-threadpool-new/hello.html create mode 100644 src/doc/book/listings/ch20-web-server/no-listing-02-impl-threadpool-new/output.txt create mode 100644 src/doc/book/listings/ch20-web-server/no-listing-02-impl-threadpool-new/src/lib.rs create mode 100644 src/doc/book/listings/ch20-web-server/no-listing-02-impl-threadpool-new/src/main.rs create mode 100644 src/doc/book/listings/ch20-web-server/no-listing-03-define-execute/404.html create mode 100644 src/doc/book/listings/ch20-web-server/no-listing-03-define-execute/Cargo.lock create mode 100644 src/doc/book/listings/ch20-web-server/no-listing-03-define-execute/Cargo.toml create mode 100644 src/doc/book/listings/ch20-web-server/no-listing-03-define-execute/hello.html create mode 100644 src/doc/book/listings/ch20-web-server/no-listing-03-define-execute/output.txt create mode 100644 src/doc/book/listings/ch20-web-server/no-listing-03-define-execute/src/lib.rs create mode 100644 src/doc/book/listings/ch20-web-server/no-listing-03-define-execute/src/main.rs create mode 100644 src/doc/book/listings/ch20-web-server/no-listing-04-update-worker-definition/404.html create mode 100644 src/doc/book/listings/ch20-web-server/no-listing-04-update-worker-definition/Cargo.lock create mode 100644 src/doc/book/listings/ch20-web-server/no-listing-04-update-worker-definition/Cargo.toml create mode 100644 src/doc/book/listings/ch20-web-server/no-listing-04-update-worker-definition/hello.html create mode 100644 src/doc/book/listings/ch20-web-server/no-listing-04-update-worker-definition/output.txt create mode 100644 src/doc/book/listings/ch20-web-server/no-listing-04-update-worker-definition/src/lib.rs create mode 100644 src/doc/book/listings/ch20-web-server/no-listing-04-update-worker-definition/src/main.rs create mode 100644 src/doc/book/listings/ch20-web-server/no-listing-05-fix-worker-new/404.html create mode 100644 src/doc/book/listings/ch20-web-server/no-listing-05-fix-worker-new/Cargo.lock create mode 100644 src/doc/book/listings/ch20-web-server/no-listing-05-fix-worker-new/Cargo.toml create mode 100644 src/doc/book/listings/ch20-web-server/no-listing-05-fix-worker-new/hello.html create mode 100644 src/doc/book/listings/ch20-web-server/no-listing-05-fix-worker-new/src/lib.rs create mode 100644 src/doc/book/listings/ch20-web-server/no-listing-05-fix-worker-new/src/main.rs create mode 100644 src/doc/book/listings/ch20-web-server/no-listing-06-fix-threadpool-drop/404.html create mode 100644 src/doc/book/listings/ch20-web-server/no-listing-06-fix-threadpool-drop/Cargo.lock create mode 100644 src/doc/book/listings/ch20-web-server/no-listing-06-fix-threadpool-drop/Cargo.toml create mode 100644 src/doc/book/listings/ch20-web-server/no-listing-06-fix-threadpool-drop/hello.html create mode 100644 src/doc/book/listings/ch20-web-server/no-listing-06-fix-threadpool-drop/src/lib.rs create mode 100644 src/doc/book/listings/ch20-web-server/no-listing-06-fix-threadpool-drop/src/main.rs create mode 100644 src/doc/book/listings/ch20-web-server/no-listing-07-final-code/404.html create mode 100644 src/doc/book/listings/ch20-web-server/no-listing-07-final-code/Cargo.lock create mode 100644 src/doc/book/listings/ch20-web-server/no-listing-07-final-code/Cargo.toml create mode 100644 src/doc/book/listings/ch20-web-server/no-listing-07-final-code/hello.html create mode 100644 src/doc/book/listings/ch20-web-server/no-listing-07-final-code/src/lib.rs create mode 100644 src/doc/book/listings/ch20-web-server/no-listing-07-final-code/src/main.rs (limited to 'src/doc/book/listings') diff --git a/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-01/Cargo.lock b/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-01/Cargo.lock new file mode 100644 index 000000000..5802b7dc9 --- /dev/null +++ b/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-01/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "guessing_game" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-01/Cargo.toml b/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-01/Cargo.toml new file mode 100644 index 000000000..78c94fef9 --- /dev/null +++ b/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-01/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "guessing_game" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-01/src/main.rs b/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-01/src/main.rs new file mode 100644 index 000000000..d44e290d7 --- /dev/null +++ b/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-01/src/main.rs @@ -0,0 +1,31 @@ +// ANCHOR: all +// ANCHOR: io +use std::io; +// ANCHOR_END: io + +// ANCHOR: main +fn main() { + // ANCHOR_END: main + // ANCHOR: print + println!("Guess the number!"); + + println!("Please input your guess."); + // ANCHOR_END: print + + // ANCHOR: string + let mut guess = String::new(); + // ANCHOR_END: string + + // ANCHOR: read + io::stdin() + .read_line(&mut guess) + // ANCHOR_END: read + // ANCHOR: expect + .expect("Failed to read line"); + // ANCHOR_END: expect + + // ANCHOR: print_guess + println!("You guessed: {guess}"); + // ANCHOR_END: print_guess +} +// ANCHOR: all diff --git a/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-02/Cargo.lock b/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-02/Cargo.lock new file mode 100644 index 000000000..0a2f222c2 --- /dev/null +++ b/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-02/Cargo.lock @@ -0,0 +1,83 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "getrandom" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "guessing_game" +version = "0.1.0" +dependencies = [ + "rand", +] + +[[package]] +name = "libc" +version = "0.2.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7282d924be3275cec7f6756ff4121987bc6481325397dde6ba3e7802b1a8b1c" + +[[package]] +name = "ppv-lite86" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" + +[[package]] +name = "rand" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", + "rand_hc", +] + +[[package]] +name = "rand_chacha" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34cf66eb183df1c5876e2dcf6b13d57340741e8dc255b48e40a26de954d06ae7" +dependencies = [ + "getrandom", +] + +[[package]] +name = "rand_hc" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73" +dependencies = [ + "rand_core", +] + +[[package]] +name = "wasi" +version = "0.10.2+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" diff --git a/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-02/Cargo.toml b/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-02/Cargo.toml new file mode 100644 index 000000000..cc63f6f02 --- /dev/null +++ b/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-02/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "guessing_game" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +rand = "0.8.3" diff --git a/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-02/src/main.rs b/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-02/src/main.rs new file mode 100644 index 000000000..b35ed0f2f --- /dev/null +++ b/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-02/src/main.rs @@ -0,0 +1,15 @@ +use std::io; + +fn main() { + println!("Guess the number!"); + + println!("Please input your guess."); + + let mut guess = String::new(); + + io::stdin() + .read_line(&mut guess) + .expect("Failed to read line"); + + println!("You guessed: {guess}"); +} diff --git a/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-03/Cargo.lock b/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-03/Cargo.lock new file mode 100644 index 000000000..0a2f222c2 --- /dev/null +++ b/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-03/Cargo.lock @@ -0,0 +1,83 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "getrandom" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "guessing_game" +version = "0.1.0" +dependencies = [ + "rand", +] + +[[package]] +name = "libc" +version = "0.2.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7282d924be3275cec7f6756ff4121987bc6481325397dde6ba3e7802b1a8b1c" + +[[package]] +name = "ppv-lite86" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" + +[[package]] +name = "rand" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", + "rand_hc", +] + +[[package]] +name = "rand_chacha" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34cf66eb183df1c5876e2dcf6b13d57340741e8dc255b48e40a26de954d06ae7" +dependencies = [ + "getrandom", +] + +[[package]] +name = "rand_hc" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73" +dependencies = [ + "rand_core", +] + +[[package]] +name = "wasi" +version = "0.10.2+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" diff --git a/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-03/Cargo.toml b/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-03/Cargo.toml new file mode 100644 index 000000000..cc63f6f02 --- /dev/null +++ b/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-03/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "guessing_game" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +rand = "0.8.3" diff --git a/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-03/src/main.rs b/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-03/src/main.rs new file mode 100644 index 000000000..1ba2d4d41 --- /dev/null +++ b/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-03/src/main.rs @@ -0,0 +1,28 @@ +// ANCHOR: all +use std::io; +// ANCHOR: ch07-04 +use rand::Rng; + +fn main() { + // ANCHOR_END: ch07-04 + println!("Guess the number!"); + + // ANCHOR: ch07-04 + let secret_number = rand::thread_rng().gen_range(1..=100); + // ANCHOR_END: ch07-04 + + println!("The secret number is: {secret_number}"); + + println!("Please input your guess."); + + let mut guess = String::new(); + + io::stdin() + .read_line(&mut guess) + .expect("Failed to read line"); + + println!("You guessed: {guess}"); + // ANCHOR: ch07-04 +} +// ANCHOR_END: ch07-04 +// ANCHOR_END: all diff --git a/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-04/Cargo.lock b/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-04/Cargo.lock new file mode 100644 index 000000000..0a2f222c2 --- /dev/null +++ b/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-04/Cargo.lock @@ -0,0 +1,83 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "getrandom" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "guessing_game" +version = "0.1.0" +dependencies = [ + "rand", +] + +[[package]] +name = "libc" +version = "0.2.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7282d924be3275cec7f6756ff4121987bc6481325397dde6ba3e7802b1a8b1c" + +[[package]] +name = "ppv-lite86" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" + +[[package]] +name = "rand" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", + "rand_hc", +] + +[[package]] +name = "rand_chacha" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34cf66eb183df1c5876e2dcf6b13d57340741e8dc255b48e40a26de954d06ae7" +dependencies = [ + "getrandom", +] + +[[package]] +name = "rand_hc" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73" +dependencies = [ + "rand_core", +] + +[[package]] +name = "wasi" +version = "0.10.2+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" diff --git a/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-04/Cargo.toml b/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-04/Cargo.toml new file mode 100644 index 000000000..cc63f6f02 --- /dev/null +++ b/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-04/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "guessing_game" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +rand = "0.8.3" diff --git a/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-04/output.txt b/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-04/output.txt new file mode 100644 index 000000000..62fe8327f --- /dev/null +++ b/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-04/output.txt @@ -0,0 +1,20 @@ +$ cargo build + Compiling libc v0.2.86 + Compiling getrandom v0.2.2 + Compiling cfg-if v1.0.0 + Compiling ppv-lite86 v0.2.10 + Compiling rand_core v0.6.2 + Compiling rand_chacha v0.3.0 + Compiling rand v0.8.3 + Compiling guessing_game v0.1.0 (file:///projects/guessing_game) +error[E0308]: mismatched types + --> src/main.rs:22:21 + | +22 | match guess.cmp(&secret_number) { + | ^^^^^^^^^^^^^^ expected struct `String`, found integer + | + = note: expected reference `&String` + found reference `&{integer}` + +For more information about this error, try `rustc --explain E0308`. +error: could not compile `guessing_game` due to previous error diff --git a/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-04/src/main.rs b/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-04/src/main.rs new file mode 100644 index 000000000..6e58a7645 --- /dev/null +++ b/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-04/src/main.rs @@ -0,0 +1,32 @@ +// ANCHOR: here +use rand::Rng; +use std::cmp::Ordering; +use std::io; + +fn main() { + // --snip-- + // ANCHOR_END: here + println!("Guess the number!"); + + let secret_number = rand::thread_rng().gen_range(1..=100); + + println!("The secret number is: {secret_number}"); + + println!("Please input your guess."); + + let mut guess = String::new(); + + io::stdin() + .read_line(&mut guess) + .expect("Failed to read line"); + // ANCHOR: here + + println!("You guessed: {guess}"); + + match guess.cmp(&secret_number) { + Ordering::Less => println!("Too small!"), + Ordering::Greater => println!("Too big!"), + Ordering::Equal => println!("You win!"), + } +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-05/Cargo.lock b/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-05/Cargo.lock new file mode 100644 index 000000000..0a2f222c2 --- /dev/null +++ b/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-05/Cargo.lock @@ -0,0 +1,83 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "getrandom" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "guessing_game" +version = "0.1.0" +dependencies = [ + "rand", +] + +[[package]] +name = "libc" +version = "0.2.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7282d924be3275cec7f6756ff4121987bc6481325397dde6ba3e7802b1a8b1c" + +[[package]] +name = "ppv-lite86" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" + +[[package]] +name = "rand" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", + "rand_hc", +] + +[[package]] +name = "rand_chacha" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34cf66eb183df1c5876e2dcf6b13d57340741e8dc255b48e40a26de954d06ae7" +dependencies = [ + "getrandom", +] + +[[package]] +name = "rand_hc" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73" +dependencies = [ + "rand_core", +] + +[[package]] +name = "wasi" +version = "0.10.2+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" diff --git a/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-05/Cargo.toml b/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-05/Cargo.toml new file mode 100644 index 000000000..cc63f6f02 --- /dev/null +++ b/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-05/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "guessing_game" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +rand = "0.8.3" diff --git a/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-05/src/main.rs b/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-05/src/main.rs new file mode 100644 index 000000000..12f497e18 --- /dev/null +++ b/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-05/src/main.rs @@ -0,0 +1,45 @@ +use rand::Rng; +use std::cmp::Ordering; +use std::io; + +fn main() { + println!("Guess the number!"); + + let secret_number = rand::thread_rng().gen_range(1..=100); + + println!("The secret number is: {secret_number}"); + + loop { + println!("Please input your guess."); + + let mut guess = String::new(); + + // ANCHOR: here + // --snip-- + + io::stdin() + .read_line(&mut guess) + .expect("Failed to read line"); + + // ANCHOR: ch19 + let guess: u32 = match guess.trim().parse() { + Ok(num) => num, + Err(_) => continue, + }; + // ANCHOR_END: ch19 + + println!("You guessed: {guess}"); + + // --snip-- + // ANCHOR_END: here + + match guess.cmp(&secret_number) { + Ordering::Less => println!("Too small!"), + Ordering::Greater => println!("Too big!"), + Ordering::Equal => { + println!("You win!"); + break; + } + } + } +} diff --git a/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-06/Cargo.lock b/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-06/Cargo.lock new file mode 100644 index 000000000..0a2f222c2 --- /dev/null +++ b/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-06/Cargo.lock @@ -0,0 +1,83 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "getrandom" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "guessing_game" +version = "0.1.0" +dependencies = [ + "rand", +] + +[[package]] +name = "libc" +version = "0.2.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7282d924be3275cec7f6756ff4121987bc6481325397dde6ba3e7802b1a8b1c" + +[[package]] +name = "ppv-lite86" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" + +[[package]] +name = "rand" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", + "rand_hc", +] + +[[package]] +name = "rand_chacha" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34cf66eb183df1c5876e2dcf6b13d57340741e8dc255b48e40a26de954d06ae7" +dependencies = [ + "getrandom", +] + +[[package]] +name = "rand_hc" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73" +dependencies = [ + "rand_core", +] + +[[package]] +name = "wasi" +version = "0.10.2+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" diff --git a/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-06/Cargo.toml b/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-06/Cargo.toml new file mode 100644 index 000000000..cc63f6f02 --- /dev/null +++ b/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-06/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "guessing_game" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +rand = "0.8.3" diff --git a/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-06/src/main.rs b/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-06/src/main.rs new file mode 100644 index 000000000..7fcbb99fb --- /dev/null +++ b/src/doc/book/listings/ch02-guessing-game-tutorial/listing-02-06/src/main.rs @@ -0,0 +1,35 @@ +use rand::Rng; +use std::cmp::Ordering; +use std::io; + +fn main() { + println!("Guess the number!"); + + let secret_number = rand::thread_rng().gen_range(1..=100); + + loop { + println!("Please input your guess."); + + let mut guess = String::new(); + + io::stdin() + .read_line(&mut guess) + .expect("Failed to read line"); + + let guess: u32 = match guess.trim().parse() { + Ok(num) => num, + Err(_) => continue, + }; + + println!("You guessed: {guess}"); + + match guess.cmp(&secret_number) { + Ordering::Less => println!("Too small!"), + Ordering::Greater => println!("Too big!"), + Ordering::Equal => { + println!("You win!"); + break; + } + } + } +} diff --git a/src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-01-cargo-new/Cargo.lock b/src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-01-cargo-new/Cargo.lock new file mode 100644 index 000000000..ee5d79095 --- /dev/null +++ b/src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-01-cargo-new/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "guessing_game" +version = "0.1.0" diff --git a/src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-01-cargo-new/Cargo.toml b/src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-01-cargo-new/Cargo.toml new file mode 100644 index 000000000..78c94fef9 --- /dev/null +++ b/src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-01-cargo-new/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "guessing_game" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-01-cargo-new/output.txt b/src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-01-cargo-new/output.txt new file mode 100644 index 000000000..2724c145d --- /dev/null +++ b/src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-01-cargo-new/output.txt @@ -0,0 +1,5 @@ +$ cargo run + Compiling guessing_game v0.1.0 (file:///projects/guessing_game) + Finished dev [unoptimized + debuginfo] target(s) in 1.50s + Running `target/debug/guessing_game` +Hello, world! diff --git a/src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-01-cargo-new/src/main.rs b/src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-01-cargo-new/src/main.rs new file mode 100644 index 000000000..e7a11a969 --- /dev/null +++ b/src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-01-cargo-new/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-02-without-expect/Cargo.lock b/src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-02-without-expect/Cargo.lock new file mode 100644 index 000000000..5802b7dc9 --- /dev/null +++ b/src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-02-without-expect/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "guessing_game" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-02-without-expect/Cargo.toml b/src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-02-without-expect/Cargo.toml new file mode 100644 index 000000000..78c94fef9 --- /dev/null +++ b/src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-02-without-expect/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "guessing_game" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-02-without-expect/output.txt b/src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-02-without-expect/output.txt new file mode 100644 index 000000000..8095bbd8d --- /dev/null +++ b/src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-02-without-expect/output.txt @@ -0,0 +1,13 @@ +$ cargo build + Compiling guessing_game v0.1.0 (file:///projects/guessing_game) +warning: unused `Result` that must be used + --> src/main.rs:10:5 + | +10 | io::stdin().read_line(&mut guess); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `#[warn(unused_must_use)]` on by default + = note: this `Result` may be an `Err` variant, which should be handled + +warning: `guessing_game` (bin "guessing_game") generated 1 warning + Finished dev [unoptimized + debuginfo] target(s) in 0.59s diff --git a/src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-02-without-expect/src/main.rs b/src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-02-without-expect/src/main.rs new file mode 100644 index 000000000..51046016f --- /dev/null +++ b/src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-02-without-expect/src/main.rs @@ -0,0 +1,13 @@ +use std::io; + +fn main() { + println!("Guess the number!"); + + println!("Please input your guess."); + + let mut guess = String::new(); + + io::stdin().read_line(&mut guess); + + println!("You guessed: {guess}"); +} diff --git a/src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-03-convert-string-to-number/Cargo.lock b/src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-03-convert-string-to-number/Cargo.lock new file mode 100644 index 000000000..0a2f222c2 --- /dev/null +++ b/src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-03-convert-string-to-number/Cargo.lock @@ -0,0 +1,83 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "getrandom" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "guessing_game" +version = "0.1.0" +dependencies = [ + "rand", +] + +[[package]] +name = "libc" +version = "0.2.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7282d924be3275cec7f6756ff4121987bc6481325397dde6ba3e7802b1a8b1c" + +[[package]] +name = "ppv-lite86" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" + +[[package]] +name = "rand" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", + "rand_hc", +] + +[[package]] +name = "rand_chacha" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34cf66eb183df1c5876e2dcf6b13d57340741e8dc255b48e40a26de954d06ae7" +dependencies = [ + "getrandom", +] + +[[package]] +name = "rand_hc" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73" +dependencies = [ + "rand_core", +] + +[[package]] +name = "wasi" +version = "0.10.2+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" diff --git a/src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-03-convert-string-to-number/Cargo.toml b/src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-03-convert-string-to-number/Cargo.toml new file mode 100644 index 000000000..cc63f6f02 --- /dev/null +++ b/src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-03-convert-string-to-number/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "guessing_game" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +rand = "0.8.3" diff --git a/src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-03-convert-string-to-number/src/main.rs b/src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-03-convert-string-to-number/src/main.rs new file mode 100644 index 000000000..7f076c592 --- /dev/null +++ b/src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-03-convert-string-to-number/src/main.rs @@ -0,0 +1,33 @@ +use rand::Rng; +use std::cmp::Ordering; +use std::io; + +fn main() { + println!("Guess the number!"); + + let secret_number = rand::thread_rng().gen_range(1..=100); + + println!("The secret number is: {secret_number}"); + + println!("Please input your guess."); + + // ANCHOR: here + // --snip-- + + let mut guess = String::new(); + + io::stdin() + .read_line(&mut guess) + .expect("Failed to read line"); + + let guess: u32 = guess.trim().parse().expect("Please type a number!"); + + println!("You guessed: {guess}"); + + match guess.cmp(&secret_number) { + Ordering::Less => println!("Too small!"), + Ordering::Greater => println!("Too big!"), + Ordering::Equal => println!("You win!"), + } + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-04-looping/Cargo.lock b/src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-04-looping/Cargo.lock new file mode 100644 index 000000000..0a2f222c2 --- /dev/null +++ b/src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-04-looping/Cargo.lock @@ -0,0 +1,83 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "getrandom" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "guessing_game" +version = "0.1.0" +dependencies = [ + "rand", +] + +[[package]] +name = "libc" +version = "0.2.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7282d924be3275cec7f6756ff4121987bc6481325397dde6ba3e7802b1a8b1c" + +[[package]] +name = "ppv-lite86" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" + +[[package]] +name = "rand" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", + "rand_hc", +] + +[[package]] +name = "rand_chacha" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34cf66eb183df1c5876e2dcf6b13d57340741e8dc255b48e40a26de954d06ae7" +dependencies = [ + "getrandom", +] + +[[package]] +name = "rand_hc" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73" +dependencies = [ + "rand_core", +] + +[[package]] +name = "wasi" +version = "0.10.2+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" diff --git a/src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-04-looping/Cargo.toml b/src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-04-looping/Cargo.toml new file mode 100644 index 000000000..cc63f6f02 --- /dev/null +++ b/src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-04-looping/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "guessing_game" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +rand = "0.8.3" diff --git a/src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-04-looping/src/main.rs b/src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-04-looping/src/main.rs new file mode 100644 index 000000000..f97d1c58c --- /dev/null +++ b/src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-04-looping/src/main.rs @@ -0,0 +1,40 @@ +use rand::Rng; +use std::cmp::Ordering; +use std::io; + +fn main() { + println!("Guess the number!"); + + let secret_number = rand::thread_rng().gen_range(1..=100); + + // ANCHOR: here + // --snip-- + + println!("The secret number is: {secret_number}"); + + loop { + println!("Please input your guess."); + + // --snip-- + + // ANCHOR_END: here + + let mut guess = String::new(); + + io::stdin() + .read_line(&mut guess) + .expect("Failed to read line"); + + let guess: u32 = guess.trim().parse().expect("Please type a number!"); + + println!("You guessed: {guess}"); + + // ANCHOR: here + match guess.cmp(&secret_number) { + Ordering::Less => println!("Too small!"), + Ordering::Greater => println!("Too big!"), + Ordering::Equal => println!("You win!"), + } + } +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-05-quitting/Cargo.lock b/src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-05-quitting/Cargo.lock new file mode 100644 index 000000000..0a2f222c2 --- /dev/null +++ b/src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-05-quitting/Cargo.lock @@ -0,0 +1,83 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "getrandom" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "guessing_game" +version = "0.1.0" +dependencies = [ + "rand", +] + +[[package]] +name = "libc" +version = "0.2.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7282d924be3275cec7f6756ff4121987bc6481325397dde6ba3e7802b1a8b1c" + +[[package]] +name = "ppv-lite86" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" + +[[package]] +name = "rand" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", + "rand_hc", +] + +[[package]] +name = "rand_chacha" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34cf66eb183df1c5876e2dcf6b13d57340741e8dc255b48e40a26de954d06ae7" +dependencies = [ + "getrandom", +] + +[[package]] +name = "rand_hc" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73" +dependencies = [ + "rand_core", +] + +[[package]] +name = "wasi" +version = "0.10.2+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" diff --git a/src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-05-quitting/Cargo.toml b/src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-05-quitting/Cargo.toml new file mode 100644 index 000000000..cc63f6f02 --- /dev/null +++ b/src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-05-quitting/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "guessing_game" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +rand = "0.8.3" diff --git a/src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-05-quitting/src/main.rs b/src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-05-quitting/src/main.rs new file mode 100644 index 000000000..def0a0e7e --- /dev/null +++ b/src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-05-quitting/src/main.rs @@ -0,0 +1,38 @@ +use rand::Rng; +use std::cmp::Ordering; +use std::io; + +fn main() { + println!("Guess the number!"); + + let secret_number = rand::thread_rng().gen_range(1..=100); + + println!("The secret number is: {secret_number}"); + + loop { + println!("Please input your guess."); + + let mut guess = String::new(); + + io::stdin() + .read_line(&mut guess) + .expect("Failed to read line"); + + let guess: u32 = guess.trim().parse().expect("Please type a number!"); + + println!("You guessed: {guess}"); + + // ANCHOR: here + // --snip-- + + match guess.cmp(&secret_number) { + Ordering::Less => println!("Too small!"), + Ordering::Greater => println!("Too big!"), + Ordering::Equal => { + println!("You win!"); + break; + } + } + } +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch03-common-programming-concepts/listing-03-01/Cargo.lock b/src/doc/book/listings/ch03-common-programming-concepts/listing-03-01/Cargo.lock new file mode 100644 index 000000000..88287d136 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/listing-03-01/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "functions" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch03-common-programming-concepts/listing-03-01/Cargo.toml b/src/doc/book/listings/ch03-common-programming-concepts/listing-03-01/Cargo.toml new file mode 100644 index 000000000..478b346fd --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/listing-03-01/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "functions" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch03-common-programming-concepts/listing-03-01/src/main.rs b/src/doc/book/listings/ch03-common-programming-concepts/listing-03-01/src/main.rs new file mode 100644 index 000000000..b492d384f --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/listing-03-01/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + let y = 6; +} diff --git a/src/doc/book/listings/ch03-common-programming-concepts/listing-03-02/Cargo.lock b/src/doc/book/listings/ch03-common-programming-concepts/listing-03-02/Cargo.lock new file mode 100644 index 000000000..4ca0c2dbd --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/listing-03-02/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "branches" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch03-common-programming-concepts/listing-03-02/Cargo.toml b/src/doc/book/listings/ch03-common-programming-concepts/listing-03-02/Cargo.toml new file mode 100644 index 000000000..659645556 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/listing-03-02/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "branches" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch03-common-programming-concepts/listing-03-02/output.txt b/src/doc/book/listings/ch03-common-programming-concepts/listing-03-02/output.txt new file mode 100644 index 000000000..3eb8d102a --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/listing-03-02/output.txt @@ -0,0 +1,5 @@ +$ cargo run + Compiling branches v0.1.0 (file:///projects/branches) + Finished dev [unoptimized + debuginfo] target(s) in 0.30s + Running `target/debug/branches` +The value of number is: 5 diff --git a/src/doc/book/listings/ch03-common-programming-concepts/listing-03-02/src/main.rs b/src/doc/book/listings/ch03-common-programming-concepts/listing-03-02/src/main.rs new file mode 100644 index 000000000..e021e41e8 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/listing-03-02/src/main.rs @@ -0,0 +1,6 @@ +fn main() { + let condition = true; + let number = if condition { 5 } else { 6 }; + + println!("The value of number is: {number}"); +} diff --git a/src/doc/book/listings/ch03-common-programming-concepts/listing-03-03/Cargo.lock b/src/doc/book/listings/ch03-common-programming-concepts/listing-03-03/Cargo.lock new file mode 100644 index 000000000..9942b362a --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/listing-03-03/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "loops" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch03-common-programming-concepts/listing-03-03/Cargo.toml b/src/doc/book/listings/ch03-common-programming-concepts/listing-03-03/Cargo.toml new file mode 100644 index 000000000..810e8bbc0 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/listing-03-03/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "loops" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch03-common-programming-concepts/listing-03-03/src/main.rs b/src/doc/book/listings/ch03-common-programming-concepts/listing-03-03/src/main.rs new file mode 100644 index 000000000..ca070c759 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/listing-03-03/src/main.rs @@ -0,0 +1,11 @@ +fn main() { + let mut number = 3; + + while number != 0 { + println!("{number}!"); + + number -= 1; + } + + println!("LIFTOFF!!!"); +} diff --git a/src/doc/book/listings/ch03-common-programming-concepts/listing-03-04/Cargo.lock b/src/doc/book/listings/ch03-common-programming-concepts/listing-03-04/Cargo.lock new file mode 100644 index 000000000..9942b362a --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/listing-03-04/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "loops" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch03-common-programming-concepts/listing-03-04/Cargo.toml b/src/doc/book/listings/ch03-common-programming-concepts/listing-03-04/Cargo.toml new file mode 100644 index 000000000..810e8bbc0 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/listing-03-04/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "loops" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch03-common-programming-concepts/listing-03-04/output.txt b/src/doc/book/listings/ch03-common-programming-concepts/listing-03-04/output.txt new file mode 100644 index 000000000..35c0f804a --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/listing-03-04/output.txt @@ -0,0 +1,9 @@ +$ cargo run + Compiling loops v0.1.0 (file:///projects/loops) + Finished dev [unoptimized + debuginfo] target(s) in 0.32s + Running `target/debug/loops` +the value is: 10 +the value is: 20 +the value is: 30 +the value is: 40 +the value is: 50 diff --git a/src/doc/book/listings/ch03-common-programming-concepts/listing-03-04/src/main.rs b/src/doc/book/listings/ch03-common-programming-concepts/listing-03-04/src/main.rs new file mode 100644 index 000000000..38fd301e6 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/listing-03-04/src/main.rs @@ -0,0 +1,10 @@ +fn main() { + let a = [10, 20, 30, 40, 50]; + let mut index = 0; + + while index < 5 { + println!("the value is: {}", a[index]); + + index += 1; + } +} diff --git a/src/doc/book/listings/ch03-common-programming-concepts/listing-03-05/Cargo.lock b/src/doc/book/listings/ch03-common-programming-concepts/listing-03-05/Cargo.lock new file mode 100644 index 000000000..9942b362a --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/listing-03-05/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "loops" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch03-common-programming-concepts/listing-03-05/Cargo.toml b/src/doc/book/listings/ch03-common-programming-concepts/listing-03-05/Cargo.toml new file mode 100644 index 000000000..810e8bbc0 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/listing-03-05/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "loops" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch03-common-programming-concepts/listing-03-05/src/main.rs b/src/doc/book/listings/ch03-common-programming-concepts/listing-03-05/src/main.rs new file mode 100644 index 000000000..b44e6b7aa --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/listing-03-05/src/main.rs @@ -0,0 +1,7 @@ +fn main() { + let a = [10, 20, 30, 40, 50]; + + for element in a { + println!("the value is: {element}"); + } +} diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-01-variables-are-immutable/Cargo.lock b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-01-variables-are-immutable/Cargo.lock new file mode 100644 index 000000000..2d62cbe7a --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-01-variables-are-immutable/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "variables" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-01-variables-are-immutable/Cargo.toml b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-01-variables-are-immutable/Cargo.toml new file mode 100644 index 000000000..4da3b8150 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-01-variables-are-immutable/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "variables" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-01-variables-are-immutable/output.txt b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-01-variables-are-immutable/output.txt new file mode 100644 index 000000000..ed87cb2c8 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-01-variables-are-immutable/output.txt @@ -0,0 +1,16 @@ +$ cargo run + Compiling variables v0.1.0 (file:///projects/variables) +error[E0384]: cannot assign twice to immutable variable `x` + --> src/main.rs:4:5 + | +2 | let x = 5; + | - + | | + | first assignment to `x` + | help: consider making this binding mutable: `mut x` +3 | println!("The value of x is: {x}"); +4 | x = 6; + | ^^^^^ cannot assign twice to immutable variable + +For more information about this error, try `rustc --explain E0384`. +error: could not compile `variables` due to previous error diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-01-variables-are-immutable/src/main.rs b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-01-variables-are-immutable/src/main.rs new file mode 100644 index 000000000..d64f46de4 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-01-variables-are-immutable/src/main.rs @@ -0,0 +1,6 @@ +fn main() { + let x = 5; + println!("The value of x is: {x}"); + x = 6; + println!("The value of x is: {x}"); +} diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-02-adding-mut/Cargo.lock b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-02-adding-mut/Cargo.lock new file mode 100644 index 000000000..2d62cbe7a --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-02-adding-mut/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "variables" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-02-adding-mut/Cargo.toml b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-02-adding-mut/Cargo.toml new file mode 100644 index 000000000..4da3b8150 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-02-adding-mut/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "variables" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-02-adding-mut/output.txt b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-02-adding-mut/output.txt new file mode 100644 index 000000000..8ed6598ff --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-02-adding-mut/output.txt @@ -0,0 +1,6 @@ +$ cargo run + Compiling variables v0.1.0 (file:///projects/variables) + Finished dev [unoptimized + debuginfo] target(s) in 0.30s + Running `target/debug/variables` +The value of x is: 5 +The value of x is: 6 diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-02-adding-mut/src/main.rs b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-02-adding-mut/src/main.rs new file mode 100644 index 000000000..a57709ccf --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-02-adding-mut/src/main.rs @@ -0,0 +1,6 @@ +fn main() { + let mut x = 5; + println!("The value of x is: {x}"); + x = 6; + println!("The value of x is: {x}"); +} diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-03-shadowing/Cargo.lock b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-03-shadowing/Cargo.lock new file mode 100644 index 000000000..2d62cbe7a --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-03-shadowing/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "variables" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-03-shadowing/Cargo.toml b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-03-shadowing/Cargo.toml new file mode 100644 index 000000000..4da3b8150 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-03-shadowing/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "variables" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-03-shadowing/output.txt b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-03-shadowing/output.txt new file mode 100644 index 000000000..f310e9ffa --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-03-shadowing/output.txt @@ -0,0 +1,6 @@ +$ cargo run + Compiling variables v0.1.0 (file:///projects/variables) + Finished dev [unoptimized + debuginfo] target(s) in 0.31s + Running `target/debug/variables` +The value of x in the inner scope is: 12 +The value of x is: 6 diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-03-shadowing/src/main.rs b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-03-shadowing/src/main.rs new file mode 100644 index 000000000..03924fe1c --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-03-shadowing/src/main.rs @@ -0,0 +1,12 @@ +fn main() { + let x = 5; + + let x = x + 1; + + { + let x = x * 2; + println!("The value of x in the inner scope is: {x}"); + } + + println!("The value of x is: {x}"); +} diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-04-shadowing-can-change-types/Cargo.lock b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-04-shadowing-can-change-types/Cargo.lock new file mode 100644 index 000000000..2d62cbe7a --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-04-shadowing-can-change-types/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "variables" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-04-shadowing-can-change-types/Cargo.toml b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-04-shadowing-can-change-types/Cargo.toml new file mode 100644 index 000000000..4da3b8150 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-04-shadowing-can-change-types/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "variables" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-04-shadowing-can-change-types/src/main.rs b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-04-shadowing-can-change-types/src/main.rs new file mode 100644 index 000000000..42589f548 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-04-shadowing-can-change-types/src/main.rs @@ -0,0 +1,6 @@ +fn main() { + // ANCHOR: here + let spaces = " "; + let spaces = spaces.len(); + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-05-mut-cant-change-types/Cargo.lock b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-05-mut-cant-change-types/Cargo.lock new file mode 100644 index 000000000..2d62cbe7a --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-05-mut-cant-change-types/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "variables" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-05-mut-cant-change-types/Cargo.toml b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-05-mut-cant-change-types/Cargo.toml new file mode 100644 index 000000000..4da3b8150 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-05-mut-cant-change-types/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "variables" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-05-mut-cant-change-types/output.txt b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-05-mut-cant-change-types/output.txt new file mode 100644 index 000000000..31a07efc4 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-05-mut-cant-change-types/output.txt @@ -0,0 +1,12 @@ +$ cargo run + Compiling variables v0.1.0 (file:///projects/variables) +error[E0308]: mismatched types + --> src/main.rs:3:14 + | +2 | let mut spaces = " "; + | ----- expected due to this value +3 | spaces = spaces.len(); + | ^^^^^^^^^^^^ expected `&str`, found `usize` + +For more information about this error, try `rustc --explain E0308`. +error: could not compile `variables` due to previous error diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-05-mut-cant-change-types/src/main.rs b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-05-mut-cant-change-types/src/main.rs new file mode 100644 index 000000000..f52635d0b --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-05-mut-cant-change-types/src/main.rs @@ -0,0 +1,6 @@ +fn main() { + // ANCHOR: here + let mut spaces = " "; + spaces = spaces.len(); + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-06-floating-point/Cargo.lock b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-06-floating-point/Cargo.lock new file mode 100644 index 000000000..3b40559a7 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-06-floating-point/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "floating-point" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-06-floating-point/Cargo.toml b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-06-floating-point/Cargo.toml new file mode 100644 index 000000000..83610e756 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-06-floating-point/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "floating-point" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-06-floating-point/src/main.rs b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-06-floating-point/src/main.rs new file mode 100644 index 000000000..6f4f0fe87 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-06-floating-point/src/main.rs @@ -0,0 +1,5 @@ +fn main() { + let x = 2.0; // f64 + + let y: f32 = 3.0; // f32 +} diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-07-numeric-operations/Cargo.lock b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-07-numeric-operations/Cargo.lock new file mode 100644 index 000000000..94a12b255 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-07-numeric-operations/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "numeric-operations" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-07-numeric-operations/Cargo.toml b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-07-numeric-operations/Cargo.toml new file mode 100644 index 000000000..b4bea55e3 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-07-numeric-operations/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "numeric-operations" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-07-numeric-operations/src/main.rs b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-07-numeric-operations/src/main.rs new file mode 100644 index 000000000..9d3b4813b --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-07-numeric-operations/src/main.rs @@ -0,0 +1,17 @@ +fn main() { + // addition + let sum = 5 + 10; + + // subtraction + let difference = 95.5 - 4.3; + + // multiplication + let product = 4 * 30; + + // division + let quotient = 56.7 / 32.2; + let floored = 2 / 3; // Results in 0 + + // remainder + let remainder = 43 % 5; +} diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-08-boolean/Cargo.lock b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-08-boolean/Cargo.lock new file mode 100644 index 000000000..5d5728ebd --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-08-boolean/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "boolean" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-08-boolean/Cargo.toml b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-08-boolean/Cargo.toml new file mode 100644 index 000000000..47e42cef8 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-08-boolean/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "boolean" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-08-boolean/src/main.rs b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-08-boolean/src/main.rs new file mode 100644 index 000000000..2c56e62f9 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-08-boolean/src/main.rs @@ -0,0 +1,5 @@ +fn main() { + let t = true; + + let f: bool = false; // with explicit type annotation +} diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-09-char/Cargo.lock b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-09-char/Cargo.lock new file mode 100644 index 000000000..bb58446cc --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-09-char/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "char" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-09-char/Cargo.toml b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-09-char/Cargo.toml new file mode 100644 index 000000000..a1ef3b537 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-09-char/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "char" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-09-char/src/main.rs b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-09-char/src/main.rs new file mode 100644 index 000000000..4921cc998 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-09-char/src/main.rs @@ -0,0 +1,5 @@ +fn main() { + let c = 'z'; + let z: char = 'ℤ'; // with explicit type annotation + let heart_eyed_cat = '😻'; +} diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-10-tuples/Cargo.lock b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-10-tuples/Cargo.lock new file mode 100644 index 000000000..f57f0767c --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-10-tuples/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "tuples" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-10-tuples/Cargo.toml b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-10-tuples/Cargo.toml new file mode 100644 index 000000000..9b0879c2c --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-10-tuples/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "tuples" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-10-tuples/src/main.rs b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-10-tuples/src/main.rs new file mode 100644 index 000000000..b7b51fb2f --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-10-tuples/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + let tup: (i32, f64, u8) = (500, 6.4, 1); +} diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-11-destructuring-tuples/Cargo.lock b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-11-destructuring-tuples/Cargo.lock new file mode 100644 index 000000000..f57f0767c --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-11-destructuring-tuples/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "tuples" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-11-destructuring-tuples/Cargo.toml b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-11-destructuring-tuples/Cargo.toml new file mode 100644 index 000000000..9b0879c2c --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-11-destructuring-tuples/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "tuples" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-11-destructuring-tuples/src/main.rs b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-11-destructuring-tuples/src/main.rs new file mode 100644 index 000000000..3002bdde4 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-11-destructuring-tuples/src/main.rs @@ -0,0 +1,7 @@ +fn main() { + let tup = (500, 6.4, 1); + + let (x, y, z) = tup; + + println!("The value of y is: {y}"); +} diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-12-tuple-indexing/Cargo.lock b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-12-tuple-indexing/Cargo.lock new file mode 100644 index 000000000..f57f0767c --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-12-tuple-indexing/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "tuples" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-12-tuple-indexing/Cargo.toml b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-12-tuple-indexing/Cargo.toml new file mode 100644 index 000000000..9b0879c2c --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-12-tuple-indexing/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "tuples" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-12-tuple-indexing/src/main.rs b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-12-tuple-indexing/src/main.rs new file mode 100644 index 000000000..1b1e646fd --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-12-tuple-indexing/src/main.rs @@ -0,0 +1,9 @@ +fn main() { + let x: (i32, f64, u8) = (500, 6.4, 1); + + let five_hundred = x.0; + + let six_point_four = x.1; + + let one = x.2; +} diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-13-arrays/Cargo.lock b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-13-arrays/Cargo.lock new file mode 100644 index 000000000..68e3c5967 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-13-arrays/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "arrays" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-13-arrays/Cargo.toml b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-13-arrays/Cargo.toml new file mode 100644 index 000000000..96be3e2b1 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-13-arrays/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "arrays" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-13-arrays/src/main.rs b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-13-arrays/src/main.rs new file mode 100644 index 000000000..d475901ce --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-13-arrays/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + let a = [1, 2, 3, 4, 5]; +} diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-14-array-indexing/Cargo.lock b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-14-array-indexing/Cargo.lock new file mode 100644 index 000000000..68e3c5967 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-14-array-indexing/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "arrays" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-14-array-indexing/Cargo.toml b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-14-array-indexing/Cargo.toml new file mode 100644 index 000000000..96be3e2b1 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-14-array-indexing/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "arrays" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-14-array-indexing/src/main.rs b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-14-array-indexing/src/main.rs new file mode 100644 index 000000000..d33e3174f --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-14-array-indexing/src/main.rs @@ -0,0 +1,6 @@ +fn main() { + let a = [1, 2, 3, 4, 5]; + + let first = a[0]; + let second = a[1]; +} diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-15-invalid-array-access/Cargo.lock b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-15-invalid-array-access/Cargo.lock new file mode 100644 index 000000000..68e3c5967 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-15-invalid-array-access/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "arrays" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-15-invalid-array-access/Cargo.toml b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-15-invalid-array-access/Cargo.toml new file mode 100644 index 000000000..96be3e2b1 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-15-invalid-array-access/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "arrays" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-15-invalid-array-access/src/main.rs b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-15-invalid-array-access/src/main.rs new file mode 100644 index 000000000..b634c9378 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-15-invalid-array-access/src/main.rs @@ -0,0 +1,22 @@ +use std::io; + +fn main() { + let a = [1, 2, 3, 4, 5]; + + println!("Please enter an array index."); + + let mut index = String::new(); + + io::stdin() + .read_line(&mut index) + .expect("Failed to read line"); + + let index: usize = index + .trim() + .parse() + .expect("Index entered was not a number"); + + let element = a[index]; + + println!("The value of the element at index {index} is: {element}"); +} diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-16-functions/Cargo.lock b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-16-functions/Cargo.lock new file mode 100644 index 000000000..88287d136 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-16-functions/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "functions" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-16-functions/Cargo.toml b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-16-functions/Cargo.toml new file mode 100644 index 000000000..478b346fd --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-16-functions/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "functions" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-16-functions/output.txt b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-16-functions/output.txt new file mode 100644 index 000000000..723fad32a --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-16-functions/output.txt @@ -0,0 +1,6 @@ +$ cargo run + Compiling functions v0.1.0 (file:///projects/functions) + Finished dev [unoptimized + debuginfo] target(s) in 0.28s + Running `target/debug/functions` +Hello, world! +Another function. diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-16-functions/src/main.rs b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-16-functions/src/main.rs new file mode 100644 index 000000000..38be8565b --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-16-functions/src/main.rs @@ -0,0 +1,9 @@ +fn main() { + println!("Hello, world!"); + + another_function(); +} + +fn another_function() { + println!("Another function."); +} diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-17-functions-with-parameters/Cargo.lock b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-17-functions-with-parameters/Cargo.lock new file mode 100644 index 000000000..88287d136 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-17-functions-with-parameters/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "functions" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-17-functions-with-parameters/Cargo.toml b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-17-functions-with-parameters/Cargo.toml new file mode 100644 index 000000000..478b346fd --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-17-functions-with-parameters/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "functions" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-17-functions-with-parameters/output.txt b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-17-functions-with-parameters/output.txt new file mode 100644 index 000000000..546bbc047 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-17-functions-with-parameters/output.txt @@ -0,0 +1,5 @@ +$ cargo run + Compiling functions v0.1.0 (file:///projects/functions) + Finished dev [unoptimized + debuginfo] target(s) in 1.21s + Running `target/debug/functions` +The value of x is: 5 diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-17-functions-with-parameters/src/main.rs b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-17-functions-with-parameters/src/main.rs new file mode 100644 index 000000000..108da4f81 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-17-functions-with-parameters/src/main.rs @@ -0,0 +1,7 @@ +fn main() { + another_function(5); +} + +fn another_function(x: i32) { + println!("The value of x is: {x}"); +} diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-18-functions-with-multiple-parameters/Cargo.lock b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-18-functions-with-multiple-parameters/Cargo.lock new file mode 100644 index 000000000..88287d136 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-18-functions-with-multiple-parameters/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "functions" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-18-functions-with-multiple-parameters/Cargo.toml b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-18-functions-with-multiple-parameters/Cargo.toml new file mode 100644 index 000000000..478b346fd --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-18-functions-with-multiple-parameters/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "functions" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-18-functions-with-multiple-parameters/output.txt b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-18-functions-with-multiple-parameters/output.txt new file mode 100644 index 000000000..6210234c9 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-18-functions-with-multiple-parameters/output.txt @@ -0,0 +1,5 @@ +$ cargo run + Compiling functions v0.1.0 (file:///projects/functions) + Finished dev [unoptimized + debuginfo] target(s) in 0.31s + Running `target/debug/functions` +The measurement is: 5h diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-18-functions-with-multiple-parameters/src/main.rs b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-18-functions-with-multiple-parameters/src/main.rs new file mode 100644 index 000000000..b070ccb23 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-18-functions-with-multiple-parameters/src/main.rs @@ -0,0 +1,7 @@ +fn main() { + print_labeled_measurement(5, 'h'); +} + +fn print_labeled_measurement(value: i32, unit_label: char) { + println!("The measurement is: {value}{unit_label}"); +} diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-19-statements-vs-expressions/Cargo.lock b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-19-statements-vs-expressions/Cargo.lock new file mode 100644 index 000000000..89a654d69 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-19-statements-vs-expressions/Cargo.lock @@ -0,0 +1,4 @@ +[[package]] +name = "functions" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-19-statements-vs-expressions/Cargo.toml b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-19-statements-vs-expressions/Cargo.toml new file mode 100644 index 000000000..478b346fd --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-19-statements-vs-expressions/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "functions" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-19-statements-vs-expressions/output.txt b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-19-statements-vs-expressions/output.txt new file mode 100644 index 000000000..a5a85f4f6 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-19-statements-vs-expressions/output.txt @@ -0,0 +1,34 @@ +$ cargo run + Compiling functions v0.1.0 (file:///projects/functions) +error: expected expression, found statement (`let`) + --> src/main.rs:2:14 + | +2 | let x = (let y = 6); + | ^^^^^^^^^ + | + = note: variable declaration using `let` is a statement + +error[E0658]: `let` expressions in this position are unstable + --> src/main.rs:2:14 + | +2 | let x = (let y = 6); + | ^^^^^^^^^ + | + = note: see issue #53667 for more information + +warning: unnecessary parentheses around assigned value + --> src/main.rs:2:13 + | +2 | let x = (let y = 6); + | ^ ^ + | + = note: `#[warn(unused_parens)]` on by default +help: remove these parentheses + | +2 - let x = (let y = 6); +2 + let x = let y = 6; + | + +For more information about this error, try `rustc --explain E0658`. +warning: `functions` (bin "functions") generated 1 warning +error: could not compile `functions` due to 2 previous errors; 1 warning emitted diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-19-statements-vs-expressions/rustfmt-ignore b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-19-statements-vs-expressions/rustfmt-ignore new file mode 100644 index 000000000..06a976dd4 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-19-statements-vs-expressions/rustfmt-ignore @@ -0,0 +1 @@ +This listing deliberately doesn't parse so rustfmt fails. diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-19-statements-vs-expressions/src/main.rs b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-19-statements-vs-expressions/src/main.rs new file mode 100644 index 000000000..988f9653e --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-19-statements-vs-expressions/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + let x = (let y = 6); +} diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-20-blocks-are-expressions/Cargo.lock b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-20-blocks-are-expressions/Cargo.lock new file mode 100644 index 000000000..88287d136 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-20-blocks-are-expressions/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "functions" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-20-blocks-are-expressions/Cargo.toml b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-20-blocks-are-expressions/Cargo.toml new file mode 100644 index 000000000..478b346fd --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-20-blocks-are-expressions/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "functions" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-20-blocks-are-expressions/src/main.rs b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-20-blocks-are-expressions/src/main.rs new file mode 100644 index 000000000..64b873297 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-20-blocks-are-expressions/src/main.rs @@ -0,0 +1,8 @@ +fn main() { + let y = { + let x = 3; + x + 1 + }; + + println!("The value of y is: {y}"); +} diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-21-function-return-values/Cargo.lock b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-21-function-return-values/Cargo.lock new file mode 100644 index 000000000..88287d136 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-21-function-return-values/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "functions" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-21-function-return-values/Cargo.toml b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-21-function-return-values/Cargo.toml new file mode 100644 index 000000000..478b346fd --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-21-function-return-values/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "functions" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-21-function-return-values/output.txt b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-21-function-return-values/output.txt new file mode 100644 index 000000000..a457e3399 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-21-function-return-values/output.txt @@ -0,0 +1,5 @@ +$ cargo run + Compiling functions v0.1.0 (file:///projects/functions) + Finished dev [unoptimized + debuginfo] target(s) in 0.30s + Running `target/debug/functions` +The value of x is: 5 diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-21-function-return-values/src/main.rs b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-21-function-return-values/src/main.rs new file mode 100644 index 000000000..a11af7ec7 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-21-function-return-values/src/main.rs @@ -0,0 +1,9 @@ +fn five() -> i32 { + 5 +} + +fn main() { + let x = five(); + + println!("The value of x is: {x}"); +} diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-22-function-parameter-and-return/Cargo.lock b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-22-function-parameter-and-return/Cargo.lock new file mode 100644 index 000000000..88287d136 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-22-function-parameter-and-return/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "functions" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-22-function-parameter-and-return/Cargo.toml b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-22-function-parameter-and-return/Cargo.toml new file mode 100644 index 000000000..478b346fd --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-22-function-parameter-and-return/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "functions" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-22-function-parameter-and-return/src/main.rs b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-22-function-parameter-and-return/src/main.rs new file mode 100644 index 000000000..da9d0ddb3 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-22-function-parameter-and-return/src/main.rs @@ -0,0 +1,9 @@ +fn main() { + let x = plus_one(5); + + println!("The value of x is: {x}"); +} + +fn plus_one(x: i32) -> i32 { + x + 1 +} diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-23-statements-dont-return-values/Cargo.lock b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-23-statements-dont-return-values/Cargo.lock new file mode 100644 index 000000000..88287d136 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-23-statements-dont-return-values/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "functions" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-23-statements-dont-return-values/Cargo.toml b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-23-statements-dont-return-values/Cargo.toml new file mode 100644 index 000000000..478b346fd --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-23-statements-dont-return-values/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "functions" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-23-statements-dont-return-values/output.txt b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-23-statements-dont-return-values/output.txt new file mode 100644 index 000000000..c0484ea1b --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-23-statements-dont-return-values/output.txt @@ -0,0 +1,14 @@ +$ cargo run + Compiling functions v0.1.0 (file:///projects/functions) +error[E0308]: mismatched types + --> src/main.rs:7:24 + | +7 | fn plus_one(x: i32) -> i32 { + | -------- ^^^ expected `i32`, found `()` + | | + | implicitly returns `()` as its body has no tail or `return` expression +8 | x + 1; + | - help: remove this semicolon + +For more information about this error, try `rustc --explain E0308`. +error: could not compile `functions` due to previous error diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-23-statements-dont-return-values/src/main.rs b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-23-statements-dont-return-values/src/main.rs new file mode 100644 index 000000000..1cec800b6 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-23-statements-dont-return-values/src/main.rs @@ -0,0 +1,9 @@ +fn main() { + let x = plus_one(5); + + println!("The value of x is: {x}"); +} + +fn plus_one(x: i32) -> i32 { + x + 1; +} diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-24-comments-end-of-line/Cargo.lock b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-24-comments-end-of-line/Cargo.lock new file mode 100644 index 000000000..a289136a6 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-24-comments-end-of-line/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "comments" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-24-comments-end-of-line/Cargo.toml b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-24-comments-end-of-line/Cargo.toml new file mode 100644 index 000000000..e0576b5dc --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-24-comments-end-of-line/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "comments" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-24-comments-end-of-line/src/main.rs b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-24-comments-end-of-line/src/main.rs new file mode 100644 index 000000000..535f4b993 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-24-comments-end-of-line/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + let lucky_number = 7; // I’m feeling lucky today +} diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-25-comments-above-line/Cargo.lock b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-25-comments-above-line/Cargo.lock new file mode 100644 index 000000000..a289136a6 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-25-comments-above-line/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "comments" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-25-comments-above-line/Cargo.toml b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-25-comments-above-line/Cargo.toml new file mode 100644 index 000000000..e0576b5dc --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-25-comments-above-line/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "comments" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-25-comments-above-line/src/main.rs b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-25-comments-above-line/src/main.rs new file mode 100644 index 000000000..81cd93559 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-25-comments-above-line/src/main.rs @@ -0,0 +1,4 @@ +fn main() { + // I’m feeling lucky today + let lucky_number = 7; +} diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-26-if-true/Cargo.lock b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-26-if-true/Cargo.lock new file mode 100644 index 000000000..4ca0c2dbd --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-26-if-true/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "branches" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-26-if-true/Cargo.toml b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-26-if-true/Cargo.toml new file mode 100644 index 000000000..659645556 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-26-if-true/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "branches" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-26-if-true/output.txt b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-26-if-true/output.txt new file mode 100644 index 000000000..3d8c7dc33 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-26-if-true/output.txt @@ -0,0 +1,5 @@ +$ cargo run + Compiling branches v0.1.0 (file:///projects/branches) + Finished dev [unoptimized + debuginfo] target(s) in 0.31s + Running `target/debug/branches` +condition was true diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-26-if-true/src/main.rs b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-26-if-true/src/main.rs new file mode 100644 index 000000000..e64a42adf --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-26-if-true/src/main.rs @@ -0,0 +1,9 @@ +fn main() { + let number = 3; + + if number < 5 { + println!("condition was true"); + } else { + println!("condition was false"); + } +} diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-27-if-false/Cargo.lock b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-27-if-false/Cargo.lock new file mode 100644 index 000000000..4ca0c2dbd --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-27-if-false/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "branches" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-27-if-false/Cargo.toml b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-27-if-false/Cargo.toml new file mode 100644 index 000000000..659645556 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-27-if-false/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "branches" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-27-if-false/output.txt b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-27-if-false/output.txt new file mode 100644 index 000000000..e40da961c --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-27-if-false/output.txt @@ -0,0 +1,5 @@ +$ cargo run + Compiling branches v0.1.0 (file:///projects/branches) + Finished dev [unoptimized + debuginfo] target(s) in 0.31s + Running `target/debug/branches` +condition was false diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-27-if-false/src/main.rs b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-27-if-false/src/main.rs new file mode 100644 index 000000000..f7d76cf55 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-27-if-false/src/main.rs @@ -0,0 +1,11 @@ +fn main() { + // ANCHOR: here + let number = 7; + // ANCHOR_END: here + + if number < 5 { + println!("condition was true"); + } else { + println!("condition was false"); + } +} diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-28-if-condition-must-be-bool/Cargo.lock b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-28-if-condition-must-be-bool/Cargo.lock new file mode 100644 index 000000000..4ca0c2dbd --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-28-if-condition-must-be-bool/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "branches" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-28-if-condition-must-be-bool/Cargo.toml b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-28-if-condition-must-be-bool/Cargo.toml new file mode 100644 index 000000000..659645556 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-28-if-condition-must-be-bool/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "branches" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-28-if-condition-must-be-bool/output.txt b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-28-if-condition-must-be-bool/output.txt new file mode 100644 index 000000000..735bfe758 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-28-if-condition-must-be-bool/output.txt @@ -0,0 +1,10 @@ +$ cargo run + Compiling branches v0.1.0 (file:///projects/branches) +error[E0308]: mismatched types + --> src/main.rs:4:8 + | +4 | if number { + | ^^^^^^ expected `bool`, found integer + +For more information about this error, try `rustc --explain E0308`. +error: could not compile `branches` due to previous error diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-28-if-condition-must-be-bool/src/main.rs b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-28-if-condition-must-be-bool/src/main.rs new file mode 100644 index 000000000..bc4af767b --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-28-if-condition-must-be-bool/src/main.rs @@ -0,0 +1,7 @@ +fn main() { + let number = 3; + + if number { + println!("number was three"); + } +} diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-29-if-not-equal-0/Cargo.lock b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-29-if-not-equal-0/Cargo.lock new file mode 100644 index 000000000..4ca0c2dbd --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-29-if-not-equal-0/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "branches" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-29-if-not-equal-0/Cargo.toml b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-29-if-not-equal-0/Cargo.toml new file mode 100644 index 000000000..659645556 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-29-if-not-equal-0/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "branches" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-29-if-not-equal-0/src/main.rs b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-29-if-not-equal-0/src/main.rs new file mode 100644 index 000000000..704650f46 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-29-if-not-equal-0/src/main.rs @@ -0,0 +1,7 @@ +fn main() { + let number = 3; + + if number != 0 { + println!("number was something other than zero"); + } +} diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-30-else-if/Cargo.lock b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-30-else-if/Cargo.lock new file mode 100644 index 000000000..4ca0c2dbd --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-30-else-if/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "branches" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-30-else-if/Cargo.toml b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-30-else-if/Cargo.toml new file mode 100644 index 000000000..659645556 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-30-else-if/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "branches" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-30-else-if/output.txt b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-30-else-if/output.txt new file mode 100644 index 000000000..b218941ad --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-30-else-if/output.txt @@ -0,0 +1,5 @@ +$ cargo run + Compiling branches v0.1.0 (file:///projects/branches) + Finished dev [unoptimized + debuginfo] target(s) in 0.31s + Running `target/debug/branches` +number is divisible by 3 diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-30-else-if/src/main.rs b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-30-else-if/src/main.rs new file mode 100644 index 000000000..d0ef9b2c1 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-30-else-if/src/main.rs @@ -0,0 +1,13 @@ +fn main() { + let number = 6; + + if number % 4 == 0 { + println!("number is divisible by 4"); + } else if number % 3 == 0 { + println!("number is divisible by 3"); + } else if number % 2 == 0 { + println!("number is divisible by 2"); + } else { + println!("number is not divisible by 4, 3, or 2"); + } +} diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-31-arms-must-return-same-type/Cargo.lock b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-31-arms-must-return-same-type/Cargo.lock new file mode 100644 index 000000000..4ca0c2dbd --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-31-arms-must-return-same-type/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "branches" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-31-arms-must-return-same-type/Cargo.toml b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-31-arms-must-return-same-type/Cargo.toml new file mode 100644 index 000000000..659645556 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-31-arms-must-return-same-type/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "branches" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-31-arms-must-return-same-type/output.txt b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-31-arms-must-return-same-type/output.txt new file mode 100644 index 000000000..e922acd29 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-31-arms-must-return-same-type/output.txt @@ -0,0 +1,12 @@ +$ cargo run + Compiling branches v0.1.0 (file:///projects/branches) +error[E0308]: `if` and `else` have incompatible types + --> src/main.rs:4:44 + | +4 | let number = if condition { 5 } else { "six" }; + | - ^^^^^ expected integer, found `&str` + | | + | expected because of this + +For more information about this error, try `rustc --explain E0308`. +error: could not compile `branches` due to previous error diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-31-arms-must-return-same-type/src/main.rs b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-31-arms-must-return-same-type/src/main.rs new file mode 100644 index 000000000..df7068bcf --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-31-arms-must-return-same-type/src/main.rs @@ -0,0 +1,7 @@ +fn main() { + let condition = true; + + let number = if condition { 5 } else { "six" }; + + println!("The value of number is: {number}"); +} diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-32-5-loop-labels/Cargo.lock b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-32-5-loop-labels/Cargo.lock new file mode 100644 index 000000000..f73867315 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-32-5-loop-labels/Cargo.lock @@ -0,0 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "loops" +version = "0.1.0" diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-32-5-loop-labels/Cargo.toml b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-32-5-loop-labels/Cargo.toml new file mode 100644 index 000000000..810e8bbc0 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-32-5-loop-labels/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "loops" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-32-5-loop-labels/output.txt b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-32-5-loop-labels/output.txt new file mode 100644 index 000000000..d4d322fb4 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-32-5-loop-labels/output.txt @@ -0,0 +1,13 @@ +$ cargo run + Compiling loops v0.1.0 (file:///projects/loops) + Finished dev [unoptimized + debuginfo] target(s) in 0.58s + Running `target/debug/loops` +count = 0 +remaining = 10 +remaining = 9 +count = 1 +remaining = 10 +remaining = 9 +count = 2 +remaining = 10 +End count = 2 diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-32-5-loop-labels/src/main.rs b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-32-5-loop-labels/src/main.rs new file mode 100644 index 000000000..dd8856403 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-32-5-loop-labels/src/main.rs @@ -0,0 +1,21 @@ +fn main() { + let mut count = 0; + 'counting_up: loop { + println!("count = {count}"); + let mut remaining = 10; + + loop { + println!("remaining = {remaining}"); + if remaining == 9 { + break; + } + if count == 2 { + break 'counting_up; + } + remaining -= 1; + } + + count += 1; + } + println!("End count = {count}"); +} diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-32-loop/Cargo.lock b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-32-loop/Cargo.lock new file mode 100644 index 000000000..9942b362a --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-32-loop/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "loops" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-32-loop/Cargo.toml b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-32-loop/Cargo.toml new file mode 100644 index 000000000..810e8bbc0 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-32-loop/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "loops" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-32-loop/src/main.rs b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-32-loop/src/main.rs new file mode 100644 index 000000000..f1692e462 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-32-loop/src/main.rs @@ -0,0 +1,5 @@ +fn main() { + loop { + println!("again!"); + } +} diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-33-return-value-from-loop/Cargo.lock b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-33-return-value-from-loop/Cargo.lock new file mode 100644 index 000000000..9942b362a --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-33-return-value-from-loop/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "loops" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-33-return-value-from-loop/Cargo.toml b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-33-return-value-from-loop/Cargo.toml new file mode 100644 index 000000000..810e8bbc0 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-33-return-value-from-loop/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "loops" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-33-return-value-from-loop/src/main.rs b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-33-return-value-from-loop/src/main.rs new file mode 100644 index 000000000..683d18bc1 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-33-return-value-from-loop/src/main.rs @@ -0,0 +1,13 @@ +fn main() { + let mut counter = 0; + + let result = loop { + counter += 1; + + if counter == 10 { + break counter * 2; + } + }; + + println!("The result is {result}"); +} diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-34-for-range/Cargo.lock b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-34-for-range/Cargo.lock new file mode 100644 index 000000000..9942b362a --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-34-for-range/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "loops" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-34-for-range/Cargo.toml b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-34-for-range/Cargo.toml new file mode 100644 index 000000000..810e8bbc0 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-34-for-range/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "loops" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch03-common-programming-concepts/no-listing-34-for-range/src/main.rs b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-34-for-range/src/main.rs new file mode 100644 index 000000000..df5b305bc --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/no-listing-34-for-range/src/main.rs @@ -0,0 +1,6 @@ +fn main() { + for number in (1..4).rev() { + println!("{number}!"); + } + println!("LIFTOFF!!!"); +} diff --git a/src/doc/book/listings/ch03-common-programming-concepts/output-only-01-no-type-annotations/Cargo.lock b/src/doc/book/listings/ch03-common-programming-concepts/output-only-01-no-type-annotations/Cargo.lock new file mode 100644 index 000000000..b5664bc33 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/output-only-01-no-type-annotations/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "no_type_annotations" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch03-common-programming-concepts/output-only-01-no-type-annotations/Cargo.toml b/src/doc/book/listings/ch03-common-programming-concepts/output-only-01-no-type-annotations/Cargo.toml new file mode 100644 index 000000000..8ad4d5aa6 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/output-only-01-no-type-annotations/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "no_type_annotations" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/src/doc/book/listings/ch03-common-programming-concepts/output-only-01-no-type-annotations/output.txt b/src/doc/book/listings/ch03-common-programming-concepts/output-only-01-no-type-annotations/output.txt new file mode 100644 index 000000000..d9807cee0 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/output-only-01-no-type-annotations/output.txt @@ -0,0 +1,10 @@ +$ cargo build + Compiling no_type_annotations v0.1.0 (file:///projects/no_type_annotations) +error[E0282]: type annotations needed + --> src/main.rs:2:9 + | +2 | let guess = "42".parse().expect("Not a number!"); + | ^^^^^ consider giving `guess` a type + +For more information about this error, try `rustc --explain E0282`. +error: could not compile `no_type_annotations` due to previous error diff --git a/src/doc/book/listings/ch03-common-programming-concepts/output-only-01-no-type-annotations/src/main.rs b/src/doc/book/listings/ch03-common-programming-concepts/output-only-01-no-type-annotations/src/main.rs new file mode 100644 index 000000000..f41c55805 --- /dev/null +++ b/src/doc/book/listings/ch03-common-programming-concepts/output-only-01-no-type-annotations/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + let guess = "42".parse().expect("Not a number!"); +} diff --git a/src/doc/book/listings/ch04-understanding-ownership/listing-04-01/Cargo.lock b/src/doc/book/listings/ch04-understanding-ownership/listing-04-01/Cargo.lock new file mode 100644 index 000000000..9e4e62ddf --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/listing-04-01/Cargo.lock @@ -0,0 +1,4 @@ +[[package]] +name = "ownership" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch04-understanding-ownership/listing-04-01/Cargo.toml b/src/doc/book/listings/ch04-understanding-ownership/listing-04-01/Cargo.toml new file mode 100644 index 000000000..e8847526d --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/listing-04-01/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "ownership" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch04-understanding-ownership/listing-04-01/rustfmt-ignore b/src/doc/book/listings/ch04-understanding-ownership/listing-04-01/rustfmt-ignore new file mode 100644 index 000000000..9a53c718a --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/listing-04-01/rustfmt-ignore @@ -0,0 +1,3 @@ +We have some weird comments pointing out borrowing scopes that we don't want to change; +unfortunately I haven't found a way to skip them with rustfmt that works so for now we're going to +manually skip those listings. See: https://github.com/rust-lang/rustfmt/issues/4028 diff --git a/src/doc/book/listings/ch04-understanding-ownership/listing-04-01/src/main.rs b/src/doc/book/listings/ch04-understanding-ownership/listing-04-01/src/main.rs new file mode 100644 index 000000000..148ad84c9 --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/listing-04-01/src/main.rs @@ -0,0 +1,9 @@ +fn main() { + // ANCHOR: here + { // s is not valid here, it’s not yet declared + let s = "hello"; // s is valid from this point forward + + // do stuff with s + } // this scope is now over, and s is no longer valid + // ANCHOR_END: here +} \ No newline at end of file diff --git a/src/doc/book/listings/ch04-understanding-ownership/listing-04-02/Cargo.lock b/src/doc/book/listings/ch04-understanding-ownership/listing-04-02/Cargo.lock new file mode 100644 index 000000000..2aa4918e5 --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/listing-04-02/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "ownership" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch04-understanding-ownership/listing-04-02/Cargo.toml b/src/doc/book/listings/ch04-understanding-ownership/listing-04-02/Cargo.toml new file mode 100644 index 000000000..e8847526d --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/listing-04-02/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "ownership" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch04-understanding-ownership/listing-04-02/src/main.rs b/src/doc/book/listings/ch04-understanding-ownership/listing-04-02/src/main.rs new file mode 100644 index 000000000..de0f1b325 --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/listing-04-02/src/main.rs @@ -0,0 +1,6 @@ +fn main() { + // ANCHOR: here + let x = 5; + let y = x; + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch04-understanding-ownership/listing-04-03/Cargo.lock b/src/doc/book/listings/ch04-understanding-ownership/listing-04-03/Cargo.lock new file mode 100644 index 000000000..9e4e62ddf --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/listing-04-03/Cargo.lock @@ -0,0 +1,4 @@ +[[package]] +name = "ownership" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch04-understanding-ownership/listing-04-03/Cargo.toml b/src/doc/book/listings/ch04-understanding-ownership/listing-04-03/Cargo.toml new file mode 100644 index 000000000..e8847526d --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/listing-04-03/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "ownership" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch04-understanding-ownership/listing-04-03/rustfmt-ignore b/src/doc/book/listings/ch04-understanding-ownership/listing-04-03/rustfmt-ignore new file mode 100644 index 000000000..9a53c718a --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/listing-04-03/rustfmt-ignore @@ -0,0 +1,3 @@ +We have some weird comments pointing out borrowing scopes that we don't want to change; +unfortunately I haven't found a way to skip them with rustfmt that works so for now we're going to +manually skip those listings. See: https://github.com/rust-lang/rustfmt/issues/4028 diff --git a/src/doc/book/listings/ch04-understanding-ownership/listing-04-03/src/main.rs b/src/doc/book/listings/ch04-understanding-ownership/listing-04-03/src/main.rs new file mode 100644 index 000000000..b001cc5f4 --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/listing-04-03/src/main.rs @@ -0,0 +1,23 @@ +fn main() { + let s = String::from("hello"); // s comes into scope + + takes_ownership(s); // s's value moves into the function... + // ... and so is no longer valid here + + let x = 5; // x comes into scope + + makes_copy(x); // x would move into the function, + // but i32 is Copy, so it's okay to still + // use x afterward + +} // Here, x goes out of scope, then s. But because s's value was moved, nothing + // special happens. + +fn takes_ownership(some_string: String) { // some_string comes into scope + println!("{}", some_string); +} // Here, some_string goes out of scope and `drop` is called. The backing + // memory is freed. + +fn makes_copy(some_integer: i32) { // some_integer comes into scope + println!("{}", some_integer); +} // Here, some_integer goes out of scope. Nothing special happens. diff --git a/src/doc/book/listings/ch04-understanding-ownership/listing-04-04/Cargo.lock b/src/doc/book/listings/ch04-understanding-ownership/listing-04-04/Cargo.lock new file mode 100644 index 000000000..9e4e62ddf --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/listing-04-04/Cargo.lock @@ -0,0 +1,4 @@ +[[package]] +name = "ownership" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch04-understanding-ownership/listing-04-04/Cargo.toml b/src/doc/book/listings/ch04-understanding-ownership/listing-04-04/Cargo.toml new file mode 100644 index 000000000..e8847526d --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/listing-04-04/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "ownership" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch04-understanding-ownership/listing-04-04/rustfmt-ignore b/src/doc/book/listings/ch04-understanding-ownership/listing-04-04/rustfmt-ignore new file mode 100644 index 000000000..9a53c718a --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/listing-04-04/rustfmt-ignore @@ -0,0 +1,3 @@ +We have some weird comments pointing out borrowing scopes that we don't want to change; +unfortunately I haven't found a way to skip them with rustfmt that works so for now we're going to +manually skip those listings. See: https://github.com/rust-lang/rustfmt/issues/4028 diff --git a/src/doc/book/listings/ch04-understanding-ownership/listing-04-04/src/main.rs b/src/doc/book/listings/ch04-understanding-ownership/listing-04-04/src/main.rs new file mode 100644 index 000000000..e206bec7a --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/listing-04-04/src/main.rs @@ -0,0 +1,29 @@ +fn main() { + let s1 = gives_ownership(); // gives_ownership moves its return + // value into s1 + + let s2 = String::from("hello"); // s2 comes into scope + + let s3 = takes_and_gives_back(s2); // s2 is moved into + // takes_and_gives_back, which also + // moves its return value into s3 +} // Here, s3 goes out of scope and is dropped. s2 was moved, so nothing + // happens. s1 goes out of scope and is dropped. + +fn gives_ownership() -> String { // gives_ownership will move its + // return value into the function + // that calls it + + let some_string = String::from("yours"); // some_string comes into scope + + some_string // some_string is returned and + // moves out to the calling + // function +} + +// This function takes a String and returns one +fn takes_and_gives_back(a_string: String) -> String { // a_string comes into + // scope + + a_string // a_string is returned and moves out to the calling function +} diff --git a/src/doc/book/listings/ch04-understanding-ownership/listing-04-05/Cargo.lock b/src/doc/book/listings/ch04-understanding-ownership/listing-04-05/Cargo.lock new file mode 100644 index 000000000..2aa4918e5 --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/listing-04-05/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "ownership" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch04-understanding-ownership/listing-04-05/Cargo.toml b/src/doc/book/listings/ch04-understanding-ownership/listing-04-05/Cargo.toml new file mode 100644 index 000000000..e8847526d --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/listing-04-05/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "ownership" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch04-understanding-ownership/listing-04-05/src/main.rs b/src/doc/book/listings/ch04-understanding-ownership/listing-04-05/src/main.rs new file mode 100644 index 000000000..22aee1419 --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/listing-04-05/src/main.rs @@ -0,0 +1,13 @@ +fn main() { + let s1 = String::from("hello"); + + let (s2, len) = calculate_length(s1); + + println!("The length of '{}' is {}.", s2, len); +} + +fn calculate_length(s: String) -> (String, usize) { + let length = s.len(); // len() returns the length of a String + + (s, length) +} diff --git a/src/doc/book/listings/ch04-understanding-ownership/listing-04-06/Cargo.lock b/src/doc/book/listings/ch04-understanding-ownership/listing-04-06/Cargo.lock new file mode 100644 index 000000000..2aa4918e5 --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/listing-04-06/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "ownership" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch04-understanding-ownership/listing-04-06/Cargo.toml b/src/doc/book/listings/ch04-understanding-ownership/listing-04-06/Cargo.toml new file mode 100644 index 000000000..e8847526d --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/listing-04-06/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "ownership" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch04-understanding-ownership/listing-04-06/output.txt b/src/doc/book/listings/ch04-understanding-ownership/listing-04-06/output.txt new file mode 100644 index 000000000..1176f4e3a --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/listing-04-06/output.txt @@ -0,0 +1,12 @@ +$ cargo run + Compiling ownership v0.1.0 (file:///projects/ownership) +error[E0596]: cannot borrow `*some_string` as mutable, as it is behind a `&` reference + --> src/main.rs:8:5 + | +7 | fn change(some_string: &String) { + | ------- help: consider changing this to be a mutable reference: `&mut String` +8 | some_string.push_str(", world"); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `some_string` is a `&` reference, so the data it refers to cannot be borrowed as mutable + +For more information about this error, try `rustc --explain E0596`. +error: could not compile `ownership` due to previous error diff --git a/src/doc/book/listings/ch04-understanding-ownership/listing-04-06/src/main.rs b/src/doc/book/listings/ch04-understanding-ownership/listing-04-06/src/main.rs new file mode 100644 index 000000000..330ffa68a --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/listing-04-06/src/main.rs @@ -0,0 +1,9 @@ +fn main() { + let s = String::from("hello"); + + change(&s); +} + +fn change(some_string: &String) { + some_string.push_str(", world"); +} diff --git a/src/doc/book/listings/ch04-understanding-ownership/listing-04-07/Cargo.lock b/src/doc/book/listings/ch04-understanding-ownership/listing-04-07/Cargo.lock new file mode 100644 index 000000000..2aa4918e5 --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/listing-04-07/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "ownership" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch04-understanding-ownership/listing-04-07/Cargo.toml b/src/doc/book/listings/ch04-understanding-ownership/listing-04-07/Cargo.toml new file mode 100644 index 000000000..e8847526d --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/listing-04-07/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "ownership" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch04-understanding-ownership/listing-04-07/src/main.rs b/src/doc/book/listings/ch04-understanding-ownership/listing-04-07/src/main.rs new file mode 100644 index 000000000..3bb3c8580 --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/listing-04-07/src/main.rs @@ -0,0 +1,21 @@ +// ANCHOR: here +fn first_word(s: &String) -> usize { + // ANCHOR: as_bytes + let bytes = s.as_bytes(); + // ANCHOR_END: as_bytes + + // ANCHOR: iter + for (i, &item) in bytes.iter().enumerate() { + // ANCHOR_END: iter + // ANCHOR: inside_for + if item == b' ' { + return i; + } + } + + s.len() + // ANCHOR_END: inside_for +} +// ANCHOR_END: here + +fn main() {} diff --git a/src/doc/book/listings/ch04-understanding-ownership/listing-04-08/Cargo.lock b/src/doc/book/listings/ch04-understanding-ownership/listing-04-08/Cargo.lock new file mode 100644 index 000000000..2aa4918e5 --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/listing-04-08/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "ownership" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch04-understanding-ownership/listing-04-08/Cargo.toml b/src/doc/book/listings/ch04-understanding-ownership/listing-04-08/Cargo.toml new file mode 100644 index 000000000..e8847526d --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/listing-04-08/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "ownership" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch04-understanding-ownership/listing-04-08/src/main.rs b/src/doc/book/listings/ch04-understanding-ownership/listing-04-08/src/main.rs new file mode 100644 index 000000000..b6182fe2b --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/listing-04-08/src/main.rs @@ -0,0 +1,24 @@ +fn first_word(s: &String) -> usize { + let bytes = s.as_bytes(); + + for (i, &item) in bytes.iter().enumerate() { + if item == b' ' { + return i; + } + } + + s.len() +} + +// ANCHOR: here +fn main() { + let mut s = String::from("hello world"); + + let word = first_word(&s); // word will get the value 5 + + s.clear(); // this empties the String, making it equal to "" + + // word still has the value 5 here, but there's no more string that + // we could meaningfully use the value 5 with. word is now totally invalid! +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch04-understanding-ownership/listing-04-09/Cargo.lock b/src/doc/book/listings/ch04-understanding-ownership/listing-04-09/Cargo.lock new file mode 100644 index 000000000..2aa4918e5 --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/listing-04-09/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "ownership" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch04-understanding-ownership/listing-04-09/Cargo.toml b/src/doc/book/listings/ch04-understanding-ownership/listing-04-09/Cargo.toml new file mode 100644 index 000000000..e8847526d --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/listing-04-09/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "ownership" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch04-understanding-ownership/listing-04-09/src/main.rs b/src/doc/book/listings/ch04-understanding-ownership/listing-04-09/src/main.rs new file mode 100644 index 000000000..5a6ceaa1e --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/listing-04-09/src/main.rs @@ -0,0 +1,36 @@ +// ANCHOR: here +fn first_word(s: &str) -> &str { + // ANCHOR_END: here + let bytes = s.as_bytes(); + + for (i, &item) in bytes.iter().enumerate() { + if item == b' ' { + return &s[0..i]; + } + } + + &s[..] +} + +// ANCHOR: usage +fn main() { + let my_string = String::from("hello world"); + + // `first_word` works on slices of `String`s, whether partial or whole + let word = first_word(&my_string[0..6]); + let word = first_word(&my_string[..]); + // `first_word` also works on references to `String`s, which are equivalent + // to whole slices of `String`s + let word = first_word(&my_string); + + let my_string_literal = "hello world"; + + // `first_word` works on slices of string literals, whether partial or whole + let word = first_word(&my_string_literal[0..6]); + let word = first_word(&my_string_literal[..]); + + // Because string literals *are* string slices already, + // this works too, without the slice syntax! + let word = first_word(my_string_literal); +} +// ANCHOR_END: usage diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-01-can-mutate-string/Cargo.lock b/src/doc/book/listings/ch04-understanding-ownership/no-listing-01-can-mutate-string/Cargo.lock new file mode 100644 index 000000000..2aa4918e5 --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-01-can-mutate-string/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "ownership" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-01-can-mutate-string/Cargo.toml b/src/doc/book/listings/ch04-understanding-ownership/no-listing-01-can-mutate-string/Cargo.toml new file mode 100644 index 000000000..e8847526d --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-01-can-mutate-string/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "ownership" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-01-can-mutate-string/src/main.rs b/src/doc/book/listings/ch04-understanding-ownership/no-listing-01-can-mutate-string/src/main.rs new file mode 100644 index 000000000..b68f0f1e7 --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-01-can-mutate-string/src/main.rs @@ -0,0 +1,9 @@ +fn main() { + // ANCHOR: here + let mut s = String::from("hello"); + + s.push_str(", world!"); // push_str() appends a literal to a String + + println!("{}", s); // This will print `hello, world!` + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-02-string-scope/Cargo.lock b/src/doc/book/listings/ch04-understanding-ownership/no-listing-02-string-scope/Cargo.lock new file mode 100644 index 000000000..9e4e62ddf --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-02-string-scope/Cargo.lock @@ -0,0 +1,4 @@ +[[package]] +name = "ownership" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-02-string-scope/Cargo.toml b/src/doc/book/listings/ch04-understanding-ownership/no-listing-02-string-scope/Cargo.toml new file mode 100644 index 000000000..e8847526d --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-02-string-scope/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "ownership" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-02-string-scope/rustfmt-ignore b/src/doc/book/listings/ch04-understanding-ownership/no-listing-02-string-scope/rustfmt-ignore new file mode 100644 index 000000000..9a53c718a --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-02-string-scope/rustfmt-ignore @@ -0,0 +1,3 @@ +We have some weird comments pointing out borrowing scopes that we don't want to change; +unfortunately I haven't found a way to skip them with rustfmt that works so for now we're going to +manually skip those listings. See: https://github.com/rust-lang/rustfmt/issues/4028 diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-02-string-scope/src/main.rs b/src/doc/book/listings/ch04-understanding-ownership/no-listing-02-string-scope/src/main.rs new file mode 100644 index 000000000..7e6d46f83 --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-02-string-scope/src/main.rs @@ -0,0 +1,10 @@ +fn main() { + // ANCHOR: here + { + let s = String::from("hello"); // s is valid from this point forward + + // do stuff with s + } // this scope is now over, and s is no + // longer valid + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-03-string-move/Cargo.lock b/src/doc/book/listings/ch04-understanding-ownership/no-listing-03-string-move/Cargo.lock new file mode 100644 index 000000000..2aa4918e5 --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-03-string-move/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "ownership" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-03-string-move/Cargo.toml b/src/doc/book/listings/ch04-understanding-ownership/no-listing-03-string-move/Cargo.toml new file mode 100644 index 000000000..e8847526d --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-03-string-move/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "ownership" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-03-string-move/src/main.rs b/src/doc/book/listings/ch04-understanding-ownership/no-listing-03-string-move/src/main.rs new file mode 100644 index 000000000..a5817e714 --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-03-string-move/src/main.rs @@ -0,0 +1,6 @@ +fn main() { + // ANCHOR: here + let s1 = String::from("hello"); + let s2 = s1; + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-04-cant-use-after-move/Cargo.lock b/src/doc/book/listings/ch04-understanding-ownership/no-listing-04-cant-use-after-move/Cargo.lock new file mode 100644 index 000000000..2aa4918e5 --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-04-cant-use-after-move/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "ownership" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-04-cant-use-after-move/Cargo.toml b/src/doc/book/listings/ch04-understanding-ownership/no-listing-04-cant-use-after-move/Cargo.toml new file mode 100644 index 000000000..e8847526d --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-04-cant-use-after-move/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "ownership" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-04-cant-use-after-move/output.txt b/src/doc/book/listings/ch04-understanding-ownership/no-listing-04-cant-use-after-move/output.txt new file mode 100644 index 000000000..6435eeb44 --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-04-cant-use-after-move/output.txt @@ -0,0 +1,17 @@ +$ cargo run + Compiling ownership v0.1.0 (file:///projects/ownership) +error[E0382]: borrow of moved value: `s1` + --> src/main.rs:5:28 + | +2 | let s1 = String::from("hello"); + | -- move occurs because `s1` has type `String`, which does not implement the `Copy` trait +3 | let s2 = s1; + | -- value moved here +4 | +5 | println!("{}, world!", s1); + | ^^ value borrowed here after move + | + = note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info) + +For more information about this error, try `rustc --explain E0382`. +error: could not compile `ownership` due to previous error diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-04-cant-use-after-move/src/main.rs b/src/doc/book/listings/ch04-understanding-ownership/no-listing-04-cant-use-after-move/src/main.rs new file mode 100644 index 000000000..d0b9f1879 --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-04-cant-use-after-move/src/main.rs @@ -0,0 +1,8 @@ +fn main() { + // ANCHOR: here + let s1 = String::from("hello"); + let s2 = s1; + + println!("{}, world!", s1); + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-05-clone/Cargo.lock b/src/doc/book/listings/ch04-understanding-ownership/no-listing-05-clone/Cargo.lock new file mode 100644 index 000000000..2aa4918e5 --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-05-clone/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "ownership" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-05-clone/Cargo.toml b/src/doc/book/listings/ch04-understanding-ownership/no-listing-05-clone/Cargo.toml new file mode 100644 index 000000000..e8847526d --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-05-clone/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "ownership" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-05-clone/src/main.rs b/src/doc/book/listings/ch04-understanding-ownership/no-listing-05-clone/src/main.rs new file mode 100644 index 000000000..4e61cc1a1 --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-05-clone/src/main.rs @@ -0,0 +1,8 @@ +fn main() { + // ANCHOR: here + let s1 = String::from("hello"); + let s2 = s1.clone(); + + println!("s1 = {}, s2 = {}", s1, s2); + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-06-copy/Cargo.lock b/src/doc/book/listings/ch04-understanding-ownership/no-listing-06-copy/Cargo.lock new file mode 100644 index 000000000..2aa4918e5 --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-06-copy/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "ownership" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-06-copy/Cargo.toml b/src/doc/book/listings/ch04-understanding-ownership/no-listing-06-copy/Cargo.toml new file mode 100644 index 000000000..e8847526d --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-06-copy/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "ownership" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-06-copy/src/main.rs b/src/doc/book/listings/ch04-understanding-ownership/no-listing-06-copy/src/main.rs new file mode 100644 index 000000000..63a1fae24 --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-06-copy/src/main.rs @@ -0,0 +1,8 @@ +fn main() { + // ANCHOR: here + let x = 5; + let y = x; + + println!("x = {}, y = {}", x, y); + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-07-reference/Cargo.lock b/src/doc/book/listings/ch04-understanding-ownership/no-listing-07-reference/Cargo.lock new file mode 100644 index 000000000..2aa4918e5 --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-07-reference/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "ownership" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-07-reference/Cargo.toml b/src/doc/book/listings/ch04-understanding-ownership/no-listing-07-reference/Cargo.toml new file mode 100644 index 000000000..e8847526d --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-07-reference/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "ownership" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-07-reference/src/main.rs b/src/doc/book/listings/ch04-understanding-ownership/no-listing-07-reference/src/main.rs new file mode 100644 index 000000000..fd32a5fc9 --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-07-reference/src/main.rs @@ -0,0 +1,15 @@ +// ANCHOR: all +fn main() { + // ANCHOR: here + let s1 = String::from("hello"); + + let len = calculate_length(&s1); + // ANCHOR_END: here + + println!("The length of '{}' is {}.", s1, len); +} + +fn calculate_length(s: &String) -> usize { + s.len() +} +// ANCHOR_END: all diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-08-reference-with-annotations/Cargo.lock b/src/doc/book/listings/ch04-understanding-ownership/no-listing-08-reference-with-annotations/Cargo.lock new file mode 100644 index 000000000..9e4e62ddf --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-08-reference-with-annotations/Cargo.lock @@ -0,0 +1,4 @@ +[[package]] +name = "ownership" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-08-reference-with-annotations/Cargo.toml b/src/doc/book/listings/ch04-understanding-ownership/no-listing-08-reference-with-annotations/Cargo.toml new file mode 100644 index 000000000..e8847526d --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-08-reference-with-annotations/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "ownership" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-08-reference-with-annotations/rustfmt-ignore b/src/doc/book/listings/ch04-understanding-ownership/no-listing-08-reference-with-annotations/rustfmt-ignore new file mode 100644 index 000000000..9a53c718a --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-08-reference-with-annotations/rustfmt-ignore @@ -0,0 +1,3 @@ +We have some weird comments pointing out borrowing scopes that we don't want to change; +unfortunately I haven't found a way to skip them with rustfmt that works so for now we're going to +manually skip those listings. See: https://github.com/rust-lang/rustfmt/issues/4028 diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-08-reference-with-annotations/src/main.rs b/src/doc/book/listings/ch04-understanding-ownership/no-listing-08-reference-with-annotations/src/main.rs new file mode 100644 index 000000000..6686a801c --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-08-reference-with-annotations/src/main.rs @@ -0,0 +1,14 @@ +fn main() { + let s1 = String::from("hello"); + + let len = calculate_length(&s1); + + println!("The length of '{}' is {}.", s1, len); +} + +// ANCHOR: here +fn calculate_length(s: &String) -> usize { // s is a reference to a String + s.len() +} // Here, s goes out of scope. But because it does not have ownership of what + // it refers to, it is not dropped. +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-09-fixes-listing-04-06/Cargo.lock b/src/doc/book/listings/ch04-understanding-ownership/no-listing-09-fixes-listing-04-06/Cargo.lock new file mode 100644 index 000000000..2aa4918e5 --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-09-fixes-listing-04-06/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "ownership" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-09-fixes-listing-04-06/Cargo.toml b/src/doc/book/listings/ch04-understanding-ownership/no-listing-09-fixes-listing-04-06/Cargo.toml new file mode 100644 index 000000000..e8847526d --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-09-fixes-listing-04-06/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "ownership" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-09-fixes-listing-04-06/src/main.rs b/src/doc/book/listings/ch04-understanding-ownership/no-listing-09-fixes-listing-04-06/src/main.rs new file mode 100644 index 000000000..fdf7f0a6f --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-09-fixes-listing-04-06/src/main.rs @@ -0,0 +1,9 @@ +fn main() { + let mut s = String::from("hello"); + + change(&mut s); +} + +fn change(some_string: &mut String) { + some_string.push_str(", world"); +} diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-10-multiple-mut-not-allowed/Cargo.lock b/src/doc/book/listings/ch04-understanding-ownership/no-listing-10-multiple-mut-not-allowed/Cargo.lock new file mode 100644 index 000000000..2aa4918e5 --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-10-multiple-mut-not-allowed/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "ownership" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-10-multiple-mut-not-allowed/Cargo.toml b/src/doc/book/listings/ch04-understanding-ownership/no-listing-10-multiple-mut-not-allowed/Cargo.toml new file mode 100644 index 000000000..e8847526d --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-10-multiple-mut-not-allowed/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "ownership" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-10-multiple-mut-not-allowed/output.txt b/src/doc/book/listings/ch04-understanding-ownership/no-listing-10-multiple-mut-not-allowed/output.txt new file mode 100644 index 000000000..71c29f68f --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-10-multiple-mut-not-allowed/output.txt @@ -0,0 +1,15 @@ +$ cargo run + Compiling ownership v0.1.0 (file:///projects/ownership) +error[E0499]: cannot borrow `s` as mutable more than once at a time + --> src/main.rs:5:14 + | +4 | let r1 = &mut s; + | ------ first mutable borrow occurs here +5 | let r2 = &mut s; + | ^^^^^^ second mutable borrow occurs here +6 | +7 | println!("{}, {}", r1, r2); + | -- first borrow later used here + +For more information about this error, try `rustc --explain E0499`. +error: could not compile `ownership` due to previous error diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-10-multiple-mut-not-allowed/src/main.rs b/src/doc/book/listings/ch04-understanding-ownership/no-listing-10-multiple-mut-not-allowed/src/main.rs new file mode 100644 index 000000000..ddbf8120f --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-10-multiple-mut-not-allowed/src/main.rs @@ -0,0 +1,10 @@ +fn main() { + // ANCHOR: here + let mut s = String::from("hello"); + + let r1 = &mut s; + let r2 = &mut s; + + println!("{}, {}", r1, r2); + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-11-muts-in-separate-scopes/Cargo.lock b/src/doc/book/listings/ch04-understanding-ownership/no-listing-11-muts-in-separate-scopes/Cargo.lock new file mode 100644 index 000000000..2aa4918e5 --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-11-muts-in-separate-scopes/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "ownership" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-11-muts-in-separate-scopes/Cargo.toml b/src/doc/book/listings/ch04-understanding-ownership/no-listing-11-muts-in-separate-scopes/Cargo.toml new file mode 100644 index 000000000..e8847526d --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-11-muts-in-separate-scopes/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "ownership" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-11-muts-in-separate-scopes/src/main.rs b/src/doc/book/listings/ch04-understanding-ownership/no-listing-11-muts-in-separate-scopes/src/main.rs new file mode 100644 index 000000000..4b1a5a383 --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-11-muts-in-separate-scopes/src/main.rs @@ -0,0 +1,11 @@ +fn main() { + // ANCHOR: here + let mut s = String::from("hello"); + + { + let r1 = &mut s; + } // r1 goes out of scope here, so we can make a new reference with no problems. + + let r2 = &mut s; + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-12-immutable-and-mutable-not-allowed/Cargo.lock b/src/doc/book/listings/ch04-understanding-ownership/no-listing-12-immutable-and-mutable-not-allowed/Cargo.lock new file mode 100644 index 000000000..2aa4918e5 --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-12-immutable-and-mutable-not-allowed/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "ownership" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-12-immutable-and-mutable-not-allowed/Cargo.toml b/src/doc/book/listings/ch04-understanding-ownership/no-listing-12-immutable-and-mutable-not-allowed/Cargo.toml new file mode 100644 index 000000000..e8847526d --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-12-immutable-and-mutable-not-allowed/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "ownership" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-12-immutable-and-mutable-not-allowed/output.txt b/src/doc/book/listings/ch04-understanding-ownership/no-listing-12-immutable-and-mutable-not-allowed/output.txt new file mode 100644 index 000000000..df94c30e9 --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-12-immutable-and-mutable-not-allowed/output.txt @@ -0,0 +1,16 @@ +$ cargo run + Compiling ownership v0.1.0 (file:///projects/ownership) +error[E0502]: cannot borrow `s` as mutable because it is also borrowed as immutable + --> src/main.rs:6:14 + | +4 | let r1 = &s; // no problem + | -- immutable borrow occurs here +5 | let r2 = &s; // no problem +6 | let r3 = &mut s; // BIG PROBLEM + | ^^^^^^ mutable borrow occurs here +7 | +8 | println!("{}, {}, and {}", r1, r2, r3); + | -- immutable borrow later used here + +For more information about this error, try `rustc --explain E0502`. +error: could not compile `ownership` due to previous error diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-12-immutable-and-mutable-not-allowed/src/main.rs b/src/doc/book/listings/ch04-understanding-ownership/no-listing-12-immutable-and-mutable-not-allowed/src/main.rs new file mode 100644 index 000000000..0da04c010 --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-12-immutable-and-mutable-not-allowed/src/main.rs @@ -0,0 +1,11 @@ +fn main() { + // ANCHOR: here + let mut s = String::from("hello"); + + let r1 = &s; // no problem + let r2 = &s; // no problem + let r3 = &mut s; // BIG PROBLEM + + println!("{}, {}, and {}", r1, r2, r3); + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-13-reference-scope-ends/Cargo.lock b/src/doc/book/listings/ch04-understanding-ownership/no-listing-13-reference-scope-ends/Cargo.lock new file mode 100644 index 000000000..2aa4918e5 --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-13-reference-scope-ends/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "ownership" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-13-reference-scope-ends/Cargo.toml b/src/doc/book/listings/ch04-understanding-ownership/no-listing-13-reference-scope-ends/Cargo.toml new file mode 100644 index 000000000..e8847526d --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-13-reference-scope-ends/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "ownership" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-13-reference-scope-ends/src/main.rs b/src/doc/book/listings/ch04-understanding-ownership/no-listing-13-reference-scope-ends/src/main.rs new file mode 100644 index 000000000..861944966 --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-13-reference-scope-ends/src/main.rs @@ -0,0 +1,13 @@ +fn main() { + // ANCHOR: here + let mut s = String::from("hello"); + + let r1 = &s; // no problem + let r2 = &s; // no problem + println!("{} and {}", r1, r2); + // variables r1 and r2 will not be used after this point + + let r3 = &mut s; // no problem + println!("{}", r3); + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-14-dangling-reference/Cargo.lock b/src/doc/book/listings/ch04-understanding-ownership/no-listing-14-dangling-reference/Cargo.lock new file mode 100644 index 000000000..2aa4918e5 --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-14-dangling-reference/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "ownership" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-14-dangling-reference/Cargo.toml b/src/doc/book/listings/ch04-understanding-ownership/no-listing-14-dangling-reference/Cargo.toml new file mode 100644 index 000000000..e8847526d --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-14-dangling-reference/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "ownership" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-14-dangling-reference/output.txt b/src/doc/book/listings/ch04-understanding-ownership/no-listing-14-dangling-reference/output.txt new file mode 100644 index 000000000..fddca683b --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-14-dangling-reference/output.txt @@ -0,0 +1,16 @@ +$ cargo run + Compiling ownership v0.1.0 (file:///projects/ownership) +error[E0106]: missing lifetime specifier + --> src/main.rs:5:16 + | +5 | fn dangle() -> &String { + | ^ expected named lifetime parameter + | + = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from +help: consider using the `'static` lifetime + | +5 | fn dangle() -> &'static String { + | ~~~~~~~~ + +For more information about this error, try `rustc --explain E0106`. +error: could not compile `ownership` due to previous error diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-14-dangling-reference/src/main.rs b/src/doc/book/listings/ch04-understanding-ownership/no-listing-14-dangling-reference/src/main.rs new file mode 100644 index 000000000..b10269781 --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-14-dangling-reference/src/main.rs @@ -0,0 +1,9 @@ +fn main() { + let reference_to_nothing = dangle(); +} + +fn dangle() -> &String { + let s = String::from("hello"); + + &s +} diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-15-dangling-reference-annotated/Cargo.lock b/src/doc/book/listings/ch04-understanding-ownership/no-listing-15-dangling-reference-annotated/Cargo.lock new file mode 100644 index 000000000..9e4e62ddf --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-15-dangling-reference-annotated/Cargo.lock @@ -0,0 +1,4 @@ +[[package]] +name = "ownership" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-15-dangling-reference-annotated/Cargo.toml b/src/doc/book/listings/ch04-understanding-ownership/no-listing-15-dangling-reference-annotated/Cargo.toml new file mode 100644 index 000000000..e8847526d --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-15-dangling-reference-annotated/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "ownership" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-15-dangling-reference-annotated/rustfmt-ignore b/src/doc/book/listings/ch04-understanding-ownership/no-listing-15-dangling-reference-annotated/rustfmt-ignore new file mode 100644 index 000000000..9a53c718a --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-15-dangling-reference-annotated/rustfmt-ignore @@ -0,0 +1,3 @@ +We have some weird comments pointing out borrowing scopes that we don't want to change; +unfortunately I haven't found a way to skip them with rustfmt that works so for now we're going to +manually skip those listings. See: https://github.com/rust-lang/rustfmt/issues/4028 diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-15-dangling-reference-annotated/src/main.rs b/src/doc/book/listings/ch04-understanding-ownership/no-listing-15-dangling-reference-annotated/src/main.rs new file mode 100644 index 000000000..9fbb372a0 --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-15-dangling-reference-annotated/src/main.rs @@ -0,0 +1,13 @@ +fn main() { + let reference_to_nothing = dangle(); +} + +// ANCHOR: here +fn dangle() -> &String { // dangle returns a reference to a String + + let s = String::from("hello"); // s is a new String + + &s // we return a reference to the String, s +} // Here, s goes out of scope, and is dropped. Its memory goes away. + // Danger! +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-16-no-dangle/Cargo.lock b/src/doc/book/listings/ch04-understanding-ownership/no-listing-16-no-dangle/Cargo.lock new file mode 100644 index 000000000..2aa4918e5 --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-16-no-dangle/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "ownership" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-16-no-dangle/Cargo.toml b/src/doc/book/listings/ch04-understanding-ownership/no-listing-16-no-dangle/Cargo.toml new file mode 100644 index 000000000..e8847526d --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-16-no-dangle/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "ownership" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-16-no-dangle/src/main.rs b/src/doc/book/listings/ch04-understanding-ownership/no-listing-16-no-dangle/src/main.rs new file mode 100644 index 000000000..9c20a3b2d --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-16-no-dangle/src/main.rs @@ -0,0 +1,11 @@ +fn main() { + let string = no_dangle(); +} + +// ANCHOR: here +fn no_dangle() -> String { + let s = String::from("hello"); + + s +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-17-slice/Cargo.lock b/src/doc/book/listings/ch04-understanding-ownership/no-listing-17-slice/Cargo.lock new file mode 100644 index 000000000..2aa4918e5 --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-17-slice/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "ownership" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-17-slice/Cargo.toml b/src/doc/book/listings/ch04-understanding-ownership/no-listing-17-slice/Cargo.toml new file mode 100644 index 000000000..e8847526d --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-17-slice/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "ownership" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-17-slice/src/main.rs b/src/doc/book/listings/ch04-understanding-ownership/no-listing-17-slice/src/main.rs new file mode 100644 index 000000000..44163af99 --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-17-slice/src/main.rs @@ -0,0 +1,8 @@ +fn main() { + // ANCHOR: here + let s = String::from("hello world"); + + let hello = &s[0..5]; + let world = &s[6..11]; + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-18-first-word-slice/Cargo.lock b/src/doc/book/listings/ch04-understanding-ownership/no-listing-18-first-word-slice/Cargo.lock new file mode 100644 index 000000000..2aa4918e5 --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-18-first-word-slice/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "ownership" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-18-first-word-slice/Cargo.toml b/src/doc/book/listings/ch04-understanding-ownership/no-listing-18-first-word-slice/Cargo.toml new file mode 100644 index 000000000..e8847526d --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-18-first-word-slice/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "ownership" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-18-first-word-slice/src/main.rs b/src/doc/book/listings/ch04-understanding-ownership/no-listing-18-first-word-slice/src/main.rs new file mode 100644 index 000000000..f44a970da --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-18-first-word-slice/src/main.rs @@ -0,0 +1,15 @@ +// ANCHOR: here +fn first_word(s: &String) -> &str { + let bytes = s.as_bytes(); + + for (i, &item) in bytes.iter().enumerate() { + if item == b' ' { + return &s[0..i]; + } + } + + &s[..] +} +// ANCHOR_END: here + +fn main() {} diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-19-slice-error/Cargo.lock b/src/doc/book/listings/ch04-understanding-ownership/no-listing-19-slice-error/Cargo.lock new file mode 100644 index 000000000..2aa4918e5 --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-19-slice-error/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "ownership" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-19-slice-error/Cargo.toml b/src/doc/book/listings/ch04-understanding-ownership/no-listing-19-slice-error/Cargo.toml new file mode 100644 index 000000000..e8847526d --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-19-slice-error/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "ownership" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-19-slice-error/output.txt b/src/doc/book/listings/ch04-understanding-ownership/no-listing-19-slice-error/output.txt new file mode 100644 index 000000000..62dc4ad52 --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-19-slice-error/output.txt @@ -0,0 +1,16 @@ +$ cargo run + Compiling ownership v0.1.0 (file:///projects/ownership) +error[E0502]: cannot borrow `s` as mutable because it is also borrowed as immutable + --> src/main.rs:18:5 + | +16 | let word = first_word(&s); + | -- immutable borrow occurs here +17 | +18 | s.clear(); // error! + | ^^^^^^^^^ mutable borrow occurs here +19 | +20 | println!("the first word is: {}", word); + | ---- immutable borrow later used here + +For more information about this error, try `rustc --explain E0502`. +error: could not compile `ownership` due to previous error diff --git a/src/doc/book/listings/ch04-understanding-ownership/no-listing-19-slice-error/src/main.rs b/src/doc/book/listings/ch04-understanding-ownership/no-listing-19-slice-error/src/main.rs new file mode 100644 index 000000000..99e04018d --- /dev/null +++ b/src/doc/book/listings/ch04-understanding-ownership/no-listing-19-slice-error/src/main.rs @@ -0,0 +1,23 @@ +fn first_word(s: &String) -> &str { + let bytes = s.as_bytes(); + + for (i, &item) in bytes.iter().enumerate() { + if item == b' ' { + return &s[0..i]; + } + } + + &s[..] +} + +// ANCHOR: here +fn main() { + let mut s = String::from("hello world"); + + let word = first_word(&s); + + s.clear(); // error! + + println!("the first word is: {}", word); +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-01/Cargo.lock b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-01/Cargo.lock new file mode 100644 index 000000000..bede081a0 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-01/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "structs" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-01/Cargo.toml b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-01/Cargo.toml new file mode 100644 index 000000000..3232b6065 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-01/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "structs" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-01/src/main.rs b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-01/src/main.rs new file mode 100644 index 000000000..16dd15b29 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-01/src/main.rs @@ -0,0 +1,10 @@ +// ANCHOR: here +struct User { + active: bool, + username: String, + email: String, + sign_in_count: u64, +} +// ANCHOR_END: here + +fn main() {} diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-02/Cargo.lock b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-02/Cargo.lock new file mode 100644 index 000000000..bede081a0 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-02/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "structs" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-02/Cargo.toml b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-02/Cargo.toml new file mode 100644 index 000000000..3232b6065 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-02/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "structs" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-02/src/main.rs b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-02/src/main.rs new file mode 100644 index 000000000..e0f7a6cd3 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-02/src/main.rs @@ -0,0 +1,17 @@ +struct User { + active: bool, + username: String, + email: String, + sign_in_count: u64, +} + +// ANCHOR: here +fn main() { + let user1 = User { + email: String::from("someone@example.com"), + username: String::from("someusername123"), + active: true, + sign_in_count: 1, + }; +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-03/Cargo.lock b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-03/Cargo.lock new file mode 100644 index 000000000..bede081a0 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-03/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "structs" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-03/Cargo.toml b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-03/Cargo.toml new file mode 100644 index 000000000..3232b6065 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-03/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "structs" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-03/src/main.rs b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-03/src/main.rs new file mode 100644 index 000000000..7a078e7e8 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-03/src/main.rs @@ -0,0 +1,19 @@ +struct User { + active: bool, + username: String, + email: String, + sign_in_count: u64, +} + +// ANCHOR: here +fn main() { + let mut user1 = User { + email: String::from("someone@example.com"), + username: String::from("someusername123"), + active: true, + sign_in_count: 1, + }; + + user1.email = String::from("anotheremail@example.com"); +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-04/Cargo.lock b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-04/Cargo.lock new file mode 100644 index 000000000..bede081a0 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-04/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "structs" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-04/Cargo.toml b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-04/Cargo.toml new file mode 100644 index 000000000..3232b6065 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-04/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "structs" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-04/src/main.rs b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-04/src/main.rs new file mode 100644 index 000000000..aa7823af4 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-04/src/main.rs @@ -0,0 +1,24 @@ +struct User { + active: bool, + username: String, + email: String, + sign_in_count: u64, +} + +// ANCHOR: here +fn build_user(email: String, username: String) -> User { + User { + email: email, + username: username, + active: true, + sign_in_count: 1, + } +} +// ANCHOR_END: here + +fn main() { + let user1 = build_user( + String::from("someone@example.com"), + String::from("someusername123"), + ); +} diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-05/Cargo.lock b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-05/Cargo.lock new file mode 100644 index 000000000..bede081a0 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-05/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "structs" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-05/Cargo.toml b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-05/Cargo.toml new file mode 100644 index 000000000..3232b6065 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-05/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "structs" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-05/src/main.rs b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-05/src/main.rs new file mode 100644 index 000000000..8d84a3060 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-05/src/main.rs @@ -0,0 +1,24 @@ +struct User { + active: bool, + username: String, + email: String, + sign_in_count: u64, +} + +// ANCHOR: here +fn build_user(email: String, username: String) -> User { + User { + email, + username, + active: true, + sign_in_count: 1, + } +} +// ANCHOR_END: here + +fn main() { + let user1 = build_user( + String::from("someone@example.com"), + String::from("someusername123"), + ); +} diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-06/Cargo.lock b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-06/Cargo.lock new file mode 100644 index 000000000..bede081a0 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-06/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "structs" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-06/Cargo.toml b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-06/Cargo.toml new file mode 100644 index 000000000..3232b6065 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-06/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "structs" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-06/src/main.rs b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-06/src/main.rs new file mode 100644 index 000000000..15e7690e1 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-06/src/main.rs @@ -0,0 +1,28 @@ +struct User { + active: bool, + username: String, + email: String, + sign_in_count: u64, +} + +// ANCHOR: here +fn main() { + // --snip-- + // ANCHOR_END: here + + let user1 = User { + email: String::from("someone@example.com"), + username: String::from("someusername123"), + active: true, + sign_in_count: 1, + }; + // ANCHOR: here + + let user2 = User { + active: user1.active, + username: user1.username, + email: String::from("another@example.com"), + sign_in_count: user1.sign_in_count, + }; +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-07/Cargo.lock b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-07/Cargo.lock new file mode 100644 index 000000000..bede081a0 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-07/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "structs" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-07/Cargo.toml b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-07/Cargo.toml new file mode 100644 index 000000000..3232b6065 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-07/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "structs" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-07/src/main.rs b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-07/src/main.rs new file mode 100644 index 000000000..008ad18f6 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-07/src/main.rs @@ -0,0 +1,26 @@ +struct User { + active: bool, + username: String, + email: String, + sign_in_count: u64, +} + +// ANCHOR: here +fn main() { + // --snip-- + // ANCHOR_END: here + + let user1 = User { + email: String::from("someone@example.com"), + username: String::from("someusername123"), + active: true, + sign_in_count: 1, + }; + // ANCHOR: here + + let user2 = User { + email: String::from("another@example.com"), + ..user1 + }; +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-08/Cargo.lock b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-08/Cargo.lock new file mode 100644 index 000000000..4aabe7da6 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-08/Cargo.lock @@ -0,0 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "rectangles" +version = "0.1.0" diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-08/Cargo.toml b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-08/Cargo.toml new file mode 100644 index 000000000..4a279a450 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-08/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "rectangles" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-08/output.txt b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-08/output.txt new file mode 100644 index 000000000..c44b58238 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-08/output.txt @@ -0,0 +1,5 @@ +$ cargo run + Compiling rectangles v0.1.0 (file:///projects/rectangles) + Finished dev [unoptimized + debuginfo] target(s) in 0.42s + Running `target/debug/rectangles` +The area of the rectangle is 1500 square pixels. diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-08/src/main.rs b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-08/src/main.rs new file mode 100644 index 000000000..f324529fd --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-08/src/main.rs @@ -0,0 +1,17 @@ +// ANCHOR: all +fn main() { + let width1 = 30; + let height1 = 50; + + println!( + "The area of the rectangle is {} square pixels.", + area(width1, height1) + ); +} + +// ANCHOR: here +fn area(width: u32, height: u32) -> u32 { + // ANCHOR_END: here + width * height +} +// ANCHOR_END: all diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-09/Cargo.lock b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-09/Cargo.lock new file mode 100644 index 000000000..4aabe7da6 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-09/Cargo.lock @@ -0,0 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "rectangles" +version = "0.1.0" diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-09/Cargo.toml b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-09/Cargo.toml new file mode 100644 index 000000000..4a279a450 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-09/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "rectangles" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-09/src/main.rs b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-09/src/main.rs new file mode 100644 index 000000000..d4b77ba7a --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-09/src/main.rs @@ -0,0 +1,12 @@ +fn main() { + let rect1 = (30, 50); + + println!( + "The area of the rectangle is {} square pixels.", + area(rect1) + ); +} + +fn area(dimensions: (u32, u32)) -> u32 { + dimensions.0 * dimensions.1 +} diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-10/Cargo.lock b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-10/Cargo.lock new file mode 100644 index 000000000..4aabe7da6 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-10/Cargo.lock @@ -0,0 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "rectangles" +version = "0.1.0" diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-10/Cargo.toml b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-10/Cargo.toml new file mode 100644 index 000000000..4a279a450 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-10/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "rectangles" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-10/src/main.rs b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-10/src/main.rs new file mode 100644 index 000000000..62ef9acd8 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-10/src/main.rs @@ -0,0 +1,20 @@ +struct Rectangle { + width: u32, + height: u32, +} + +fn main() { + let rect1 = Rectangle { + width: 30, + height: 50, + }; + + println!( + "The area of the rectangle is {} square pixels.", + area(&rect1) + ); +} + +fn area(rectangle: &Rectangle) -> u32 { + rectangle.width * rectangle.height +} diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-11/Cargo.lock b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-11/Cargo.lock new file mode 100644 index 000000000..4aabe7da6 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-11/Cargo.lock @@ -0,0 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "rectangles" +version = "0.1.0" diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-11/Cargo.toml b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-11/Cargo.toml new file mode 100644 index 000000000..4a279a450 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-11/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "rectangles" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-11/output.txt b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-11/output.txt new file mode 100644 index 000000000..b761fccd6 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-11/output.txt @@ -0,0 +1,14 @@ +$ cargo run + Compiling rectangles v0.1.0 (file:///projects/rectangles) +error[E0277]: `Rectangle` doesn't implement `std::fmt::Display` + --> src/main.rs:12:29 + | +12 | println!("rect1 is {}", rect1); + | ^^^^^ `Rectangle` cannot be formatted with the default formatter + | + = help: the trait `std::fmt::Display` is not implemented for `Rectangle` + = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead + = note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info) + +For more information about this error, try `rustc --explain E0277`. +error: could not compile `rectangles` due to previous error diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-11/src/main.rs b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-11/src/main.rs new file mode 100644 index 000000000..0ff8dcc8c --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-11/src/main.rs @@ -0,0 +1,13 @@ +struct Rectangle { + width: u32, + height: u32, +} + +fn main() { + let rect1 = Rectangle { + width: 30, + height: 50, + }; + + println!("rect1 is {}", rect1); +} diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-12/Cargo.lock b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-12/Cargo.lock new file mode 100644 index 000000000..4aabe7da6 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-12/Cargo.lock @@ -0,0 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "rectangles" +version = "0.1.0" diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-12/Cargo.toml b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-12/Cargo.toml new file mode 100644 index 000000000..4a279a450 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-12/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "rectangles" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-12/output.txt b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-12/output.txt new file mode 100644 index 000000000..c37be6b5b --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-12/output.txt @@ -0,0 +1,5 @@ +$ cargo run + Compiling rectangles v0.1.0 (file:///projects/rectangles) + Finished dev [unoptimized + debuginfo] target(s) in 0.48s + Running `target/debug/rectangles` +rect1 is Rectangle { width: 30, height: 50 } diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-12/src/main.rs b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-12/src/main.rs new file mode 100644 index 000000000..2ffc4b8e7 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-12/src/main.rs @@ -0,0 +1,14 @@ +#[derive(Debug)] +struct Rectangle { + width: u32, + height: u32, +} + +fn main() { + let rect1 = Rectangle { + width: 30, + height: 50, + }; + + println!("rect1 is {:?}", rect1); +} diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-13/Cargo.lock b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-13/Cargo.lock new file mode 100644 index 000000000..4aabe7da6 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-13/Cargo.lock @@ -0,0 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "rectangles" +version = "0.1.0" diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-13/Cargo.toml b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-13/Cargo.toml new file mode 100644 index 000000000..4a279a450 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-13/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "rectangles" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-13/src/main.rs b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-13/src/main.rs new file mode 100644 index 000000000..e4f45e868 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-13/src/main.rs @@ -0,0 +1,23 @@ +#[derive(Debug)] +struct Rectangle { + width: u32, + height: u32, +} + +impl Rectangle { + fn area(&self) -> u32 { + self.width * self.height + } +} + +fn main() { + let rect1 = Rectangle { + width: 30, + height: 50, + }; + + println!( + "The area of the rectangle is {} square pixels.", + rect1.area() + ); +} diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-14/Cargo.lock b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-14/Cargo.lock new file mode 100644 index 000000000..4aabe7da6 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-14/Cargo.lock @@ -0,0 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "rectangles" +version = "0.1.0" diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-14/Cargo.toml b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-14/Cargo.toml new file mode 100644 index 000000000..4a279a450 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-14/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "rectangles" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-14/src/main.rs b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-14/src/main.rs new file mode 100644 index 000000000..843dab481 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-14/src/main.rs @@ -0,0 +1,17 @@ +fn main() { + let rect1 = Rectangle { + width: 30, + height: 50, + }; + let rect2 = Rectangle { + width: 10, + height: 40, + }; + let rect3 = Rectangle { + width: 60, + height: 45, + }; + + println!("Can rect1 hold rect2? {}", rect1.can_hold(&rect2)); + println!("Can rect1 hold rect3? {}", rect1.can_hold(&rect3)); +} diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-15/Cargo.lock b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-15/Cargo.lock new file mode 100644 index 000000000..4aabe7da6 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-15/Cargo.lock @@ -0,0 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "rectangles" +version = "0.1.0" diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-15/Cargo.toml b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-15/Cargo.toml new file mode 100644 index 000000000..4a279a450 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-15/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "rectangles" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-15/src/main.rs b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-15/src/main.rs new file mode 100644 index 000000000..e6a32723f --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-15/src/main.rs @@ -0,0 +1,35 @@ +#[derive(Debug)] +struct Rectangle { + width: u32, + height: u32, +} + +// ANCHOR: here +impl Rectangle { + fn area(&self) -> u32 { + self.width * self.height + } + + fn can_hold(&self, other: &Rectangle) -> bool { + self.width > other.width && self.height > other.height + } +} +// ANCHOR_END: here + +fn main() { + let rect1 = Rectangle { + width: 30, + height: 50, + }; + let rect2 = Rectangle { + width: 10, + height: 40, + }; + let rect3 = Rectangle { + width: 60, + height: 45, + }; + + println!("Can rect1 hold rect2? {}", rect1.can_hold(&rect2)); + println!("Can rect1 hold rect3? {}", rect1.can_hold(&rect3)); +} diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-16/Cargo.lock b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-16/Cargo.lock new file mode 100644 index 000000000..4aabe7da6 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-16/Cargo.lock @@ -0,0 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "rectangles" +version = "0.1.0" diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-16/Cargo.toml b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-16/Cargo.toml new file mode 100644 index 000000000..4a279a450 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-16/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "rectangles" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-16/src/main.rs b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-16/src/main.rs new file mode 100644 index 000000000..a5d3f772a --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/listing-05-16/src/main.rs @@ -0,0 +1,37 @@ +#[derive(Debug)] +struct Rectangle { + width: u32, + height: u32, +} + +// ANCHOR: here +impl Rectangle { + fn area(&self) -> u32 { + self.width * self.height + } +} + +impl Rectangle { + fn can_hold(&self, other: &Rectangle) -> bool { + self.width > other.width && self.height > other.height + } +} +// ANCHOR_END: here + +fn main() { + let rect1 = Rectangle { + width: 30, + height: 50, + }; + let rect2 = Rectangle { + width: 10, + height: 40, + }; + let rect3 = Rectangle { + width: 60, + height: 45, + }; + + println!("Can rect1 hold rect2? {}", rect1.can_hold(&rect2)); + println!("Can rect1 hold rect3? {}", rect1.can_hold(&rect3)); +} diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-01-tuple-structs/Cargo.lock b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-01-tuple-structs/Cargo.lock new file mode 100644 index 000000000..bede081a0 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-01-tuple-structs/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "structs" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-01-tuple-structs/Cargo.toml b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-01-tuple-structs/Cargo.toml new file mode 100644 index 000000000..3232b6065 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-01-tuple-structs/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "structs" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-01-tuple-structs/src/main.rs b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-01-tuple-structs/src/main.rs new file mode 100644 index 000000000..0d993162b --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-01-tuple-structs/src/main.rs @@ -0,0 +1,7 @@ +struct Color(i32, i32, i32); +struct Point(i32, i32, i32); + +fn main() { + let black = Color(0, 0, 0); + let origin = Point(0, 0, 0); +} diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-02-reference-in-struct/Cargo.lock b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-02-reference-in-struct/Cargo.lock new file mode 100644 index 000000000..bede081a0 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-02-reference-in-struct/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "structs" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-02-reference-in-struct/Cargo.toml b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-02-reference-in-struct/Cargo.toml new file mode 100644 index 000000000..d36dbc1d3 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-02-reference-in-struct/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "structs" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-02-reference-in-struct/output.txt b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-02-reference-in-struct/output.txt new file mode 100644 index 000000000..e28da599c --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-02-reference-in-struct/output.txt @@ -0,0 +1,31 @@ +$ cargo run + Compiling structs v0.1.0 (file:///projects/structs) +error[E0106]: missing lifetime specifier + --> src/main.rs:3:15 + | +3 | username: &str, + | ^ expected named lifetime parameter + | +help: consider introducing a named lifetime parameter + | +1 ~ struct User<'a> { +2 | active: bool, +3 ~ username: &'a str, + | + +error[E0106]: missing lifetime specifier + --> src/main.rs:4:12 + | +4 | email: &str, + | ^ expected named lifetime parameter + | +help: consider introducing a named lifetime parameter + | +1 ~ struct User<'a> { +2 | active: bool, +3 | username: &str, +4 ~ email: &'a str, + | + +For more information about this error, try `rustc --explain E0106`. +error: could not compile `structs` due to 2 previous errors diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-02-reference-in-struct/src/main.rs b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-02-reference-in-struct/src/main.rs new file mode 100644 index 000000000..96092d042 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-02-reference-in-struct/src/main.rs @@ -0,0 +1,15 @@ +struct User { + active: bool, + username: &str, + email: &str, + sign_in_count: u64, +} + +fn main() { + let user1 = User { + email: "someone@example.com", + username: "someusername123", + active: true, + sign_in_count: 1, + }; +} diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-03-associated-functions/Cargo.lock b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-03-associated-functions/Cargo.lock new file mode 100644 index 000000000..4aabe7da6 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-03-associated-functions/Cargo.lock @@ -0,0 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "rectangles" +version = "0.1.0" diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-03-associated-functions/Cargo.toml b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-03-associated-functions/Cargo.toml new file mode 100644 index 000000000..4a279a450 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-03-associated-functions/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "rectangles" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-03-associated-functions/src/main.rs b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-03-associated-functions/src/main.rs new file mode 100644 index 000000000..47fedc552 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-03-associated-functions/src/main.rs @@ -0,0 +1,20 @@ +#[derive(Debug)] +struct Rectangle { + width: u32, + height: u32, +} + +// ANCHOR: here +impl Rectangle { + fn square(size: u32) -> Self { + Self { + width: size, + height: size, + } + } +} +// ANCHOR_END: here + +fn main() { + let sq = Rectangle::square(3); +} diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-04-unit-like-structs/Cargo.lock b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-04-unit-like-structs/Cargo.lock new file mode 100644 index 000000000..fb30ed9c8 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-04-unit-like-structs/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "structs" +version = "0.1.0" diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-04-unit-like-structs/Cargo.toml b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-04-unit-like-structs/Cargo.toml new file mode 100644 index 000000000..3232b6065 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-04-unit-like-structs/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "structs" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-04-unit-like-structs/src/main.rs b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-04-unit-like-structs/src/main.rs new file mode 100644 index 000000000..d48c94e99 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-04-unit-like-structs/src/main.rs @@ -0,0 +1,5 @@ +struct AlwaysEqual; + +fn main() { + let subject = AlwaysEqual; +} diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-05-dbg-macro/Cargo.lock b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-05-dbg-macro/Cargo.lock new file mode 100644 index 000000000..4aabe7da6 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-05-dbg-macro/Cargo.lock @@ -0,0 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "rectangles" +version = "0.1.0" diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-05-dbg-macro/Cargo.toml b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-05-dbg-macro/Cargo.toml new file mode 100644 index 000000000..4a279a450 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-05-dbg-macro/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "rectangles" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-05-dbg-macro/output.txt b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-05-dbg-macro/output.txt new file mode 100644 index 000000000..268585995 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-05-dbg-macro/output.txt @@ -0,0 +1,9 @@ +$ cargo run + Compiling rectangles v0.1.0 (file:///projects/rectangles) + Finished dev [unoptimized + debuginfo] target(s) in 0.61s + Running `target/debug/rectangles` +[src/main.rs:10] 30 * scale = 60 +[src/main.rs:14] &rect1 = Rectangle { + width: 60, + height: 50, +} diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-05-dbg-macro/src/main.rs b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-05-dbg-macro/src/main.rs new file mode 100644 index 000000000..dd0342959 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-05-dbg-macro/src/main.rs @@ -0,0 +1,15 @@ +#[derive(Debug)] +struct Rectangle { + width: u32, + height: u32, +} + +fn main() { + let scale = 2; + let rect1 = Rectangle { + width: dbg!(30 * scale), + height: 50, + }; + + dbg!(&rect1); +} diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-06-method-field-interaction/Cargo.lock b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-06-method-field-interaction/Cargo.lock new file mode 100644 index 000000000..4aabe7da6 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-06-method-field-interaction/Cargo.lock @@ -0,0 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "rectangles" +version = "0.1.0" diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-06-method-field-interaction/Cargo.toml b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-06-method-field-interaction/Cargo.toml new file mode 100644 index 000000000..4a279a450 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-06-method-field-interaction/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "rectangles" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-06-method-field-interaction/src/main.rs b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-06-method-field-interaction/src/main.rs new file mode 100644 index 000000000..00e1de83b --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/no-listing-06-method-field-interaction/src/main.rs @@ -0,0 +1,24 @@ +#[derive(Debug)] +struct Rectangle { + width: u32, + height: u32, +} + +// ANCHOR: here +impl Rectangle { + fn width(&self) -> bool { + self.width > 0 + } +} + +fn main() { + let rect1 = Rectangle { + width: 30, + height: 50, + }; + + if rect1.width() { + println!("The rectangle has a nonzero width; it is {}", rect1.width); + } +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/output-only-01-debug/Cargo.lock b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/output-only-01-debug/Cargo.lock new file mode 100644 index 000000000..4aabe7da6 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/output-only-01-debug/Cargo.lock @@ -0,0 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "rectangles" +version = "0.1.0" diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/output-only-01-debug/Cargo.toml b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/output-only-01-debug/Cargo.toml new file mode 100644 index 000000000..4a279a450 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/output-only-01-debug/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "rectangles" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/output-only-01-debug/output.txt b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/output-only-01-debug/output.txt new file mode 100644 index 000000000..69c8b38a6 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/output-only-01-debug/output.txt @@ -0,0 +1,18 @@ +$ cargo run + Compiling rectangles v0.1.0 (file:///projects/rectangles) +error[E0277]: `Rectangle` doesn't implement `Debug` + --> src/main.rs:12:31 + | +12 | println!("rect1 is {:?}", rect1); + | ^^^^^ `Rectangle` cannot be formatted using `{:?}` + | + = help: the trait `Debug` is not implemented for `Rectangle` + = note: add `#[derive(Debug)]` to `Rectangle` or manually `impl Debug for Rectangle` + = note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info) +help: consider annotating `Rectangle` with `#[derive(Debug)]` + | +1 | #[derive(Debug)] + | + +For more information about this error, try `rustc --explain E0277`. +error: could not compile `rectangles` due to previous error diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/output-only-01-debug/src/main.rs b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/output-only-01-debug/src/main.rs new file mode 100644 index 000000000..019a357ab --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/output-only-01-debug/src/main.rs @@ -0,0 +1,13 @@ +struct Rectangle { + width: u32, + height: u32, +} + +fn main() { + let rect1 = Rectangle { + width: 30, + height: 50, + }; + + println!("rect1 is {:?}", rect1); +} diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/output-only-02-pretty-debug/Cargo.lock b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/output-only-02-pretty-debug/Cargo.lock new file mode 100644 index 000000000..4aabe7da6 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/output-only-02-pretty-debug/Cargo.lock @@ -0,0 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "rectangles" +version = "0.1.0" diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/output-only-02-pretty-debug/Cargo.toml b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/output-only-02-pretty-debug/Cargo.toml new file mode 100644 index 000000000..4a279a450 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/output-only-02-pretty-debug/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "rectangles" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/output-only-02-pretty-debug/output.txt b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/output-only-02-pretty-debug/output.txt new file mode 100644 index 000000000..db6deed9b --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/output-only-02-pretty-debug/output.txt @@ -0,0 +1,8 @@ +$ cargo run + Compiling rectangles v0.1.0 (file:///projects/rectangles) + Finished dev [unoptimized + debuginfo] target(s) in 0.48s + Running `target/debug/rectangles` +rect1 is Rectangle { + width: 30, + height: 50, +} diff --git a/src/doc/book/listings/ch05-using-structs-to-structure-related-data/output-only-02-pretty-debug/src/main.rs b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/output-only-02-pretty-debug/src/main.rs new file mode 100644 index 000000000..84e32aee4 --- /dev/null +++ b/src/doc/book/listings/ch05-using-structs-to-structure-related-data/output-only-02-pretty-debug/src/main.rs @@ -0,0 +1,14 @@ +#[derive(Debug)] +struct Rectangle { + width: u32, + height: u32, +} + +fn main() { + let rect1 = Rectangle { + width: 30, + height: 50, + }; + + println!("rect1 is {:#?}", rect1); +} diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-01/Cargo.lock b/src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-01/Cargo.lock new file mode 100644 index 000000000..f62e8ac45 --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-01/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "enums" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-01/Cargo.toml b/src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-01/Cargo.toml new file mode 100644 index 000000000..e959295f9 --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-01/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "enums" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-01/src/main.rs b/src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-01/src/main.rs new file mode 100644 index 000000000..5b688e0f2 --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-01/src/main.rs @@ -0,0 +1,23 @@ +fn main() { + // ANCHOR: here + enum IpAddrKind { + V4, + V6, + } + + struct IpAddr { + kind: IpAddrKind, + address: String, + } + + let home = IpAddr { + kind: IpAddrKind::V4, + address: String::from("127.0.0.1"), + }; + + let loopback = IpAddr { + kind: IpAddrKind::V6, + address: String::from("::1"), + }; + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-02/Cargo.lock b/src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-02/Cargo.lock new file mode 100644 index 000000000..f62e8ac45 --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-02/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "enums" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-02/Cargo.toml b/src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-02/Cargo.toml new file mode 100644 index 000000000..e959295f9 --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-02/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "enums" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-02/src/main.rs b/src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-02/src/main.rs new file mode 100644 index 000000000..3ba749788 --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-02/src/main.rs @@ -0,0 +1,10 @@ +// ANCHOR: here +enum Message { + Quit, + Move { x: i32, y: i32 }, + Write(String), + ChangeColor(i32, i32, i32), +} +// ANCHOR_END: here + +fn main() {} diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-03/Cargo.lock b/src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-03/Cargo.lock new file mode 100644 index 000000000..f62e8ac45 --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-03/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "enums" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-03/Cargo.toml b/src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-03/Cargo.toml new file mode 100644 index 000000000..e959295f9 --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-03/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "enums" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-03/src/main.rs b/src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-03/src/main.rs new file mode 100644 index 000000000..93dce48cb --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-03/src/main.rs @@ -0,0 +1,19 @@ +// ANCHOR: here +enum Coin { + Penny, + Nickel, + Dime, + Quarter, +} + +fn value_in_cents(coin: Coin) -> u8 { + match coin { + Coin::Penny => 1, + Coin::Nickel => 5, + Coin::Dime => 10, + Coin::Quarter => 25, + } +} +// ANCHOR_END: here + +fn main() {} diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-04/Cargo.lock b/src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-04/Cargo.lock new file mode 100644 index 000000000..f62e8ac45 --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-04/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "enums" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-04/Cargo.toml b/src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-04/Cargo.toml new file mode 100644 index 000000000..e959295f9 --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-04/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "enums" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-04/src/main.rs b/src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-04/src/main.rs new file mode 100644 index 000000000..3ba384fba --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-04/src/main.rs @@ -0,0 +1,17 @@ +// ANCHOR: here +#[derive(Debug)] // so we can inspect the state in a minute +enum UsState { + Alabama, + Alaska, + // --snip-- +} + +enum Coin { + Penny, + Nickel, + Dime, + Quarter(UsState), +} +// ANCHOR_END: here + +fn main() {} diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-05/Cargo.lock b/src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-05/Cargo.lock new file mode 100644 index 000000000..f62e8ac45 --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-05/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "enums" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-05/Cargo.toml b/src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-05/Cargo.toml new file mode 100644 index 000000000..e959295f9 --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-05/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "enums" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-05/src/main.rs b/src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-05/src/main.rs new file mode 100644 index 000000000..c86190aa7 --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-05/src/main.rs @@ -0,0 +1,18 @@ +fn main() { + // ANCHOR: here + fn plus_one(x: Option) -> Option { + match x { + // ANCHOR: first_arm + None => None, + // ANCHOR_END: first_arm + // ANCHOR: second_arm + Some(i) => Some(i + 1), + // ANCHOR_END: second_arm + } + } + + let five = Some(5); + let six = plus_one(five); + let none = plus_one(None); + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-06/Cargo.lock b/src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-06/Cargo.lock new file mode 100644 index 000000000..f62e8ac45 --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-06/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "enums" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-06/Cargo.toml b/src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-06/Cargo.toml new file mode 100644 index 000000000..e959295f9 --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-06/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "enums" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-06/src/main.rs b/src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-06/src/main.rs new file mode 100644 index 000000000..dc2bffb91 --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/listing-06-06/src/main.rs @@ -0,0 +1,9 @@ +fn main() { + // ANCHOR: here + let config_max = Some(3u8); + match config_max { + Some(max) => println!("The maximum is configured to be {}", max), + _ => (), + } + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-01-defining-enums/Cargo.lock b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-01-defining-enums/Cargo.lock new file mode 100644 index 000000000..f62e8ac45 --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-01-defining-enums/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "enums" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-01-defining-enums/Cargo.toml b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-01-defining-enums/Cargo.toml new file mode 100644 index 000000000..e959295f9 --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-01-defining-enums/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "enums" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-01-defining-enums/src/main.rs b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-01-defining-enums/src/main.rs new file mode 100644 index 000000000..c631e56ba --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-01-defining-enums/src/main.rs @@ -0,0 +1,22 @@ +// ANCHOR: def +enum IpAddrKind { + V4, + V6, +} +// ANCHOR_END: def + +fn main() { + // ANCHOR: instance + let four = IpAddrKind::V4; + let six = IpAddrKind::V6; + // ANCHOR_END: instance + + // ANCHOR: fn_call + route(IpAddrKind::V4); + route(IpAddrKind::V6); + // ANCHOR_END: fn_call +} + +// ANCHOR: fn +fn route(ip_kind: IpAddrKind) {} +// ANCHOR_END: fn diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-02-enum-with-data/Cargo.lock b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-02-enum-with-data/Cargo.lock new file mode 100644 index 000000000..f62e8ac45 --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-02-enum-with-data/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "enums" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-02-enum-with-data/Cargo.toml b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-02-enum-with-data/Cargo.toml new file mode 100644 index 000000000..e959295f9 --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-02-enum-with-data/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "enums" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-02-enum-with-data/src/main.rs b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-02-enum-with-data/src/main.rs new file mode 100644 index 000000000..7d59b811f --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-02-enum-with-data/src/main.rs @@ -0,0 +1,12 @@ +fn main() { + // ANCHOR: here + enum IpAddr { + V4(String), + V6(String), + } + + let home = IpAddr::V4(String::from("127.0.0.1")); + + let loopback = IpAddr::V6(String::from("::1")); + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-03-variants-with-different-data/Cargo.lock b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-03-variants-with-different-data/Cargo.lock new file mode 100644 index 000000000..f62e8ac45 --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-03-variants-with-different-data/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "enums" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-03-variants-with-different-data/Cargo.toml b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-03-variants-with-different-data/Cargo.toml new file mode 100644 index 000000000..e959295f9 --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-03-variants-with-different-data/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "enums" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-03-variants-with-different-data/src/main.rs b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-03-variants-with-different-data/src/main.rs new file mode 100644 index 000000000..844a14041 --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-03-variants-with-different-data/src/main.rs @@ -0,0 +1,12 @@ +fn main() { + // ANCHOR: here + enum IpAddr { + V4(u8, u8, u8, u8), + V6(String), + } + + let home = IpAddr::V4(127, 0, 0, 1); + + let loopback = IpAddr::V6(String::from("::1")); + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-04-structs-similar-to-message-enum/Cargo.lock b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-04-structs-similar-to-message-enum/Cargo.lock new file mode 100644 index 000000000..f62e8ac45 --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-04-structs-similar-to-message-enum/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "enums" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-04-structs-similar-to-message-enum/Cargo.toml b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-04-structs-similar-to-message-enum/Cargo.toml new file mode 100644 index 000000000..e959295f9 --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-04-structs-similar-to-message-enum/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "enums" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-04-structs-similar-to-message-enum/src/main.rs b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-04-structs-similar-to-message-enum/src/main.rs new file mode 100644 index 000000000..df451be8b --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-04-structs-similar-to-message-enum/src/main.rs @@ -0,0 +1,11 @@ +// ANCHOR: here +struct QuitMessage; // unit struct +struct MoveMessage { + x: i32, + y: i32, +} +struct WriteMessage(String); // tuple struct +struct ChangeColorMessage(i32, i32, i32); // tuple struct + // ANCHOR_END: here + +fn main() {} diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-05-methods-on-enums/Cargo.lock b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-05-methods-on-enums/Cargo.lock new file mode 100644 index 000000000..f62e8ac45 --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-05-methods-on-enums/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "enums" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-05-methods-on-enums/Cargo.toml b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-05-methods-on-enums/Cargo.toml new file mode 100644 index 000000000..e959295f9 --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-05-methods-on-enums/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "enums" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-05-methods-on-enums/src/main.rs b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-05-methods-on-enums/src/main.rs new file mode 100644 index 000000000..66e0b6da1 --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-05-methods-on-enums/src/main.rs @@ -0,0 +1,19 @@ +fn main() { + enum Message { + Quit, + Move { x: i32, y: i32 }, + Write(String), + ChangeColor(i32, i32, i32), + } + + // ANCHOR: here + impl Message { + fn call(&self) { + // method body would be defined here + } + } + + let m = Message::Write(String::from("hello")); + m.call(); + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-06-option-examples/Cargo.lock b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-06-option-examples/Cargo.lock new file mode 100644 index 000000000..f62e8ac45 --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-06-option-examples/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "enums" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-06-option-examples/Cargo.toml b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-06-option-examples/Cargo.toml new file mode 100644 index 000000000..e959295f9 --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-06-option-examples/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "enums" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-06-option-examples/src/main.rs b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-06-option-examples/src/main.rs new file mode 100644 index 000000000..be552bfa5 --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-06-option-examples/src/main.rs @@ -0,0 +1,8 @@ +fn main() { + // ANCHOR: here + let some_number = Some(5); + let some_char = Some('e'); + + let absent_number: Option = None; + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-07-cant-use-option-directly/Cargo.lock b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-07-cant-use-option-directly/Cargo.lock new file mode 100644 index 000000000..f62e8ac45 --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-07-cant-use-option-directly/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "enums" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-07-cant-use-option-directly/Cargo.toml b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-07-cant-use-option-directly/Cargo.toml new file mode 100644 index 000000000..e959295f9 --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-07-cant-use-option-directly/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "enums" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-07-cant-use-option-directly/output.txt b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-07-cant-use-option-directly/output.txt new file mode 100644 index 000000000..d4a040e8e --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-07-cant-use-option-directly/output.txt @@ -0,0 +1,22 @@ +$ cargo run + Compiling enums v0.1.0 (file:///projects/enums) +error[E0277]: cannot add `Option` to `i8` + --> src/main.rs:5:17 + | +5 | let sum = x + y; + | ^ no implementation for `i8 + Option` + | + = help: the trait `Add>` is not implemented for `i8` + = help: the following other types implement trait `Add`: + <&'a f32 as Add> + <&'a f64 as Add> + <&'a i128 as Add> + <&'a i16 as Add> + <&'a i32 as Add> + <&'a i64 as Add> + <&'a i8 as Add> + <&'a isize as Add> + and 48 others + +For more information about this error, try `rustc --explain E0277`. +error: could not compile `enums` due to previous error diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-07-cant-use-option-directly/src/main.rs b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-07-cant-use-option-directly/src/main.rs new file mode 100644 index 000000000..ec65565d4 --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-07-cant-use-option-directly/src/main.rs @@ -0,0 +1,8 @@ +fn main() { + // ANCHOR: here + let x: i8 = 5; + let y: Option = Some(5); + + let sum = x + y; + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-08-match-arm-multiple-lines/Cargo.lock b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-08-match-arm-multiple-lines/Cargo.lock new file mode 100644 index 000000000..f62e8ac45 --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-08-match-arm-multiple-lines/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "enums" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-08-match-arm-multiple-lines/Cargo.toml b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-08-match-arm-multiple-lines/Cargo.toml new file mode 100644 index 000000000..e959295f9 --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-08-match-arm-multiple-lines/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "enums" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-08-match-arm-multiple-lines/src/main.rs b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-08-match-arm-multiple-lines/src/main.rs new file mode 100644 index 000000000..3f909dcaf --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-08-match-arm-multiple-lines/src/main.rs @@ -0,0 +1,22 @@ +enum Coin { + Penny, + Nickel, + Dime, + Quarter, +} + +// ANCHOR: here +fn value_in_cents(coin: Coin) -> u8 { + match coin { + Coin::Penny => { + println!("Lucky penny!"); + 1 + } + Coin::Nickel => 5, + Coin::Dime => 10, + Coin::Quarter => 25, + } +} +// ANCHOR_END: here + +fn main() {} diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-09-variable-in-pattern/Cargo.lock b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-09-variable-in-pattern/Cargo.lock new file mode 100644 index 000000000..f62e8ac45 --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-09-variable-in-pattern/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "enums" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-09-variable-in-pattern/Cargo.toml b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-09-variable-in-pattern/Cargo.toml new file mode 100644 index 000000000..e959295f9 --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-09-variable-in-pattern/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "enums" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-09-variable-in-pattern/src/main.rs b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-09-variable-in-pattern/src/main.rs new file mode 100644 index 000000000..a4d500c11 --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-09-variable-in-pattern/src/main.rs @@ -0,0 +1,31 @@ +#[derive(Debug)] +enum UsState { + Alabama, + Alaska, + // --snip-- +} + +enum Coin { + Penny, + Nickel, + Dime, + Quarter(UsState), +} + +// ANCHOR: here +fn value_in_cents(coin: Coin) -> u8 { + match coin { + Coin::Penny => 1, + Coin::Nickel => 5, + Coin::Dime => 10, + Coin::Quarter(state) => { + println!("State quarter from {:?}!", state); + 25 + } + } +} +// ANCHOR_END: here + +fn main() { + value_in_cents(Coin::Quarter(UsState::Alaska)); +} diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-10-non-exhaustive-match/Cargo.lock b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-10-non-exhaustive-match/Cargo.lock new file mode 100644 index 000000000..f62e8ac45 --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-10-non-exhaustive-match/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "enums" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-10-non-exhaustive-match/Cargo.toml b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-10-non-exhaustive-match/Cargo.toml new file mode 100644 index 000000000..e959295f9 --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-10-non-exhaustive-match/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "enums" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-10-non-exhaustive-match/output.txt b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-10-non-exhaustive-match/output.txt new file mode 100644 index 000000000..c5a6c51bb --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-10-non-exhaustive-match/output.txt @@ -0,0 +1,18 @@ +$ cargo run + Compiling enums v0.1.0 (file:///projects/enums) +error[E0004]: non-exhaustive patterns: `None` not covered + --> src/main.rs:3:15 + | +3 | match x { + | ^ pattern `None` not covered + | +note: `Option` defined here + = note: the matched value is of type `Option` +help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown + | +4 ~ Some(i) => Some(i + 1), +5 ~ None => todo!(), + | + +For more information about this error, try `rustc --explain E0004`. +error: could not compile `enums` due to previous error diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-10-non-exhaustive-match/src/main.rs b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-10-non-exhaustive-match/src/main.rs new file mode 100644 index 000000000..f1963d0c9 --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-10-non-exhaustive-match/src/main.rs @@ -0,0 +1,13 @@ +fn main() { + // ANCHOR: here + fn plus_one(x: Option) -> Option { + match x { + Some(i) => Some(i + 1), + } + } + // ANCHOR_END: here + + let five = Some(5); + let six = plus_one(five); + let none = plus_one(None); +} diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-12-if-let/Cargo.lock b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-12-if-let/Cargo.lock new file mode 100644 index 000000000..f62e8ac45 --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-12-if-let/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "enums" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-12-if-let/Cargo.toml b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-12-if-let/Cargo.toml new file mode 100644 index 000000000..e959295f9 --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-12-if-let/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "enums" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-12-if-let/src/main.rs b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-12-if-let/src/main.rs new file mode 100644 index 000000000..735086d4e --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-12-if-let/src/main.rs @@ -0,0 +1,8 @@ +fn main() { + // ANCHOR: here + let config_max = Some(3u8); + if let Some(max) = config_max { + println!("The maximum is configured to be {}", max); + } + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-13-count-and-announce-match/Cargo.lock b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-13-count-and-announce-match/Cargo.lock new file mode 100644 index 000000000..f62e8ac45 --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-13-count-and-announce-match/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "enums" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-13-count-and-announce-match/Cargo.toml b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-13-count-and-announce-match/Cargo.toml new file mode 100644 index 000000000..e959295f9 --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-13-count-and-announce-match/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "enums" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-13-count-and-announce-match/src/main.rs b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-13-count-and-announce-match/src/main.rs new file mode 100644 index 000000000..12c4c0fec --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-13-count-and-announce-match/src/main.rs @@ -0,0 +1,24 @@ +#[derive(Debug)] +enum UsState { + Alabama, + Alaska, + // --snip-- +} + +enum Coin { + Penny, + Nickel, + Dime, + Quarter(UsState), +} + +fn main() { + let coin = Coin::Penny; + // ANCHOR: here + let mut count = 0; + match coin { + Coin::Quarter(state) => println!("State quarter from {:?}!", state), + _ => count += 1, + } + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-14-count-and-announce-if-let-else/Cargo.lock b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-14-count-and-announce-if-let-else/Cargo.lock new file mode 100644 index 000000000..f62e8ac45 --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-14-count-and-announce-if-let-else/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "enums" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-14-count-and-announce-if-let-else/Cargo.toml b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-14-count-and-announce-if-let-else/Cargo.toml new file mode 100644 index 000000000..e959295f9 --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-14-count-and-announce-if-let-else/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "enums" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-14-count-and-announce-if-let-else/src/main.rs b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-14-count-and-announce-if-let-else/src/main.rs new file mode 100644 index 000000000..ba7eda27b --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-14-count-and-announce-if-let-else/src/main.rs @@ -0,0 +1,25 @@ +#[derive(Debug)] +enum UsState { + Alabama, + Alaska, + // --snip-- +} + +enum Coin { + Penny, + Nickel, + Dime, + Quarter(UsState), +} + +fn main() { + let coin = Coin::Penny; + // ANCHOR: here + let mut count = 0; + if let Coin::Quarter(state) = coin { + println!("State quarter from {:?}!", state); + } else { + count += 1; + } + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-15-binding-catchall/Cargo.lock b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-15-binding-catchall/Cargo.lock new file mode 100644 index 000000000..f62e8ac45 --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-15-binding-catchall/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "enums" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-15-binding-catchall/Cargo.toml b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-15-binding-catchall/Cargo.toml new file mode 100644 index 000000000..e959295f9 --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-15-binding-catchall/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "enums" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-15-binding-catchall/src/main.rs b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-15-binding-catchall/src/main.rs new file mode 100644 index 000000000..6ce0b5998 --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-15-binding-catchall/src/main.rs @@ -0,0 +1,14 @@ +fn main() { + // ANCHOR: here + let dice_roll = 9; + match dice_roll { + 3 => add_fancy_hat(), + 7 => remove_fancy_hat(), + other => move_player(other), + } + + fn add_fancy_hat() {} + fn remove_fancy_hat() {} + fn move_player(num_spaces: u8) {} + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-16-underscore-catchall/Cargo.lock b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-16-underscore-catchall/Cargo.lock new file mode 100644 index 000000000..f62e8ac45 --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-16-underscore-catchall/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "enums" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-16-underscore-catchall/Cargo.toml b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-16-underscore-catchall/Cargo.toml new file mode 100644 index 000000000..e959295f9 --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-16-underscore-catchall/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "enums" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-16-underscore-catchall/src/main.rs b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-16-underscore-catchall/src/main.rs new file mode 100644 index 000000000..586e23751 --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-16-underscore-catchall/src/main.rs @@ -0,0 +1,14 @@ +fn main() { + // ANCHOR: here + let dice_roll = 9; + match dice_roll { + 3 => add_fancy_hat(), + 7 => remove_fancy_hat(), + _ => reroll(), + } + + fn add_fancy_hat() {} + fn remove_fancy_hat() {} + fn reroll() {} + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-17-underscore-unit/Cargo.lock b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-17-underscore-unit/Cargo.lock new file mode 100644 index 000000000..f62e8ac45 --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-17-underscore-unit/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "enums" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-17-underscore-unit/Cargo.toml b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-17-underscore-unit/Cargo.toml new file mode 100644 index 000000000..e959295f9 --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-17-underscore-unit/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "enums" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-17-underscore-unit/src/main.rs b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-17-underscore-unit/src/main.rs new file mode 100644 index 000000000..e791742ee --- /dev/null +++ b/src/doc/book/listings/ch06-enums-and-pattern-matching/no-listing-17-underscore-unit/src/main.rs @@ -0,0 +1,13 @@ +fn main() { + // ANCHOR: here + let dice_roll = 9; + match dice_roll { + 3 => add_fancy_hat(), + 7 => remove_fancy_hat(), + _ => (), + } + + fn add_fancy_hat() {} + fn remove_fancy_hat() {} + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-01/Cargo.lock b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-01/Cargo.lock new file mode 100644 index 000000000..f25ab358f --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-01/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "restaurant" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-01/Cargo.toml b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-01/Cargo.toml new file mode 100644 index 000000000..60cec7cb0 --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-01/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "restaurant" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-01/src/lib.rs b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-01/src/lib.rs new file mode 100644 index 000000000..591e24557 --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-01/src/lib.rs @@ -0,0 +1,15 @@ +mod front_of_house { + mod hosting { + fn add_to_waitlist() {} + + fn seat_at_table() {} + } + + mod serving { + fn take_order() {} + + fn serve_order() {} + + fn take_payment() {} + } +} diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-03/Cargo.lock b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-03/Cargo.lock new file mode 100644 index 000000000..f25ab358f --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-03/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "restaurant" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-03/Cargo.toml b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-03/Cargo.toml new file mode 100644 index 000000000..60cec7cb0 --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-03/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "restaurant" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-03/output.txt b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-03/output.txt new file mode 100644 index 000000000..481dcb3f7 --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-03/output.txt @@ -0,0 +1,28 @@ +$ cargo build + Compiling restaurant v0.1.0 (file:///projects/restaurant) +error[E0603]: module `hosting` is private + --> src/lib.rs:9:28 + | +9 | crate::front_of_house::hosting::add_to_waitlist(); + | ^^^^^^^ private module + | +note: the module `hosting` is defined here + --> src/lib.rs:2:5 + | +2 | mod hosting { + | ^^^^^^^^^^^ + +error[E0603]: module `hosting` is private + --> src/lib.rs:12:21 + | +12 | front_of_house::hosting::add_to_waitlist(); + | ^^^^^^^ private module + | +note: the module `hosting` is defined here + --> src/lib.rs:2:5 + | +2 | mod hosting { + | ^^^^^^^^^^^ + +For more information about this error, try `rustc --explain E0603`. +error: could not compile `restaurant` due to 2 previous errors diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-03/src/lib.rs b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-03/src/lib.rs new file mode 100644 index 000000000..0b8a43c6b --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-03/src/lib.rs @@ -0,0 +1,13 @@ +mod front_of_house { + mod hosting { + fn add_to_waitlist() {} + } +} + +pub fn eat_at_restaurant() { + // Absolute path + crate::front_of_house::hosting::add_to_waitlist(); + + // Relative path + front_of_house::hosting::add_to_waitlist(); +} diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-05/Cargo.lock b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-05/Cargo.lock new file mode 100644 index 000000000..f25ab358f --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-05/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "restaurant" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-05/Cargo.toml b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-05/Cargo.toml new file mode 100644 index 000000000..60cec7cb0 --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-05/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "restaurant" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-05/output.txt b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-05/output.txt new file mode 100644 index 000000000..63eb89a14 --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-05/output.txt @@ -0,0 +1,28 @@ +$ cargo build + Compiling restaurant v0.1.0 (file:///projects/restaurant) +error[E0603]: function `add_to_waitlist` is private + --> src/lib.rs:9:37 + | +9 | crate::front_of_house::hosting::add_to_waitlist(); + | ^^^^^^^^^^^^^^^ private function + | +note: the function `add_to_waitlist` is defined here + --> src/lib.rs:3:9 + | +3 | fn add_to_waitlist() {} + | ^^^^^^^^^^^^^^^^^^^^ + +error[E0603]: function `add_to_waitlist` is private + --> src/lib.rs:12:30 + | +12 | front_of_house::hosting::add_to_waitlist(); + | ^^^^^^^^^^^^^^^ private function + | +note: the function `add_to_waitlist` is defined here + --> src/lib.rs:3:9 + | +3 | fn add_to_waitlist() {} + | ^^^^^^^^^^^^^^^^^^^^ + +For more information about this error, try `rustc --explain E0603`. +error: could not compile `restaurant` due to 2 previous errors diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-05/src/lib.rs b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-05/src/lib.rs new file mode 100644 index 000000000..05372dbe5 --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-05/src/lib.rs @@ -0,0 +1,13 @@ +mod front_of_house { + pub mod hosting { + fn add_to_waitlist() {} + } +} + +pub fn eat_at_restaurant() { + // Absolute path + crate::front_of_house::hosting::add_to_waitlist(); + + // Relative path + front_of_house::hosting::add_to_waitlist(); +} diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-07/Cargo.lock b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-07/Cargo.lock new file mode 100644 index 000000000..f25ab358f --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-07/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "restaurant" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-07/Cargo.toml b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-07/Cargo.toml new file mode 100644 index 000000000..60cec7cb0 --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-07/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "restaurant" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-07/src/lib.rs b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-07/src/lib.rs new file mode 100644 index 000000000..7b89ee7cd --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-07/src/lib.rs @@ -0,0 +1,13 @@ +mod front_of_house { + pub mod hosting { + pub fn add_to_waitlist() {} + } +} + +pub fn eat_at_restaurant() { + // Absolute path + crate::front_of_house::hosting::add_to_waitlist(); + + // Relative path + front_of_house::hosting::add_to_waitlist(); +} diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-08/Cargo.lock b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-08/Cargo.lock new file mode 100644 index 000000000..f25ab358f --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-08/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "restaurant" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-08/Cargo.toml b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-08/Cargo.toml new file mode 100644 index 000000000..60cec7cb0 --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-08/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "restaurant" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-08/src/lib.rs b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-08/src/lib.rs new file mode 100644 index 000000000..b3ddb4f0f --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-08/src/lib.rs @@ -0,0 +1,10 @@ +fn deliver_order() {} + +mod back_of_house { + fn fix_incorrect_order() { + cook_order(); + super::deliver_order(); + } + + fn cook_order() {} +} diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-09/Cargo.lock b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-09/Cargo.lock new file mode 100644 index 000000000..f25ab358f --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-09/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "restaurant" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-09/Cargo.toml b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-09/Cargo.toml new file mode 100644 index 000000000..60cec7cb0 --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-09/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "restaurant" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-09/src/lib.rs b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-09/src/lib.rs new file mode 100644 index 000000000..92c4695d5 --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-09/src/lib.rs @@ -0,0 +1,27 @@ +mod back_of_house { + pub struct Breakfast { + pub toast: String, + seasonal_fruit: String, + } + + impl Breakfast { + pub fn summer(toast: &str) -> Breakfast { + Breakfast { + toast: String::from(toast), + seasonal_fruit: String::from("peaches"), + } + } + } +} + +pub fn eat_at_restaurant() { + // Order a breakfast in the summer with Rye toast + let mut meal = back_of_house::Breakfast::summer("Rye"); + // Change our mind about what bread we'd like + meal.toast = String::from("Wheat"); + println!("I'd like {} toast please", meal.toast); + + // The next line won't compile if we uncomment it; we're not allowed + // to see or modify the seasonal fruit that comes with the meal + // meal.seasonal_fruit = String::from("blueberries"); +} diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-10/Cargo.lock b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-10/Cargo.lock new file mode 100644 index 000000000..f25ab358f --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-10/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "restaurant" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-10/Cargo.toml b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-10/Cargo.toml new file mode 100644 index 000000000..60cec7cb0 --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-10/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "restaurant" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-10/src/lib.rs b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-10/src/lib.rs new file mode 100644 index 000000000..908f1dfb7 --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-10/src/lib.rs @@ -0,0 +1,11 @@ +mod back_of_house { + pub enum Appetizer { + Soup, + Salad, + } +} + +pub fn eat_at_restaurant() { + let order1 = back_of_house::Appetizer::Soup; + let order2 = back_of_house::Appetizer::Salad; +} diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-11/Cargo.lock b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-11/Cargo.lock new file mode 100644 index 000000000..f25ab358f --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-11/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "restaurant" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-11/Cargo.toml b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-11/Cargo.toml new file mode 100644 index 000000000..60cec7cb0 --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-11/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "restaurant" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-11/src/lib.rs b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-11/src/lib.rs new file mode 100644 index 000000000..cf31a9c97 --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-11/src/lib.rs @@ -0,0 +1,11 @@ +mod front_of_house { + pub mod hosting { + pub fn add_to_waitlist() {} + } +} + +use crate::front_of_house::hosting; + +pub fn eat_at_restaurant() { + hosting::add_to_waitlist(); +} diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-12/Cargo.lock b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-12/Cargo.lock new file mode 100644 index 000000000..f25ab358f --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-12/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "restaurant" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-12/Cargo.toml b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-12/Cargo.toml new file mode 100644 index 000000000..60cec7cb0 --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-12/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "restaurant" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-12/output.txt b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-12/output.txt new file mode 100644 index 000000000..39b650540 --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-12/output.txt @@ -0,0 +1,19 @@ +$ cargo build + Compiling restaurant v0.1.0 (file:///projects/restaurant) +error[E0433]: failed to resolve: use of undeclared crate or module `hosting` + --> src/lib.rs:11:9 + | +11 | hosting::add_to_waitlist(); + | ^^^^^^^ use of undeclared crate or module `hosting` + +warning: unused import: `crate::front_of_house::hosting` + --> src/lib.rs:7:5 + | +7 | use crate::front_of_house::hosting; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `#[warn(unused_imports)]` on by default + +For more information about this error, try `rustc --explain E0433`. +warning: `restaurant` (lib) generated 1 warning +error: could not compile `restaurant` due to previous error; 1 warning emitted diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-12/src/lib.rs b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-12/src/lib.rs new file mode 100644 index 000000000..afc759423 --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-12/src/lib.rs @@ -0,0 +1,13 @@ +mod front_of_house { + pub mod hosting { + pub fn add_to_waitlist() {} + } +} + +use crate::front_of_house::hosting; + +mod customer { + pub fn eat_at_restaurant() { + hosting::add_to_waitlist(); + } +} diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-13/Cargo.lock b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-13/Cargo.lock new file mode 100644 index 000000000..f25ab358f --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-13/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "restaurant" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-13/Cargo.toml b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-13/Cargo.toml new file mode 100644 index 000000000..60cec7cb0 --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-13/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "restaurant" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-13/src/lib.rs b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-13/src/lib.rs new file mode 100644 index 000000000..c72994efe --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-13/src/lib.rs @@ -0,0 +1,11 @@ +mod front_of_house { + pub mod hosting { + pub fn add_to_waitlist() {} + } +} + +use crate::front_of_house::hosting::add_to_waitlist; + +pub fn eat_at_restaurant() { + add_to_waitlist(); +} diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-14/Cargo.lock b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-14/Cargo.lock new file mode 100644 index 000000000..f25ab358f --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-14/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "restaurant" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-14/Cargo.toml b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-14/Cargo.toml new file mode 100644 index 000000000..60cec7cb0 --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-14/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "restaurant" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-14/src/main.rs b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-14/src/main.rs new file mode 100644 index 000000000..4379e7c79 --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-14/src/main.rs @@ -0,0 +1,6 @@ +use std::collections::HashMap; + +fn main() { + let mut map = HashMap::new(); + map.insert(1, 2); +} diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-15/Cargo.lock b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-15/Cargo.lock new file mode 100644 index 000000000..f25ab358f --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-15/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "restaurant" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-15/Cargo.toml b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-15/Cargo.toml new file mode 100644 index 000000000..60cec7cb0 --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-15/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "restaurant" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-15/src/lib.rs b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-15/src/lib.rs new file mode 100644 index 000000000..bfac3a07a --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-15/src/lib.rs @@ -0,0 +1,18 @@ +// ANCHOR: here +use std::fmt; +use std::io; + +fn function1() -> fmt::Result { + // --snip-- + // ANCHOR_END: here + Ok(()) + // ANCHOR: here +} + +fn function2() -> io::Result<()> { + // --snip-- + // ANCHOR_END: here + Ok(()) + // ANCHOR: here +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-16/Cargo.lock b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-16/Cargo.lock new file mode 100644 index 000000000..f25ab358f --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-16/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "restaurant" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-16/Cargo.toml b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-16/Cargo.toml new file mode 100644 index 000000000..60cec7cb0 --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-16/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "restaurant" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-16/src/lib.rs b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-16/src/lib.rs new file mode 100644 index 000000000..843490b4d --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-16/src/lib.rs @@ -0,0 +1,18 @@ +// ANCHOR: here +use std::fmt::Result; +use std::io::Result as IoResult; + +fn function1() -> Result { + // --snip-- + // ANCHOR_END: here + Ok(()) + // ANCHOR: here +} + +fn function2() -> IoResult<()> { + // --snip-- + // ANCHOR_END: here + Ok(()) + // ANCHOR: here +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-17/Cargo.lock b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-17/Cargo.lock new file mode 100644 index 000000000..f25ab358f --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-17/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "restaurant" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-17/Cargo.toml b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-17/Cargo.toml new file mode 100644 index 000000000..60cec7cb0 --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-17/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "restaurant" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-17/src/lib.rs b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-17/src/lib.rs new file mode 100644 index 000000000..45cf1bac9 --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-17/src/lib.rs @@ -0,0 +1,11 @@ +mod front_of_house { + pub mod hosting { + pub fn add_to_waitlist() {} + } +} + +pub use crate::front_of_house::hosting; + +pub fn eat_at_restaurant() { + hosting::add_to_waitlist(); +} diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-18/Cargo.lock b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-18/Cargo.lock new file mode 100644 index 000000000..0a2f222c2 --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-18/Cargo.lock @@ -0,0 +1,83 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "getrandom" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "guessing_game" +version = "0.1.0" +dependencies = [ + "rand", +] + +[[package]] +name = "libc" +version = "0.2.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7282d924be3275cec7f6756ff4121987bc6481325397dde6ba3e7802b1a8b1c" + +[[package]] +name = "ppv-lite86" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" + +[[package]] +name = "rand" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", + "rand_hc", +] + +[[package]] +name = "rand_chacha" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34cf66eb183df1c5876e2dcf6b13d57340741e8dc255b48e40a26de954d06ae7" +dependencies = [ + "getrandom", +] + +[[package]] +name = "rand_hc" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73" +dependencies = [ + "rand_core", +] + +[[package]] +name = "wasi" +version = "0.10.2+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-18/Cargo.toml b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-18/Cargo.toml new file mode 100644 index 000000000..15b3fffca --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-18/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "guessing_game" +version = "0.1.0" +edition = "2021" + +[dependencies] +rand = "0.8.3" diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-18/src/main.rs b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-18/src/main.rs new file mode 100644 index 000000000..2f69412a4 --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-18/src/main.rs @@ -0,0 +1,32 @@ +use rand::Rng; +// ANCHOR: here +// --snip-- +use std::{cmp::Ordering, io}; +// --snip-- +// ANCHOR_END: here + +fn main() { + println!("Guess the number!"); + + let secret_number = rand::thread_rng().gen_range(1..=100); + + println!("The secret number is: {secret_number}"); + + println!("Please input your guess."); + + let mut guess = String::new(); + + io::stdin() + .read_line(&mut guess) + .expect("Failed to read line"); + + let guess: u32 = guess.trim().parse().expect("Please type a number!"); + + println!("You guessed: {guess}"); + + match guess.cmp(&secret_number) { + Ordering::Less => println!("Too small!"), + Ordering::Greater => println!("Too big!"), + Ordering::Equal => println!("You win!"), + } +} diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-19/Cargo.lock b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-19/Cargo.lock new file mode 100644 index 000000000..f25ab358f --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-19/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "restaurant" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-19/Cargo.toml b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-19/Cargo.toml new file mode 100644 index 000000000..60cec7cb0 --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-19/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "restaurant" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-19/src/lib.rs b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-19/src/lib.rs new file mode 100644 index 000000000..3fee46c44 --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-19/src/lib.rs @@ -0,0 +1,2 @@ +use std::io; +use std::io::Write; diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-20/Cargo.lock b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-20/Cargo.lock new file mode 100644 index 000000000..f25ab358f --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-20/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "restaurant" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-20/Cargo.toml b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-20/Cargo.toml new file mode 100644 index 000000000..60cec7cb0 --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-20/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "restaurant" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-20/src/lib.rs b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-20/src/lib.rs new file mode 100644 index 000000000..341f40a47 --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-20/src/lib.rs @@ -0,0 +1 @@ +use std::io::{self, Write}; diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-21-and-22/Cargo.lock b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-21-and-22/Cargo.lock new file mode 100644 index 000000000..f25ab358f --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-21-and-22/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "restaurant" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-21-and-22/Cargo.toml b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-21-and-22/Cargo.toml new file mode 100644 index 000000000..60cec7cb0 --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-21-and-22/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "restaurant" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-21-and-22/src/front_of_house.rs b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-21-and-22/src/front_of_house.rs new file mode 100644 index 000000000..6875dfdb6 --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-21-and-22/src/front_of_house.rs @@ -0,0 +1,3 @@ +pub mod hosting { + pub fn add_to_waitlist() {} +} diff --git a/src/doc/book/listings/ch07-managing-growing-projects/listing-07-21-and-22/src/lib.rs b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-21-and-22/src/lib.rs new file mode 100644 index 000000000..d6769556a --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/listing-07-21-and-22/src/lib.rs @@ -0,0 +1,7 @@ +mod front_of_house; + +pub use crate::front_of_house::hosting; + +pub fn eat_at_restaurant() { + hosting::add_to_waitlist(); +} diff --git a/src/doc/book/listings/ch07-managing-growing-projects/no-listing-01-use-std-unnested/Cargo.lock b/src/doc/book/listings/ch07-managing-growing-projects/no-listing-01-use-std-unnested/Cargo.lock new file mode 100644 index 000000000..0a2f222c2 --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/no-listing-01-use-std-unnested/Cargo.lock @@ -0,0 +1,83 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "getrandom" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "guessing_game" +version = "0.1.0" +dependencies = [ + "rand", +] + +[[package]] +name = "libc" +version = "0.2.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7282d924be3275cec7f6756ff4121987bc6481325397dde6ba3e7802b1a8b1c" + +[[package]] +name = "ppv-lite86" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" + +[[package]] +name = "rand" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", + "rand_hc", +] + +[[package]] +name = "rand_chacha" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34cf66eb183df1c5876e2dcf6b13d57340741e8dc255b48e40a26de954d06ae7" +dependencies = [ + "getrandom", +] + +[[package]] +name = "rand_hc" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73" +dependencies = [ + "rand_core", +] + +[[package]] +name = "wasi" +version = "0.10.2+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" diff --git a/src/doc/book/listings/ch07-managing-growing-projects/no-listing-01-use-std-unnested/Cargo.toml b/src/doc/book/listings/ch07-managing-growing-projects/no-listing-01-use-std-unnested/Cargo.toml new file mode 100644 index 000000000..cc63f6f02 --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/no-listing-01-use-std-unnested/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "guessing_game" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +rand = "0.8.3" diff --git a/src/doc/book/listings/ch07-managing-growing-projects/no-listing-01-use-std-unnested/src/main.rs b/src/doc/book/listings/ch07-managing-growing-projects/no-listing-01-use-std-unnested/src/main.rs new file mode 100644 index 000000000..3a02c9963 --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/no-listing-01-use-std-unnested/src/main.rs @@ -0,0 +1,31 @@ +use rand::Rng; +// ANCHOR: here +// --snip-- +use std::cmp::Ordering; +use std::io; +// --snip-- +// ANCHOR_END: here + +fn main() { + println!("Guess the number!"); + + let secret_number = rand::thread_rng().gen_range(1..=100); + + println!("The secret number is: {secret_number}"); + + println!("Please input your guess."); + + let mut guess = String::new(); + + io::stdin() + .read_line(&mut guess) + .expect("Failed to read line"); + + println!("You guessed: {guess}"); + + match guess.cmp(&secret_number) { + Ordering::Less => println!("Too small!"), + Ordering::Greater => println!("Too big!"), + Ordering::Equal => println!("You win!"), + } +} diff --git a/src/doc/book/listings/ch07-managing-growing-projects/no-listing-02-extracting-hosting/Cargo.lock b/src/doc/book/listings/ch07-managing-growing-projects/no-listing-02-extracting-hosting/Cargo.lock new file mode 100644 index 000000000..f25ab358f --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/no-listing-02-extracting-hosting/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "restaurant" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch07-managing-growing-projects/no-listing-02-extracting-hosting/Cargo.toml b/src/doc/book/listings/ch07-managing-growing-projects/no-listing-02-extracting-hosting/Cargo.toml new file mode 100644 index 000000000..60cec7cb0 --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/no-listing-02-extracting-hosting/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "restaurant" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch07-managing-growing-projects/no-listing-02-extracting-hosting/src/front_of_house.rs b/src/doc/book/listings/ch07-managing-growing-projects/no-listing-02-extracting-hosting/src/front_of_house.rs new file mode 100644 index 000000000..d0a8154ea --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/no-listing-02-extracting-hosting/src/front_of_house.rs @@ -0,0 +1 @@ +pub mod hosting; diff --git a/src/doc/book/listings/ch07-managing-growing-projects/no-listing-02-extracting-hosting/src/front_of_house/hosting.rs b/src/doc/book/listings/ch07-managing-growing-projects/no-listing-02-extracting-hosting/src/front_of_house/hosting.rs new file mode 100644 index 000000000..d65f3afd1 --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/no-listing-02-extracting-hosting/src/front_of_house/hosting.rs @@ -0,0 +1 @@ +pub fn add_to_waitlist() {} diff --git a/src/doc/book/listings/ch07-managing-growing-projects/no-listing-02-extracting-hosting/src/lib.rs b/src/doc/book/listings/ch07-managing-growing-projects/no-listing-02-extracting-hosting/src/lib.rs new file mode 100644 index 000000000..d6769556a --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/no-listing-02-extracting-hosting/src/lib.rs @@ -0,0 +1,7 @@ +mod front_of_house; + +pub use crate::front_of_house::hosting; + +pub fn eat_at_restaurant() { + hosting::add_to_waitlist(); +} diff --git a/src/doc/book/listings/ch07-managing-growing-projects/quick-reference-example/Cargo.lock b/src/doc/book/listings/ch07-managing-growing-projects/quick-reference-example/Cargo.lock new file mode 100644 index 000000000..4773c201d --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/quick-reference-example/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "backyard" +version = "0.1.0" diff --git a/src/doc/book/listings/ch07-managing-growing-projects/quick-reference-example/Cargo.toml b/src/doc/book/listings/ch07-managing-growing-projects/quick-reference-example/Cargo.toml new file mode 100644 index 000000000..6e904abbe --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/quick-reference-example/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "backyard" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch07-managing-growing-projects/quick-reference-example/output.txt b/src/doc/book/listings/ch07-managing-growing-projects/quick-reference-example/output.txt new file mode 100644 index 000000000..e36a45eb0 --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/quick-reference-example/output.txt @@ -0,0 +1,5 @@ +$ cargo run + Compiling backyard v0.1.0 (file:///projects/backyard) + Finished dev [unoptimized + debuginfo] target(s) in 0.36s + Running `target/debug/backyard` +I'm growing Asparagus! diff --git a/src/doc/book/listings/ch07-managing-growing-projects/quick-reference-example/src/garden.rs b/src/doc/book/listings/ch07-managing-growing-projects/quick-reference-example/src/garden.rs new file mode 100644 index 000000000..6c7f9b1cb --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/quick-reference-example/src/garden.rs @@ -0,0 +1 @@ +pub mod vegetables; diff --git a/src/doc/book/listings/ch07-managing-growing-projects/quick-reference-example/src/garden/vegetables.rs b/src/doc/book/listings/ch07-managing-growing-projects/quick-reference-example/src/garden/vegetables.rs new file mode 100644 index 000000000..b00f785ef --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/quick-reference-example/src/garden/vegetables.rs @@ -0,0 +1,2 @@ +#[derive(Debug)] +pub struct Asparagus {} diff --git a/src/doc/book/listings/ch07-managing-growing-projects/quick-reference-example/src/main.rs b/src/doc/book/listings/ch07-managing-growing-projects/quick-reference-example/src/main.rs new file mode 100644 index 000000000..7a024a9a0 --- /dev/null +++ b/src/doc/book/listings/ch07-managing-growing-projects/quick-reference-example/src/main.rs @@ -0,0 +1,8 @@ +use crate::garden::vegetables::Asparagus; + +pub mod garden; + +fn main() { + let plant = Asparagus {}; + println!("I'm growing {:?}!", plant); +} diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-01/Cargo.lock b/src/doc/book/listings/ch08-common-collections/listing-08-01/Cargo.lock new file mode 100644 index 000000000..d3daeff7d --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-01/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "collections" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-01/Cargo.toml b/src/doc/book/listings/ch08-common-collections/listing-08-01/Cargo.toml new file mode 100644 index 000000000..fe4959823 --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-01/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "collections" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-01/src/main.rs b/src/doc/book/listings/ch08-common-collections/listing-08-01/src/main.rs new file mode 100644 index 000000000..45e45581e --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-01/src/main.rs @@ -0,0 +1,5 @@ +fn main() { + // ANCHOR: here + let v: Vec = Vec::new(); + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-02/Cargo.lock b/src/doc/book/listings/ch08-common-collections/listing-08-02/Cargo.lock new file mode 100644 index 000000000..d3daeff7d --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-02/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "collections" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-02/Cargo.toml b/src/doc/book/listings/ch08-common-collections/listing-08-02/Cargo.toml new file mode 100644 index 000000000..fe4959823 --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-02/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "collections" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-02/src/main.rs b/src/doc/book/listings/ch08-common-collections/listing-08-02/src/main.rs new file mode 100644 index 000000000..3b10a53e8 --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-02/src/main.rs @@ -0,0 +1,5 @@ +fn main() { + // ANCHOR: here + let v = vec![1, 2, 3]; + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-03/Cargo.lock b/src/doc/book/listings/ch08-common-collections/listing-08-03/Cargo.lock new file mode 100644 index 000000000..d3daeff7d --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-03/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "collections" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-03/Cargo.toml b/src/doc/book/listings/ch08-common-collections/listing-08-03/Cargo.toml new file mode 100644 index 000000000..fe4959823 --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-03/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "collections" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-03/src/main.rs b/src/doc/book/listings/ch08-common-collections/listing-08-03/src/main.rs new file mode 100644 index 000000000..147223f9a --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-03/src/main.rs @@ -0,0 +1,10 @@ +fn main() { + // ANCHOR: here + let mut v = Vec::new(); + + v.push(5); + v.push(6); + v.push(7); + v.push(8); + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-04/Cargo.lock b/src/doc/book/listings/ch08-common-collections/listing-08-04/Cargo.lock new file mode 100644 index 000000000..d3daeff7d --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-04/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "collections" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-04/Cargo.toml b/src/doc/book/listings/ch08-common-collections/listing-08-04/Cargo.toml new file mode 100644 index 000000000..fe4959823 --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-04/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "collections" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-04/src/main.rs b/src/doc/book/listings/ch08-common-collections/listing-08-04/src/main.rs new file mode 100644 index 000000000..fa4e090d5 --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-04/src/main.rs @@ -0,0 +1,14 @@ +fn main() { + // ANCHOR: here + let v = vec![1, 2, 3, 4, 5]; + + let third: &i32 = &v[2]; + println!("The third element is {}", third); + + let third: Option<&i32> = v.get(2); + match third { + Some(third) => println!("The third element is {}", third), + None => println!("There is no third element."), + } + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-05/Cargo.lock b/src/doc/book/listings/ch08-common-collections/listing-08-05/Cargo.lock new file mode 100644 index 000000000..d3daeff7d --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-05/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "collections" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-05/Cargo.toml b/src/doc/book/listings/ch08-common-collections/listing-08-05/Cargo.toml new file mode 100644 index 000000000..fe4959823 --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-05/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "collections" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-05/src/main.rs b/src/doc/book/listings/ch08-common-collections/listing-08-05/src/main.rs new file mode 100644 index 000000000..783d9b110 --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-05/src/main.rs @@ -0,0 +1,8 @@ +fn main() { + // ANCHOR: here + let v = vec![1, 2, 3, 4, 5]; + + let does_not_exist = &v[100]; + let does_not_exist = v.get(100); + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-06/Cargo.lock b/src/doc/book/listings/ch08-common-collections/listing-08-06/Cargo.lock new file mode 100644 index 000000000..d3daeff7d --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-06/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "collections" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-06/Cargo.toml b/src/doc/book/listings/ch08-common-collections/listing-08-06/Cargo.toml new file mode 100644 index 000000000..fe4959823 --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-06/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "collections" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-06/output.txt b/src/doc/book/listings/ch08-common-collections/listing-08-06/output.txt new file mode 100644 index 000000000..ab512a9e6 --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-06/output.txt @@ -0,0 +1,16 @@ +$ cargo run + Compiling collections v0.1.0 (file:///projects/collections) +error[E0502]: cannot borrow `v` as mutable because it is also borrowed as immutable + --> src/main.rs:6:5 + | +4 | let first = &v[0]; + | - immutable borrow occurs here +5 | +6 | v.push(6); + | ^^^^^^^^^ mutable borrow occurs here +7 | +8 | println!("The first element is: {}", first); + | ----- immutable borrow later used here + +For more information about this error, try `rustc --explain E0502`. +error: could not compile `collections` due to previous error diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-06/src/main.rs b/src/doc/book/listings/ch08-common-collections/listing-08-06/src/main.rs new file mode 100644 index 000000000..1b42274a6 --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-06/src/main.rs @@ -0,0 +1,11 @@ +fn main() { + // ANCHOR: here + let mut v = vec![1, 2, 3, 4, 5]; + + let first = &v[0]; + + v.push(6); + + println!("The first element is: {}", first); + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-07/Cargo.lock b/src/doc/book/listings/ch08-common-collections/listing-08-07/Cargo.lock new file mode 100644 index 000000000..d3daeff7d --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-07/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "collections" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-07/Cargo.toml b/src/doc/book/listings/ch08-common-collections/listing-08-07/Cargo.toml new file mode 100644 index 000000000..fe4959823 --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-07/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "collections" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-07/src/main.rs b/src/doc/book/listings/ch08-common-collections/listing-08-07/src/main.rs new file mode 100644 index 000000000..38b97784b --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-07/src/main.rs @@ -0,0 +1,8 @@ +fn main() { + // ANCHOR: here + let v = vec![100, 32, 57]; + for i in &v { + println!("{}", i); + } + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-08/Cargo.lock b/src/doc/book/listings/ch08-common-collections/listing-08-08/Cargo.lock new file mode 100644 index 000000000..d3daeff7d --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-08/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "collections" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-08/Cargo.toml b/src/doc/book/listings/ch08-common-collections/listing-08-08/Cargo.toml new file mode 100644 index 000000000..fe4959823 --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-08/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "collections" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-08/src/main.rs b/src/doc/book/listings/ch08-common-collections/listing-08-08/src/main.rs new file mode 100644 index 000000000..c62ba21b4 --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-08/src/main.rs @@ -0,0 +1,8 @@ +fn main() { + // ANCHOR: here + let mut v = vec![100, 32, 57]; + for i in &mut v { + *i += 50; + } + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-09/Cargo.lock b/src/doc/book/listings/ch08-common-collections/listing-08-09/Cargo.lock new file mode 100644 index 000000000..d3daeff7d --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-09/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "collections" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-09/Cargo.toml b/src/doc/book/listings/ch08-common-collections/listing-08-09/Cargo.toml new file mode 100644 index 000000000..fe4959823 --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-09/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "collections" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-09/src/main.rs b/src/doc/book/listings/ch08-common-collections/listing-08-09/src/main.rs new file mode 100644 index 000000000..c2198883b --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-09/src/main.rs @@ -0,0 +1,15 @@ +fn main() { + // ANCHOR: here + enum SpreadsheetCell { + Int(i32), + Float(f64), + Text(String), + } + + let row = vec![ + SpreadsheetCell::Int(3), + SpreadsheetCell::Text(String::from("blue")), + SpreadsheetCell::Float(10.12), + ]; + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-10/Cargo.lock b/src/doc/book/listings/ch08-common-collections/listing-08-10/Cargo.lock new file mode 100644 index 000000000..d3daeff7d --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-10/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "collections" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-10/Cargo.toml b/src/doc/book/listings/ch08-common-collections/listing-08-10/Cargo.toml new file mode 100644 index 000000000..fe4959823 --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-10/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "collections" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-10/src/main.rs b/src/doc/book/listings/ch08-common-collections/listing-08-10/src/main.rs new file mode 100644 index 000000000..abda2db66 --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-10/src/main.rs @@ -0,0 +1,9 @@ +fn main() { + // ANCHOR: here + { + let v = vec![1, 2, 3, 4]; + + // do stuff with v + } // <- v goes out of scope and is freed here + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-11/Cargo.lock b/src/doc/book/listings/ch08-common-collections/listing-08-11/Cargo.lock new file mode 100644 index 000000000..d3daeff7d --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-11/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "collections" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-11/Cargo.toml b/src/doc/book/listings/ch08-common-collections/listing-08-11/Cargo.toml new file mode 100644 index 000000000..fe4959823 --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-11/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "collections" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-11/src/main.rs b/src/doc/book/listings/ch08-common-collections/listing-08-11/src/main.rs new file mode 100644 index 000000000..4cf4c81c2 --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-11/src/main.rs @@ -0,0 +1,5 @@ +fn main() { + // ANCHOR: here + let mut s = String::new(); + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-12/Cargo.lock b/src/doc/book/listings/ch08-common-collections/listing-08-12/Cargo.lock new file mode 100644 index 000000000..d3daeff7d --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-12/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "collections" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-12/Cargo.toml b/src/doc/book/listings/ch08-common-collections/listing-08-12/Cargo.toml new file mode 100644 index 000000000..fe4959823 --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-12/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "collections" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-12/src/main.rs b/src/doc/book/listings/ch08-common-collections/listing-08-12/src/main.rs new file mode 100644 index 000000000..d9e5e768a --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-12/src/main.rs @@ -0,0 +1,10 @@ +fn main() { + // ANCHOR: here + let data = "initial contents"; + + let s = data.to_string(); + + // the method also works on a literal directly: + let s = "initial contents".to_string(); + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-13/Cargo.lock b/src/doc/book/listings/ch08-common-collections/listing-08-13/Cargo.lock new file mode 100644 index 000000000..d3daeff7d --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-13/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "collections" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-13/Cargo.toml b/src/doc/book/listings/ch08-common-collections/listing-08-13/Cargo.toml new file mode 100644 index 000000000..fe4959823 --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-13/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "collections" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-13/src/main.rs b/src/doc/book/listings/ch08-common-collections/listing-08-13/src/main.rs new file mode 100644 index 000000000..b81e37453 --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-13/src/main.rs @@ -0,0 +1,5 @@ +fn main() { + // ANCHOR: here + let s = String::from("initial contents"); + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-14/Cargo.lock b/src/doc/book/listings/ch08-common-collections/listing-08-14/Cargo.lock new file mode 100644 index 000000000..d3daeff7d --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-14/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "collections" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-14/Cargo.toml b/src/doc/book/listings/ch08-common-collections/listing-08-14/Cargo.toml new file mode 100644 index 000000000..fe4959823 --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-14/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "collections" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-14/src/main.rs b/src/doc/book/listings/ch08-common-collections/listing-08-14/src/main.rs new file mode 100644 index 000000000..f701fd578 --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-14/src/main.rs @@ -0,0 +1,19 @@ +fn main() { + // ANCHOR: here + let hello = String::from("السلام عليكم"); + let hello = String::from("Dobrý den"); + let hello = String::from("Hello"); + let hello = String::from("שָׁלוֹם"); + let hello = String::from("नमस्ते"); + let hello = String::from("こんにちは"); + let hello = String::from("안녕하세요"); + let hello = String::from("你好"); + let hello = String::from("Olá"); + // ANCHOR: russian + let hello = String::from("Здравствуйте"); + // ANCHOR_END: russian + // ANCHOR: spanish + let hello = String::from("Hola"); + // ANCHOR_END: spanish + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-15/Cargo.lock b/src/doc/book/listings/ch08-common-collections/listing-08-15/Cargo.lock new file mode 100644 index 000000000..d3daeff7d --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-15/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "collections" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-15/Cargo.toml b/src/doc/book/listings/ch08-common-collections/listing-08-15/Cargo.toml new file mode 100644 index 000000000..fe4959823 --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-15/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "collections" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-15/src/main.rs b/src/doc/book/listings/ch08-common-collections/listing-08-15/src/main.rs new file mode 100644 index 000000000..7dec657d9 --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-15/src/main.rs @@ -0,0 +1,6 @@ +fn main() { + // ANCHOR: here + let mut s = String::from("foo"); + s.push_str("bar"); + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-16/Cargo.lock b/src/doc/book/listings/ch08-common-collections/listing-08-16/Cargo.lock new file mode 100644 index 000000000..d3daeff7d --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-16/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "collections" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-16/Cargo.toml b/src/doc/book/listings/ch08-common-collections/listing-08-16/Cargo.toml new file mode 100644 index 000000000..fe4959823 --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-16/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "collections" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-16/src/main.rs b/src/doc/book/listings/ch08-common-collections/listing-08-16/src/main.rs new file mode 100644 index 000000000..8938dc143 --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-16/src/main.rs @@ -0,0 +1,8 @@ +fn main() { + // ANCHOR: here + let mut s1 = String::from("foo"); + let s2 = "bar"; + s1.push_str(s2); + println!("s2 is {}", s2); + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-17/Cargo.lock b/src/doc/book/listings/ch08-common-collections/listing-08-17/Cargo.lock new file mode 100644 index 000000000..d3daeff7d --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-17/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "collections" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-17/Cargo.toml b/src/doc/book/listings/ch08-common-collections/listing-08-17/Cargo.toml new file mode 100644 index 000000000..fe4959823 --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-17/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "collections" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-17/src/main.rs b/src/doc/book/listings/ch08-common-collections/listing-08-17/src/main.rs new file mode 100644 index 000000000..0a9e8cc0a --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-17/src/main.rs @@ -0,0 +1,6 @@ +fn main() { + // ANCHOR: here + let mut s = String::from("lo"); + s.push('l'); + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-18/Cargo.lock b/src/doc/book/listings/ch08-common-collections/listing-08-18/Cargo.lock new file mode 100644 index 000000000..d3daeff7d --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-18/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "collections" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-18/Cargo.toml b/src/doc/book/listings/ch08-common-collections/listing-08-18/Cargo.toml new file mode 100644 index 000000000..fe4959823 --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-18/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "collections" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-18/src/main.rs b/src/doc/book/listings/ch08-common-collections/listing-08-18/src/main.rs new file mode 100644 index 000000000..93939a69f --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-18/src/main.rs @@ -0,0 +1,7 @@ +fn main() { + // ANCHOR: here + let s1 = String::from("Hello, "); + let s2 = String::from("world!"); + let s3 = s1 + &s2; // note s1 has been moved here and can no longer be used + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-19/Cargo.lock b/src/doc/book/listings/ch08-common-collections/listing-08-19/Cargo.lock new file mode 100644 index 000000000..d3daeff7d --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-19/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "collections" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-19/Cargo.toml b/src/doc/book/listings/ch08-common-collections/listing-08-19/Cargo.toml new file mode 100644 index 000000000..fe4959823 --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-19/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "collections" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-19/output.txt b/src/doc/book/listings/ch08-common-collections/listing-08-19/output.txt new file mode 100644 index 000000000..95577772e --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-19/output.txt @@ -0,0 +1,20 @@ +$ cargo run + Compiling collections v0.1.0 (file:///projects/collections) +error[E0277]: the type `String` cannot be indexed by `{integer}` + --> src/main.rs:3:13 + | +3 | let h = s1[0]; + | ^^^^^ `String` cannot be indexed by `{integer}` + | + = help: the trait `Index<{integer}>` is not implemented for `String` + = help: the following other types implement trait `Index`: + >> + > + >> + >> + >> + >> + > + +For more information about this error, try `rustc --explain E0277`. +error: could not compile `collections` due to previous error diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-19/src/main.rs b/src/doc/book/listings/ch08-common-collections/listing-08-19/src/main.rs new file mode 100644 index 000000000..fc08e9cea --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-19/src/main.rs @@ -0,0 +1,6 @@ +fn main() { + // ANCHOR: here + let s1 = String::from("hello"); + let h = s1[0]; + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-20/Cargo.lock b/src/doc/book/listings/ch08-common-collections/listing-08-20/Cargo.lock new file mode 100644 index 000000000..d3daeff7d --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-20/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "collections" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-20/Cargo.toml b/src/doc/book/listings/ch08-common-collections/listing-08-20/Cargo.toml new file mode 100644 index 000000000..fe4959823 --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-20/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "collections" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-20/src/main.rs b/src/doc/book/listings/ch08-common-collections/listing-08-20/src/main.rs new file mode 100644 index 000000000..54c201091 --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-20/src/main.rs @@ -0,0 +1,10 @@ +fn main() { + // ANCHOR: here + use std::collections::HashMap; + + let mut scores = HashMap::new(); + + scores.insert(String::from("Blue"), 10); + scores.insert(String::from("Yellow"), 50); + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-21/Cargo.lock b/src/doc/book/listings/ch08-common-collections/listing-08-21/Cargo.lock new file mode 100644 index 000000000..d3daeff7d --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-21/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "collections" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-21/Cargo.toml b/src/doc/book/listings/ch08-common-collections/listing-08-21/Cargo.toml new file mode 100644 index 000000000..fe4959823 --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-21/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "collections" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-21/src/main.rs b/src/doc/book/listings/ch08-common-collections/listing-08-21/src/main.rs new file mode 100644 index 000000000..07551549d --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-21/src/main.rs @@ -0,0 +1,13 @@ +fn main() { + // ANCHOR: here + use std::collections::HashMap; + + let mut scores = HashMap::new(); + + scores.insert(String::from("Blue"), 10); + scores.insert(String::from("Yellow"), 50); + + let team_name = String::from("Blue"); + let score = scores.get(&team_name).copied().unwrap_or(0); + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-22/Cargo.lock b/src/doc/book/listings/ch08-common-collections/listing-08-22/Cargo.lock new file mode 100644 index 000000000..d3daeff7d --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-22/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "collections" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-22/Cargo.toml b/src/doc/book/listings/ch08-common-collections/listing-08-22/Cargo.toml new file mode 100644 index 000000000..fe4959823 --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-22/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "collections" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-22/src/main.rs b/src/doc/book/listings/ch08-common-collections/listing-08-22/src/main.rs new file mode 100644 index 000000000..2b2a73f94 --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-22/src/main.rs @@ -0,0 +1,13 @@ +fn main() { + // ANCHOR: here + use std::collections::HashMap; + + let field_name = String::from("Favorite color"); + let field_value = String::from("Blue"); + + let mut map = HashMap::new(); + map.insert(field_name, field_value); + // field_name and field_value are invalid at this point, try using them and + // see what compiler error you get! + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-23/Cargo.lock b/src/doc/book/listings/ch08-common-collections/listing-08-23/Cargo.lock new file mode 100644 index 000000000..d3daeff7d --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-23/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "collections" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-23/Cargo.toml b/src/doc/book/listings/ch08-common-collections/listing-08-23/Cargo.toml new file mode 100644 index 000000000..fe4959823 --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-23/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "collections" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-23/src/main.rs b/src/doc/book/listings/ch08-common-collections/listing-08-23/src/main.rs new file mode 100644 index 000000000..e8684cf2b --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-23/src/main.rs @@ -0,0 +1,12 @@ +fn main() { + // ANCHOR: here + use std::collections::HashMap; + + let mut scores = HashMap::new(); + + scores.insert(String::from("Blue"), 10); + scores.insert(String::from("Blue"), 25); + + println!("{:?}", scores); + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-24/Cargo.lock b/src/doc/book/listings/ch08-common-collections/listing-08-24/Cargo.lock new file mode 100644 index 000000000..d3daeff7d --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-24/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "collections" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-24/Cargo.toml b/src/doc/book/listings/ch08-common-collections/listing-08-24/Cargo.toml new file mode 100644 index 000000000..fe4959823 --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-24/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "collections" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-24/src/main.rs b/src/doc/book/listings/ch08-common-collections/listing-08-24/src/main.rs new file mode 100644 index 000000000..3ad97b57a --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-24/src/main.rs @@ -0,0 +1,13 @@ +fn main() { + // ANCHOR: here + use std::collections::HashMap; + + let mut scores = HashMap::new(); + scores.insert(String::from("Blue"), 10); + + scores.entry(String::from("Yellow")).or_insert(50); + scores.entry(String::from("Blue")).or_insert(50); + + println!("{:?}", scores); + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-25/Cargo.lock b/src/doc/book/listings/ch08-common-collections/listing-08-25/Cargo.lock new file mode 100644 index 000000000..d3daeff7d --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-25/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "collections" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-25/Cargo.toml b/src/doc/book/listings/ch08-common-collections/listing-08-25/Cargo.toml new file mode 100644 index 000000000..fe4959823 --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-25/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "collections" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch08-common-collections/listing-08-25/src/main.rs b/src/doc/book/listings/ch08-common-collections/listing-08-25/src/main.rs new file mode 100644 index 000000000..f3f6aa166 --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/listing-08-25/src/main.rs @@ -0,0 +1,16 @@ +fn main() { + // ANCHOR: here + use std::collections::HashMap; + + let text = "hello world wonderful world"; + + let mut map = HashMap::new(); + + for word in text.split_whitespace() { + let count = map.entry(word).or_insert(0); + *count += 1; + } + + println!("{:?}", map); + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch08-common-collections/no-listing-01-concat-multiple-strings/Cargo.lock b/src/doc/book/listings/ch08-common-collections/no-listing-01-concat-multiple-strings/Cargo.lock new file mode 100644 index 000000000..d3daeff7d --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/no-listing-01-concat-multiple-strings/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "collections" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch08-common-collections/no-listing-01-concat-multiple-strings/Cargo.toml b/src/doc/book/listings/ch08-common-collections/no-listing-01-concat-multiple-strings/Cargo.toml new file mode 100644 index 000000000..fe4959823 --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/no-listing-01-concat-multiple-strings/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "collections" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch08-common-collections/no-listing-01-concat-multiple-strings/src/main.rs b/src/doc/book/listings/ch08-common-collections/no-listing-01-concat-multiple-strings/src/main.rs new file mode 100644 index 000000000..4995650d1 --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/no-listing-01-concat-multiple-strings/src/main.rs @@ -0,0 +1,9 @@ +fn main() { + // ANCHOR: here + let s1 = String::from("tic"); + let s2 = String::from("tac"); + let s3 = String::from("toe"); + + let s = s1 + "-" + &s2 + "-" + &s3; + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch08-common-collections/no-listing-02-format/Cargo.lock b/src/doc/book/listings/ch08-common-collections/no-listing-02-format/Cargo.lock new file mode 100644 index 000000000..d3daeff7d --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/no-listing-02-format/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "collections" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch08-common-collections/no-listing-02-format/Cargo.toml b/src/doc/book/listings/ch08-common-collections/no-listing-02-format/Cargo.toml new file mode 100644 index 000000000..fe4959823 --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/no-listing-02-format/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "collections" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch08-common-collections/no-listing-02-format/src/main.rs b/src/doc/book/listings/ch08-common-collections/no-listing-02-format/src/main.rs new file mode 100644 index 000000000..4a38e63d2 --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/no-listing-02-format/src/main.rs @@ -0,0 +1,9 @@ +fn main() { + // ANCHOR: here + let s1 = String::from("tic"); + let s2 = String::from("tac"); + let s3 = String::from("toe"); + + let s = format!("{}-{}-{}", s1, s2, s3); + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch08-common-collections/no-listing-03-iterate-over-hashmap/Cargo.lock b/src/doc/book/listings/ch08-common-collections/no-listing-03-iterate-over-hashmap/Cargo.lock new file mode 100644 index 000000000..d3daeff7d --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/no-listing-03-iterate-over-hashmap/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "collections" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch08-common-collections/no-listing-03-iterate-over-hashmap/Cargo.toml b/src/doc/book/listings/ch08-common-collections/no-listing-03-iterate-over-hashmap/Cargo.toml new file mode 100644 index 000000000..fe4959823 --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/no-listing-03-iterate-over-hashmap/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "collections" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch08-common-collections/no-listing-03-iterate-over-hashmap/src/main.rs b/src/doc/book/listings/ch08-common-collections/no-listing-03-iterate-over-hashmap/src/main.rs new file mode 100644 index 000000000..2e7dc02e6 --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/no-listing-03-iterate-over-hashmap/src/main.rs @@ -0,0 +1,14 @@ +fn main() { + // ANCHOR: here + use std::collections::HashMap; + + let mut scores = HashMap::new(); + + scores.insert(String::from("Blue"), 10); + scores.insert(String::from("Yellow"), 50); + + for (key, value) in &scores { + println!("{}: {}", key, value); + } + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch08-common-collections/output-only-01-not-char-boundary/Cargo.lock b/src/doc/book/listings/ch08-common-collections/output-only-01-not-char-boundary/Cargo.lock new file mode 100644 index 000000000..d3daeff7d --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/output-only-01-not-char-boundary/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "collections" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch08-common-collections/output-only-01-not-char-boundary/Cargo.toml b/src/doc/book/listings/ch08-common-collections/output-only-01-not-char-boundary/Cargo.toml new file mode 100644 index 000000000..fe4959823 --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/output-only-01-not-char-boundary/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "collections" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch08-common-collections/output-only-01-not-char-boundary/output.txt b/src/doc/book/listings/ch08-common-collections/output-only-01-not-char-boundary/output.txt new file mode 100644 index 000000000..98d1f183a --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/output-only-01-not-char-boundary/output.txt @@ -0,0 +1,6 @@ +$ cargo run + Compiling collections v0.1.0 (file:///projects/collections) + Finished dev [unoptimized + debuginfo] target(s) in 0.43s + Running `target/debug/collections` +thread 'main' panicked at 'byte index 1 is not a char boundary; it is inside 'З' (bytes 0..2) of `Здравствуйте`', library/core/src/str/mod.rs:127:5 +note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace diff --git a/src/doc/book/listings/ch08-common-collections/output-only-01-not-char-boundary/src/main.rs b/src/doc/book/listings/ch08-common-collections/output-only-01-not-char-boundary/src/main.rs new file mode 100644 index 000000000..9283ff5bd --- /dev/null +++ b/src/doc/book/listings/ch08-common-collections/output-only-01-not-char-boundary/src/main.rs @@ -0,0 +1,5 @@ +fn main() { + let hello = "Здравствуйте"; + + let s = &hello[0..1]; +} diff --git a/src/doc/book/listings/ch09-error-handling/listing-09-01/Cargo.lock b/src/doc/book/listings/ch09-error-handling/listing-09-01/Cargo.lock new file mode 100644 index 000000000..4fe030fab --- /dev/null +++ b/src/doc/book/listings/ch09-error-handling/listing-09-01/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "panic" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch09-error-handling/listing-09-01/Cargo.toml b/src/doc/book/listings/ch09-error-handling/listing-09-01/Cargo.toml new file mode 100644 index 000000000..660e2c819 --- /dev/null +++ b/src/doc/book/listings/ch09-error-handling/listing-09-01/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "panic" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch09-error-handling/listing-09-01/output.txt b/src/doc/book/listings/ch09-error-handling/listing-09-01/output.txt new file mode 100644 index 000000000..89aebb952 --- /dev/null +++ b/src/doc/book/listings/ch09-error-handling/listing-09-01/output.txt @@ -0,0 +1,6 @@ +$ cargo run + Compiling panic v0.1.0 (file:///projects/panic) + Finished dev [unoptimized + debuginfo] target(s) in 0.27s + Running `target/debug/panic` +thread 'main' panicked at 'index out of bounds: the len is 3 but the index is 99', src/main.rs:4:5 +note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace diff --git a/src/doc/book/listings/ch09-error-handling/listing-09-01/src/main.rs b/src/doc/book/listings/ch09-error-handling/listing-09-01/src/main.rs new file mode 100644 index 000000000..70194abd7 --- /dev/null +++ b/src/doc/book/listings/ch09-error-handling/listing-09-01/src/main.rs @@ -0,0 +1,5 @@ +fn main() { + let v = vec![1, 2, 3]; + + v[99]; +} diff --git a/src/doc/book/listings/ch09-error-handling/listing-09-03/Cargo.lock b/src/doc/book/listings/ch09-error-handling/listing-09-03/Cargo.lock new file mode 100644 index 000000000..1fa96b797 --- /dev/null +++ b/src/doc/book/listings/ch09-error-handling/listing-09-03/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "error-handling" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch09-error-handling/listing-09-03/Cargo.toml b/src/doc/book/listings/ch09-error-handling/listing-09-03/Cargo.toml new file mode 100644 index 000000000..c496db783 --- /dev/null +++ b/src/doc/book/listings/ch09-error-handling/listing-09-03/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "error-handling" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch09-error-handling/listing-09-03/src/main.rs b/src/doc/book/listings/ch09-error-handling/listing-09-03/src/main.rs new file mode 100644 index 000000000..2342904ed --- /dev/null +++ b/src/doc/book/listings/ch09-error-handling/listing-09-03/src/main.rs @@ -0,0 +1,5 @@ +use std::fs::File; + +fn main() { + let greeting_file_result = File::open("hello.txt"); +} diff --git a/src/doc/book/listings/ch09-error-handling/listing-09-04/Cargo.lock b/src/doc/book/listings/ch09-error-handling/listing-09-04/Cargo.lock new file mode 100644 index 000000000..1fa96b797 --- /dev/null +++ b/src/doc/book/listings/ch09-error-handling/listing-09-04/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "error-handling" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch09-error-handling/listing-09-04/Cargo.toml b/src/doc/book/listings/ch09-error-handling/listing-09-04/Cargo.toml new file mode 100644 index 000000000..c496db783 --- /dev/null +++ b/src/doc/book/listings/ch09-error-handling/listing-09-04/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "error-handling" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch09-error-handling/listing-09-04/output.txt b/src/doc/book/listings/ch09-error-handling/listing-09-04/output.txt new file mode 100644 index 000000000..f776a591c --- /dev/null +++ b/src/doc/book/listings/ch09-error-handling/listing-09-04/output.txt @@ -0,0 +1,6 @@ +$ cargo run + Compiling error-handling v0.1.0 (file:///projects/error-handling) + Finished dev [unoptimized + debuginfo] target(s) in 0.73s + Running `target/debug/error-handling` +thread 'main' panicked at 'Problem opening the file: Os { code: 2, kind: NotFound, message: "No such file or directory" }', src/main.rs:8:23 +note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace diff --git a/src/doc/book/listings/ch09-error-handling/listing-09-04/src/main.rs b/src/doc/book/listings/ch09-error-handling/listing-09-04/src/main.rs new file mode 100644 index 000000000..69da109fe --- /dev/null +++ b/src/doc/book/listings/ch09-error-handling/listing-09-04/src/main.rs @@ -0,0 +1,10 @@ +use std::fs::File; + +fn main() { + let greeting_file_result = File::open("hello.txt"); + + let greeting_file = match greeting_file_result { + Ok(file) => file, + Err(error) => panic!("Problem opening the file: {:?}", error), + }; +} diff --git a/src/doc/book/listings/ch09-error-handling/listing-09-05/Cargo.lock b/src/doc/book/listings/ch09-error-handling/listing-09-05/Cargo.lock new file mode 100644 index 000000000..1fa96b797 --- /dev/null +++ b/src/doc/book/listings/ch09-error-handling/listing-09-05/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "error-handling" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch09-error-handling/listing-09-05/Cargo.toml b/src/doc/book/listings/ch09-error-handling/listing-09-05/Cargo.toml new file mode 100644 index 000000000..c496db783 --- /dev/null +++ b/src/doc/book/listings/ch09-error-handling/listing-09-05/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "error-handling" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch09-error-handling/listing-09-05/src/main.rs b/src/doc/book/listings/ch09-error-handling/listing-09-05/src/main.rs new file mode 100644 index 000000000..83ea01044 --- /dev/null +++ b/src/doc/book/listings/ch09-error-handling/listing-09-05/src/main.rs @@ -0,0 +1,19 @@ +use std::fs::File; +use std::io::ErrorKind; + +fn main() { + let greeting_file_result = File::open("hello.txt"); + + let greeting_file = match greeting_file_result { + Ok(file) => file, + Err(error) => match error.kind() { + ErrorKind::NotFound => match File::create("hello.txt") { + Ok(fc) => fc, + Err(e) => panic!("Problem creating the file: {:?}", e), + }, + other_error => { + panic!("Problem opening the file: {:?}", other_error); + } + }, + }; +} diff --git a/src/doc/book/listings/ch09-error-handling/listing-09-06/Cargo.lock b/src/doc/book/listings/ch09-error-handling/listing-09-06/Cargo.lock new file mode 100644 index 000000000..1fa96b797 --- /dev/null +++ b/src/doc/book/listings/ch09-error-handling/listing-09-06/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "error-handling" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch09-error-handling/listing-09-06/Cargo.toml b/src/doc/book/listings/ch09-error-handling/listing-09-06/Cargo.toml new file mode 100644 index 000000000..c496db783 --- /dev/null +++ b/src/doc/book/listings/ch09-error-handling/listing-09-06/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "error-handling" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch09-error-handling/listing-09-06/src/main.rs b/src/doc/book/listings/ch09-error-handling/listing-09-06/src/main.rs new file mode 100644 index 000000000..a70734cb5 --- /dev/null +++ b/src/doc/book/listings/ch09-error-handling/listing-09-06/src/main.rs @@ -0,0 +1,24 @@ +// ANCHOR: here +use std::fs::File; +use std::io::{self, Read}; + +fn read_username_from_file() -> Result { + let username_file_result = File::open("hello.txt"); + + let mut username_file = match username_file_result { + Ok(file) => file, + Err(e) => return Err(e), + }; + + let mut username = String::new(); + + match username_file.read_to_string(&mut username) { + Ok(_) => Ok(username), + Err(e) => Err(e), + } +} +// ANCHOR_END: here + +fn main() { + let username = read_username_from_file().expect("Unable to get username"); +} diff --git a/src/doc/book/listings/ch09-error-handling/listing-09-07/Cargo.lock b/src/doc/book/listings/ch09-error-handling/listing-09-07/Cargo.lock new file mode 100644 index 000000000..1fa96b797 --- /dev/null +++ b/src/doc/book/listings/ch09-error-handling/listing-09-07/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "error-handling" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch09-error-handling/listing-09-07/Cargo.toml b/src/doc/book/listings/ch09-error-handling/listing-09-07/Cargo.toml new file mode 100644 index 000000000..c496db783 --- /dev/null +++ b/src/doc/book/listings/ch09-error-handling/listing-09-07/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "error-handling" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch09-error-handling/listing-09-07/src/main.rs b/src/doc/book/listings/ch09-error-handling/listing-09-07/src/main.rs new file mode 100644 index 000000000..f4564f670 --- /dev/null +++ b/src/doc/book/listings/ch09-error-handling/listing-09-07/src/main.rs @@ -0,0 +1,16 @@ +// ANCHOR: here +use std::fs::File; +use std::io; +use std::io::Read; + +fn read_username_from_file() -> Result { + let mut username_file = File::open("hello.txt")?; + let mut username = String::new(); + username_file.read_to_string(&mut username)?; + Ok(username) +} +// ANCHOR_END: here + +fn main() { + let username = read_username_from_file().expect("Unable to get username"); +} diff --git a/src/doc/book/listings/ch09-error-handling/listing-09-08/Cargo.lock b/src/doc/book/listings/ch09-error-handling/listing-09-08/Cargo.lock new file mode 100644 index 000000000..1fa96b797 --- /dev/null +++ b/src/doc/book/listings/ch09-error-handling/listing-09-08/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "error-handling" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch09-error-handling/listing-09-08/Cargo.toml b/src/doc/book/listings/ch09-error-handling/listing-09-08/Cargo.toml new file mode 100644 index 000000000..c496db783 --- /dev/null +++ b/src/doc/book/listings/ch09-error-handling/listing-09-08/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "error-handling" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch09-error-handling/listing-09-08/src/main.rs b/src/doc/book/listings/ch09-error-handling/listing-09-08/src/main.rs new file mode 100644 index 000000000..c3c6e23ef --- /dev/null +++ b/src/doc/book/listings/ch09-error-handling/listing-09-08/src/main.rs @@ -0,0 +1,17 @@ +// ANCHOR: here +use std::fs::File; +use std::io; +use std::io::Read; + +fn read_username_from_file() -> Result { + let mut username = String::new(); + + File::open("hello.txt")?.read_to_string(&mut username)?; + + Ok(username) +} +// ANCHOR_END: here + +fn main() { + let username = read_username_from_file().expect("Unable to get username"); +} diff --git a/src/doc/book/listings/ch09-error-handling/listing-09-09/Cargo.lock b/src/doc/book/listings/ch09-error-handling/listing-09-09/Cargo.lock new file mode 100644 index 000000000..1fa96b797 --- /dev/null +++ b/src/doc/book/listings/ch09-error-handling/listing-09-09/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "error-handling" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch09-error-handling/listing-09-09/Cargo.toml b/src/doc/book/listings/ch09-error-handling/listing-09-09/Cargo.toml new file mode 100644 index 000000000..c496db783 --- /dev/null +++ b/src/doc/book/listings/ch09-error-handling/listing-09-09/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "error-handling" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch09-error-handling/listing-09-09/src/main.rs b/src/doc/book/listings/ch09-error-handling/listing-09-09/src/main.rs new file mode 100644 index 000000000..4597dc2ee --- /dev/null +++ b/src/doc/book/listings/ch09-error-handling/listing-09-09/src/main.rs @@ -0,0 +1,12 @@ +// ANCHOR: here +use std::fs; +use std::io; + +fn read_username_from_file() -> Result { + fs::read_to_string("hello.txt") +} +// ANCHOR_END: here + +fn main() { + let username = read_username_from_file().expect("Unable to get username"); +} diff --git a/src/doc/book/listings/ch09-error-handling/listing-09-10/Cargo.lock b/src/doc/book/listings/ch09-error-handling/listing-09-10/Cargo.lock new file mode 100644 index 000000000..1fa96b797 --- /dev/null +++ b/src/doc/book/listings/ch09-error-handling/listing-09-10/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "error-handling" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch09-error-handling/listing-09-10/Cargo.toml b/src/doc/book/listings/ch09-error-handling/listing-09-10/Cargo.toml new file mode 100644 index 000000000..c496db783 --- /dev/null +++ b/src/doc/book/listings/ch09-error-handling/listing-09-10/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "error-handling" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch09-error-handling/listing-09-10/output.txt b/src/doc/book/listings/ch09-error-handling/listing-09-10/output.txt new file mode 100644 index 000000000..26e4ff8cc --- /dev/null +++ b/src/doc/book/listings/ch09-error-handling/listing-09-10/output.txt @@ -0,0 +1,15 @@ +$ cargo run + Compiling error-handling v0.1.0 (file:///projects/error-handling) +error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`) + --> src/main.rs:4:48 + | +3 | / fn main() { +4 | | let greeting_file = File::open("hello.txt")?; + | | ^ cannot use the `?` operator in a function that returns `()` +5 | | } + | |_- this function should return `Result` or `Option` to accept `?` + | + = help: the trait `FromResidual>` is not implemented for `()` + +For more information about this error, try `rustc --explain E0277`. +error: could not compile `error-handling` due to previous error diff --git a/src/doc/book/listings/ch09-error-handling/listing-09-10/src/main.rs b/src/doc/book/listings/ch09-error-handling/listing-09-10/src/main.rs new file mode 100644 index 000000000..38b005480 --- /dev/null +++ b/src/doc/book/listings/ch09-error-handling/listing-09-10/src/main.rs @@ -0,0 +1,5 @@ +use std::fs::File; + +fn main() { + let greeting_file = File::open("hello.txt")?; +} diff --git a/src/doc/book/listings/ch09-error-handling/listing-09-11/Cargo.lock b/src/doc/book/listings/ch09-error-handling/listing-09-11/Cargo.lock new file mode 100644 index 000000000..7320ae639 --- /dev/null +++ b/src/doc/book/listings/ch09-error-handling/listing-09-11/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "error-handling" +version = "0.1.0" diff --git a/src/doc/book/listings/ch09-error-handling/listing-09-11/Cargo.toml b/src/doc/book/listings/ch09-error-handling/listing-09-11/Cargo.toml new file mode 100644 index 000000000..c496db783 --- /dev/null +++ b/src/doc/book/listings/ch09-error-handling/listing-09-11/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "error-handling" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch09-error-handling/listing-09-11/src/main.rs b/src/doc/book/listings/ch09-error-handling/listing-09-11/src/main.rs new file mode 100644 index 000000000..bd5322786 --- /dev/null +++ b/src/doc/book/listings/ch09-error-handling/listing-09-11/src/main.rs @@ -0,0 +1,15 @@ +// ANCHOR: here +fn last_char_of_first_line(text: &str) -> Option { + text.lines().next()?.chars().last() +} +// ANCHOR_END: here + +fn main() { + assert_eq!( + last_char_of_first_line("Hello, world\nHow are you today?"), + Some('d') + ); + + assert_eq!(last_char_of_first_line(""), None); + assert_eq!(last_char_of_first_line("\nhi"), None); +} diff --git a/src/doc/book/listings/ch09-error-handling/listing-09-12/Cargo.lock b/src/doc/book/listings/ch09-error-handling/listing-09-12/Cargo.lock new file mode 100644 index 000000000..1fa96b797 --- /dev/null +++ b/src/doc/book/listings/ch09-error-handling/listing-09-12/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "error-handling" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch09-error-handling/listing-09-12/Cargo.toml b/src/doc/book/listings/ch09-error-handling/listing-09-12/Cargo.toml new file mode 100644 index 000000000..c496db783 --- /dev/null +++ b/src/doc/book/listings/ch09-error-handling/listing-09-12/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "error-handling" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch09-error-handling/listing-09-12/src/main.rs b/src/doc/book/listings/ch09-error-handling/listing-09-12/src/main.rs new file mode 100644 index 000000000..b0f7445f4 --- /dev/null +++ b/src/doc/book/listings/ch09-error-handling/listing-09-12/src/main.rs @@ -0,0 +1,8 @@ +use std::error::Error; +use std::fs::File; + +fn main() -> Result<(), Box> { + let greeting_file = File::open("hello.txt")?; + + Ok(()) +} diff --git a/src/doc/book/listings/ch09-error-handling/listing-09-13/Cargo.lock b/src/doc/book/listings/ch09-error-handling/listing-09-13/Cargo.lock new file mode 100644 index 000000000..0a2f222c2 --- /dev/null +++ b/src/doc/book/listings/ch09-error-handling/listing-09-13/Cargo.lock @@ -0,0 +1,83 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "getrandom" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "guessing_game" +version = "0.1.0" +dependencies = [ + "rand", +] + +[[package]] +name = "libc" +version = "0.2.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7282d924be3275cec7f6756ff4121987bc6481325397dde6ba3e7802b1a8b1c" + +[[package]] +name = "ppv-lite86" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" + +[[package]] +name = "rand" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", + "rand_hc", +] + +[[package]] +name = "rand_chacha" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34cf66eb183df1c5876e2dcf6b13d57340741e8dc255b48e40a26de954d06ae7" +dependencies = [ + "getrandom", +] + +[[package]] +name = "rand_hc" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73" +dependencies = [ + "rand_core", +] + +[[package]] +name = "wasi" +version = "0.10.2+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" diff --git a/src/doc/book/listings/ch09-error-handling/listing-09-13/Cargo.toml b/src/doc/book/listings/ch09-error-handling/listing-09-13/Cargo.toml new file mode 100644 index 000000000..15b3fffca --- /dev/null +++ b/src/doc/book/listings/ch09-error-handling/listing-09-13/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "guessing_game" +version = "0.1.0" +edition = "2021" + +[dependencies] +rand = "0.8.3" diff --git a/src/doc/book/listings/ch09-error-handling/listing-09-13/src/main.rs b/src/doc/book/listings/ch09-error-handling/listing-09-13/src/main.rs new file mode 100644 index 000000000..9e07c1ee2 --- /dev/null +++ b/src/doc/book/listings/ch09-error-handling/listing-09-13/src/main.rs @@ -0,0 +1,55 @@ +use rand::Rng; +use std::cmp::Ordering; +use std::io; + +// ANCHOR: here +pub struct Guess { + value: i32, +} + +impl Guess { + pub fn new(value: i32) -> Guess { + if value < 1 || value > 100 { + panic!("Guess value must be between 1 and 100, got {}.", value); + } + + Guess { value } + } + + pub fn value(&self) -> i32 { + self.value + } +} +// ANCHOR_END: here + +fn main() { + println!("Guess the number!"); + + let secret_number = rand::thread_rng().gen_range(1..=100); + + loop { + println!("Please input your guess."); + + let mut guess = String::new(); + + io::stdin() + .read_line(&mut guess) + .expect("Failed to read line"); + + let guess: i32 = match guess.trim().parse() { + Ok(num) => num, + Err(_) => continue, + }; + + let guess = Guess::new(guess); + + match guess.value().cmp(&secret_number) { + Ordering::Less => println!("Too small!"), + Ordering::Greater => println!("Too big!"), + Ordering::Equal => { + println!("You win!"); + break; + } + } + } +} diff --git a/src/doc/book/listings/ch09-error-handling/no-listing-01-panic/Cargo.lock b/src/doc/book/listings/ch09-error-handling/no-listing-01-panic/Cargo.lock new file mode 100644 index 000000000..4fe030fab --- /dev/null +++ b/src/doc/book/listings/ch09-error-handling/no-listing-01-panic/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "panic" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch09-error-handling/no-listing-01-panic/Cargo.toml b/src/doc/book/listings/ch09-error-handling/no-listing-01-panic/Cargo.toml new file mode 100644 index 000000000..660e2c819 --- /dev/null +++ b/src/doc/book/listings/ch09-error-handling/no-listing-01-panic/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "panic" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch09-error-handling/no-listing-01-panic/output.txt b/src/doc/book/listings/ch09-error-handling/no-listing-01-panic/output.txt new file mode 100644 index 000000000..b25ed85d7 --- /dev/null +++ b/src/doc/book/listings/ch09-error-handling/no-listing-01-panic/output.txt @@ -0,0 +1,6 @@ +$ cargo run + Compiling panic v0.1.0 (file:///projects/panic) + Finished dev [unoptimized + debuginfo] target(s) in 0.25s + Running `target/debug/panic` +thread 'main' panicked at 'crash and burn', src/main.rs:2:5 +note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace diff --git a/src/doc/book/listings/ch09-error-handling/no-listing-01-panic/src/main.rs b/src/doc/book/listings/ch09-error-handling/no-listing-01-panic/src/main.rs new file mode 100644 index 000000000..32a4c243d --- /dev/null +++ b/src/doc/book/listings/ch09-error-handling/no-listing-01-panic/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + panic!("crash and burn"); +} diff --git a/src/doc/book/listings/ch09-error-handling/no-listing-04-unwrap/Cargo.lock b/src/doc/book/listings/ch09-error-handling/no-listing-04-unwrap/Cargo.lock new file mode 100644 index 000000000..1fa96b797 --- /dev/null +++ b/src/doc/book/listings/ch09-error-handling/no-listing-04-unwrap/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "error-handling" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch09-error-handling/no-listing-04-unwrap/Cargo.toml b/src/doc/book/listings/ch09-error-handling/no-listing-04-unwrap/Cargo.toml new file mode 100644 index 000000000..c496db783 --- /dev/null +++ b/src/doc/book/listings/ch09-error-handling/no-listing-04-unwrap/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "error-handling" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch09-error-handling/no-listing-04-unwrap/src/main.rs b/src/doc/book/listings/ch09-error-handling/no-listing-04-unwrap/src/main.rs new file mode 100644 index 000000000..92e9452f1 --- /dev/null +++ b/src/doc/book/listings/ch09-error-handling/no-listing-04-unwrap/src/main.rs @@ -0,0 +1,5 @@ +use std::fs::File; + +fn main() { + let greeting_file = File::open("hello.txt").unwrap(); +} diff --git a/src/doc/book/listings/ch09-error-handling/no-listing-05-expect/Cargo.lock b/src/doc/book/listings/ch09-error-handling/no-listing-05-expect/Cargo.lock new file mode 100644 index 000000000..1fa96b797 --- /dev/null +++ b/src/doc/book/listings/ch09-error-handling/no-listing-05-expect/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "error-handling" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch09-error-handling/no-listing-05-expect/Cargo.toml b/src/doc/book/listings/ch09-error-handling/no-listing-05-expect/Cargo.toml new file mode 100644 index 000000000..c496db783 --- /dev/null +++ b/src/doc/book/listings/ch09-error-handling/no-listing-05-expect/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "error-handling" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch09-error-handling/no-listing-05-expect/src/main.rs b/src/doc/book/listings/ch09-error-handling/no-listing-05-expect/src/main.rs new file mode 100644 index 000000000..3d36fa5b1 --- /dev/null +++ b/src/doc/book/listings/ch09-error-handling/no-listing-05-expect/src/main.rs @@ -0,0 +1,6 @@ +use std::fs::File; + +fn main() { + let greeting_file = File::open("hello.txt") + .expect("hello.txt should be included in this project"); +} diff --git a/src/doc/book/listings/ch09-error-handling/no-listing-08-unwrap-that-cant-fail/Cargo.lock b/src/doc/book/listings/ch09-error-handling/no-listing-08-unwrap-that-cant-fail/Cargo.lock new file mode 100644 index 000000000..1fa96b797 --- /dev/null +++ b/src/doc/book/listings/ch09-error-handling/no-listing-08-unwrap-that-cant-fail/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "error-handling" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch09-error-handling/no-listing-08-unwrap-that-cant-fail/Cargo.toml b/src/doc/book/listings/ch09-error-handling/no-listing-08-unwrap-that-cant-fail/Cargo.toml new file mode 100644 index 000000000..c496db783 --- /dev/null +++ b/src/doc/book/listings/ch09-error-handling/no-listing-08-unwrap-that-cant-fail/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "error-handling" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch09-error-handling/no-listing-08-unwrap-that-cant-fail/src/main.rs b/src/doc/book/listings/ch09-error-handling/no-listing-08-unwrap-that-cant-fail/src/main.rs new file mode 100644 index 000000000..3e1835266 --- /dev/null +++ b/src/doc/book/listings/ch09-error-handling/no-listing-08-unwrap-that-cant-fail/src/main.rs @@ -0,0 +1,9 @@ +fn main() { + // ANCHOR: here + use std::net::IpAddr; + + let home: IpAddr = "127.0.0.1" + .parse() + .expect("Hardcoded IP address should be valid"); + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch09-error-handling/no-listing-09-guess-out-of-range/Cargo.lock b/src/doc/book/listings/ch09-error-handling/no-listing-09-guess-out-of-range/Cargo.lock new file mode 100644 index 000000000..0a2f222c2 --- /dev/null +++ b/src/doc/book/listings/ch09-error-handling/no-listing-09-guess-out-of-range/Cargo.lock @@ -0,0 +1,83 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "getrandom" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "guessing_game" +version = "0.1.0" +dependencies = [ + "rand", +] + +[[package]] +name = "libc" +version = "0.2.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7282d924be3275cec7f6756ff4121987bc6481325397dde6ba3e7802b1a8b1c" + +[[package]] +name = "ppv-lite86" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" + +[[package]] +name = "rand" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", + "rand_hc", +] + +[[package]] +name = "rand_chacha" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34cf66eb183df1c5876e2dcf6b13d57340741e8dc255b48e40a26de954d06ae7" +dependencies = [ + "getrandom", +] + +[[package]] +name = "rand_hc" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73" +dependencies = [ + "rand_core", +] + +[[package]] +name = "wasi" +version = "0.10.2+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" diff --git a/src/doc/book/listings/ch09-error-handling/no-listing-09-guess-out-of-range/Cargo.toml b/src/doc/book/listings/ch09-error-handling/no-listing-09-guess-out-of-range/Cargo.toml new file mode 100644 index 000000000..15b3fffca --- /dev/null +++ b/src/doc/book/listings/ch09-error-handling/no-listing-09-guess-out-of-range/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "guessing_game" +version = "0.1.0" +edition = "2021" + +[dependencies] +rand = "0.8.3" diff --git a/src/doc/book/listings/ch09-error-handling/no-listing-09-guess-out-of-range/src/main.rs b/src/doc/book/listings/ch09-error-handling/no-listing-09-guess-out-of-range/src/main.rs new file mode 100644 index 000000000..fc22cbc5e --- /dev/null +++ b/src/doc/book/listings/ch09-error-handling/no-listing-09-guess-out-of-range/src/main.rs @@ -0,0 +1,47 @@ +use rand::Rng; +use std::cmp::Ordering; +use std::io; + +fn main() { + println!("Guess the number!"); + + let secret_number = rand::thread_rng().gen_range(1..=100); + + // ANCHOR: here + loop { + // --snip-- + + // ANCHOR_END: here + println!("Please input your guess."); + + let mut guess = String::new(); + + io::stdin() + .read_line(&mut guess) + .expect("Failed to read line"); + + // ANCHOR: here + let guess: i32 = match guess.trim().parse() { + Ok(num) => num, + Err(_) => continue, + }; + + if guess < 1 || guess > 100 { + println!("The secret number will be between 1 and 100."); + continue; + } + + match guess.cmp(&secret_number) { + // --snip-- + // ANCHOR_END: here + Ordering::Less => println!("Too small!"), + Ordering::Greater => println!("Too big!"), + Ordering::Equal => { + println!("You win!"); + break; + } + } + // ANCHOR: here + } + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-01/Cargo.lock b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-01/Cargo.lock new file mode 100644 index 000000000..e8007a19b --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-01/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "chapter10" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-01/Cargo.toml b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-01/Cargo.toml new file mode 100644 index 000000000..489f80967 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-01/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "chapter10" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-01/src/main.rs b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-01/src/main.rs new file mode 100644 index 000000000..a4dba7ed4 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-01/src/main.rs @@ -0,0 +1,18 @@ +// ANCHOR: here +fn main() { + let number_list = vec![34, 50, 25, 100, 65]; + + let mut largest = &number_list[0]; + + for number in &number_list { + if number > largest { + largest = number; + } + } + + println!("The largest number is {}", largest); + // ANCHOR_END: here + assert_eq!(*largest, 100); + // ANCHOR: here +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-02/Cargo.lock b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-02/Cargo.lock new file mode 100644 index 000000000..e8007a19b --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-02/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "chapter10" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-02/Cargo.toml b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-02/Cargo.toml new file mode 100644 index 000000000..489f80967 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-02/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "chapter10" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-02/src/main.rs b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-02/src/main.rs new file mode 100644 index 000000000..8c523a8be --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-02/src/main.rs @@ -0,0 +1,25 @@ +fn main() { + let number_list = vec![34, 50, 25, 100, 65]; + + let mut largest = &number_list[0]; + + for number in &number_list { + if number > largest { + largest = number; + } + } + + println!("The largest number is {}", largest); + + let number_list = vec![102, 34, 6000, 89, 54, 2, 43, 8]; + + let mut largest = &number_list[0]; + + for number in &number_list { + if number > largest { + largest = number; + } + } + + println!("The largest number is {}", largest); +} diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-03/Cargo.lock b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-03/Cargo.lock new file mode 100644 index 000000000..e8007a19b --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-03/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "chapter10" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-03/Cargo.toml b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-03/Cargo.toml new file mode 100644 index 000000000..489f80967 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-03/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "chapter10" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-03/src/main.rs b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-03/src/main.rs new file mode 100644 index 000000000..899222909 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-03/src/main.rs @@ -0,0 +1,31 @@ +// ANCHOR: here +fn largest(list: &[i32]) -> &i32 { + let mut largest = &list[0]; + + for item in list { + if item > largest { + largest = item; + } + } + + largest +} + +fn main() { + let number_list = vec![34, 50, 25, 100, 65]; + + let result = largest(&number_list); + println!("The largest number is {}", result); + // ANCHOR_END: here + assert_eq!(*result, 100); + // ANCHOR: here + + let number_list = vec![102, 34, 6000, 89, 54, 2, 43, 8]; + + let result = largest(&number_list); + println!("The largest number is {}", result); + // ANCHOR_END: here + assert_eq!(*result, 6000); + // ANCHOR: here +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-04/Cargo.lock b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-04/Cargo.lock new file mode 100644 index 000000000..e8007a19b --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-04/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "chapter10" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-04/Cargo.toml b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-04/Cargo.toml new file mode 100644 index 000000000..489f80967 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-04/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "chapter10" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-04/src/main.rs b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-04/src/main.rs new file mode 100644 index 000000000..a47e3f232 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-04/src/main.rs @@ -0,0 +1,43 @@ +// ANCHOR: here +fn largest_i32(list: &[i32]) -> &i32 { + let mut largest = &list[0]; + + for item in list { + if item > largest { + largest = item; + } + } + + largest +} + +fn largest_char(list: &[char]) -> &char { + let mut largest = &list[0]; + + for item in list { + if item > largest { + largest = item; + } + } + + largest +} + +fn main() { + let number_list = vec![34, 50, 25, 100, 65]; + + let result = largest_i32(&number_list); + println!("The largest number is {}", result); + // ANCHOR_END: here + assert_eq!(*result, 100); + // ANCHOR: here + + let char_list = vec!['y', 'm', 'a', 'q']; + + let result = largest_char(&char_list); + println!("The largest char is {}", result); + // ANCHOR_END: here + assert_eq!(*result, 'y'); + // ANCHOR: here +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-05/Cargo.lock b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-05/Cargo.lock new file mode 100644 index 000000000..e8007a19b --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-05/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "chapter10" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-05/Cargo.toml b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-05/Cargo.toml new file mode 100644 index 000000000..489f80967 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-05/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "chapter10" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-05/output.txt b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-05/output.txt new file mode 100644 index 000000000..1a705ed57 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-05/output.txt @@ -0,0 +1,17 @@ +$ cargo run + Compiling chapter10 v0.1.0 (file:///projects/chapter10) +error[E0369]: binary operation `>` cannot be applied to type `&T` + --> src/main.rs:5:17 + | +5 | if item > largest { + | ---- ^ ------- &T + | | + | &T + | +help: consider restricting type parameter `T` + | +1 | fn largest(list: &[T]) -> &T { + | ++++++++++++++++++++++ + +For more information about this error, try `rustc --explain E0369`. +error: could not compile `chapter10` due to previous error diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-05/src/main.rs b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-05/src/main.rs new file mode 100644 index 000000000..df33743f7 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-05/src/main.rs @@ -0,0 +1,23 @@ +fn largest(list: &[T]) -> &T { + let mut largest = &list[0]; + + for item in list { + if item > largest { + largest = item; + } + } + + largest +} + +fn main() { + let number_list = vec![34, 50, 25, 100, 65]; + + let result = largest(&number_list); + println!("The largest number is {}", result); + + let char_list = vec!['y', 'm', 'a', 'q']; + + let result = largest(&char_list); + println!("The largest char is {}", result); +} diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-06/Cargo.lock b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-06/Cargo.lock new file mode 100644 index 000000000..e8007a19b --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-06/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "chapter10" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-06/Cargo.toml b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-06/Cargo.toml new file mode 100644 index 000000000..489f80967 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-06/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "chapter10" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-06/src/main.rs b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-06/src/main.rs new file mode 100644 index 000000000..4252593df --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-06/src/main.rs @@ -0,0 +1,9 @@ +struct Point { + x: T, + y: T, +} + +fn main() { + let integer = Point { x: 5, y: 10 }; + let float = Point { x: 1.0, y: 4.0 }; +} diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-07/Cargo.lock b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-07/Cargo.lock new file mode 100644 index 000000000..e8007a19b --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-07/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "chapter10" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-07/Cargo.toml b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-07/Cargo.toml new file mode 100644 index 000000000..489f80967 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-07/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "chapter10" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-07/output.txt b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-07/output.txt new file mode 100644 index 000000000..2482c3843 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-07/output.txt @@ -0,0 +1,10 @@ +$ cargo run + Compiling chapter10 v0.1.0 (file:///projects/chapter10) +error[E0308]: mismatched types + --> src/main.rs:7:38 + | +7 | let wont_work = Point { x: 5, y: 4.0 }; + | ^^^ expected integer, found floating-point number + +For more information about this error, try `rustc --explain E0308`. +error: could not compile `chapter10` due to previous error diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-07/src/main.rs b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-07/src/main.rs new file mode 100644 index 000000000..7883db1a6 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-07/src/main.rs @@ -0,0 +1,8 @@ +struct Point { + x: T, + y: T, +} + +fn main() { + let wont_work = Point { x: 5, y: 4.0 }; +} diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-08/Cargo.lock b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-08/Cargo.lock new file mode 100644 index 000000000..e8007a19b --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-08/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "chapter10" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-08/Cargo.toml b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-08/Cargo.toml new file mode 100644 index 000000000..489f80967 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-08/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "chapter10" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-08/src/main.rs b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-08/src/main.rs new file mode 100644 index 000000000..615b78cfd --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-08/src/main.rs @@ -0,0 +1,10 @@ +struct Point { + x: T, + y: U, +} + +fn main() { + let both_integer = Point { x: 5, y: 10 }; + let both_float = Point { x: 1.0, y: 4.0 }; + let integer_and_float = Point { x: 5, y: 4.0 }; +} diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-09/Cargo.lock b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-09/Cargo.lock new file mode 100644 index 000000000..e8007a19b --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-09/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "chapter10" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-09/Cargo.toml b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-09/Cargo.toml new file mode 100644 index 000000000..489f80967 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-09/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "chapter10" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-09/src/main.rs b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-09/src/main.rs new file mode 100644 index 000000000..288b64eac --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-09/src/main.rs @@ -0,0 +1,16 @@ +struct Point { + x: T, + y: T, +} + +impl Point { + fn x(&self) -> &T { + &self.x + } +} + +fn main() { + let p = Point { x: 5, y: 10 }; + + println!("p.x = {}", p.x()); +} diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-10/Cargo.lock b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-10/Cargo.lock new file mode 100644 index 000000000..e8007a19b --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-10/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "chapter10" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-10/Cargo.toml b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-10/Cargo.toml new file mode 100644 index 000000000..489f80967 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-10/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "chapter10" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-10/src/main.rs b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-10/src/main.rs new file mode 100644 index 000000000..4c5b01bdc --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-10/src/main.rs @@ -0,0 +1,24 @@ +struct Point { + x: T, + y: T, +} + +impl Point { + fn x(&self) -> &T { + &self.x + } +} + +// ANCHOR: here +impl Point { + fn distance_from_origin(&self) -> f32 { + (self.x.powi(2) + self.y.powi(2)).sqrt() + } +} +// ANCHOR_END: here + +fn main() { + let p = Point { x: 5, y: 10 }; + + println!("p.x = {}", p.x()); +} diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-11/Cargo.lock b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-11/Cargo.lock new file mode 100644 index 000000000..e8007a19b --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-11/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "chapter10" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-11/Cargo.toml b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-11/Cargo.toml new file mode 100644 index 000000000..489f80967 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-11/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "chapter10" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-11/src/main.rs b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-11/src/main.rs new file mode 100644 index 000000000..86b028108 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-11/src/main.rs @@ -0,0 +1,22 @@ +struct Point { + x: X1, + y: Y1, +} + +impl Point { + fn mixup(self, other: Point) -> Point { + Point { + x: self.x, + y: other.y, + } + } +} + +fn main() { + let p1 = Point { x: 5, y: 10.4 }; + let p2 = Point { x: "Hello", y: 'c' }; + + let p3 = p1.mixup(p2); + + println!("p3.x = {}, p3.y = {}", p3.x, p3.y); +} diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-12/Cargo.lock b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-12/Cargo.lock new file mode 100644 index 000000000..2835471f0 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-12/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aggregator" +version = "0.1.0" diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-12/Cargo.toml b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-12/Cargo.toml new file mode 100644 index 000000000..46f46a7f4 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-12/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "aggregator" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-12/src/lib.rs b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-12/src/lib.rs new file mode 100644 index 000000000..cfaedb02f --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-12/src/lib.rs @@ -0,0 +1,3 @@ +pub trait Summary { + fn summarize(&self) -> String; +} diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-13/Cargo.lock b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-13/Cargo.lock new file mode 100644 index 000000000..2835471f0 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-13/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aggregator" +version = "0.1.0" diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-13/Cargo.toml b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-13/Cargo.toml new file mode 100644 index 000000000..46f46a7f4 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-13/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "aggregator" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-13/src/lib.rs b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-13/src/lib.rs new file mode 100644 index 000000000..c4c83329e --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-13/src/lib.rs @@ -0,0 +1,31 @@ +pub trait Summary { + fn summarize(&self) -> String; +} + +// ANCHOR: here +pub struct NewsArticle { + pub headline: String, + pub location: String, + pub author: String, + pub content: String, +} + +impl Summary for NewsArticle { + fn summarize(&self) -> String { + format!("{}, by {} ({})", self.headline, self.author, self.location) + } +} + +pub struct Tweet { + pub username: String, + pub content: String, + pub reply: bool, + pub retweet: bool, +} + +impl Summary for Tweet { + fn summarize(&self) -> String { + format!("{}: {}", self.username, self.content) + } +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-14/Cargo.lock b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-14/Cargo.lock new file mode 100644 index 000000000..2835471f0 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-14/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aggregator" +version = "0.1.0" diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-14/Cargo.toml b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-14/Cargo.toml new file mode 100644 index 000000000..46f46a7f4 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-14/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "aggregator" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-14/src/lib.rs b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-14/src/lib.rs new file mode 100644 index 000000000..fb59b84fb --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-14/src/lib.rs @@ -0,0 +1,29 @@ +// ANCHOR: here +pub trait Summary { + fn summarize(&self) -> String { + String::from("(Read more...)") + } +} +// ANCHOR_END: here + +pub struct NewsArticle { + pub headline: String, + pub location: String, + pub author: String, + pub content: String, +} + +impl Summary for NewsArticle {} + +pub struct Tweet { + pub username: String, + pub content: String, + pub reply: bool, + pub retweet: bool, +} + +impl Summary for Tweet { + fn summarize(&self) -> String { + format!("{}: {}", self.username, self.content) + } +} diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-15/Cargo.lock b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-15/Cargo.lock new file mode 100644 index 000000000..e8007a19b --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-15/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "chapter10" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-15/Cargo.toml b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-15/Cargo.toml new file mode 100644 index 000000000..489f80967 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-15/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "chapter10" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-15/src/lib.rs b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-15/src/lib.rs new file mode 100644 index 000000000..669cc5fdc --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-15/src/lib.rs @@ -0,0 +1,22 @@ +use std::fmt::Display; + +struct Pair { + x: T, + y: T, +} + +impl Pair { + fn new(x: T, y: T) -> Self { + Self { x, y } + } +} + +impl Pair { + fn cmp_display(&self) { + if self.x >= self.y { + println!("The largest member is x = {}", self.x); + } else { + println!("The largest member is y = {}", self.y); + } + } +} diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-16/Cargo.lock b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-16/Cargo.lock new file mode 100644 index 000000000..e8007a19b --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-16/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "chapter10" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-16/Cargo.toml b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-16/Cargo.toml new file mode 100644 index 000000000..489f80967 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-16/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "chapter10" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-16/output.txt b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-16/output.txt new file mode 100644 index 000000000..b63bf83a1 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-16/output.txt @@ -0,0 +1,15 @@ +$ cargo run + Compiling chapter10 v0.1.0 (file:///projects/chapter10) +error[E0597]: `x` does not live long enough + --> src/main.rs:6:13 + | +6 | r = &x; + | ^^ borrowed value does not live long enough +7 | } + | - `x` dropped here while still borrowed +8 | +9 | println!("r: {}", r); + | - borrow later used here + +For more information about this error, try `rustc --explain E0597`. +error: could not compile `chapter10` due to previous error diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-16/src/main.rs b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-16/src/main.rs new file mode 100644 index 000000000..d71134ea0 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-16/src/main.rs @@ -0,0 +1,10 @@ +fn main() { + let r; + + { + let x = 5; + r = &x; + } + + println!("r: {}", r); +} diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-17/Cargo.lock b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-17/Cargo.lock new file mode 100644 index 000000000..6388bb2b5 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-17/Cargo.lock @@ -0,0 +1,4 @@ +[[package]] +name = "chapter10" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-17/Cargo.toml b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-17/Cargo.toml new file mode 100644 index 000000000..489f80967 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-17/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "chapter10" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-17/rustfmt-ignore b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-17/rustfmt-ignore new file mode 100644 index 000000000..9a53c718a --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-17/rustfmt-ignore @@ -0,0 +1,3 @@ +We have some weird comments pointing out borrowing scopes that we don't want to change; +unfortunately I haven't found a way to skip them with rustfmt that works so for now we're going to +manually skip those listings. See: https://github.com/rust-lang/rustfmt/issues/4028 diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-17/src/main.rs b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-17/src/main.rs new file mode 100644 index 000000000..e8ca92328 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-17/src/main.rs @@ -0,0 +1,10 @@ +fn main() { + let r; // ---------+-- 'a + // | + { // | + let x = 5; // -+-- 'b | + r = &x; // | | + } // -+ | + // | + println!("r: {}", r); // | +} // ---------+ diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-18/Cargo.lock b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-18/Cargo.lock new file mode 100644 index 000000000..6388bb2b5 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-18/Cargo.lock @@ -0,0 +1,4 @@ +[[package]] +name = "chapter10" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-18/Cargo.toml b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-18/Cargo.toml new file mode 100644 index 000000000..489f80967 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-18/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "chapter10" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-18/rustfmt-ignore b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-18/rustfmt-ignore new file mode 100644 index 000000000..9a53c718a --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-18/rustfmt-ignore @@ -0,0 +1,3 @@ +We have some weird comments pointing out borrowing scopes that we don't want to change; +unfortunately I haven't found a way to skip them with rustfmt that works so for now we're going to +manually skip those listings. See: https://github.com/rust-lang/rustfmt/issues/4028 diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-18/src/main.rs b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-18/src/main.rs new file mode 100644 index 000000000..09ae3919c --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-18/src/main.rs @@ -0,0 +1,8 @@ +fn main() { + let x = 5; // ----------+-- 'b + // | + let r = &x; // --+-- 'a | + // | | + println!("r: {}", r); // | | + // --+ | +} // ----------+ diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-19/Cargo.lock b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-19/Cargo.lock new file mode 100644 index 000000000..e8007a19b --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-19/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "chapter10" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-19/Cargo.toml b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-19/Cargo.toml new file mode 100644 index 000000000..489f80967 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-19/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "chapter10" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-19/src/main.rs b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-19/src/main.rs new file mode 100644 index 000000000..0f076a71d --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-19/src/main.rs @@ -0,0 +1,7 @@ +fn main() { + let string1 = String::from("abcd"); + let string2 = "xyz"; + + let result = longest(string1.as_str(), string2); + println!("The longest string is {}", result); +} diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-20/Cargo.lock b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-20/Cargo.lock new file mode 100644 index 000000000..e8007a19b --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-20/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "chapter10" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-20/Cargo.toml b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-20/Cargo.toml new file mode 100644 index 000000000..489f80967 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-20/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "chapter10" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-20/output.txt b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-20/output.txt new file mode 100644 index 000000000..534a984a6 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-20/output.txt @@ -0,0 +1,16 @@ +$ cargo run + Compiling chapter10 v0.1.0 (file:///projects/chapter10) +error[E0106]: missing lifetime specifier + --> src/main.rs:9:33 + | +9 | fn longest(x: &str, y: &str) -> &str { + | ---- ---- ^ expected named lifetime parameter + | + = help: this function's return type contains a borrowed value, but the signature does not say whether it is borrowed from `x` or `y` +help: consider introducing a named lifetime parameter + | +9 | fn longest<'a>(x: &'a str, y: &'a str) -> &'a str { + | ++++ ++ ++ ++ + +For more information about this error, try `rustc --explain E0106`. +error: could not compile `chapter10` due to previous error diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-20/src/main.rs b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-20/src/main.rs new file mode 100644 index 000000000..6af8c9f0d --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-20/src/main.rs @@ -0,0 +1,17 @@ +fn main() { + let string1 = String::from("abcd"); + let string2 = "xyz"; + + let result = longest(string1.as_str(), string2); + println!("The longest string is {}", result); +} + +// ANCHOR: here +fn longest(x: &str, y: &str) -> &str { + if x.len() > y.len() { + x + } else { + y + } +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-21/Cargo.lock b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-21/Cargo.lock new file mode 100644 index 000000000..e8007a19b --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-21/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "chapter10" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-21/Cargo.toml b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-21/Cargo.toml new file mode 100644 index 000000000..489f80967 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-21/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "chapter10" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-21/src/main.rs b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-21/src/main.rs new file mode 100644 index 000000000..09c3a0daa --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-21/src/main.rs @@ -0,0 +1,17 @@ +fn main() { + let string1 = String::from("abcd"); + let string2 = "xyz"; + + let result = longest(string1.as_str(), string2); + println!("The longest string is {}", result); +} + +// ANCHOR: here +fn longest<'a>(x: &'a str, y: &'a str) -> &'a str { + if x.len() > y.len() { + x + } else { + y + } +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-22/Cargo.lock b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-22/Cargo.lock new file mode 100644 index 000000000..e8007a19b --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-22/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "chapter10" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-22/Cargo.toml b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-22/Cargo.toml new file mode 100644 index 000000000..489f80967 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-22/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "chapter10" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-22/src/main.rs b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-22/src/main.rs new file mode 100644 index 000000000..836ec7295 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-22/src/main.rs @@ -0,0 +1,19 @@ +// ANCHOR: here +fn main() { + let string1 = String::from("long string is long"); + + { + let string2 = String::from("xyz"); + let result = longest(string1.as_str(), string2.as_str()); + println!("The longest string is {}", result); + } +} +// ANCHOR_END: here + +fn longest<'a>(x: &'a str, y: &'a str) -> &'a str { + if x.len() > y.len() { + x + } else { + y + } +} diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-23/Cargo.lock b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-23/Cargo.lock new file mode 100644 index 000000000..e8007a19b --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-23/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "chapter10" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-23/Cargo.toml b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-23/Cargo.toml new file mode 100644 index 000000000..489f80967 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-23/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "chapter10" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-23/output.txt b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-23/output.txt new file mode 100644 index 000000000..7f31ce02c --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-23/output.txt @@ -0,0 +1,14 @@ +$ cargo run + Compiling chapter10 v0.1.0 (file:///projects/chapter10) +error[E0597]: `string2` does not live long enough + --> src/main.rs:6:44 + | +6 | result = longest(string1.as_str(), string2.as_str()); + | ^^^^^^^^^^^^^^^^ borrowed value does not live long enough +7 | } + | - `string2` dropped here while still borrowed +8 | println!("The longest string is {}", result); + | ------ borrow later used here + +For more information about this error, try `rustc --explain E0597`. +error: could not compile `chapter10` due to previous error diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-23/src/main.rs b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-23/src/main.rs new file mode 100644 index 000000000..2a6fa5898 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-23/src/main.rs @@ -0,0 +1,19 @@ +// ANCHOR: here +fn main() { + let string1 = String::from("long string is long"); + let result; + { + let string2 = String::from("xyz"); + result = longest(string1.as_str(), string2.as_str()); + } + println!("The longest string is {}", result); +} +// ANCHOR_END: here + +fn longest<'a>(x: &'a str, y: &'a str) -> &'a str { + if x.len() > y.len() { + x + } else { + y + } +} diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-24/Cargo.lock b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-24/Cargo.lock new file mode 100644 index 000000000..e8007a19b --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-24/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "chapter10" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-24/Cargo.toml b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-24/Cargo.toml new file mode 100644 index 000000000..489f80967 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-24/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "chapter10" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-24/src/main.rs b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-24/src/main.rs new file mode 100644 index 000000000..2937b194c --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-24/src/main.rs @@ -0,0 +1,11 @@ +struct ImportantExcerpt<'a> { + part: &'a str, +} + +fn main() { + let novel = String::from("Call me Ishmael. Some years ago..."); + let first_sentence = novel.split('.').next().expect("Could not find a '.'"); + let i = ImportantExcerpt { + part: first_sentence, + }; +} diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-25/Cargo.lock b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-25/Cargo.lock new file mode 100644 index 000000000..2aa4918e5 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-25/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "ownership" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-25/Cargo.toml b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-25/Cargo.toml new file mode 100644 index 000000000..e8847526d --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-25/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "ownership" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-25/src/main.rs b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-25/src/main.rs new file mode 100644 index 000000000..431a261d2 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-25/src/main.rs @@ -0,0 +1,29 @@ +// ANCHOR: here +fn first_word(s: &str) -> &str { + let bytes = s.as_bytes(); + + for (i, &item) in bytes.iter().enumerate() { + if item == b' ' { + return &s[0..i]; + } + } + + &s[..] +} +// ANCHOR_END: here + +fn main() { + let my_string = String::from("hello world"); + + // first_word works on slices of `String`s + let word = first_word(&my_string[..]); + + let my_string_literal = "hello world"; + + // first_word works on slices of string literals + let word = first_word(&my_string_literal[..]); + + // Because string literals *are* string slices already, + // this works too, without the slice syntax! + let word = first_word(my_string_literal); +} diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-01-calling-trait-method/Cargo.lock b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-01-calling-trait-method/Cargo.lock new file mode 100644 index 000000000..2835471f0 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-01-calling-trait-method/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aggregator" +version = "0.1.0" diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-01-calling-trait-method/Cargo.toml b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-01-calling-trait-method/Cargo.toml new file mode 100644 index 000000000..46f46a7f4 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-01-calling-trait-method/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "aggregator" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-01-calling-trait-method/src/lib.rs b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-01-calling-trait-method/src/lib.rs new file mode 100644 index 000000000..fa644ca4f --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-01-calling-trait-method/src/lib.rs @@ -0,0 +1,29 @@ +pub trait Summary { + fn summarize(&self) -> String; +} + +pub struct NewsArticle { + pub headline: String, + pub location: String, + pub author: String, + pub content: String, +} + +impl Summary for NewsArticle { + fn summarize(&self) -> String { + format!("{}, by {} ({})", self.headline, self.author, self.location) + } +} + +pub struct Tweet { + pub username: String, + pub content: String, + pub reply: bool, + pub retweet: bool, +} + +impl Summary for Tweet { + fn summarize(&self) -> String { + format!("{}: {}", self.username, self.content) + } +} diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-01-calling-trait-method/src/main.rs b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-01-calling-trait-method/src/main.rs new file mode 100644 index 000000000..0b51121d9 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-01-calling-trait-method/src/main.rs @@ -0,0 +1,14 @@ +use aggregator::{Summary, Tweet}; + +fn main() { + let tweet = Tweet { + username: String::from("horse_ebooks"), + content: String::from( + "of course, as you probably already know, people", + ), + reply: false, + retweet: false, + }; + + println!("1 new tweet: {}", tweet.summarize()); +} diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-02-calling-default-impl/Cargo.lock b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-02-calling-default-impl/Cargo.lock new file mode 100644 index 000000000..2835471f0 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-02-calling-default-impl/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aggregator" +version = "0.1.0" diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-02-calling-default-impl/Cargo.toml b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-02-calling-default-impl/Cargo.toml new file mode 100644 index 000000000..46f46a7f4 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-02-calling-default-impl/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "aggregator" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-02-calling-default-impl/src/lib.rs b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-02-calling-default-impl/src/lib.rs new file mode 100644 index 000000000..b6f93a68f --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-02-calling-default-impl/src/lib.rs @@ -0,0 +1,27 @@ +pub trait Summary { + fn summarize(&self) -> String { + String::from("(Read more...)") + } +} + +pub struct NewsArticle { + pub headline: String, + pub location: String, + pub author: String, + pub content: String, +} + +impl Summary for NewsArticle {} + +pub struct Tweet { + pub username: String, + pub content: String, + pub reply: bool, + pub retweet: bool, +} + +impl Summary for Tweet { + fn summarize(&self) -> String { + format!("{}: {}", self.username, self.content) + } +} diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-02-calling-default-impl/src/main.rs b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-02-calling-default-impl/src/main.rs new file mode 100644 index 000000000..cc9b98e31 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-02-calling-default-impl/src/main.rs @@ -0,0 +1,17 @@ +use aggregator::{self, NewsArticle, Summary}; + +fn main() { + // ANCHOR: here + let article = NewsArticle { + headline: String::from("Penguins win the Stanley Cup Championship!"), + location: String::from("Pittsburgh, PA, USA"), + author: String::from("Iceburgh"), + content: String::from( + "The Pittsburgh Penguins once again are the best \ + hockey team in the NHL.", + ), + }; + + println!("New article available! {}", article.summarize()); + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-03-default-impl-calls-other-methods/Cargo.lock b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-03-default-impl-calls-other-methods/Cargo.lock new file mode 100644 index 000000000..2835471f0 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-03-default-impl-calls-other-methods/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aggregator" +version = "0.1.0" diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-03-default-impl-calls-other-methods/Cargo.toml b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-03-default-impl-calls-other-methods/Cargo.toml new file mode 100644 index 000000000..46f46a7f4 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-03-default-impl-calls-other-methods/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "aggregator" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-03-default-impl-calls-other-methods/src/lib.rs b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-03-default-impl-calls-other-methods/src/lib.rs new file mode 100644 index 000000000..643906f69 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-03-default-impl-calls-other-methods/src/lib.rs @@ -0,0 +1,24 @@ +// ANCHOR: here +pub trait Summary { + fn summarize_author(&self) -> String; + + fn summarize(&self) -> String { + format!("(Read more from {}...)", self.summarize_author()) + } +} +// ANCHOR_END: here + +pub struct Tweet { + pub username: String, + pub content: String, + pub reply: bool, + pub retweet: bool, +} + +// ANCHOR: impl +impl Summary for Tweet { + fn summarize_author(&self) -> String { + format!("@{}", self.username) + } +} +// ANCHOR_END: impl diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-03-default-impl-calls-other-methods/src/main.rs b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-03-default-impl-calls-other-methods/src/main.rs new file mode 100644 index 000000000..e05e8e1c6 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-03-default-impl-calls-other-methods/src/main.rs @@ -0,0 +1,16 @@ +use aggregator::{self, Summary, Tweet}; + +fn main() { + // ANCHOR: here + let tweet = Tweet { + username: String::from("horse_ebooks"), + content: String::from( + "of course, as you probably already know, people", + ), + reply: false, + retweet: false, + }; + + println!("1 new tweet: {}", tweet.summarize()); + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-04-traits-as-parameters/Cargo.lock b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-04-traits-as-parameters/Cargo.lock new file mode 100644 index 000000000..2835471f0 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-04-traits-as-parameters/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aggregator" +version = "0.1.0" diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-04-traits-as-parameters/Cargo.toml b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-04-traits-as-parameters/Cargo.toml new file mode 100644 index 000000000..46f46a7f4 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-04-traits-as-parameters/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "aggregator" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-04-traits-as-parameters/src/lib.rs b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-04-traits-as-parameters/src/lib.rs new file mode 100644 index 000000000..261994351 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-04-traits-as-parameters/src/lib.rs @@ -0,0 +1,35 @@ +pub trait Summary { + fn summarize(&self) -> String; +} + +pub struct NewsArticle { + pub headline: String, + pub location: String, + pub author: String, + pub content: String, +} + +impl Summary for NewsArticle { + fn summarize(&self) -> String { + format!("{}, by {} ({})", self.headline, self.author, self.location) + } +} + +pub struct Tweet { + pub username: String, + pub content: String, + pub reply: bool, + pub retweet: bool, +} + +impl Summary for Tweet { + fn summarize(&self) -> String { + format!("{}: {}", self.username, self.content) + } +} + +// ANCHOR: here +pub fn notify(item: &impl Summary) { + println!("Breaking news! {}", item.summarize()); +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-05-returning-impl-trait/Cargo.lock b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-05-returning-impl-trait/Cargo.lock new file mode 100644 index 000000000..2835471f0 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-05-returning-impl-trait/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aggregator" +version = "0.1.0" diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-05-returning-impl-trait/Cargo.toml b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-05-returning-impl-trait/Cargo.toml new file mode 100644 index 000000000..46f46a7f4 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-05-returning-impl-trait/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "aggregator" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-05-returning-impl-trait/src/lib.rs b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-05-returning-impl-trait/src/lib.rs new file mode 100644 index 000000000..a611fce38 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-05-returning-impl-trait/src/lib.rs @@ -0,0 +1,42 @@ +pub trait Summary { + fn summarize(&self) -> String; +} + +pub struct NewsArticle { + pub headline: String, + pub location: String, + pub author: String, + pub content: String, +} + +impl Summary for NewsArticle { + fn summarize(&self) -> String { + format!("{}, by {} ({})", self.headline, self.author, self.location) + } +} + +pub struct Tweet { + pub username: String, + pub content: String, + pub reply: bool, + pub retweet: bool, +} + +impl Summary for Tweet { + fn summarize(&self) -> String { + format!("{}: {}", self.username, self.content) + } +} + +// ANCHOR: here +fn returns_summarizable() -> impl Summary { + Tweet { + username: String::from("horse_ebooks"), + content: String::from( + "of course, as you probably already know, people", + ), + reply: false, + retweet: false, + } +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-06-impl-trait-returns-one-type/Cargo.lock b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-06-impl-trait-returns-one-type/Cargo.lock new file mode 100644 index 000000000..2835471f0 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-06-impl-trait-returns-one-type/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aggregator" +version = "0.1.0" diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-06-impl-trait-returns-one-type/Cargo.toml b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-06-impl-trait-returns-one-type/Cargo.toml new file mode 100644 index 000000000..46f46a7f4 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-06-impl-trait-returns-one-type/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "aggregator" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-06-impl-trait-returns-one-type/src/lib.rs b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-06-impl-trait-returns-one-type/src/lib.rs new file mode 100644 index 000000000..7cd81d4c3 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-06-impl-trait-returns-one-type/src/lib.rs @@ -0,0 +1,56 @@ +pub trait Summary { + fn summarize(&self) -> String; +} + +pub struct NewsArticle { + pub headline: String, + pub location: String, + pub author: String, + pub content: String, +} + +impl Summary for NewsArticle { + fn summarize(&self) -> String { + format!("{}, by {} ({})", self.headline, self.author, self.location) + } +} + +pub struct Tweet { + pub username: String, + pub content: String, + pub reply: bool, + pub retweet: bool, +} + +impl Summary for Tweet { + fn summarize(&self) -> String { + format!("{}: {}", self.username, self.content) + } +} + +// ANCHOR: here +fn returns_summarizable(switch: bool) -> impl Summary { + if switch { + NewsArticle { + headline: String::from( + "Penguins win the Stanley Cup Championship!", + ), + location: String::from("Pittsburgh, PA, USA"), + author: String::from("Iceburgh"), + content: String::from( + "The Pittsburgh Penguins once again are the best \ + hockey team in the NHL.", + ), + } + } else { + Tweet { + username: String::from("horse_ebooks"), + content: String::from( + "of course, as you probably already know, people", + ), + reply: false, + retweet: false, + } + } +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-07-where-clause/Cargo.toml b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-07-where-clause/Cargo.toml new file mode 100644 index 000000000..4dbde9090 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-07-where-clause/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "chapter10" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-07-where-clause/src/lib.rs b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-07-where-clause/src/lib.rs new file mode 100644 index 000000000..05b07c31a --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-07-where-clause/src/lib.rs @@ -0,0 +1,9 @@ +// ANCHOR: here +fn some_function(t: &T, u: &U) -> i32 +where + T: Display + Clone, + U: Clone + Debug, +{ + // ANCHOR_END: here + unimplemented!() +} diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-08-only-one-reference-with-lifetime/Cargo.lock b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-08-only-one-reference-with-lifetime/Cargo.lock new file mode 100644 index 000000000..e8007a19b --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-08-only-one-reference-with-lifetime/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "chapter10" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-08-only-one-reference-with-lifetime/Cargo.toml b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-08-only-one-reference-with-lifetime/Cargo.toml new file mode 100644 index 000000000..489f80967 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-08-only-one-reference-with-lifetime/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "chapter10" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-08-only-one-reference-with-lifetime/src/main.rs b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-08-only-one-reference-with-lifetime/src/main.rs new file mode 100644 index 000000000..d144305cb --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-08-only-one-reference-with-lifetime/src/main.rs @@ -0,0 +1,13 @@ +fn main() { + let string1 = String::from("abcd"); + let string2 = "efghijklmnopqrstuvwxyz"; + + let result = longest(string1.as_str(), string2); + println!("The longest string is {}", result); +} + +// ANCHOR: here +fn longest<'a>(x: &'a str, y: &str) -> &'a str { + x +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-09-unrelated-lifetime/Cargo.lock b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-09-unrelated-lifetime/Cargo.lock new file mode 100644 index 000000000..e8007a19b --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-09-unrelated-lifetime/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "chapter10" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-09-unrelated-lifetime/Cargo.toml b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-09-unrelated-lifetime/Cargo.toml new file mode 100644 index 000000000..489f80967 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-09-unrelated-lifetime/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "chapter10" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-09-unrelated-lifetime/output.txt b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-09-unrelated-lifetime/output.txt new file mode 100644 index 000000000..0c628b697 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-09-unrelated-lifetime/output.txt @@ -0,0 +1,10 @@ +$ cargo run + Compiling chapter10 v0.1.0 (file:///projects/chapter10) +error[E0515]: cannot return reference to local variable `result` + --> src/main.rs:11:5 + | +11 | result.as_str() + | ^^^^^^^^^^^^^^^ returns a reference to data owned by the current function + +For more information about this error, try `rustc --explain E0515`. +error: could not compile `chapter10` due to previous error diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-09-unrelated-lifetime/src/main.rs b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-09-unrelated-lifetime/src/main.rs new file mode 100644 index 000000000..aca4be0a7 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-09-unrelated-lifetime/src/main.rs @@ -0,0 +1,14 @@ +fn main() { + let string1 = String::from("abcd"); + let string2 = "xyz"; + + let result = longest(string1.as_str(), string2); + println!("The longest string is {}", result); +} + +// ANCHOR: here +fn longest<'a>(x: &str, y: &str) -> &'a str { + let result = String::from("really long string"); + result.as_str() +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-10-lifetimes-on-methods/Cargo.lock b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-10-lifetimes-on-methods/Cargo.lock new file mode 100644 index 000000000..e8007a19b --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-10-lifetimes-on-methods/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "chapter10" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-10-lifetimes-on-methods/Cargo.toml b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-10-lifetimes-on-methods/Cargo.toml new file mode 100644 index 000000000..489f80967 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-10-lifetimes-on-methods/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "chapter10" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-10-lifetimes-on-methods/src/main.rs b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-10-lifetimes-on-methods/src/main.rs new file mode 100644 index 000000000..32ad530b5 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-10-lifetimes-on-methods/src/main.rs @@ -0,0 +1,28 @@ +struct ImportantExcerpt<'a> { + part: &'a str, +} + +// ANCHOR: 1st +impl<'a> ImportantExcerpt<'a> { + fn level(&self) -> i32 { + 3 + } +} +// ANCHOR_END: 1st + +// ANCHOR: 3rd +impl<'a> ImportantExcerpt<'a> { + fn announce_and_return_part(&self, announcement: &str) -> &str { + println!("Attention please: {}", announcement); + self.part + } +} +// ANCHOR_END: 3rd + +fn main() { + let novel = String::from("Call me Ishmael. Some years ago..."); + let first_sentence = novel.split('.').next().expect("Could not find a '.'"); + let i = ImportantExcerpt { + part: first_sentence, + }; +} diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-11-generics-traits-and-lifetimes/Cargo.lock b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-11-generics-traits-and-lifetimes/Cargo.lock new file mode 100644 index 000000000..e8007a19b --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-11-generics-traits-and-lifetimes/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "chapter10" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-11-generics-traits-and-lifetimes/Cargo.toml b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-11-generics-traits-and-lifetimes/Cargo.toml new file mode 100644 index 000000000..489f80967 --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-11-generics-traits-and-lifetimes/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "chapter10" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-11-generics-traits-and-lifetimes/src/main.rs b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-11-generics-traits-and-lifetimes/src/main.rs new file mode 100644 index 000000000..cfafa9a6d --- /dev/null +++ b/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/no-listing-11-generics-traits-and-lifetimes/src/main.rs @@ -0,0 +1,31 @@ +fn main() { + let string1 = String::from("abcd"); + let string2 = "xyz"; + + let result = longest_with_an_announcement( + string1.as_str(), + string2, + "Today is someone's birthday!", + ); + println!("The longest string is {}", result); +} + +// ANCHOR: here +use std::fmt::Display; + +fn longest_with_an_announcement<'a, T>( + x: &'a str, + y: &'a str, + ann: T, +) -> &'a str +where + T: Display, +{ + println!("Announcement! {}", ann); + if x.len() > y.len() { + x + } else { + y + } +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch11-writing-automated-tests/listing-11-01/Cargo.lock b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-01/Cargo.lock new file mode 100644 index 000000000..d37189b33 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-01/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "adder" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch11-writing-automated-tests/listing-11-01/Cargo.toml b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-01/Cargo.toml new file mode 100644 index 000000000..e61cb12e3 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-01/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "adder" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch11-writing-automated-tests/listing-11-01/output.txt b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-01/output.txt new file mode 100644 index 000000000..c3e812ed8 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-01/output.txt @@ -0,0 +1,16 @@ +$ cargo test + Compiling adder v0.1.0 (file:///projects/adder) + Finished test [unoptimized + debuginfo] target(s) in 0.57s + Running unittests src/lib.rs (target/debug/deps/adder-92948b65e88960b4) + +running 1 test +test tests::it_works ... ok + +test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s + + Doc-tests adder + +running 0 tests + +test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s + diff --git a/src/doc/book/listings/ch11-writing-automated-tests/listing-11-01/src/lib.rs b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-01/src/lib.rs new file mode 100644 index 000000000..1b4a90c93 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-01/src/lib.rs @@ -0,0 +1,8 @@ +#[cfg(test)] +mod tests { + #[test] + fn it_works() { + let result = 2 + 2; + assert_eq!(result, 4); + } +} diff --git a/src/doc/book/listings/ch11-writing-automated-tests/listing-11-03/Cargo.lock b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-03/Cargo.lock new file mode 100644 index 000000000..d37189b33 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-03/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "adder" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch11-writing-automated-tests/listing-11-03/Cargo.toml b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-03/Cargo.toml new file mode 100644 index 000000000..e61cb12e3 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-03/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "adder" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch11-writing-automated-tests/listing-11-03/output.txt b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-03/output.txt new file mode 100644 index 000000000..0c2fd0327 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-03/output.txt @@ -0,0 +1,22 @@ +$ cargo test + Compiling adder v0.1.0 (file:///projects/adder) + Finished test [unoptimized + debuginfo] target(s) in 0.72s + Running unittests src/lib.rs (target/debug/deps/adder-92948b65e88960b4) + +running 2 tests +test tests::another ... FAILED +test tests::exploration ... ok + +failures: + +---- tests::another stdout ---- +thread 'main' panicked at 'Make this test fail', src/lib.rs:10:9 +note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace + + +failures: + tests::another + +test result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s + +error: test failed, to rerun pass '--lib' diff --git a/src/doc/book/listings/ch11-writing-automated-tests/listing-11-03/src/lib.rs b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-03/src/lib.rs new file mode 100644 index 000000000..a9ec00891 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-03/src/lib.rs @@ -0,0 +1,14 @@ +// ANCHOR: here +#[cfg(test)] +mod tests { + #[test] + fn exploration() { + assert_eq!(2 + 2, 4); + } + + #[test] + fn another() { + panic!("Make this test fail"); + } +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch11-writing-automated-tests/listing-11-05/Cargo.lock b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-05/Cargo.lock new file mode 100644 index 000000000..9dcd5439d --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-05/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "rectangle" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch11-writing-automated-tests/listing-11-05/Cargo.toml b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-05/Cargo.toml new file mode 100644 index 000000000..2447c67f5 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-05/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "rectangle" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch11-writing-automated-tests/listing-11-05/src/lib.rs b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-05/src/lib.rs new file mode 100644 index 000000000..0f1bc4e08 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-05/src/lib.rs @@ -0,0 +1,13 @@ +// ANCHOR: here +#[derive(Debug)] +struct Rectangle { + width: u32, + height: u32, +} + +impl Rectangle { + fn can_hold(&self, other: &Rectangle) -> bool { + self.width > other.width && self.height > other.height + } +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch11-writing-automated-tests/listing-11-06/Cargo.lock b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-06/Cargo.lock new file mode 100644 index 000000000..9dcd5439d --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-06/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "rectangle" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch11-writing-automated-tests/listing-11-06/Cargo.toml b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-06/Cargo.toml new file mode 100644 index 000000000..2447c67f5 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-06/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "rectangle" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch11-writing-automated-tests/listing-11-06/output.txt b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-06/output.txt new file mode 100644 index 000000000..dad02b460 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-06/output.txt @@ -0,0 +1,16 @@ +$ cargo test + Compiling rectangle v0.1.0 (file:///projects/rectangle) + Finished test [unoptimized + debuginfo] target(s) in 0.66s + Running unittests src/lib.rs (target/debug/deps/rectangle-6584c4561e48942e) + +running 1 test +test tests::larger_can_hold_smaller ... ok + +test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s + + Doc-tests rectangle + +running 0 tests + +test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s + diff --git a/src/doc/book/listings/ch11-writing-automated-tests/listing-11-06/src/lib.rs b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-06/src/lib.rs new file mode 100644 index 000000000..6ad1512ed --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-06/src/lib.rs @@ -0,0 +1,32 @@ +#[derive(Debug)] +struct Rectangle { + width: u32, + height: u32, +} + +impl Rectangle { + fn can_hold(&self, other: &Rectangle) -> bool { + self.width > other.width && self.height > other.height + } +} + +// ANCHOR: here +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn larger_can_hold_smaller() { + let larger = Rectangle { + width: 8, + height: 7, + }; + let smaller = Rectangle { + width: 5, + height: 1, + }; + + assert!(larger.can_hold(&smaller)); + } +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch11-writing-automated-tests/listing-11-07/Cargo.lock b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-07/Cargo.lock new file mode 100644 index 000000000..d37189b33 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-07/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "adder" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch11-writing-automated-tests/listing-11-07/Cargo.toml b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-07/Cargo.toml new file mode 100644 index 000000000..e61cb12e3 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-07/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "adder" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch11-writing-automated-tests/listing-11-07/output.txt b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-07/output.txt new file mode 100644 index 000000000..fa02835bd --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-07/output.txt @@ -0,0 +1,16 @@ +$ cargo test + Compiling adder v0.1.0 (file:///projects/adder) + Finished test [unoptimized + debuginfo] target(s) in 0.58s + Running unittests src/lib.rs (target/debug/deps/adder-92948b65e88960b4) + +running 1 test +test tests::it_adds_two ... ok + +test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s + + Doc-tests adder + +running 0 tests + +test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s + diff --git a/src/doc/book/listings/ch11-writing-automated-tests/listing-11-07/src/lib.rs b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-07/src/lib.rs new file mode 100644 index 000000000..3e5d66bfa --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-07/src/lib.rs @@ -0,0 +1,13 @@ +pub fn add_two(a: i32) -> i32 { + a + 2 +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn it_adds_two() { + assert_eq!(4, add_two(2)); + } +} diff --git a/src/doc/book/listings/ch11-writing-automated-tests/listing-11-08/Cargo.lock b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-08/Cargo.lock new file mode 100644 index 000000000..5802b7dc9 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-08/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "guessing_game" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch11-writing-automated-tests/listing-11-08/Cargo.toml b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-08/Cargo.toml new file mode 100644 index 000000000..4e348c8d2 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-08/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "guessing_game" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch11-writing-automated-tests/listing-11-08/output.txt b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-08/output.txt new file mode 100644 index 000000000..caca1542f --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-08/output.txt @@ -0,0 +1,16 @@ +$ cargo test + Compiling guessing_game v0.1.0 (file:///projects/guessing_game) + Finished test [unoptimized + debuginfo] target(s) in 0.58s + Running unittests src/lib.rs (target/debug/deps/guessing_game-57d70c3acb738f4d) + +running 1 test +test tests::greater_than_100 - should panic ... ok + +test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s + + Doc-tests guessing_game + +running 0 tests + +test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s + diff --git a/src/doc/book/listings/ch11-writing-automated-tests/listing-11-08/src/lib.rs b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-08/src/lib.rs new file mode 100644 index 000000000..9391be5b1 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-08/src/lib.rs @@ -0,0 +1,24 @@ +pub struct Guess { + value: i32, +} + +impl Guess { + pub fn new(value: i32) -> Guess { + if value < 1 || value > 100 { + panic!("Guess value must be between 1 and 100, got {}.", value); + } + + Guess { value } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + #[should_panic] + fn greater_than_100() { + Guess::new(200); + } +} diff --git a/src/doc/book/listings/ch11-writing-automated-tests/listing-11-09/Cargo.lock b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-09/Cargo.lock new file mode 100644 index 000000000..5802b7dc9 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-09/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "guessing_game" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch11-writing-automated-tests/listing-11-09/Cargo.toml b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-09/Cargo.toml new file mode 100644 index 000000000..4e348c8d2 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-09/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "guessing_game" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch11-writing-automated-tests/listing-11-09/src/lib.rs b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-09/src/lib.rs new file mode 100644 index 000000000..cc1c5c35d --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-09/src/lib.rs @@ -0,0 +1,36 @@ +pub struct Guess { + value: i32, +} + +// ANCHOR: here +// --snip-- + +impl Guess { + pub fn new(value: i32) -> Guess { + if value < 1 { + panic!( + "Guess value must be greater than or equal to 1, got {}.", + value + ); + } else if value > 100 { + panic!( + "Guess value must be less than or equal to 100, got {}.", + value + ); + } + + Guess { value } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + #[should_panic(expected = "less than or equal to 100")] + fn greater_than_100() { + Guess::new(200); + } +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch11-writing-automated-tests/listing-11-10/Cargo.lock b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-10/Cargo.lock new file mode 100644 index 000000000..a6d35e395 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-10/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "silly-function" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch11-writing-automated-tests/listing-11-10/Cargo.toml b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-10/Cargo.toml new file mode 100644 index 000000000..f751864de --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-10/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "silly-function" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch11-writing-automated-tests/listing-11-10/output.txt b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-10/output.txt new file mode 100644 index 000000000..0512cd59f --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-10/output.txt @@ -0,0 +1,25 @@ +$ cargo test + Compiling silly-function v0.1.0 (file:///projects/silly-function) + Finished test [unoptimized + debuginfo] target(s) in 0.58s + Running unittests src/lib.rs (target/debug/deps/silly_function-160869f38cff9166) + +running 2 tests +test tests::this_test_will_fail ... FAILED +test tests::this_test_will_pass ... ok + +failures: + +---- tests::this_test_will_fail stdout ---- +I got the value 8 +thread 'main' panicked at 'assertion failed: `(left == right)` + left: `5`, + right: `10`', src/lib.rs:19:9 +note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace + + +failures: + tests::this_test_will_fail + +test result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s + +error: test failed, to rerun pass '--lib' diff --git a/src/doc/book/listings/ch11-writing-automated-tests/listing-11-10/src/lib.rs b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-10/src/lib.rs new file mode 100644 index 000000000..6fd76ce00 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-10/src/lib.rs @@ -0,0 +1,21 @@ +fn prints_and_returns_10(a: i32) -> i32 { + println!("I got the value {}", a); + 10 +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn this_test_will_pass() { + let value = prints_and_returns_10(4); + assert_eq!(10, value); + } + + #[test] + fn this_test_will_fail() { + let value = prints_and_returns_10(8); + assert_eq!(5, value); + } +} diff --git a/src/doc/book/listings/ch11-writing-automated-tests/listing-11-11/Cargo.lock b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-11/Cargo.lock new file mode 100644 index 000000000..d37189b33 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-11/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "adder" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch11-writing-automated-tests/listing-11-11/Cargo.toml b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-11/Cargo.toml new file mode 100644 index 000000000..e61cb12e3 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-11/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "adder" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch11-writing-automated-tests/listing-11-11/output.txt b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-11/output.txt new file mode 100644 index 000000000..fe19c83c4 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-11/output.txt @@ -0,0 +1,18 @@ +$ cargo test + Compiling adder v0.1.0 (file:///projects/adder) + Finished test [unoptimized + debuginfo] target(s) in 0.62s + Running unittests src/lib.rs (target/debug/deps/adder-92948b65e88960b4) + +running 3 tests +test tests::add_three_and_two ... ok +test tests::add_two_and_two ... ok +test tests::one_hundred ... ok + +test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s + + Doc-tests adder + +running 0 tests + +test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s + diff --git a/src/doc/book/listings/ch11-writing-automated-tests/listing-11-11/src/lib.rs b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-11/src/lib.rs new file mode 100644 index 000000000..f56715263 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-11/src/lib.rs @@ -0,0 +1,23 @@ +pub fn add_two(a: i32) -> i32 { + a + 2 +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn add_two_and_two() { + assert_eq!(4, add_two(2)); + } + + #[test] + fn add_three_and_two() { + assert_eq!(5, add_two(3)); + } + + #[test] + fn one_hundred() { + assert_eq!(102, add_two(100)); + } +} diff --git a/src/doc/book/listings/ch11-writing-automated-tests/listing-11-12/Cargo.lock b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-12/Cargo.lock new file mode 100644 index 000000000..d37189b33 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-12/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "adder" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch11-writing-automated-tests/listing-11-12/Cargo.toml b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-12/Cargo.toml new file mode 100644 index 000000000..e61cb12e3 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-12/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "adder" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch11-writing-automated-tests/listing-11-12/src/lib.rs b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-12/src/lib.rs new file mode 100644 index 000000000..c3961b1f6 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-12/src/lib.rs @@ -0,0 +1,17 @@ +pub fn add_two(a: i32) -> i32 { + internal_adder(a, 2) +} + +fn internal_adder(a: i32, b: i32) -> i32 { + a + b +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn internal() { + assert_eq!(4, internal_adder(2, 2)); + } +} diff --git a/src/doc/book/listings/ch11-writing-automated-tests/listing-11-13/Cargo.lock b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-13/Cargo.lock new file mode 100644 index 000000000..d37189b33 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-13/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "adder" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch11-writing-automated-tests/listing-11-13/Cargo.toml b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-13/Cargo.toml new file mode 100644 index 000000000..e61cb12e3 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-13/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "adder" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch11-writing-automated-tests/listing-11-13/output.txt b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-13/output.txt new file mode 100644 index 000000000..22970e9e1 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-13/output.txt @@ -0,0 +1,23 @@ +$ cargo test + Compiling adder v0.1.0 (file:///projects/adder) + Finished test [unoptimized + debuginfo] target(s) in 1.31s + Running unittests src/lib.rs (target/debug/deps/adder-1082c4b063a8fbe6) + +running 1 test +test tests::internal ... ok + +test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s + + Running tests/integration_test.rs (target/debug/deps/integration_test-1082c4b063a8fbe6) + +running 1 test +test it_adds_two ... ok + +test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s + + Doc-tests adder + +running 0 tests + +test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s + diff --git a/src/doc/book/listings/ch11-writing-automated-tests/listing-11-13/src/lib.rs b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-13/src/lib.rs new file mode 100644 index 000000000..c3961b1f6 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-13/src/lib.rs @@ -0,0 +1,17 @@ +pub fn add_two(a: i32) -> i32 { + internal_adder(a, 2) +} + +fn internal_adder(a: i32, b: i32) -> i32 { + a + b +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn internal() { + assert_eq!(4, internal_adder(2, 2)); + } +} diff --git a/src/doc/book/listings/ch11-writing-automated-tests/listing-11-13/tests/integration_test.rs b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-13/tests/integration_test.rs new file mode 100644 index 000000000..e26fa7109 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/listing-11-13/tests/integration_test.rs @@ -0,0 +1,6 @@ +use adder; + +#[test] +fn it_adds_two() { + assert_eq!(4, adder::add_two(2)); +} diff --git a/src/doc/book/listings/ch11-writing-automated-tests/no-listing-01-changing-test-name/Cargo.lock b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-01-changing-test-name/Cargo.lock new file mode 100644 index 000000000..d37189b33 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-01-changing-test-name/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "adder" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch11-writing-automated-tests/no-listing-01-changing-test-name/Cargo.toml b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-01-changing-test-name/Cargo.toml new file mode 100644 index 000000000..e61cb12e3 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-01-changing-test-name/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "adder" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch11-writing-automated-tests/no-listing-01-changing-test-name/output.txt b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-01-changing-test-name/output.txt new file mode 100644 index 000000000..8a3330844 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-01-changing-test-name/output.txt @@ -0,0 +1,16 @@ +$ cargo test + Compiling adder v0.1.0 (file:///projects/adder) + Finished test [unoptimized + debuginfo] target(s) in 0.59s + Running unittests src/lib.rs (target/debug/deps/adder-92948b65e88960b4) + +running 1 test +test tests::exploration ... ok + +test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s + + Doc-tests adder + +running 0 tests + +test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s + diff --git a/src/doc/book/listings/ch11-writing-automated-tests/no-listing-01-changing-test-name/src/lib.rs b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-01-changing-test-name/src/lib.rs new file mode 100644 index 000000000..330bddf6a --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-01-changing-test-name/src/lib.rs @@ -0,0 +1,7 @@ +#[cfg(test)] +mod tests { + #[test] + fn exploration() { + assert_eq!(2 + 2, 4); + } +} diff --git a/src/doc/book/listings/ch11-writing-automated-tests/no-listing-02-adding-another-rectangle-test/Cargo.lock b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-02-adding-another-rectangle-test/Cargo.lock new file mode 100644 index 000000000..9dcd5439d --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-02-adding-another-rectangle-test/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "rectangle" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch11-writing-automated-tests/no-listing-02-adding-another-rectangle-test/Cargo.toml b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-02-adding-another-rectangle-test/Cargo.toml new file mode 100644 index 000000000..2447c67f5 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-02-adding-another-rectangle-test/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "rectangle" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch11-writing-automated-tests/no-listing-02-adding-another-rectangle-test/output.txt b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-02-adding-another-rectangle-test/output.txt new file mode 100644 index 000000000..30e45e525 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-02-adding-another-rectangle-test/output.txt @@ -0,0 +1,17 @@ +$ cargo test + Compiling rectangle v0.1.0 (file:///projects/rectangle) + Finished test [unoptimized + debuginfo] target(s) in 0.66s + Running unittests src/lib.rs (target/debug/deps/rectangle-6584c4561e48942e) + +running 2 tests +test tests::larger_can_hold_smaller ... ok +test tests::smaller_cannot_hold_larger ... ok + +test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s + + Doc-tests rectangle + +running 0 tests + +test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s + diff --git a/src/doc/book/listings/ch11-writing-automated-tests/no-listing-02-adding-another-rectangle-test/src/lib.rs b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-02-adding-another-rectangle-test/src/lib.rs new file mode 100644 index 000000000..ee4fc45b9 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-02-adding-another-rectangle-test/src/lib.rs @@ -0,0 +1,49 @@ +#[derive(Debug)] +struct Rectangle { + width: u32, + height: u32, +} + +impl Rectangle { + fn can_hold(&self, other: &Rectangle) -> bool { + self.width > other.width && self.height > other.height + } +} + +// ANCHOR: here +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn larger_can_hold_smaller() { + // --snip-- + // ANCHOR_END: here + let larger = Rectangle { + width: 8, + height: 7, + }; + let smaller = Rectangle { + width: 5, + height: 1, + }; + + assert!(larger.can_hold(&smaller)); + // ANCHOR: here + } + + #[test] + fn smaller_cannot_hold_larger() { + let larger = Rectangle { + width: 8, + height: 7, + }; + let smaller = Rectangle { + width: 5, + height: 1, + }; + + assert!(!smaller.can_hold(&larger)); + } +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch11-writing-automated-tests/no-listing-03-introducing-a-bug/Cargo.lock b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-03-introducing-a-bug/Cargo.lock new file mode 100644 index 000000000..9dcd5439d --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-03-introducing-a-bug/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "rectangle" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch11-writing-automated-tests/no-listing-03-introducing-a-bug/Cargo.toml b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-03-introducing-a-bug/Cargo.toml new file mode 100644 index 000000000..2447c67f5 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-03-introducing-a-bug/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "rectangle" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch11-writing-automated-tests/no-listing-03-introducing-a-bug/output.txt b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-03-introducing-a-bug/output.txt new file mode 100644 index 000000000..fdeb597e3 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-03-introducing-a-bug/output.txt @@ -0,0 +1,22 @@ +$ cargo test + Compiling rectangle v0.1.0 (file:///projects/rectangle) + Finished test [unoptimized + debuginfo] target(s) in 0.66s + Running unittests src/lib.rs (target/debug/deps/rectangle-6584c4561e48942e) + +running 2 tests +test tests::larger_can_hold_smaller ... FAILED +test tests::smaller_cannot_hold_larger ... ok + +failures: + +---- tests::larger_can_hold_smaller stdout ---- +thread 'main' panicked at 'assertion failed: larger.can_hold(&smaller)', src/lib.rs:28:9 +note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace + + +failures: + tests::larger_can_hold_smaller + +test result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s + +error: test failed, to rerun pass '--lib' diff --git a/src/doc/book/listings/ch11-writing-automated-tests/no-listing-03-introducing-a-bug/src/lib.rs b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-03-introducing-a-bug/src/lib.rs new file mode 100644 index 000000000..f5968fcaf --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-03-introducing-a-bug/src/lib.rs @@ -0,0 +1,47 @@ +#[derive(Debug)] +struct Rectangle { + width: u32, + height: u32, +} + +// ANCHOR: here +// --snip-- +impl Rectangle { + fn can_hold(&self, other: &Rectangle) -> bool { + self.width < other.width && self.height > other.height + } +} +// ANCHOR_END: here + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn larger_can_hold_smaller() { + let larger = Rectangle { + width: 8, + height: 7, + }; + let smaller = Rectangle { + width: 5, + height: 1, + }; + + assert!(larger.can_hold(&smaller)); + } + + #[test] + fn smaller_cannot_hold_larger() { + let larger = Rectangle { + width: 8, + height: 7, + }; + let smaller = Rectangle { + width: 5, + height: 1, + }; + + assert!(!smaller.can_hold(&larger)); + } +} diff --git a/src/doc/book/listings/ch11-writing-automated-tests/no-listing-04-bug-in-add-two/Cargo.lock b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-04-bug-in-add-two/Cargo.lock new file mode 100644 index 000000000..d37189b33 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-04-bug-in-add-two/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "adder" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch11-writing-automated-tests/no-listing-04-bug-in-add-two/Cargo.toml b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-04-bug-in-add-two/Cargo.toml new file mode 100644 index 000000000..e61cb12e3 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-04-bug-in-add-two/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "adder" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch11-writing-automated-tests/no-listing-04-bug-in-add-two/output.txt b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-04-bug-in-add-two/output.txt new file mode 100644 index 000000000..0af0401e3 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-04-bug-in-add-two/output.txt @@ -0,0 +1,23 @@ +$ cargo test + Compiling adder v0.1.0 (file:///projects/adder) + Finished test [unoptimized + debuginfo] target(s) in 0.61s + Running unittests src/lib.rs (target/debug/deps/adder-92948b65e88960b4) + +running 1 test +test tests::it_adds_two ... FAILED + +failures: + +---- tests::it_adds_two stdout ---- +thread 'main' panicked at 'assertion failed: `(left == right)` + left: `4`, + right: `5`', src/lib.rs:11:9 +note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace + + +failures: + tests::it_adds_two + +test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s + +error: test failed, to rerun pass '--lib' diff --git a/src/doc/book/listings/ch11-writing-automated-tests/no-listing-04-bug-in-add-two/src/lib.rs b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-04-bug-in-add-two/src/lib.rs new file mode 100644 index 000000000..f18662526 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-04-bug-in-add-two/src/lib.rs @@ -0,0 +1,15 @@ +// ANCHOR: here +pub fn add_two(a: i32) -> i32 { + a + 3 +} +// ANCHOR_END: here + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn it_adds_two() { + assert_eq!(4, add_two(2)); + } +} diff --git a/src/doc/book/listings/ch11-writing-automated-tests/no-listing-05-greeter/Cargo.lock b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-05-greeter/Cargo.lock new file mode 100644 index 000000000..9a7ecdd9f --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-05-greeter/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "greeter" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch11-writing-automated-tests/no-listing-05-greeter/Cargo.toml b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-05-greeter/Cargo.toml new file mode 100644 index 000000000..90a826cf4 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-05-greeter/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "greeter" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch11-writing-automated-tests/no-listing-05-greeter/src/lib.rs b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-05-greeter/src/lib.rs new file mode 100644 index 000000000..3ba3d8819 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-05-greeter/src/lib.rs @@ -0,0 +1,14 @@ +pub fn greeting(name: &str) -> String { + format!("Hello {}!", name) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn greeting_contains_name() { + let result = greeting("Carol"); + assert!(result.contains("Carol")); + } +} diff --git a/src/doc/book/listings/ch11-writing-automated-tests/no-listing-06-greeter-with-bug/Cargo.lock b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-06-greeter-with-bug/Cargo.lock new file mode 100644 index 000000000..9a7ecdd9f --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-06-greeter-with-bug/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "greeter" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch11-writing-automated-tests/no-listing-06-greeter-with-bug/Cargo.toml b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-06-greeter-with-bug/Cargo.toml new file mode 100644 index 000000000..90a826cf4 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-06-greeter-with-bug/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "greeter" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch11-writing-automated-tests/no-listing-06-greeter-with-bug/output.txt b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-06-greeter-with-bug/output.txt new file mode 100644 index 000000000..3049543a7 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-06-greeter-with-bug/output.txt @@ -0,0 +1,21 @@ +$ cargo test + Compiling greeter v0.1.0 (file:///projects/greeter) + Finished test [unoptimized + debuginfo] target(s) in 0.91s + Running unittests src/lib.rs (target/debug/deps/greeter-170b942eb5bf5e3a) + +running 1 test +test tests::greeting_contains_name ... FAILED + +failures: + +---- tests::greeting_contains_name stdout ---- +thread 'main' panicked at 'assertion failed: result.contains(\"Carol\")', src/lib.rs:12:9 +note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace + + +failures: + tests::greeting_contains_name + +test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s + +error: test failed, to rerun pass '--lib' diff --git a/src/doc/book/listings/ch11-writing-automated-tests/no-listing-06-greeter-with-bug/src/lib.rs b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-06-greeter-with-bug/src/lib.rs new file mode 100644 index 000000000..6f28fc52a --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-06-greeter-with-bug/src/lib.rs @@ -0,0 +1,16 @@ +// ANCHOR: here +pub fn greeting(name: &str) -> String { + String::from("Hello!") +} +// ANCHOR_END: here + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn greeting_contains_name() { + let result = greeting("Carol"); + assert!(result.contains("Carol")); + } +} diff --git a/src/doc/book/listings/ch11-writing-automated-tests/no-listing-07-custom-failure-message/Cargo.lock b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-07-custom-failure-message/Cargo.lock new file mode 100644 index 000000000..9a7ecdd9f --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-07-custom-failure-message/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "greeter" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch11-writing-automated-tests/no-listing-07-custom-failure-message/Cargo.toml b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-07-custom-failure-message/Cargo.toml new file mode 100644 index 000000000..90a826cf4 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-07-custom-failure-message/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "greeter" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch11-writing-automated-tests/no-listing-07-custom-failure-message/output.txt b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-07-custom-failure-message/output.txt new file mode 100644 index 000000000..d2ba1961b --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-07-custom-failure-message/output.txt @@ -0,0 +1,21 @@ +$ cargo test + Compiling greeter v0.1.0 (file:///projects/greeter) + Finished test [unoptimized + debuginfo] target(s) in 0.93s + Running unittests src/lib.rs (target/debug/deps/greeter-170b942eb5bf5e3a) + +running 1 test +test tests::greeting_contains_name ... FAILED + +failures: + +---- tests::greeting_contains_name stdout ---- +thread 'main' panicked at 'Greeting did not contain name, value was `Hello!`', src/lib.rs:12:9 +note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace + + +failures: + tests::greeting_contains_name + +test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s + +error: test failed, to rerun pass '--lib' diff --git a/src/doc/book/listings/ch11-writing-automated-tests/no-listing-07-custom-failure-message/src/lib.rs b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-07-custom-failure-message/src/lib.rs new file mode 100644 index 000000000..519c7a4c6 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-07-custom-failure-message/src/lib.rs @@ -0,0 +1,20 @@ +pub fn greeting(name: &str) -> String { + String::from("Hello!") +} + +#[cfg(test)] +mod tests { + use super::*; + + // ANCHOR: here + #[test] + fn greeting_contains_name() { + let result = greeting("Carol"); + assert!( + result.contains("Carol"), + "Greeting did not contain name, value was `{}`", + result + ); + } + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch11-writing-automated-tests/no-listing-08-guess-with-bug/Cargo.lock b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-08-guess-with-bug/Cargo.lock new file mode 100644 index 000000000..5802b7dc9 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-08-guess-with-bug/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "guessing_game" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch11-writing-automated-tests/no-listing-08-guess-with-bug/Cargo.toml b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-08-guess-with-bug/Cargo.toml new file mode 100644 index 000000000..4e348c8d2 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-08-guess-with-bug/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "guessing_game" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch11-writing-automated-tests/no-listing-08-guess-with-bug/output.txt b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-08-guess-with-bug/output.txt new file mode 100644 index 000000000..f59dddec4 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-08-guess-with-bug/output.txt @@ -0,0 +1,19 @@ +$ cargo test + Compiling guessing_game v0.1.0 (file:///projects/guessing_game) + Finished test [unoptimized + debuginfo] target(s) in 0.62s + Running unittests src/lib.rs (target/debug/deps/guessing_game-57d70c3acb738f4d) + +running 1 test +test tests::greater_than_100 - should panic ... FAILED + +failures: + +---- tests::greater_than_100 stdout ---- +note: test did not panic as expected + +failures: + tests::greater_than_100 + +test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s + +error: test failed, to rerun pass '--lib' diff --git a/src/doc/book/listings/ch11-writing-automated-tests/no-listing-08-guess-with-bug/src/lib.rs b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-08-guess-with-bug/src/lib.rs new file mode 100644 index 000000000..32540ba83 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-08-guess-with-bug/src/lib.rs @@ -0,0 +1,27 @@ +pub struct Guess { + value: i32, +} + +// ANCHOR: here +// --snip-- +impl Guess { + pub fn new(value: i32) -> Guess { + if value < 1 { + panic!("Guess value must be between 1 and 100, got {}.", value); + } + + Guess { value } + } +} +// ANCHOR_END: here + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + #[should_panic] + fn greater_than_100() { + Guess::new(200); + } +} diff --git a/src/doc/book/listings/ch11-writing-automated-tests/no-listing-09-guess-with-panic-msg-bug/Cargo.lock b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-09-guess-with-panic-msg-bug/Cargo.lock new file mode 100644 index 000000000..5802b7dc9 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-09-guess-with-panic-msg-bug/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "guessing_game" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch11-writing-automated-tests/no-listing-09-guess-with-panic-msg-bug/Cargo.toml b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-09-guess-with-panic-msg-bug/Cargo.toml new file mode 100644 index 000000000..4e348c8d2 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-09-guess-with-panic-msg-bug/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "guessing_game" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch11-writing-automated-tests/no-listing-09-guess-with-panic-msg-bug/output.txt b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-09-guess-with-panic-msg-bug/output.txt new file mode 100644 index 000000000..c8cba98fe --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-09-guess-with-panic-msg-bug/output.txt @@ -0,0 +1,23 @@ +$ cargo test + Compiling guessing_game v0.1.0 (file:///projects/guessing_game) + Finished test [unoptimized + debuginfo] target(s) in 0.66s + Running unittests src/lib.rs (target/debug/deps/guessing_game-57d70c3acb738f4d) + +running 1 test +test tests::greater_than_100 - should panic ... FAILED + +failures: + +---- tests::greater_than_100 stdout ---- +thread 'main' panicked at 'Guess value must be greater than or equal to 1, got 200.', src/lib.rs:13:13 +note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace +note: panic did not contain expected string + panic message: `"Guess value must be greater than or equal to 1, got 200."`, + expected substring: `"less than or equal to 100"` + +failures: + tests::greater_than_100 + +test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s + +error: test failed, to rerun pass '--lib' diff --git a/src/doc/book/listings/ch11-writing-automated-tests/no-listing-09-guess-with-panic-msg-bug/src/lib.rs b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-09-guess-with-panic-msg-bug/src/lib.rs new file mode 100644 index 000000000..7703dd75a --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-09-guess-with-panic-msg-bug/src/lib.rs @@ -0,0 +1,34 @@ +pub struct Guess { + value: i32, +} + +impl Guess { + pub fn new(value: i32) -> Guess { + // ANCHOR: here + if value < 1 { + panic!( + "Guess value must be less than or equal to 100, got {}.", + value + ); + } else if value > 100 { + panic!( + "Guess value must be greater than or equal to 1, got {}.", + value + ); + } + // ANCHOR_END: here + + Guess { value } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + #[should_panic(expected = "less than or equal to 100")] + fn greater_than_100() { + Guess::new(200); + } +} diff --git a/src/doc/book/listings/ch11-writing-automated-tests/no-listing-10-result-in-tests/Cargo.lock b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-10-result-in-tests/Cargo.lock new file mode 100644 index 000000000..d37189b33 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-10-result-in-tests/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "adder" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch11-writing-automated-tests/no-listing-10-result-in-tests/Cargo.toml b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-10-result-in-tests/Cargo.toml new file mode 100644 index 000000000..e61cb12e3 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-10-result-in-tests/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "adder" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch11-writing-automated-tests/no-listing-10-result-in-tests/src/lib.rs b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-10-result-in-tests/src/lib.rs new file mode 100644 index 000000000..6284f4f29 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-10-result-in-tests/src/lib.rs @@ -0,0 +1,11 @@ +#[cfg(test)] +mod tests { + #[test] + fn it_works() -> Result<(), String> { + if 2 + 2 == 4 { + Ok(()) + } else { + Err(String::from("two plus two does not equal four")) + } + } +} diff --git a/src/doc/book/listings/ch11-writing-automated-tests/no-listing-11-ignore-a-test/Cargo.lock b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-11-ignore-a-test/Cargo.lock new file mode 100644 index 000000000..d37189b33 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-11-ignore-a-test/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "adder" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch11-writing-automated-tests/no-listing-11-ignore-a-test/Cargo.toml b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-11-ignore-a-test/Cargo.toml new file mode 100644 index 000000000..e61cb12e3 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-11-ignore-a-test/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "adder" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch11-writing-automated-tests/no-listing-11-ignore-a-test/output.txt b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-11-ignore-a-test/output.txt new file mode 100644 index 000000000..c559de8d0 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-11-ignore-a-test/output.txt @@ -0,0 +1,17 @@ +$ cargo test + Compiling adder v0.1.0 (file:///projects/adder) + Finished test [unoptimized + debuginfo] target(s) in 0.60s + Running unittests src/lib.rs (target/debug/deps/adder-92948b65e88960b4) + +running 2 tests +test expensive_test ... ignored +test it_works ... ok + +test result: ok. 1 passed; 0 failed; 1 ignored; 0 measured; 0 filtered out; finished in 0.00s + + Doc-tests adder + +running 0 tests + +test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s + diff --git a/src/doc/book/listings/ch11-writing-automated-tests/no-listing-11-ignore-a-test/src/lib.rs b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-11-ignore-a-test/src/lib.rs new file mode 100644 index 000000000..d54a6095d --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-11-ignore-a-test/src/lib.rs @@ -0,0 +1,10 @@ +#[test] +fn it_works() { + assert_eq!(2 + 2, 4); +} + +#[test] +#[ignore] +fn expensive_test() { + // code that takes an hour to run +} diff --git a/src/doc/book/listings/ch11-writing-automated-tests/no-listing-12-shared-test-code-problem/Cargo.lock b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-12-shared-test-code-problem/Cargo.lock new file mode 100644 index 000000000..d37189b33 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-12-shared-test-code-problem/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "adder" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch11-writing-automated-tests/no-listing-12-shared-test-code-problem/Cargo.toml b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-12-shared-test-code-problem/Cargo.toml new file mode 100644 index 000000000..e61cb12e3 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-12-shared-test-code-problem/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "adder" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch11-writing-automated-tests/no-listing-12-shared-test-code-problem/output.txt b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-12-shared-test-code-problem/output.txt new file mode 100644 index 000000000..324d566ab --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-12-shared-test-code-problem/output.txt @@ -0,0 +1,29 @@ +$ cargo test + Compiling adder v0.1.0 (file:///projects/adder) + Finished test [unoptimized + debuginfo] target(s) in 0.89s + Running unittests src/lib.rs (target/debug/deps/adder-92948b65e88960b4) + +running 1 test +test tests::internal ... ok + +test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s + + Running tests/common.rs (target/debug/deps/common-92948b65e88960b4) + +running 0 tests + +test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s + + Running tests/integration_test.rs (target/debug/deps/integration_test-92948b65e88960b4) + +running 1 test +test it_adds_two ... ok + +test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s + + Doc-tests adder + +running 0 tests + +test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s + diff --git a/src/doc/book/listings/ch11-writing-automated-tests/no-listing-12-shared-test-code-problem/src/lib.rs b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-12-shared-test-code-problem/src/lib.rs new file mode 100644 index 000000000..c3961b1f6 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-12-shared-test-code-problem/src/lib.rs @@ -0,0 +1,17 @@ +pub fn add_two(a: i32) -> i32 { + internal_adder(a, 2) +} + +fn internal_adder(a: i32, b: i32) -> i32 { + a + b +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn internal() { + assert_eq!(4, internal_adder(2, 2)); + } +} diff --git a/src/doc/book/listings/ch11-writing-automated-tests/no-listing-12-shared-test-code-problem/tests/common.rs b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-12-shared-test-code-problem/tests/common.rs new file mode 100644 index 000000000..5fb7a390a --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-12-shared-test-code-problem/tests/common.rs @@ -0,0 +1,3 @@ +pub fn setup() { + // setup code specific to your library's tests would go here +} diff --git a/src/doc/book/listings/ch11-writing-automated-tests/no-listing-12-shared-test-code-problem/tests/integration_test.rs b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-12-shared-test-code-problem/tests/integration_test.rs new file mode 100644 index 000000000..e26fa7109 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-12-shared-test-code-problem/tests/integration_test.rs @@ -0,0 +1,6 @@ +use adder; + +#[test] +fn it_adds_two() { + assert_eq!(4, adder::add_two(2)); +} diff --git a/src/doc/book/listings/ch11-writing-automated-tests/no-listing-13-fix-shared-test-code-problem/Cargo.lock b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-13-fix-shared-test-code-problem/Cargo.lock new file mode 100644 index 000000000..d37189b33 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-13-fix-shared-test-code-problem/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "adder" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch11-writing-automated-tests/no-listing-13-fix-shared-test-code-problem/Cargo.toml b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-13-fix-shared-test-code-problem/Cargo.toml new file mode 100644 index 000000000..e61cb12e3 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-13-fix-shared-test-code-problem/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "adder" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch11-writing-automated-tests/no-listing-13-fix-shared-test-code-problem/src/lib.rs b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-13-fix-shared-test-code-problem/src/lib.rs new file mode 100644 index 000000000..c3961b1f6 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-13-fix-shared-test-code-problem/src/lib.rs @@ -0,0 +1,17 @@ +pub fn add_two(a: i32) -> i32 { + internal_adder(a, 2) +} + +fn internal_adder(a: i32, b: i32) -> i32 { + a + b +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn internal() { + assert_eq!(4, internal_adder(2, 2)); + } +} diff --git a/src/doc/book/listings/ch11-writing-automated-tests/no-listing-13-fix-shared-test-code-problem/tests/common/mod.rs b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-13-fix-shared-test-code-problem/tests/common/mod.rs new file mode 100644 index 000000000..5fb7a390a --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-13-fix-shared-test-code-problem/tests/common/mod.rs @@ -0,0 +1,3 @@ +pub fn setup() { + // setup code specific to your library's tests would go here +} diff --git a/src/doc/book/listings/ch11-writing-automated-tests/no-listing-13-fix-shared-test-code-problem/tests/integration_test.rs b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-13-fix-shared-test-code-problem/tests/integration_test.rs new file mode 100644 index 000000000..58b8b7b89 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/no-listing-13-fix-shared-test-code-problem/tests/integration_test.rs @@ -0,0 +1,9 @@ +use adder; + +mod common; + +#[test] +fn it_adds_two() { + common::setup(); + assert_eq!(4, adder::add_two(2)); +} diff --git a/src/doc/book/listings/ch11-writing-automated-tests/output-only-01-show-output/Cargo.lock b/src/doc/book/listings/ch11-writing-automated-tests/output-only-01-show-output/Cargo.lock new file mode 100644 index 000000000..a6d35e395 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/output-only-01-show-output/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "silly-function" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch11-writing-automated-tests/output-only-01-show-output/Cargo.toml b/src/doc/book/listings/ch11-writing-automated-tests/output-only-01-show-output/Cargo.toml new file mode 100644 index 000000000..f751864de --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/output-only-01-show-output/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "silly-function" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch11-writing-automated-tests/output-only-01-show-output/output.txt b/src/doc/book/listings/ch11-writing-automated-tests/output-only-01-show-output/output.txt new file mode 100644 index 000000000..8caaeda06 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/output-only-01-show-output/output.txt @@ -0,0 +1,34 @@ +$ cargo test -- --show-output + Compiling silly-function v0.1.0 (file:///projects/silly-function) + Finished test [unoptimized + debuginfo] target(s) in 0.60s + Running unittests src/lib.rs (target/debug/deps/silly_function-160869f38cff9166) + +running 2 tests +test tests::this_test_will_fail ... FAILED +test tests::this_test_will_pass ... ok + +successes: + +---- tests::this_test_will_pass stdout ---- +I got the value 4 + + +successes: + tests::this_test_will_pass + +failures: + +---- tests::this_test_will_fail stdout ---- +I got the value 8 +thread 'main' panicked at 'assertion failed: `(left == right)` + left: `5`, + right: `10`', src/lib.rs:19:9 +note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace + + +failures: + tests::this_test_will_fail + +test result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s + +error: test failed, to rerun pass '--lib' diff --git a/src/doc/book/listings/ch11-writing-automated-tests/output-only-01-show-output/src/lib.rs b/src/doc/book/listings/ch11-writing-automated-tests/output-only-01-show-output/src/lib.rs new file mode 100644 index 000000000..43c4c92f9 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/output-only-01-show-output/src/lib.rs @@ -0,0 +1,21 @@ +pub fn prints_and_returns_10(a: i32) -> i32 { + println!("I got the value {}", a); + 10 +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn this_test_will_pass() { + let value = prints_and_returns_10(4); + assert_eq!(10, value); + } + + #[test] + fn this_test_will_fail() { + let value = prints_and_returns_10(8); + assert_eq!(5, value); + } +} diff --git a/src/doc/book/listings/ch11-writing-automated-tests/output-only-02-single-test/Cargo.lock b/src/doc/book/listings/ch11-writing-automated-tests/output-only-02-single-test/Cargo.lock new file mode 100644 index 000000000..d37189b33 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/output-only-02-single-test/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "adder" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch11-writing-automated-tests/output-only-02-single-test/Cargo.toml b/src/doc/book/listings/ch11-writing-automated-tests/output-only-02-single-test/Cargo.toml new file mode 100644 index 000000000..e61cb12e3 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/output-only-02-single-test/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "adder" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch11-writing-automated-tests/output-only-02-single-test/output.txt b/src/doc/book/listings/ch11-writing-automated-tests/output-only-02-single-test/output.txt new file mode 100644 index 000000000..f2da98442 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/output-only-02-single-test/output.txt @@ -0,0 +1,10 @@ +$ cargo test one_hundred + Compiling adder v0.1.0 (file:///projects/adder) + Finished test [unoptimized + debuginfo] target(s) in 0.69s + Running unittests src/lib.rs (target/debug/deps/adder-92948b65e88960b4) + +running 1 test +test tests::one_hundred ... ok + +test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 2 filtered out; finished in 0.00s + diff --git a/src/doc/book/listings/ch11-writing-automated-tests/output-only-02-single-test/src/lib.rs b/src/doc/book/listings/ch11-writing-automated-tests/output-only-02-single-test/src/lib.rs new file mode 100644 index 000000000..f56715263 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/output-only-02-single-test/src/lib.rs @@ -0,0 +1,23 @@ +pub fn add_two(a: i32) -> i32 { + a + 2 +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn add_two_and_two() { + assert_eq!(4, add_two(2)); + } + + #[test] + fn add_three_and_two() { + assert_eq!(5, add_two(3)); + } + + #[test] + fn one_hundred() { + assert_eq!(102, add_two(100)); + } +} diff --git a/src/doc/book/listings/ch11-writing-automated-tests/output-only-03-multiple-tests/Cargo.lock b/src/doc/book/listings/ch11-writing-automated-tests/output-only-03-multiple-tests/Cargo.lock new file mode 100644 index 000000000..d37189b33 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/output-only-03-multiple-tests/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "adder" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch11-writing-automated-tests/output-only-03-multiple-tests/Cargo.toml b/src/doc/book/listings/ch11-writing-automated-tests/output-only-03-multiple-tests/Cargo.toml new file mode 100644 index 000000000..e61cb12e3 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/output-only-03-multiple-tests/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "adder" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch11-writing-automated-tests/output-only-03-multiple-tests/output.txt b/src/doc/book/listings/ch11-writing-automated-tests/output-only-03-multiple-tests/output.txt new file mode 100644 index 000000000..255a051b5 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/output-only-03-multiple-tests/output.txt @@ -0,0 +1,11 @@ +$ cargo test add + Compiling adder v0.1.0 (file:///projects/adder) + Finished test [unoptimized + debuginfo] target(s) in 0.61s + Running unittests src/lib.rs (target/debug/deps/adder-92948b65e88960b4) + +running 2 tests +test tests::add_three_and_two ... ok +test tests::add_two_and_two ... ok + +test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 1 filtered out; finished in 0.00s + diff --git a/src/doc/book/listings/ch11-writing-automated-tests/output-only-03-multiple-tests/src/lib.rs b/src/doc/book/listings/ch11-writing-automated-tests/output-only-03-multiple-tests/src/lib.rs new file mode 100644 index 000000000..f56715263 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/output-only-03-multiple-tests/src/lib.rs @@ -0,0 +1,23 @@ +pub fn add_two(a: i32) -> i32 { + a + 2 +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn add_two_and_two() { + assert_eq!(4, add_two(2)); + } + + #[test] + fn add_three_and_two() { + assert_eq!(5, add_two(3)); + } + + #[test] + fn one_hundred() { + assert_eq!(102, add_two(100)); + } +} diff --git a/src/doc/book/listings/ch11-writing-automated-tests/output-only-04-running-ignored/Cargo.lock b/src/doc/book/listings/ch11-writing-automated-tests/output-only-04-running-ignored/Cargo.lock new file mode 100644 index 000000000..d37189b33 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/output-only-04-running-ignored/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "adder" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch11-writing-automated-tests/output-only-04-running-ignored/Cargo.toml b/src/doc/book/listings/ch11-writing-automated-tests/output-only-04-running-ignored/Cargo.toml new file mode 100644 index 000000000..e61cb12e3 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/output-only-04-running-ignored/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "adder" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch11-writing-automated-tests/output-only-04-running-ignored/output.txt b/src/doc/book/listings/ch11-writing-automated-tests/output-only-04-running-ignored/output.txt new file mode 100644 index 000000000..b37868d3d --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/output-only-04-running-ignored/output.txt @@ -0,0 +1,16 @@ +$ cargo test -- --ignored + Compiling adder v0.1.0 (file:///projects/adder) + Finished test [unoptimized + debuginfo] target(s) in 0.61s + Running unittests src/lib.rs (target/debug/deps/adder-92948b65e88960b4) + +running 1 test +test expensive_test ... ok + +test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 1 filtered out; finished in 0.00s + + Doc-tests adder + +running 0 tests + +test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s + diff --git a/src/doc/book/listings/ch11-writing-automated-tests/output-only-04-running-ignored/src/lib.rs b/src/doc/book/listings/ch11-writing-automated-tests/output-only-04-running-ignored/src/lib.rs new file mode 100644 index 000000000..2290c699d --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/output-only-04-running-ignored/src/lib.rs @@ -0,0 +1,12 @@ +// ANCHOR: here +#[test] +fn it_works() { + assert_eq!(2 + 2, 4); +} + +#[test] +#[ignore] +fn expensive_test() { + // code that takes an hour to run +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch11-writing-automated-tests/output-only-05-single-integration/Cargo.lock b/src/doc/book/listings/ch11-writing-automated-tests/output-only-05-single-integration/Cargo.lock new file mode 100644 index 000000000..d37189b33 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/output-only-05-single-integration/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "adder" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch11-writing-automated-tests/output-only-05-single-integration/Cargo.toml b/src/doc/book/listings/ch11-writing-automated-tests/output-only-05-single-integration/Cargo.toml new file mode 100644 index 000000000..e61cb12e3 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/output-only-05-single-integration/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "adder" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch11-writing-automated-tests/output-only-05-single-integration/output.txt b/src/doc/book/listings/ch11-writing-automated-tests/output-only-05-single-integration/output.txt new file mode 100644 index 000000000..260beaa2d --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/output-only-05-single-integration/output.txt @@ -0,0 +1,10 @@ +$ cargo test --test integration_test + Compiling adder v0.1.0 (file:///projects/adder) + Finished test [unoptimized + debuginfo] target(s) in 0.64s + Running tests/integration_test.rs (target/debug/deps/integration_test-82e7799c1bc62298) + +running 1 test +test it_adds_two ... ok + +test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s + diff --git a/src/doc/book/listings/ch11-writing-automated-tests/output-only-05-single-integration/src/lib.rs b/src/doc/book/listings/ch11-writing-automated-tests/output-only-05-single-integration/src/lib.rs new file mode 100644 index 000000000..c3961b1f6 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/output-only-05-single-integration/src/lib.rs @@ -0,0 +1,17 @@ +pub fn add_two(a: i32) -> i32 { + internal_adder(a, 2) +} + +fn internal_adder(a: i32, b: i32) -> i32 { + a + b +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn internal() { + assert_eq!(4, internal_adder(2, 2)); + } +} diff --git a/src/doc/book/listings/ch11-writing-automated-tests/output-only-05-single-integration/tests/integration_test.rs b/src/doc/book/listings/ch11-writing-automated-tests/output-only-05-single-integration/tests/integration_test.rs new file mode 100644 index 000000000..e26fa7109 --- /dev/null +++ b/src/doc/book/listings/ch11-writing-automated-tests/output-only-05-single-integration/tests/integration_test.rs @@ -0,0 +1,6 @@ +use adder; + +#[test] +fn it_adds_two() { + assert_eq!(4, adder::add_two(2)); +} diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-01/Cargo.lock b/src/doc/book/listings/ch12-an-io-project/listing-12-01/Cargo.lock new file mode 100644 index 000000000..88bf82d16 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-01/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "minigrep" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-01/Cargo.toml b/src/doc/book/listings/ch12-an-io-project/listing-12-01/Cargo.toml new file mode 100644 index 000000000..64c2a3f52 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-01/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "minigrep" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-01/output.txt b/src/doc/book/listings/ch12-an-io-project/listing-12-01/output.txt new file mode 100644 index 000000000..529115f8d --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-01/output.txt @@ -0,0 +1,7 @@ +$ cargo run + Compiling minigrep v0.1.0 (file:///projects/minigrep) + Finished dev [unoptimized + debuginfo] target(s) in 0.61s + Running `target/debug/minigrep` +[src/main.rs:5] args = [ + "target/debug/minigrep", +] diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-01/src/main.rs b/src/doc/book/listings/ch12-an-io-project/listing-12-01/src/main.rs new file mode 100644 index 000000000..ae7def53d --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-01/src/main.rs @@ -0,0 +1,6 @@ +use std::env; + +fn main() { + let args: Vec = env::args().collect(); + dbg!(args); +} diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-02/Cargo.lock b/src/doc/book/listings/ch12-an-io-project/listing-12-02/Cargo.lock new file mode 100644 index 000000000..88bf82d16 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-02/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "minigrep" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-02/Cargo.toml b/src/doc/book/listings/ch12-an-io-project/listing-12-02/Cargo.toml new file mode 100644 index 000000000..64c2a3f52 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-02/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "minigrep" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-02/output.txt b/src/doc/book/listings/ch12-an-io-project/listing-12-02/output.txt new file mode 100644 index 000000000..6ef87f7ce --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-02/output.txt @@ -0,0 +1,6 @@ +$ cargo run -- test sample.txt + Compiling minigrep v0.1.0 (file:///projects/minigrep) + Finished dev [unoptimized + debuginfo] target(s) in 0.0s + Running `target/debug/minigrep test sample.txt` +Searching for test +In file sample.txt diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-02/src/main.rs b/src/doc/book/listings/ch12-an-io-project/listing-12-02/src/main.rs new file mode 100644 index 000000000..ae2fa7bb1 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-02/src/main.rs @@ -0,0 +1,11 @@ +use std::env; + +fn main() { + let args: Vec = env::args().collect(); + + let query = &args[1]; + let file_path = &args[2]; + + println!("Searching for {}", query); + println!("In file {}", file_path); +} diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-03/Cargo.lock b/src/doc/book/listings/ch12-an-io-project/listing-12-03/Cargo.lock new file mode 100644 index 000000000..88bf82d16 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-03/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "minigrep" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-03/Cargo.toml b/src/doc/book/listings/ch12-an-io-project/listing-12-03/Cargo.toml new file mode 100644 index 000000000..64c2a3f52 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-03/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "minigrep" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-03/poem.txt b/src/doc/book/listings/ch12-an-io-project/listing-12-03/poem.txt new file mode 100644 index 000000000..870752731 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-03/poem.txt @@ -0,0 +1,9 @@ +I'm nobody! Who are you? +Are you nobody, too? +Then there's a pair of us - don't tell! +They'd banish us, you know. + +How dreary to be somebody! +How public, like a frog +To tell your name the livelong day +To an admiring bog! diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-03/src/main.rs b/src/doc/book/listings/ch12-an-io-project/listing-12-03/src/main.rs new file mode 100644 index 000000000..ae2fa7bb1 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-03/src/main.rs @@ -0,0 +1,11 @@ +use std::env; + +fn main() { + let args: Vec = env::args().collect(); + + let query = &args[1]; + let file_path = &args[2]; + + println!("Searching for {}", query); + println!("In file {}", file_path); +} diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-04/Cargo.lock b/src/doc/book/listings/ch12-an-io-project/listing-12-04/Cargo.lock new file mode 100644 index 000000000..88bf82d16 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-04/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "minigrep" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-04/Cargo.toml b/src/doc/book/listings/ch12-an-io-project/listing-12-04/Cargo.toml new file mode 100644 index 000000000..64c2a3f52 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-04/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "minigrep" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-04/output.txt b/src/doc/book/listings/ch12-an-io-project/listing-12-04/output.txt new file mode 100644 index 000000000..6582ca169 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-04/output.txt @@ -0,0 +1,17 @@ +$ cargo run -- the poem.txt + Compiling minigrep v0.1.0 (file:///projects/minigrep) + Finished dev [unoptimized + debuginfo] target(s) in 0.0s + Running `target/debug/minigrep the poem.txt` +Searching for the +In file poem.txt +With text: +I'm nobody! Who are you? +Are you nobody, too? +Then there's a pair of us - don't tell! +They'd banish us, you know. + +How dreary to be somebody! +How public, like a frog +To tell your name the livelong day +To an admiring bog! + diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-04/poem.txt b/src/doc/book/listings/ch12-an-io-project/listing-12-04/poem.txt new file mode 100644 index 000000000..870752731 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-04/poem.txt @@ -0,0 +1,9 @@ +I'm nobody! Who are you? +Are you nobody, too? +Then there's a pair of us - don't tell! +They'd banish us, you know. + +How dreary to be somebody! +How public, like a frog +To tell your name the livelong day +To an admiring bog! diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-04/src/main.rs b/src/doc/book/listings/ch12-an-io-project/listing-12-04/src/main.rs new file mode 100644 index 000000000..944e4300e --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-04/src/main.rs @@ -0,0 +1,22 @@ +// ANCHOR: here +use std::env; +use std::fs; + +fn main() { + // --snip-- + // ANCHOR_END: here + let args: Vec = env::args().collect(); + + let query = &args[1]; + let file_path = &args[2]; + + println!("Searching for {}", query); + // ANCHOR: here + println!("In file {}", file_path); + + let contents = fs::read_to_string(file_path) + .expect("Should have been able to read the file"); + + println!("With text:\n{contents}"); +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-05/Cargo.lock b/src/doc/book/listings/ch12-an-io-project/listing-12-05/Cargo.lock new file mode 100644 index 000000000..88bf82d16 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-05/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "minigrep" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-05/Cargo.toml b/src/doc/book/listings/ch12-an-io-project/listing-12-05/Cargo.toml new file mode 100644 index 000000000..64c2a3f52 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-05/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "minigrep" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-05/poem.txt b/src/doc/book/listings/ch12-an-io-project/listing-12-05/poem.txt new file mode 100644 index 000000000..870752731 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-05/poem.txt @@ -0,0 +1,9 @@ +I'm nobody! Who are you? +Are you nobody, too? +Then there's a pair of us - don't tell! +They'd banish us, you know. + +How dreary to be somebody! +How public, like a frog +To tell your name the livelong day +To an admiring bog! diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-05/src/main.rs b/src/doc/book/listings/ch12-an-io-project/listing-12-05/src/main.rs new file mode 100644 index 000000000..061591833 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-05/src/main.rs @@ -0,0 +1,29 @@ +use std::env; +use std::fs; + +// ANCHOR: here +fn main() { + let args: Vec = env::args().collect(); + + let (query, file_path) = parse_config(&args); + + // --snip-- + // ANCHOR_END: here + + println!("Searching for {}", query); + println!("In file {}", file_path); + + let contents = fs::read_to_string(file_path) + .expect("Should have been able to read the file"); + + println!("With text:\n{contents}"); + // ANCHOR: here +} + +fn parse_config(args: &[String]) -> (&str, &str) { + let query = &args[1]; + let file_path = &args[2]; + + (query, file_path) +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-06/Cargo.lock b/src/doc/book/listings/ch12-an-io-project/listing-12-06/Cargo.lock new file mode 100644 index 000000000..88bf82d16 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-06/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "minigrep" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-06/Cargo.toml b/src/doc/book/listings/ch12-an-io-project/listing-12-06/Cargo.toml new file mode 100644 index 000000000..64c2a3f52 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-06/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "minigrep" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-06/poem.txt b/src/doc/book/listings/ch12-an-io-project/listing-12-06/poem.txt new file mode 100644 index 000000000..870752731 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-06/poem.txt @@ -0,0 +1,9 @@ +I'm nobody! Who are you? +Are you nobody, too? +Then there's a pair of us - don't tell! +They'd banish us, you know. + +How dreary to be somebody! +How public, like a frog +To tell your name the livelong day +To an admiring bog! diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-06/src/main.rs b/src/doc/book/listings/ch12-an-io-project/listing-12-06/src/main.rs new file mode 100644 index 000000000..c77e848d8 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-06/src/main.rs @@ -0,0 +1,34 @@ +use std::env; +use std::fs; + +// ANCHOR: here +fn main() { + let args: Vec = env::args().collect(); + + let config = parse_config(&args); + + println!("Searching for {}", config.query); + println!("In file {}", config.file_path); + + let contents = fs::read_to_string(config.file_path) + .expect("Should have been able to read the file"); + + // --snip-- + // ANCHOR_END: here + + println!("With text:\n{contents}"); + // ANCHOR: here +} + +struct Config { + query: String, + file_path: String, +} + +fn parse_config(args: &[String]) -> Config { + let query = args[1].clone(); + let file_path = args[2].clone(); + + Config { query, file_path } +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-07/Cargo.lock b/src/doc/book/listings/ch12-an-io-project/listing-12-07/Cargo.lock new file mode 100644 index 000000000..88bf82d16 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-07/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "minigrep" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-07/Cargo.toml b/src/doc/book/listings/ch12-an-io-project/listing-12-07/Cargo.toml new file mode 100644 index 000000000..64c2a3f52 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-07/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "minigrep" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-07/output.txt b/src/doc/book/listings/ch12-an-io-project/listing-12-07/output.txt new file mode 100644 index 000000000..d3fa7777d --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-07/output.txt @@ -0,0 +1,6 @@ +$ cargo run + Compiling minigrep v0.1.0 (file:///projects/minigrep) + Finished dev [unoptimized + debuginfo] target(s) in 0.0s + Running `target/debug/minigrep` +thread 'main' panicked at 'index out of bounds: the len is 1 but the index is 1', src/main.rs:27:21 +note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-07/poem.txt b/src/doc/book/listings/ch12-an-io-project/listing-12-07/poem.txt new file mode 100644 index 000000000..870752731 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-07/poem.txt @@ -0,0 +1,9 @@ +I'm nobody! Who are you? +Are you nobody, too? +Then there's a pair of us - don't tell! +They'd banish us, you know. + +How dreary to be somebody! +How public, like a frog +To tell your name the livelong day +To an admiring bog! diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-07/src/main.rs b/src/doc/book/listings/ch12-an-io-project/listing-12-07/src/main.rs new file mode 100644 index 000000000..ff6c29420 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-07/src/main.rs @@ -0,0 +1,40 @@ +use std::env; +use std::fs; + +// ANCHOR: here +fn main() { + let args: Vec = env::args().collect(); + + let config = Config::new(&args); + // ANCHOR_END: here + + println!("Searching for {}", config.query); + println!("In file {}", config.file_path); + + let contents = fs::read_to_string(config.file_path) + .expect("Should have been able to read the file"); + + println!("With text:\n{contents}"); + // ANCHOR: here + + // --snip-- +} + +// --snip-- + +// ANCHOR_END: here +struct Config { + query: String, + file_path: String, +} + +// ANCHOR: here +impl Config { + fn new(args: &[String]) -> Config { + let query = args[1].clone(); + let file_path = args[2].clone(); + + Config { query, file_path } + } +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-08/Cargo.lock b/src/doc/book/listings/ch12-an-io-project/listing-12-08/Cargo.lock new file mode 100644 index 000000000..88bf82d16 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-08/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "minigrep" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-08/Cargo.toml b/src/doc/book/listings/ch12-an-io-project/listing-12-08/Cargo.toml new file mode 100644 index 000000000..64c2a3f52 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-08/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "minigrep" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-08/output.txt b/src/doc/book/listings/ch12-an-io-project/listing-12-08/output.txt new file mode 100644 index 000000000..de2abd1af --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-08/output.txt @@ -0,0 +1,6 @@ +$ cargo run + Compiling minigrep v0.1.0 (file:///projects/minigrep) + Finished dev [unoptimized + debuginfo] target(s) in 0.0s + Running `target/debug/minigrep` +thread 'main' panicked at 'not enough arguments', src/main.rs:26:13 +note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-08/poem.txt b/src/doc/book/listings/ch12-an-io-project/listing-12-08/poem.txt new file mode 100644 index 000000000..870752731 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-08/poem.txt @@ -0,0 +1,9 @@ +I'm nobody! Who are you? +Are you nobody, too? +Then there's a pair of us - don't tell! +They'd banish us, you know. + +How dreary to be somebody! +How public, like a frog +To tell your name the livelong day +To an admiring bog! diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-08/src/main.rs b/src/doc/book/listings/ch12-an-io-project/listing-12-08/src/main.rs new file mode 100644 index 000000000..cecd15abf --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-08/src/main.rs @@ -0,0 +1,38 @@ +use std::env; +use std::fs; + +fn main() { + let args: Vec = env::args().collect(); + + let config = Config::new(&args); + + println!("Searching for {}", config.query); + println!("In file {}", config.file_path); + + let contents = fs::read_to_string(config.file_path) + .expect("Should have been able to read the file"); + + println!("With text:\n{contents}"); +} + +struct Config { + query: String, + file_path: String, +} + +impl Config { + // ANCHOR: here + // --snip-- + fn new(args: &[String]) -> Config { + if args.len() < 3 { + panic!("not enough arguments"); + } + // --snip-- + // ANCHOR_END: here + + let query = args[1].clone(); + let file_path = args[2].clone(); + + Config { query, file_path } + } +} diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-09/Cargo.lock b/src/doc/book/listings/ch12-an-io-project/listing-12-09/Cargo.lock new file mode 100644 index 000000000..88bf82d16 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-09/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "minigrep" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-09/Cargo.toml b/src/doc/book/listings/ch12-an-io-project/listing-12-09/Cargo.toml new file mode 100644 index 000000000..64c2a3f52 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-09/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "minigrep" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-09/poem.txt b/src/doc/book/listings/ch12-an-io-project/listing-12-09/poem.txt new file mode 100644 index 000000000..870752731 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-09/poem.txt @@ -0,0 +1,9 @@ +I'm nobody! Who are you? +Are you nobody, too? +Then there's a pair of us - don't tell! +They'd banish us, you know. + +How dreary to be somebody! +How public, like a frog +To tell your name the livelong day +To an admiring bog! diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-09/src/main.rs b/src/doc/book/listings/ch12-an-io-project/listing-12-09/src/main.rs new file mode 100644 index 000000000..3418e718c --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-09/src/main.rs @@ -0,0 +1,36 @@ +use std::env; +use std::fs; + +fn main() { + let args: Vec = env::args().collect(); + + let config = Config::new(&args); + + println!("Searching for {}", config.query); + println!("In file {}", config.file_path); + + let contents = fs::read_to_string(config.file_path) + .expect("Should have been able to read the file"); + + println!("With text:\n{contents}"); +} + +struct Config { + query: String, + file_path: String, +} + +// ANCHOR: here +impl Config { + fn build(args: &[String]) -> Result { + if args.len() < 3 { + return Err("not enough arguments"); + } + + let query = args[1].clone(); + let file_path = args[2].clone(); + + Ok(Config { query, file_path }) + } +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-10/Cargo.lock b/src/doc/book/listings/ch12-an-io-project/listing-12-10/Cargo.lock new file mode 100644 index 000000000..88bf82d16 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-10/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "minigrep" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-10/Cargo.toml b/src/doc/book/listings/ch12-an-io-project/listing-12-10/Cargo.toml new file mode 100644 index 000000000..64c2a3f52 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-10/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "minigrep" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-10/output.txt b/src/doc/book/listings/ch12-an-io-project/listing-12-10/output.txt new file mode 100644 index 000000000..7aad57f52 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-10/output.txt @@ -0,0 +1,5 @@ +$ cargo run + Compiling minigrep v0.1.0 (file:///projects/minigrep) + Finished dev [unoptimized + debuginfo] target(s) in 0.48s + Running `target/debug/minigrep` +Problem parsing arguments: not enough arguments diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-10/poem.txt b/src/doc/book/listings/ch12-an-io-project/listing-12-10/poem.txt new file mode 100644 index 000000000..870752731 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-10/poem.txt @@ -0,0 +1,9 @@ +I'm nobody! Who are you? +Are you nobody, too? +Then there's a pair of us - don't tell! +They'd banish us, you know. + +How dreary to be somebody! +How public, like a frog +To tell your name the livelong day +To an admiring bog! diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-10/src/main.rs b/src/doc/book/listings/ch12-an-io-project/listing-12-10/src/main.rs new file mode 100644 index 000000000..ab6d3e548 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-10/src/main.rs @@ -0,0 +1,42 @@ +use std::env; +use std::fs; +// ANCHOR: here +use std::process; + +fn main() { + let args: Vec = env::args().collect(); + + let config = Config::build(&args).unwrap_or_else(|err| { + println!("Problem parsing arguments: {err}"); + process::exit(1); + }); + + // --snip-- + // ANCHOR_END: here + + println!("Searching for {}", config.query); + println!("In file {}", config.file_path); + + let contents = fs::read_to_string(config.file_path) + .expect("Should have been able to read the file"); + + println!("With text:\n{contents}"); +} + +struct Config { + query: String, + file_path: String, +} + +impl Config { + fn build(args: &[String]) -> Result { + if args.len() < 3 { + return Err("not enough arguments"); + } + + let query = args[1].clone(); + let file_path = args[2].clone(); + + Ok(Config { query, file_path }) + } +} diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-11/Cargo.lock b/src/doc/book/listings/ch12-an-io-project/listing-12-11/Cargo.lock new file mode 100644 index 000000000..88bf82d16 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-11/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "minigrep" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-11/Cargo.toml b/src/doc/book/listings/ch12-an-io-project/listing-12-11/Cargo.toml new file mode 100644 index 000000000..64c2a3f52 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-11/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "minigrep" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-11/poem.txt b/src/doc/book/listings/ch12-an-io-project/listing-12-11/poem.txt new file mode 100644 index 000000000..870752731 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-11/poem.txt @@ -0,0 +1,9 @@ +I'm nobody! Who are you? +Are you nobody, too? +Then there's a pair of us - don't tell! +They'd banish us, you know. + +How dreary to be somebody! +How public, like a frog +To tell your name the livelong day +To an admiring bog! diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-11/src/main.rs b/src/doc/book/listings/ch12-an-io-project/listing-12-11/src/main.rs new file mode 100644 index 000000000..3f476b975 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-11/src/main.rs @@ -0,0 +1,50 @@ +use std::env; +use std::fs; +use std::process; + +// ANCHOR: here +fn main() { + // --snip-- + + // ANCHOR_END: here + let args: Vec = env::args().collect(); + + let config = Config::build(&args).unwrap_or_else(|err| { + println!("Problem parsing arguments: {err}"); + process::exit(1); + }); + + // ANCHOR: here + println!("Searching for {}", config.query); + println!("In file {}", config.file_path); + + run(config); +} + +fn run(config: Config) { + let contents = fs::read_to_string(config.file_path) + .expect("Should have been able to read the file"); + + println!("With text:\n{contents}"); +} + +// --snip-- +// ANCHOR_END: here + +struct Config { + query: String, + file_path: String, +} + +impl Config { + fn build(args: &[String]) -> Result { + if args.len() < 3 { + return Err("not enough arguments"); + } + + let query = args[1].clone(); + let file_path = args[2].clone(); + + Ok(Config { query, file_path }) + } +} diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-12/Cargo.lock b/src/doc/book/listings/ch12-an-io-project/listing-12-12/Cargo.lock new file mode 100644 index 000000000..88bf82d16 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-12/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "minigrep" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-12/Cargo.toml b/src/doc/book/listings/ch12-an-io-project/listing-12-12/Cargo.toml new file mode 100644 index 000000000..64c2a3f52 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-12/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "minigrep" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-12/output.txt b/src/doc/book/listings/ch12-an-io-project/listing-12-12/output.txt new file mode 100644 index 000000000..c18902518 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-12/output.txt @@ -0,0 +1,27 @@ +$ cargo run the poem.txt + Compiling minigrep v0.1.0 (file:///projects/minigrep) +warning: unused `Result` that must be used + --> src/main.rs:19:5 + | +19 | run(config); + | ^^^^^^^^^^^^ + | + = note: `#[warn(unused_must_use)]` on by default + = note: this `Result` may be an `Err` variant, which should be handled + +warning: `minigrep` (bin "minigrep") generated 1 warning + Finished dev [unoptimized + debuginfo] target(s) in 0.71s + Running `target/debug/minigrep the poem.txt` +Searching for the +In file poem.txt +With text: +I'm nobody! Who are you? +Are you nobody, too? +Then there's a pair of us - don't tell! +They'd banish us, you know. + +How dreary to be somebody! +How public, like a frog +To tell your name the livelong day +To an admiring bog! + diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-12/poem.txt b/src/doc/book/listings/ch12-an-io-project/listing-12-12/poem.txt new file mode 100644 index 000000000..870752731 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-12/poem.txt @@ -0,0 +1,9 @@ +I'm nobody! Who are you? +Are you nobody, too? +Then there's a pair of us - don't tell! +They'd banish us, you know. + +How dreary to be somebody! +How public, like a frog +To tell your name the livelong day +To an admiring bog! diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-12/src/main.rs b/src/doc/book/listings/ch12-an-io-project/listing-12-12/src/main.rs new file mode 100644 index 000000000..ab5fdb894 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-12/src/main.rs @@ -0,0 +1,51 @@ +use std::env; +use std::fs; +use std::process; +// ANCHOR: here +use std::error::Error; + +// --snip-- + +// ANCHOR_END: here + +fn main() { + let args: Vec = env::args().collect(); + + let config = Config::build(&args).unwrap_or_else(|err| { + println!("Problem parsing arguments: {err}"); + process::exit(1); + }); + + println!("Searching for {}", config.query); + println!("In file {}", config.file_path); + + run(config); +} + +// ANCHOR: here +fn run(config: Config) -> Result<(), Box> { + let contents = fs::read_to_string(config.file_path)?; + + println!("With text:\n{contents}"); + + Ok(()) +} +// ANCHOR_END: here + +struct Config { + query: String, + file_path: String, +} + +impl Config { + fn build(args: &[String]) -> Result { + if args.len() < 3 { + return Err("not enough arguments"); + } + + let query = args[1].clone(); + let file_path = args[2].clone(); + + Ok(Config { query, file_path }) + } +} diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-13/Cargo.lock b/src/doc/book/listings/ch12-an-io-project/listing-12-13/Cargo.lock new file mode 100644 index 000000000..88bf82d16 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-13/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "minigrep" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-13/Cargo.toml b/src/doc/book/listings/ch12-an-io-project/listing-12-13/Cargo.toml new file mode 100644 index 000000000..64c2a3f52 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-13/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "minigrep" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-13/poem.txt b/src/doc/book/listings/ch12-an-io-project/listing-12-13/poem.txt new file mode 100644 index 000000000..870752731 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-13/poem.txt @@ -0,0 +1,9 @@ +I'm nobody! Who are you? +Are you nobody, too? +Then there's a pair of us - don't tell! +They'd banish us, you know. + +How dreary to be somebody! +How public, like a frog +To tell your name the livelong day +To an admiring bog! diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-13/src/lib.rs b/src/doc/book/listings/ch12-an-io-project/listing-12-13/src/lib.rs new file mode 100644 index 000000000..1a3c48089 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-13/src/lib.rs @@ -0,0 +1,36 @@ +// ANCHOR: here +use std::error::Error; +use std::fs; + +pub struct Config { + pub query: String, + pub file_path: String, +} + +impl Config { + pub fn build(args: &[String]) -> Result { + // --snip-- + // ANCHOR_END: here + if args.len() < 3 { + return Err("not enough arguments"); + } + + let query = args[1].clone(); + let file_path = args[2].clone(); + + Ok(Config { query, file_path }) + // ANCHOR: here + } +} + +pub fn run(config: Config) -> Result<(), Box> { + // --snip-- + // ANCHOR_END: here + let contents = fs::read_to_string(config.file_path)?; + + println!("With text:\n{contents}"); + + Ok(()) + // ANCHOR: here +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-13/src/main.rs b/src/doc/book/listings/ch12-an-io-project/listing-12-13/src/main.rs new file mode 100644 index 000000000..09756ca3f --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-13/src/main.rs @@ -0,0 +1,19 @@ +use std::env; +use std::process; + +fn main() { + let args: Vec = env::args().collect(); + + let config = Config::build(&args).unwrap_or_else(|err| { + println!("Problem parsing arguments: {err}"); + process::exit(1); + }); + + println!("Searching for {}", config.query); + println!("In file {}", config.file_path); + + if let Err(e) = run(config) { + println!("Application error: {e}"); + process::exit(1); + } +} diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-14/Cargo.lock b/src/doc/book/listings/ch12-an-io-project/listing-12-14/Cargo.lock new file mode 100644 index 000000000..88bf82d16 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-14/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "minigrep" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-14/Cargo.toml b/src/doc/book/listings/ch12-an-io-project/listing-12-14/Cargo.toml new file mode 100644 index 000000000..64c2a3f52 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-14/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "minigrep" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-14/poem.txt b/src/doc/book/listings/ch12-an-io-project/listing-12-14/poem.txt new file mode 100644 index 000000000..870752731 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-14/poem.txt @@ -0,0 +1,9 @@ +I'm nobody! Who are you? +Are you nobody, too? +Then there's a pair of us - don't tell! +They'd banish us, you know. + +How dreary to be somebody! +How public, like a frog +To tell your name the livelong day +To an admiring bog! diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-14/src/lib.rs b/src/doc/book/listings/ch12-an-io-project/listing-12-14/src/lib.rs new file mode 100644 index 000000000..4f3a4e865 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-14/src/lib.rs @@ -0,0 +1,28 @@ +use std::error::Error; +use std::fs; + +pub struct Config { + pub query: String, + pub file_path: String, +} + +impl Config { + pub fn build(args: &[String]) -> Result { + if args.len() < 3 { + return Err("not enough arguments"); + } + + let query = args[1].clone(); + let file_path = args[2].clone(); + + Ok(Config { query, file_path }) + } +} + +pub fn run(config: Config) -> Result<(), Box> { + let contents = fs::read_to_string(config.file_path)?; + + println!("With text:\n{contents}"); + + Ok(()) +} diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-14/src/main.rs b/src/doc/book/listings/ch12-an-io-project/listing-12-14/src/main.rs new file mode 100644 index 000000000..3b76009b5 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-14/src/main.rs @@ -0,0 +1,29 @@ +// ANCHOR: here +use std::env; +use std::process; + +use minigrep::Config; + +fn main() { + // --snip-- + // ANCHOR_END: here + let args: Vec = env::args().collect(); + + let config = Config::build(&args).unwrap_or_else(|err| { + println!("Problem parsing arguments: {err}"); + process::exit(1); + }); + + println!("Searching for {}", config.query); + println!("In file {}", config.file_path); + + // ANCHOR: here + if let Err(e) = minigrep::run(config) { + // --snip-- + // ANCHOR_END: here + println!("Application error: {e}"); + process::exit(1); + // ANCHOR: here + } +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-15/Cargo.lock b/src/doc/book/listings/ch12-an-io-project/listing-12-15/Cargo.lock new file mode 100644 index 000000000..88bf82d16 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-15/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "minigrep" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-15/Cargo.toml b/src/doc/book/listings/ch12-an-io-project/listing-12-15/Cargo.toml new file mode 100644 index 000000000..64c2a3f52 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-15/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "minigrep" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-15/poem.txt b/src/doc/book/listings/ch12-an-io-project/listing-12-15/poem.txt new file mode 100644 index 000000000..870752731 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-15/poem.txt @@ -0,0 +1,9 @@ +I'm nobody! Who are you? +Are you nobody, too? +Then there's a pair of us - don't tell! +They'd banish us, you know. + +How dreary to be somebody! +How public, like a frog +To tell your name the livelong day +To an admiring bog! diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-15/src/lib.rs b/src/doc/book/listings/ch12-an-io-project/listing-12-15/src/lib.rs new file mode 100644 index 000000000..20c4a782b --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-15/src/lib.rs @@ -0,0 +1,44 @@ +use std::error::Error; +use std::fs; + +pub struct Config { + pub query: String, + pub file_path: String, +} + +impl Config { + pub fn build(args: &[String]) -> Result { + if args.len() < 3 { + return Err("not enough arguments"); + } + + let query = args[1].clone(); + let file_path = args[2].clone(); + + Ok(Config { query, file_path }) + } +} + +pub fn run(config: Config) -> Result<(), Box> { + let contents = fs::read_to_string(config.file_path)?; + + Ok(()) +} + +// ANCHOR: here +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn one_result() { + let query = "duct"; + let contents = "\ +Rust: +safe, fast, productive. +Pick three."; + + assert_eq!(vec!["safe, fast, productive."], search(query, contents)); + } +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-15/src/main.rs b/src/doc/book/listings/ch12-an-io-project/listing-12-15/src/main.rs new file mode 100644 index 000000000..a4f8a7411 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-15/src/main.rs @@ -0,0 +1,18 @@ +use std::env; +use std::process; + +use minigrep::Config; + +fn main() { + let args: Vec = env::args().collect(); + + let config = Config::build(&args).unwrap_or_else(|err| { + println!("Problem parsing arguments: {err}"); + process::exit(1); + }); + + if let Err(e) = minigrep::run(config) { + println!("Application error: {e}"); + process::exit(1); + } +} diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-16/Cargo.lock b/src/doc/book/listings/ch12-an-io-project/listing-12-16/Cargo.lock new file mode 100644 index 000000000..88bf82d16 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-16/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "minigrep" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-16/Cargo.toml b/src/doc/book/listings/ch12-an-io-project/listing-12-16/Cargo.toml new file mode 100644 index 000000000..64c2a3f52 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-16/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "minigrep" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-16/output.txt b/src/doc/book/listings/ch12-an-io-project/listing-12-16/output.txt new file mode 100644 index 000000000..d0cda6024 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-16/output.txt @@ -0,0 +1,23 @@ +$ cargo test + Compiling minigrep v0.1.0 (file:///projects/minigrep) + Finished test [unoptimized + debuginfo] target(s) in 0.97s + Running unittests src/lib.rs (target/debug/deps/minigrep-9cd200e5fac0fc94) + +running 1 test +test tests::one_result ... FAILED + +failures: + +---- tests::one_result stdout ---- +thread 'main' panicked at 'assertion failed: `(left == right)` + left: `["safe, fast, productive."]`, + right: `[]`', src/lib.rs:44:9 +note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace + + +failures: + tests::one_result + +test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s + +error: test failed, to rerun pass '--lib' diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-16/poem.txt b/src/doc/book/listings/ch12-an-io-project/listing-12-16/poem.txt new file mode 100644 index 000000000..870752731 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-16/poem.txt @@ -0,0 +1,9 @@ +I'm nobody! Who are you? +Are you nobody, too? +Then there's a pair of us - don't tell! +They'd banish us, you know. + +How dreary to be somebody! +How public, like a frog +To tell your name the livelong day +To an admiring bog! diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-16/src/lib.rs b/src/doc/book/listings/ch12-an-io-project/listing-12-16/src/lib.rs new file mode 100644 index 000000000..f5e593484 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-16/src/lib.rs @@ -0,0 +1,48 @@ +use std::error::Error; +use std::fs; + +pub struct Config { + pub query: String, + pub file_path: String, +} + +impl Config { + pub fn build(args: &[String]) -> Result { + if args.len() < 3 { + return Err("not enough arguments"); + } + + let query = args[1].clone(); + let file_path = args[2].clone(); + + Ok(Config { query, file_path }) + } +} + +pub fn run(config: Config) -> Result<(), Box> { + let contents = fs::read_to_string(config.file_path)?; + + Ok(()) +} + +// ANCHOR: here +pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { + vec![] +} +// ANCHOR_END: here + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn one_result() { + let query = "duct"; + let contents = "\ +Rust: +safe, fast, productive. +Pick three."; + + assert_eq!(vec!["safe, fast, productive."], search(query, contents)); + } +} diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-16/src/main.rs b/src/doc/book/listings/ch12-an-io-project/listing-12-16/src/main.rs new file mode 100644 index 000000000..a4f8a7411 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-16/src/main.rs @@ -0,0 +1,18 @@ +use std::env; +use std::process; + +use minigrep::Config; + +fn main() { + let args: Vec = env::args().collect(); + + let config = Config::build(&args).unwrap_or_else(|err| { + println!("Problem parsing arguments: {err}"); + process::exit(1); + }); + + if let Err(e) = minigrep::run(config) { + println!("Application error: {e}"); + process::exit(1); + } +} diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-17/Cargo.lock b/src/doc/book/listings/ch12-an-io-project/listing-12-17/Cargo.lock new file mode 100644 index 000000000..88bf82d16 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-17/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "minigrep" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-17/Cargo.toml b/src/doc/book/listings/ch12-an-io-project/listing-12-17/Cargo.toml new file mode 100644 index 000000000..64c2a3f52 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-17/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "minigrep" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-17/poem.txt b/src/doc/book/listings/ch12-an-io-project/listing-12-17/poem.txt new file mode 100644 index 000000000..870752731 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-17/poem.txt @@ -0,0 +1,9 @@ +I'm nobody! Who are you? +Are you nobody, too? +Then there's a pair of us - don't tell! +They'd banish us, you know. + +How dreary to be somebody! +How public, like a frog +To tell your name the livelong day +To an admiring bog! diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-17/src/lib.rs b/src/doc/book/listings/ch12-an-io-project/listing-12-17/src/lib.rs new file mode 100644 index 000000000..cb9fab401 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-17/src/lib.rs @@ -0,0 +1,50 @@ +use std::error::Error; +use std::fs; + +pub struct Config { + pub query: String, + pub file_path: String, +} + +impl Config { + pub fn build(args: &[String]) -> Result { + if args.len() < 3 { + return Err("not enough arguments"); + } + + let query = args[1].clone(); + let file_path = args[2].clone(); + + Ok(Config { query, file_path }) + } +} + +pub fn run(config: Config) -> Result<(), Box> { + let contents = fs::read_to_string(config.file_path)?; + + Ok(()) +} + +// ANCHOR: here +pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { + for line in contents.lines() { + // do something with line + } +} +// ANCHOR_END: here + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn one_result() { + let query = "duct"; + let contents = "\ +Rust: +safe, fast, productive. +Pick three."; + + assert_eq!(vec!["safe, fast, productive."], search(query, contents)); + } +} diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-17/src/main.rs b/src/doc/book/listings/ch12-an-io-project/listing-12-17/src/main.rs new file mode 100644 index 000000000..a4f8a7411 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-17/src/main.rs @@ -0,0 +1,18 @@ +use std::env; +use std::process; + +use minigrep::Config; + +fn main() { + let args: Vec = env::args().collect(); + + let config = Config::build(&args).unwrap_or_else(|err| { + println!("Problem parsing arguments: {err}"); + process::exit(1); + }); + + if let Err(e) = minigrep::run(config) { + println!("Application error: {e}"); + process::exit(1); + } +} diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-18/Cargo.lock b/src/doc/book/listings/ch12-an-io-project/listing-12-18/Cargo.lock new file mode 100644 index 000000000..88bf82d16 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-18/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "minigrep" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-18/Cargo.toml b/src/doc/book/listings/ch12-an-io-project/listing-12-18/Cargo.toml new file mode 100644 index 000000000..64c2a3f52 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-18/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "minigrep" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-18/poem.txt b/src/doc/book/listings/ch12-an-io-project/listing-12-18/poem.txt new file mode 100644 index 000000000..870752731 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-18/poem.txt @@ -0,0 +1,9 @@ +I'm nobody! Who are you? +Are you nobody, too? +Then there's a pair of us - don't tell! +They'd banish us, you know. + +How dreary to be somebody! +How public, like a frog +To tell your name the livelong day +To an admiring bog! diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-18/src/lib.rs b/src/doc/book/listings/ch12-an-io-project/listing-12-18/src/lib.rs new file mode 100644 index 000000000..a05d046d7 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-18/src/lib.rs @@ -0,0 +1,52 @@ +use std::error::Error; +use std::fs; + +pub struct Config { + pub query: String, + pub file_path: String, +} + +impl Config { + pub fn build(args: &[String]) -> Result { + if args.len() < 3 { + return Err("not enough arguments"); + } + + let query = args[1].clone(); + let file_path = args[2].clone(); + + Ok(Config { query, file_path }) + } +} + +pub fn run(config: Config) -> Result<(), Box> { + let contents = fs::read_to_string(config.file_path)?; + + Ok(()) +} + +// ANCHOR: here +pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { + for line in contents.lines() { + if line.contains(query) { + // do something with line + } + } +} +// ANCHOR_END: here + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn one_result() { + let query = "duct"; + let contents = "\ +Rust: +safe, fast, productive. +Pick three."; + + assert_eq!(vec!["safe, fast, productive."], search(query, contents)); + } +} diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-18/src/main.rs b/src/doc/book/listings/ch12-an-io-project/listing-12-18/src/main.rs new file mode 100644 index 000000000..a4f8a7411 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-18/src/main.rs @@ -0,0 +1,18 @@ +use std::env; +use std::process; + +use minigrep::Config; + +fn main() { + let args: Vec = env::args().collect(); + + let config = Config::build(&args).unwrap_or_else(|err| { + println!("Problem parsing arguments: {err}"); + process::exit(1); + }); + + if let Err(e) = minigrep::run(config) { + println!("Application error: {e}"); + process::exit(1); + } +} diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-19/Cargo.lock b/src/doc/book/listings/ch12-an-io-project/listing-12-19/Cargo.lock new file mode 100644 index 000000000..88bf82d16 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-19/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "minigrep" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-19/Cargo.toml b/src/doc/book/listings/ch12-an-io-project/listing-12-19/Cargo.toml new file mode 100644 index 000000000..64c2a3f52 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-19/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "minigrep" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-19/output.txt b/src/doc/book/listings/ch12-an-io-project/listing-12-19/output.txt new file mode 100644 index 000000000..9b2078cb8 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-19/output.txt @@ -0,0 +1,22 @@ +$ cargo test + Compiling minigrep v0.1.0 (file:///projects/minigrep) + Finished test [unoptimized + debuginfo] target(s) in 1.22s + Running unittests src/lib.rs (target/debug/deps/minigrep-9cd200e5fac0fc94) + +running 1 test +test tests::one_result ... ok + +test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s + + Running unittests src/main.rs (target/debug/deps/minigrep-9cd200e5fac0fc94) + +running 0 tests + +test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s + + Doc-tests minigrep + +running 0 tests + +test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s + diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-19/poem.txt b/src/doc/book/listings/ch12-an-io-project/listing-12-19/poem.txt new file mode 100644 index 000000000..870752731 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-19/poem.txt @@ -0,0 +1,9 @@ +I'm nobody! Who are you? +Are you nobody, too? +Then there's a pair of us - don't tell! +They'd banish us, you know. + +How dreary to be somebody! +How public, like a frog +To tell your name the livelong day +To an admiring bog! diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-19/src/lib.rs b/src/doc/book/listings/ch12-an-io-project/listing-12-19/src/lib.rs new file mode 100644 index 000000000..f5d3ffa9f --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-19/src/lib.rs @@ -0,0 +1,58 @@ +use std::error::Error; +use std::fs; + +pub struct Config { + pub query: String, + pub file_path: String, +} + +impl Config { + pub fn build(args: &[String]) -> Result { + if args.len() < 3 { + return Err("not enough arguments"); + } + + let query = args[1].clone(); + let file_path = args[2].clone(); + + Ok(Config { query, file_path }) + } +} + +pub fn run(config: Config) -> Result<(), Box> { + let contents = fs::read_to_string(config.file_path)?; + + Ok(()) +} + +// ANCHOR: here +// ANCHOR: ch13 +pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { + let mut results = Vec::new(); + + for line in contents.lines() { + if line.contains(query) { + results.push(line); + } + } + + results +} +// ANCHOR_END: ch13 +// ANCHOR_END: here + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn one_result() { + let query = "duct"; + let contents = "\ +Rust: +safe, fast, productive. +Pick three."; + + assert_eq!(vec!["safe, fast, productive."], search(query, contents)); + } +} diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-19/src/main.rs b/src/doc/book/listings/ch12-an-io-project/listing-12-19/src/main.rs new file mode 100644 index 000000000..a4f8a7411 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-19/src/main.rs @@ -0,0 +1,18 @@ +use std::env; +use std::process; + +use minigrep::Config; + +fn main() { + let args: Vec = env::args().collect(); + + let config = Config::build(&args).unwrap_or_else(|err| { + println!("Problem parsing arguments: {err}"); + process::exit(1); + }); + + if let Err(e) = minigrep::run(config) { + println!("Application error: {e}"); + process::exit(1); + } +} diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-20/Cargo.lock b/src/doc/book/listings/ch12-an-io-project/listing-12-20/Cargo.lock new file mode 100644 index 000000000..88bf82d16 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-20/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "minigrep" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-20/Cargo.toml b/src/doc/book/listings/ch12-an-io-project/listing-12-20/Cargo.toml new file mode 100644 index 000000000..64c2a3f52 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-20/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "minigrep" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-20/poem.txt b/src/doc/book/listings/ch12-an-io-project/listing-12-20/poem.txt new file mode 100644 index 000000000..870752731 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-20/poem.txt @@ -0,0 +1,9 @@ +I'm nobody! Who are you? +Are you nobody, too? +Then there's a pair of us - don't tell! +They'd banish us, you know. + +How dreary to be somebody! +How public, like a frog +To tell your name the livelong day +To an admiring bog! diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-20/src/lib.rs b/src/doc/book/listings/ch12-an-io-project/listing-12-20/src/lib.rs new file mode 100644 index 000000000..a757f7f55 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-20/src/lib.rs @@ -0,0 +1,76 @@ +use std::error::Error; +use std::fs; + +pub struct Config { + pub query: String, + pub file_path: String, +} + +impl Config { + pub fn build(args: &[String]) -> Result { + if args.len() < 3 { + return Err("not enough arguments"); + } + + let query = args[1].clone(); + let file_path = args[2].clone(); + + Ok(Config { query, file_path }) + } +} + +pub fn run(config: Config) -> Result<(), Box> { + let contents = fs::read_to_string(config.file_path)?; + + for line in search(&config.query, &contents) { + println!("{line}"); + } + + Ok(()) +} + +pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { + let mut results = Vec::new(); + + for line in contents.lines() { + if line.contains(query) { + results.push(line); + } + } + + results +} + +// ANCHOR: here +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn case_sensitive() { + let query = "duct"; + let contents = "\ +Rust: +safe, fast, productive. +Pick three. +Duct tape."; + + assert_eq!(vec!["safe, fast, productive."], search(query, contents)); + } + + #[test] + fn case_insensitive() { + let query = "rUsT"; + let contents = "\ +Rust: +safe, fast, productive. +Pick three. +Trust me."; + + assert_eq!( + vec!["Rust:", "Trust me."], + search_case_insensitive(query, contents) + ); + } +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-20/src/main.rs b/src/doc/book/listings/ch12-an-io-project/listing-12-20/src/main.rs new file mode 100644 index 000000000..a4f8a7411 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-20/src/main.rs @@ -0,0 +1,18 @@ +use std::env; +use std::process; + +use minigrep::Config; + +fn main() { + let args: Vec = env::args().collect(); + + let config = Config::build(&args).unwrap_or_else(|err| { + println!("Problem parsing arguments: {err}"); + process::exit(1); + }); + + if let Err(e) = minigrep::run(config) { + println!("Application error: {e}"); + process::exit(1); + } +} diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-21/Cargo.lock b/src/doc/book/listings/ch12-an-io-project/listing-12-21/Cargo.lock new file mode 100644 index 000000000..88bf82d16 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-21/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "minigrep" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-21/Cargo.toml b/src/doc/book/listings/ch12-an-io-project/listing-12-21/Cargo.toml new file mode 100644 index 000000000..64c2a3f52 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-21/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "minigrep" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-21/output.txt b/src/doc/book/listings/ch12-an-io-project/listing-12-21/output.txt new file mode 100644 index 000000000..dafeb7862 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-21/output.txt @@ -0,0 +1,23 @@ +$ cargo test + Compiling minigrep v0.1.0 (file:///projects/minigrep) + Finished test [unoptimized + debuginfo] target(s) in 1.33s + Running unittests src/lib.rs (target/debug/deps/minigrep-9cd200e5fac0fc94) + +running 2 tests +test tests::case_insensitive ... ok +test tests::case_sensitive ... ok + +test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s + + Running unittests src/main.rs (target/debug/deps/minigrep-9cd200e5fac0fc94) + +running 0 tests + +test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s + + Doc-tests minigrep + +running 0 tests + +test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s + diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-21/poem.txt b/src/doc/book/listings/ch12-an-io-project/listing-12-21/poem.txt new file mode 100644 index 000000000..870752731 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-21/poem.txt @@ -0,0 +1,9 @@ +I'm nobody! Who are you? +Are you nobody, too? +Then there's a pair of us - don't tell! +They'd banish us, you know. + +How dreary to be somebody! +How public, like a frog +To tell your name the livelong day +To an admiring bog! diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-21/src/lib.rs b/src/doc/book/listings/ch12-an-io-project/listing-12-21/src/lib.rs new file mode 100644 index 000000000..3aaa04082 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-21/src/lib.rs @@ -0,0 +1,92 @@ +use std::error::Error; +use std::fs; + +pub struct Config { + pub query: String, + pub file_path: String, +} + +impl Config { + pub fn build(args: &[String]) -> Result { + if args.len() < 3 { + return Err("not enough arguments"); + } + + let query = args[1].clone(); + let file_path = args[2].clone(); + + Ok(Config { query, file_path }) + } +} + +pub fn run(config: Config) -> Result<(), Box> { + let contents = fs::read_to_string(config.file_path)?; + + for line in search(&config.query, &contents) { + println!("{line}"); + } + + Ok(()) +} + +pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { + let mut results = Vec::new(); + + for line in contents.lines() { + if line.contains(query) { + results.push(line); + } + } + + results +} + +// ANCHOR: here +pub fn search_case_insensitive<'a>( + query: &str, + contents: &'a str, +) -> Vec<&'a str> { + let query = query.to_lowercase(); + let mut results = Vec::new(); + + for line in contents.lines() { + if line.to_lowercase().contains(&query) { + results.push(line); + } + } + + results +} +// ANCHOR_END: here + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn case_sensitive() { + let query = "duct"; + let contents = "\ +Rust: +safe, fast, productive. +Pick three. +Duct tape."; + + assert_eq!(vec!["safe, fast, productive."], search(query, contents)); + } + + #[test] + fn case_insensitive() { + let query = "rUsT"; + let contents = "\ +Rust: +safe, fast, productive. +Pick three. +Trust me."; + + assert_eq!( + vec!["Rust:", "Trust me."], + search_case_insensitive(query, contents) + ); + } +} diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-21/src/main.rs b/src/doc/book/listings/ch12-an-io-project/listing-12-21/src/main.rs new file mode 100644 index 000000000..a4f8a7411 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-21/src/main.rs @@ -0,0 +1,18 @@ +use std::env; +use std::process; + +use minigrep::Config; + +fn main() { + let args: Vec = env::args().collect(); + + let config = Config::build(&args).unwrap_or_else(|err| { + println!("Problem parsing arguments: {err}"); + process::exit(1); + }); + + if let Err(e) = minigrep::run(config) { + println!("Application error: {e}"); + process::exit(1); + } +} diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-22/Cargo.lock b/src/doc/book/listings/ch12-an-io-project/listing-12-22/Cargo.lock new file mode 100644 index 000000000..88bf82d16 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-22/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "minigrep" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-22/Cargo.toml b/src/doc/book/listings/ch12-an-io-project/listing-12-22/Cargo.toml new file mode 100644 index 000000000..64c2a3f52 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-22/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "minigrep" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-22/poem.txt b/src/doc/book/listings/ch12-an-io-project/listing-12-22/poem.txt new file mode 100644 index 000000000..870752731 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-22/poem.txt @@ -0,0 +1,9 @@ +I'm nobody! Who are you? +Are you nobody, too? +Then there's a pair of us - don't tell! +They'd banish us, you know. + +How dreary to be somebody! +How public, like a frog +To tell your name the livelong day +To an admiring bog! diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-22/src/lib.rs b/src/doc/book/listings/ch12-an-io-project/listing-12-22/src/lib.rs new file mode 100644 index 000000000..c3f4723f1 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-22/src/lib.rs @@ -0,0 +1,101 @@ +use std::error::Error; +use std::fs; + +// ANCHOR: here +pub struct Config { + pub query: String, + pub file_path: String, + pub ignore_case: bool, +} +// ANCHOR_END: here + +impl Config { + pub fn build(args: &[String]) -> Result { + if args.len() < 3 { + return Err("not enough arguments"); + } + + let query = args[1].clone(); + let file_path = args[2].clone(); + + Ok(Config { query, file_path }) + } +} + +// ANCHOR: there +pub fn run(config: Config) -> Result<(), Box> { + let contents = fs::read_to_string(config.file_path)?; + + let results = if config.ignore_case { + search_case_insensitive(&config.query, &contents) + } else { + search(&config.query, &contents) + }; + + for line in results { + println!("{line}"); + } + + Ok(()) +} +// ANCHOR_END: there + +pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { + let mut results = Vec::new(); + + for line in contents.lines() { + if line.contains(query) { + results.push(line); + } + } + + results +} + +pub fn search_case_insensitive<'a>( + query: &str, + contents: &'a str, +) -> Vec<&'a str> { + let query = query.to_lowercase(); + let mut results = Vec::new(); + + for line in contents.lines() { + if line.to_lowercase().contains(&query) { + results.push(line); + } + } + + results +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn case_sensitive() { + let query = "duct"; + let contents = "\ +Rust: +safe, fast, productive. +Pick three. +Duct tape."; + + assert_eq!(vec!["safe, fast, productive."], search(query, contents)); + } + + #[test] + fn case_insensitive() { + let query = "rUsT"; + let contents = "\ +Rust: +safe, fast, productive. +Pick three. +Trust me."; + + assert_eq!( + vec!["Rust:", "Trust me."], + search_case_insensitive(query, contents) + ); + } +} diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-22/src/main.rs b/src/doc/book/listings/ch12-an-io-project/listing-12-22/src/main.rs new file mode 100644 index 000000000..a4f8a7411 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-22/src/main.rs @@ -0,0 +1,18 @@ +use std::env; +use std::process; + +use minigrep::Config; + +fn main() { + let args: Vec = env::args().collect(); + + let config = Config::build(&args).unwrap_or_else(|err| { + println!("Problem parsing arguments: {err}"); + process::exit(1); + }); + + if let Err(e) = minigrep::run(config) { + println!("Application error: {e}"); + process::exit(1); + } +} diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-23/Cargo.lock b/src/doc/book/listings/ch12-an-io-project/listing-12-23/Cargo.lock new file mode 100644 index 000000000..88bf82d16 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-23/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "minigrep" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-23/Cargo.toml b/src/doc/book/listings/ch12-an-io-project/listing-12-23/Cargo.toml new file mode 100644 index 000000000..64c2a3f52 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-23/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "minigrep" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-23/output.txt b/src/doc/book/listings/ch12-an-io-project/listing-12-23/output.txt new file mode 100644 index 000000000..eaffc2f24 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-23/output.txt @@ -0,0 +1,6 @@ +$ cargo run -- to poem.txt + Compiling minigrep v0.1.0 (file:///projects/minigrep) + Finished dev [unoptimized + debuginfo] target(s) in 0.0s + Running `target/debug/minigrep to poem.txt` +Are you nobody, too? +How dreary to be somebody! diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-23/poem.txt b/src/doc/book/listings/ch12-an-io-project/listing-12-23/poem.txt new file mode 100644 index 000000000..870752731 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-23/poem.txt @@ -0,0 +1,9 @@ +I'm nobody! Who are you? +Are you nobody, too? +Then there's a pair of us - don't tell! +They'd banish us, you know. + +How dreary to be somebody! +How public, like a frog +To tell your name the livelong day +To an admiring bog! diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-23/src/lib.rs b/src/doc/book/listings/ch12-an-io-project/listing-12-23/src/lib.rs new file mode 100644 index 000000000..20eda2197 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-23/src/lib.rs @@ -0,0 +1,110 @@ +// ANCHOR: here +use std::env; +// --snip-- + +// ANCHOR_END: here +use std::error::Error; +use std::fs; + +pub struct Config { + pub query: String, + pub file_path: String, + pub ignore_case: bool, +} + +// ANCHOR: here +impl Config { + pub fn build(args: &[String]) -> Result { + if args.len() < 3 { + return Err("not enough arguments"); + } + + let query = args[1].clone(); + let file_path = args[2].clone(); + + let ignore_case = env::var("IGNORE_CASE").is_ok(); + + Ok(Config { + query, + file_path, + ignore_case, + }) + } +} +// ANCHOR_END: here + +pub fn run(config: Config) -> Result<(), Box> { + let contents = fs::read_to_string(config.file_path)?; + + let results = if config.ignore_case { + search_case_insensitive(&config.query, &contents) + } else { + search(&config.query, &contents) + }; + + for line in results { + println!("{line}"); + } + + Ok(()) +} + +pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { + let mut results = Vec::new(); + + for line in contents.lines() { + if line.contains(query) { + results.push(line); + } + } + + results +} + +pub fn search_case_insensitive<'a>( + query: &str, + contents: &'a str, +) -> Vec<&'a str> { + let query = query.to_lowercase(); + let mut results = Vec::new(); + + for line in contents.lines() { + if line.to_lowercase().contains(&query) { + results.push(line); + } + } + + results +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn case_sensitive() { + let query = "duct"; + let contents = "\ +Rust: +safe, fast, productive. +Pick three. +Duct tape."; + + assert_eq!(vec!["safe, fast, productive."], search(query, contents)); + } + + #[test] + fn case_insensitive() { + let query = "rUsT"; + let contents = "\ +Rust: +safe, fast, productive. +Pick three. +Trust me."; + + assert_eq!( + vec!["Rust:", "Trust me."], + search_case_insensitive(query, contents) + ); + } +} diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-23/src/main.rs b/src/doc/book/listings/ch12-an-io-project/listing-12-23/src/main.rs new file mode 100644 index 000000000..a4f8a7411 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-23/src/main.rs @@ -0,0 +1,18 @@ +use std::env; +use std::process; + +use minigrep::Config; + +fn main() { + let args: Vec = env::args().collect(); + + let config = Config::build(&args).unwrap_or_else(|err| { + println!("Problem parsing arguments: {err}"); + process::exit(1); + }); + + if let Err(e) = minigrep::run(config) { + println!("Application error: {e}"); + process::exit(1); + } +} diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-24/Cargo.lock b/src/doc/book/listings/ch12-an-io-project/listing-12-24/Cargo.lock new file mode 100644 index 000000000..88bf82d16 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-24/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "minigrep" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-24/Cargo.toml b/src/doc/book/listings/ch12-an-io-project/listing-12-24/Cargo.toml new file mode 100644 index 000000000..64c2a3f52 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-24/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "minigrep" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-24/poem.txt b/src/doc/book/listings/ch12-an-io-project/listing-12-24/poem.txt new file mode 100644 index 000000000..870752731 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-24/poem.txt @@ -0,0 +1,9 @@ +I'm nobody! Who are you? +Are you nobody, too? +Then there's a pair of us - don't tell! +They'd banish us, you know. + +How dreary to be somebody! +How public, like a frog +To tell your name the livelong day +To an admiring bog! diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-24/src/lib.rs b/src/doc/book/listings/ch12-an-io-project/listing-12-24/src/lib.rs new file mode 100644 index 000000000..292b09789 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-24/src/lib.rs @@ -0,0 +1,104 @@ +use std::env; +use std::error::Error; +use std::fs; + +pub struct Config { + pub query: String, + pub file_path: String, + pub ignore_case: bool, +} + +impl Config { + pub fn build(args: &[String]) -> Result { + if args.len() < 3 { + return Err("not enough arguments"); + } + + let query = args[1].clone(); + let file_path = args[2].clone(); + + let ignore_case = env::var("IGNORE_CASE").is_ok(); + + Ok(Config { + query, + file_path, + ignore_case, + }) + } +} + +pub fn run(config: Config) -> Result<(), Box> { + let contents = fs::read_to_string(config.file_path)?; + + let results = if config.ignore_case { + search_case_insensitive(&config.query, &contents) + } else { + search(&config.query, &contents) + }; + + for line in results { + println!("{line}"); + } + + Ok(()) +} + +pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { + let mut results = Vec::new(); + + for line in contents.lines() { + if line.contains(query) { + results.push(line); + } + } + + results +} + +pub fn search_case_insensitive<'a>( + query: &str, + contents: &'a str, +) -> Vec<&'a str> { + let query = query.to_lowercase(); + let mut results = Vec::new(); + + for line in contents.lines() { + if line.to_lowercase().contains(&query) { + results.push(line); + } + } + + results +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn case_sensitive() { + let query = "duct"; + let contents = "\ +Rust: +safe, fast, productive. +Pick three. +Duct tape."; + + assert_eq!(vec!["safe, fast, productive."], search(query, contents)); + } + + #[test] + fn case_insensitive() { + let query = "rUsT"; + let contents = "\ +Rust: +safe, fast, productive. +Pick three. +Trust me."; + + assert_eq!( + vec!["Rust:", "Trust me."], + search_case_insensitive(query, contents) + ); + } +} diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-24/src/main.rs b/src/doc/book/listings/ch12-an-io-project/listing-12-24/src/main.rs new file mode 100644 index 000000000..1278a6c17 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/listing-12-24/src/main.rs @@ -0,0 +1,20 @@ +use std::env; +use std::process; + +use minigrep::Config; + +// ANCHOR: here +fn main() { + let args: Vec = env::args().collect(); + + let config = Config::build(&args).unwrap_or_else(|err| { + eprintln!("Problem parsing arguments: {err}"); + process::exit(1); + }); + + if let Err(e) = minigrep::run(config) { + eprintln!("Application error: {e}"); + process::exit(1); + } +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch12-an-io-project/no-listing-01-handling-errors-in-main/Cargo.lock b/src/doc/book/listings/ch12-an-io-project/no-listing-01-handling-errors-in-main/Cargo.lock new file mode 100644 index 000000000..88bf82d16 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/no-listing-01-handling-errors-in-main/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "minigrep" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch12-an-io-project/no-listing-01-handling-errors-in-main/Cargo.toml b/src/doc/book/listings/ch12-an-io-project/no-listing-01-handling-errors-in-main/Cargo.toml new file mode 100644 index 000000000..64c2a3f52 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/no-listing-01-handling-errors-in-main/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "minigrep" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch12-an-io-project/no-listing-01-handling-errors-in-main/poem.txt b/src/doc/book/listings/ch12-an-io-project/no-listing-01-handling-errors-in-main/poem.txt new file mode 100644 index 000000000..870752731 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/no-listing-01-handling-errors-in-main/poem.txt @@ -0,0 +1,9 @@ +I'm nobody! Who are you? +Are you nobody, too? +Then there's a pair of us - don't tell! +They'd banish us, you know. + +How dreary to be somebody! +How public, like a frog +To tell your name the livelong day +To an admiring bog! diff --git a/src/doc/book/listings/ch12-an-io-project/no-listing-01-handling-errors-in-main/src/main.rs b/src/doc/book/listings/ch12-an-io-project/no-listing-01-handling-errors-in-main/src/main.rs new file mode 100644 index 000000000..e51319efe --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/no-listing-01-handling-errors-in-main/src/main.rs @@ -0,0 +1,53 @@ +use std::env; +use std::error::Error; +use std::fs; +use std::process; + +// ANCHOR: here +fn main() { + // --snip-- + + // ANCHOR_END: here + let args: Vec = env::args().collect(); + + let config = Config::build(&args).unwrap_or_else(|err| { + println!("Problem parsing arguments: {err}"); + process::exit(1); + }); + + // ANCHOR: here + println!("Searching for {}", config.query); + println!("In file {}", config.file_path); + + if let Err(e) = run(config) { + println!("Application error: {e}"); + process::exit(1); + } +} +// ANCHOR_END: here + +fn run(config: Config) -> Result<(), Box> { + let contents = fs::read_to_string(config.file_path)?; + + println!("With text:\n{contents}"); + + Ok(()) +} + +struct Config { + query: String, + file_path: String, +} + +impl Config { + fn build(args: &[String]) -> Result { + if args.len() < 3 { + return Err("not enough arguments"); + } + + let query = args[1].clone(); + let file_path = args[2].clone(); + + Ok(Config { query, file_path }) + } +} diff --git a/src/doc/book/listings/ch12-an-io-project/no-listing-02-using-search-in-run/Cargo.lock b/src/doc/book/listings/ch12-an-io-project/no-listing-02-using-search-in-run/Cargo.lock new file mode 100644 index 000000000..88bf82d16 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/no-listing-02-using-search-in-run/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "minigrep" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch12-an-io-project/no-listing-02-using-search-in-run/Cargo.toml b/src/doc/book/listings/ch12-an-io-project/no-listing-02-using-search-in-run/Cargo.toml new file mode 100644 index 000000000..64c2a3f52 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/no-listing-02-using-search-in-run/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "minigrep" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch12-an-io-project/no-listing-02-using-search-in-run/output.txt b/src/doc/book/listings/ch12-an-io-project/no-listing-02-using-search-in-run/output.txt new file mode 100644 index 000000000..a5a4ef8c2 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/no-listing-02-using-search-in-run/output.txt @@ -0,0 +1,5 @@ +$ cargo run -- frog poem.txt + Compiling minigrep v0.1.0 (file:///projects/minigrep) + Finished dev [unoptimized + debuginfo] target(s) in 0.38s + Running `target/debug/minigrep frog poem.txt` +How public, like a frog diff --git a/src/doc/book/listings/ch12-an-io-project/no-listing-02-using-search-in-run/poem.txt b/src/doc/book/listings/ch12-an-io-project/no-listing-02-using-search-in-run/poem.txt new file mode 100644 index 000000000..870752731 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/no-listing-02-using-search-in-run/poem.txt @@ -0,0 +1,9 @@ +I'm nobody! Who are you? +Are you nobody, too? +Then there's a pair of us - don't tell! +They'd banish us, you know. + +How dreary to be somebody! +How public, like a frog +To tell your name the livelong day +To an admiring bog! diff --git a/src/doc/book/listings/ch12-an-io-project/no-listing-02-using-search-in-run/src/lib.rs b/src/doc/book/listings/ch12-an-io-project/no-listing-02-using-search-in-run/src/lib.rs new file mode 100644 index 000000000..e06eae4cd --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/no-listing-02-using-search-in-run/src/lib.rs @@ -0,0 +1,60 @@ +use std::error::Error; +use std::fs; + +pub struct Config { + pub query: String, + pub file_path: String, +} + +impl Config { + pub fn build(args: &[String]) -> Result { + if args.len() < 3 { + return Err("not enough arguments"); + } + + let query = args[1].clone(); + let file_path = args[2].clone(); + + Ok(Config { query, file_path }) + } +} + +// ANCHOR: here +pub fn run(config: Config) -> Result<(), Box> { + let contents = fs::read_to_string(config.file_path)?; + + for line in search(&config.query, &contents) { + println!("{line}"); + } + + Ok(()) +} +// ANCHOR_END: here + +pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { + let mut results = Vec::new(); + + for line in contents.lines() { + if line.contains(query) { + results.push(line); + } + } + + results +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn one_result() { + let query = "duct"; + let contents = "\ +Rust: +safe, fast, productive. +Pick three."; + + assert_eq!(vec!["safe, fast, productive."], search(query, contents)); + } +} diff --git a/src/doc/book/listings/ch12-an-io-project/no-listing-02-using-search-in-run/src/main.rs b/src/doc/book/listings/ch12-an-io-project/no-listing-02-using-search-in-run/src/main.rs new file mode 100644 index 000000000..a4f8a7411 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/no-listing-02-using-search-in-run/src/main.rs @@ -0,0 +1,18 @@ +use std::env; +use std::process; + +use minigrep::Config; + +fn main() { + let args: Vec = env::args().collect(); + + let config = Config::build(&args).unwrap_or_else(|err| { + println!("Problem parsing arguments: {err}"); + process::exit(1); + }); + + if let Err(e) = minigrep::run(config) { + println!("Application error: {e}"); + process::exit(1); + } +} diff --git a/src/doc/book/listings/ch12-an-io-project/output-only-01-with-args/Cargo.lock b/src/doc/book/listings/ch12-an-io-project/output-only-01-with-args/Cargo.lock new file mode 100644 index 000000000..88bf82d16 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/output-only-01-with-args/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "minigrep" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch12-an-io-project/output-only-01-with-args/Cargo.toml b/src/doc/book/listings/ch12-an-io-project/output-only-01-with-args/Cargo.toml new file mode 100644 index 000000000..64c2a3f52 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/output-only-01-with-args/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "minigrep" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch12-an-io-project/output-only-01-with-args/output.txt b/src/doc/book/listings/ch12-an-io-project/output-only-01-with-args/output.txt new file mode 100644 index 000000000..b48bb0e10 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/output-only-01-with-args/output.txt @@ -0,0 +1,9 @@ +$ cargo run -- needle haystack + Compiling minigrep v0.1.0 (file:///projects/minigrep) + Finished dev [unoptimized + debuginfo] target(s) in 1.57s + Running `target/debug/minigrep needle haystack` +[src/main.rs:5] args = [ + "target/debug/minigrep", + "needle", + "haystack", +] diff --git a/src/doc/book/listings/ch12-an-io-project/output-only-01-with-args/src/main.rs b/src/doc/book/listings/ch12-an-io-project/output-only-01-with-args/src/main.rs new file mode 100644 index 000000000..ae7def53d --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/output-only-01-with-args/src/main.rs @@ -0,0 +1,6 @@ +use std::env; + +fn main() { + let args: Vec = env::args().collect(); + dbg!(args); +} diff --git a/src/doc/book/listings/ch12-an-io-project/output-only-02-missing-lifetimes/Cargo.lock b/src/doc/book/listings/ch12-an-io-project/output-only-02-missing-lifetimes/Cargo.lock new file mode 100644 index 000000000..88bf82d16 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/output-only-02-missing-lifetimes/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "minigrep" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch12-an-io-project/output-only-02-missing-lifetimes/Cargo.toml b/src/doc/book/listings/ch12-an-io-project/output-only-02-missing-lifetimes/Cargo.toml new file mode 100644 index 000000000..64c2a3f52 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/output-only-02-missing-lifetimes/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "minigrep" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch12-an-io-project/output-only-02-missing-lifetimes/output.txt b/src/doc/book/listings/ch12-an-io-project/output-only-02-missing-lifetimes/output.txt new file mode 100644 index 000000000..93116dd5e --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/output-only-02-missing-lifetimes/output.txt @@ -0,0 +1,16 @@ +$ cargo build + Compiling minigrep v0.1.0 (file:///projects/minigrep) +error[E0106]: missing lifetime specifier + --> src/lib.rs:28:51 + | +28 | pub fn search(query: &str, contents: &str) -> Vec<&str> { + | ---- ---- ^ expected named lifetime parameter + | + = help: this function's return type contains a borrowed value, but the signature does not say whether it is borrowed from `query` or `contents` +help: consider introducing a named lifetime parameter + | +28 | pub fn search<'a>(query: &'a str, contents: &'a str) -> Vec<&'a str> { + | ++++ ++ ++ ++ + +For more information about this error, try `rustc --explain E0106`. +error: could not compile `minigrep` due to previous error diff --git a/src/doc/book/listings/ch12-an-io-project/output-only-02-missing-lifetimes/poem.txt b/src/doc/book/listings/ch12-an-io-project/output-only-02-missing-lifetimes/poem.txt new file mode 100644 index 000000000..870752731 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/output-only-02-missing-lifetimes/poem.txt @@ -0,0 +1,9 @@ +I'm nobody! Who are you? +Are you nobody, too? +Then there's a pair of us - don't tell! +They'd banish us, you know. + +How dreary to be somebody! +How public, like a frog +To tell your name the livelong day +To an admiring bog! diff --git a/src/doc/book/listings/ch12-an-io-project/output-only-02-missing-lifetimes/src/lib.rs b/src/doc/book/listings/ch12-an-io-project/output-only-02-missing-lifetimes/src/lib.rs new file mode 100644 index 000000000..df623bdea --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/output-only-02-missing-lifetimes/src/lib.rs @@ -0,0 +1,48 @@ +use std::error::Error; +use std::fs; + +pub struct Config { + pub query: String, + pub file_path: String, +} + +impl Config { + pub fn build(args: &[String]) -> Result { + if args.len() < 3 { + return Err("not enough arguments"); + } + + let query = args[1].clone(); + let file_path = args[2].clone(); + + Ok(Config { query, file_path }) + } +} + +pub fn run(config: Config) -> Result<(), Box> { + let contents = fs::read_to_string(config.file_path)?; + + Ok(()) +} + +// ANCHOR: here +pub fn search(query: &str, contents: &str) -> Vec<&str> { + vec![] +} +// ANCHOR_END: here + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn one_result() { + let query = "duct"; + let contents = "\ +Rust: +safe, fast, productive. +Pick three."; + + assert_eq!(vec!["safe, fast, productive."], search(query, contents)); + } +} diff --git a/src/doc/book/listings/ch12-an-io-project/output-only-02-missing-lifetimes/src/main.rs b/src/doc/book/listings/ch12-an-io-project/output-only-02-missing-lifetimes/src/main.rs new file mode 100644 index 000000000..a4f8a7411 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/output-only-02-missing-lifetimes/src/main.rs @@ -0,0 +1,18 @@ +use std::env; +use std::process; + +use minigrep::Config; + +fn main() { + let args: Vec = env::args().collect(); + + let config = Config::build(&args).unwrap_or_else(|err| { + println!("Problem parsing arguments: {err}"); + process::exit(1); + }); + + if let Err(e) = minigrep::run(config) { + println!("Application error: {e}"); + process::exit(1); + } +} diff --git a/src/doc/book/listings/ch12-an-io-project/output-only-03-multiple-matches/Cargo.lock b/src/doc/book/listings/ch12-an-io-project/output-only-03-multiple-matches/Cargo.lock new file mode 100644 index 000000000..88bf82d16 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/output-only-03-multiple-matches/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "minigrep" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch12-an-io-project/output-only-03-multiple-matches/Cargo.toml b/src/doc/book/listings/ch12-an-io-project/output-only-03-multiple-matches/Cargo.toml new file mode 100644 index 000000000..64c2a3f52 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/output-only-03-multiple-matches/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "minigrep" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch12-an-io-project/output-only-03-multiple-matches/output.txt b/src/doc/book/listings/ch12-an-io-project/output-only-03-multiple-matches/output.txt new file mode 100644 index 000000000..b46835766 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/output-only-03-multiple-matches/output.txt @@ -0,0 +1,7 @@ +$ cargo run -- body poem.txt + Compiling minigrep v0.1.0 (file:///projects/minigrep) + Finished dev [unoptimized + debuginfo] target(s) in 0.0s + Running `target/debug/minigrep body poem.txt` +I'm nobody! Who are you? +Are you nobody, too? +How dreary to be somebody! diff --git a/src/doc/book/listings/ch12-an-io-project/output-only-03-multiple-matches/poem.txt b/src/doc/book/listings/ch12-an-io-project/output-only-03-multiple-matches/poem.txt new file mode 100644 index 000000000..870752731 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/output-only-03-multiple-matches/poem.txt @@ -0,0 +1,9 @@ +I'm nobody! Who are you? +Are you nobody, too? +Then there's a pair of us - don't tell! +They'd banish us, you know. + +How dreary to be somebody! +How public, like a frog +To tell your name the livelong day +To an admiring bog! diff --git a/src/doc/book/listings/ch12-an-io-project/output-only-03-multiple-matches/src/lib.rs b/src/doc/book/listings/ch12-an-io-project/output-only-03-multiple-matches/src/lib.rs new file mode 100644 index 000000000..e06eae4cd --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/output-only-03-multiple-matches/src/lib.rs @@ -0,0 +1,60 @@ +use std::error::Error; +use std::fs; + +pub struct Config { + pub query: String, + pub file_path: String, +} + +impl Config { + pub fn build(args: &[String]) -> Result { + if args.len() < 3 { + return Err("not enough arguments"); + } + + let query = args[1].clone(); + let file_path = args[2].clone(); + + Ok(Config { query, file_path }) + } +} + +// ANCHOR: here +pub fn run(config: Config) -> Result<(), Box> { + let contents = fs::read_to_string(config.file_path)?; + + for line in search(&config.query, &contents) { + println!("{line}"); + } + + Ok(()) +} +// ANCHOR_END: here + +pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { + let mut results = Vec::new(); + + for line in contents.lines() { + if line.contains(query) { + results.push(line); + } + } + + results +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn one_result() { + let query = "duct"; + let contents = "\ +Rust: +safe, fast, productive. +Pick three."; + + assert_eq!(vec!["safe, fast, productive."], search(query, contents)); + } +} diff --git a/src/doc/book/listings/ch12-an-io-project/output-only-03-multiple-matches/src/main.rs b/src/doc/book/listings/ch12-an-io-project/output-only-03-multiple-matches/src/main.rs new file mode 100644 index 000000000..a4f8a7411 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/output-only-03-multiple-matches/src/main.rs @@ -0,0 +1,18 @@ +use std::env; +use std::process; + +use minigrep::Config; + +fn main() { + let args: Vec = env::args().collect(); + + let config = Config::build(&args).unwrap_or_else(|err| { + println!("Problem parsing arguments: {err}"); + process::exit(1); + }); + + if let Err(e) = minigrep::run(config) { + println!("Application error: {e}"); + process::exit(1); + } +} diff --git a/src/doc/book/listings/ch12-an-io-project/output-only-04-no-matches/Cargo.lock b/src/doc/book/listings/ch12-an-io-project/output-only-04-no-matches/Cargo.lock new file mode 100644 index 000000000..88bf82d16 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/output-only-04-no-matches/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "minigrep" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch12-an-io-project/output-only-04-no-matches/Cargo.toml b/src/doc/book/listings/ch12-an-io-project/output-only-04-no-matches/Cargo.toml new file mode 100644 index 000000000..64c2a3f52 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/output-only-04-no-matches/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "minigrep" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch12-an-io-project/output-only-04-no-matches/output.txt b/src/doc/book/listings/ch12-an-io-project/output-only-04-no-matches/output.txt new file mode 100644 index 000000000..a53624f83 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/output-only-04-no-matches/output.txt @@ -0,0 +1,4 @@ +$ cargo run -- monomorphization poem.txt + Compiling minigrep v0.1.0 (file:///projects/minigrep) + Finished dev [unoptimized + debuginfo] target(s) in 0.0s + Running `target/debug/minigrep monomorphization poem.txt` diff --git a/src/doc/book/listings/ch12-an-io-project/output-only-04-no-matches/poem.txt b/src/doc/book/listings/ch12-an-io-project/output-only-04-no-matches/poem.txt new file mode 100644 index 000000000..870752731 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/output-only-04-no-matches/poem.txt @@ -0,0 +1,9 @@ +I'm nobody! Who are you? +Are you nobody, too? +Then there's a pair of us - don't tell! +They'd banish us, you know. + +How dreary to be somebody! +How public, like a frog +To tell your name the livelong day +To an admiring bog! diff --git a/src/doc/book/listings/ch12-an-io-project/output-only-04-no-matches/src/lib.rs b/src/doc/book/listings/ch12-an-io-project/output-only-04-no-matches/src/lib.rs new file mode 100644 index 000000000..e06eae4cd --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/output-only-04-no-matches/src/lib.rs @@ -0,0 +1,60 @@ +use std::error::Error; +use std::fs; + +pub struct Config { + pub query: String, + pub file_path: String, +} + +impl Config { + pub fn build(args: &[String]) -> Result { + if args.len() < 3 { + return Err("not enough arguments"); + } + + let query = args[1].clone(); + let file_path = args[2].clone(); + + Ok(Config { query, file_path }) + } +} + +// ANCHOR: here +pub fn run(config: Config) -> Result<(), Box> { + let contents = fs::read_to_string(config.file_path)?; + + for line in search(&config.query, &contents) { + println!("{line}"); + } + + Ok(()) +} +// ANCHOR_END: here + +pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { + let mut results = Vec::new(); + + for line in contents.lines() { + if line.contains(query) { + results.push(line); + } + } + + results +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn one_result() { + let query = "duct"; + let contents = "\ +Rust: +safe, fast, productive. +Pick three."; + + assert_eq!(vec!["safe, fast, productive."], search(query, contents)); + } +} diff --git a/src/doc/book/listings/ch12-an-io-project/output-only-04-no-matches/src/main.rs b/src/doc/book/listings/ch12-an-io-project/output-only-04-no-matches/src/main.rs new file mode 100644 index 000000000..a4f8a7411 --- /dev/null +++ b/src/doc/book/listings/ch12-an-io-project/output-only-04-no-matches/src/main.rs @@ -0,0 +1,18 @@ +use std::env; +use std::process; + +use minigrep::Config; + +fn main() { + let args: Vec = env::args().collect(); + + let config = Config::build(&args).unwrap_or_else(|err| { + println!("Problem parsing arguments: {err}"); + process::exit(1); + }); + + if let Err(e) = minigrep::run(config) { + println!("Application error: {e}"); + process::exit(1); + } +} diff --git a/src/doc/book/listings/ch13-functional-features/listing-12-23-reproduced/Cargo.lock b/src/doc/book/listings/ch13-functional-features/listing-12-23-reproduced/Cargo.lock new file mode 100644 index 000000000..88bf82d16 --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-12-23-reproduced/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "minigrep" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch13-functional-features/listing-12-23-reproduced/Cargo.toml b/src/doc/book/listings/ch13-functional-features/listing-12-23-reproduced/Cargo.toml new file mode 100644 index 000000000..64c2a3f52 --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-12-23-reproduced/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "minigrep" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch13-functional-features/listing-12-23-reproduced/poem.txt b/src/doc/book/listings/ch13-functional-features/listing-12-23-reproduced/poem.txt new file mode 100644 index 000000000..870752731 --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-12-23-reproduced/poem.txt @@ -0,0 +1,9 @@ +I'm nobody! Who are you? +Are you nobody, too? +Then there's a pair of us - don't tell! +They'd banish us, you know. + +How dreary to be somebody! +How public, like a frog +To tell your name the livelong day +To an admiring bog! diff --git a/src/doc/book/listings/ch13-functional-features/listing-12-23-reproduced/src/lib.rs b/src/doc/book/listings/ch13-functional-features/listing-12-23-reproduced/src/lib.rs new file mode 100644 index 000000000..e54343d24 --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-12-23-reproduced/src/lib.rs @@ -0,0 +1,106 @@ +use std::env; +use std::error::Error; +use std::fs; + +pub struct Config { + pub query: String, + pub file_path: String, + pub ignore_case: bool, +} + +// ANCHOR: ch13 +impl Config { + pub fn build(args: &[String]) -> Result { + if args.len() < 3 { + return Err("not enough arguments"); + } + + let query = args[1].clone(); + let file_path = args[2].clone(); + + let ignore_case = env::var("IGNORE_CASE").is_ok(); + + Ok(Config { + query, + file_path, + ignore_case, + }) + } +} +// ANCHOR_END: ch13 + +pub fn run(config: Config) -> Result<(), Box> { + let contents = fs::read_to_string(config.file_path)?; + + let results = if config.ignore_case { + search_case_insensitive(&config.query, &contents) + } else { + search(&config.query, &contents) + }; + + for line in results { + println!("{line}"); + } + + Ok(()) +} + +pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { + let mut results = Vec::new(); + + for line in contents.lines() { + if line.contains(query) { + results.push(line); + } + } + + results +} + +pub fn search_case_insensitive<'a>( + query: &str, + contents: &'a str, +) -> Vec<&'a str> { + let query = query.to_lowercase(); + let mut results = Vec::new(); + + for line in contents.lines() { + if line.to_lowercase().contains(&query) { + results.push(line); + } + } + + results +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn case_sensitive() { + let query = "duct"; + let contents = "\ +Rust: +safe, fast, productive. +Pick three. +Duct tape."; + + assert_eq!(vec!["safe, fast, productive."], search(query, contents)); + } + + #[test] + fn case_insensitive() { + let query = "rUsT"; + let contents = "\ +Rust: +safe, fast, productive. +Pick three. +Trust me."; + + assert_eq!( + vec!["Rust:", "Trust me."], + search_case_insensitive(query, contents) + ); + } +} diff --git a/src/doc/book/listings/ch13-functional-features/listing-12-23-reproduced/src/main.rs b/src/doc/book/listings/ch13-functional-features/listing-12-23-reproduced/src/main.rs new file mode 100644 index 000000000..a4f8a7411 --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-12-23-reproduced/src/main.rs @@ -0,0 +1,18 @@ +use std::env; +use std::process; + +use minigrep::Config; + +fn main() { + let args: Vec = env::args().collect(); + + let config = Config::build(&args).unwrap_or_else(|err| { + println!("Problem parsing arguments: {err}"); + process::exit(1); + }); + + if let Err(e) = minigrep::run(config) { + println!("Application error: {e}"); + process::exit(1); + } +} diff --git a/src/doc/book/listings/ch13-functional-features/listing-12-24-reproduced/Cargo.lock b/src/doc/book/listings/ch13-functional-features/listing-12-24-reproduced/Cargo.lock new file mode 100644 index 000000000..88bf82d16 --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-12-24-reproduced/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "minigrep" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch13-functional-features/listing-12-24-reproduced/Cargo.toml b/src/doc/book/listings/ch13-functional-features/listing-12-24-reproduced/Cargo.toml new file mode 100644 index 000000000..64c2a3f52 --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-12-24-reproduced/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "minigrep" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch13-functional-features/listing-12-24-reproduced/poem.txt b/src/doc/book/listings/ch13-functional-features/listing-12-24-reproduced/poem.txt new file mode 100644 index 000000000..870752731 --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-12-24-reproduced/poem.txt @@ -0,0 +1,9 @@ +I'm nobody! Who are you? +Are you nobody, too? +Then there's a pair of us - don't tell! +They'd banish us, you know. + +How dreary to be somebody! +How public, like a frog +To tell your name the livelong day +To an admiring bog! diff --git a/src/doc/book/listings/ch13-functional-features/listing-12-24-reproduced/src/lib.rs b/src/doc/book/listings/ch13-functional-features/listing-12-24-reproduced/src/lib.rs new file mode 100644 index 000000000..292b09789 --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-12-24-reproduced/src/lib.rs @@ -0,0 +1,104 @@ +use std::env; +use std::error::Error; +use std::fs; + +pub struct Config { + pub query: String, + pub file_path: String, + pub ignore_case: bool, +} + +impl Config { + pub fn build(args: &[String]) -> Result { + if args.len() < 3 { + return Err("not enough arguments"); + } + + let query = args[1].clone(); + let file_path = args[2].clone(); + + let ignore_case = env::var("IGNORE_CASE").is_ok(); + + Ok(Config { + query, + file_path, + ignore_case, + }) + } +} + +pub fn run(config: Config) -> Result<(), Box> { + let contents = fs::read_to_string(config.file_path)?; + + let results = if config.ignore_case { + search_case_insensitive(&config.query, &contents) + } else { + search(&config.query, &contents) + }; + + for line in results { + println!("{line}"); + } + + Ok(()) +} + +pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { + let mut results = Vec::new(); + + for line in contents.lines() { + if line.contains(query) { + results.push(line); + } + } + + results +} + +pub fn search_case_insensitive<'a>( + query: &str, + contents: &'a str, +) -> Vec<&'a str> { + let query = query.to_lowercase(); + let mut results = Vec::new(); + + for line in contents.lines() { + if line.to_lowercase().contains(&query) { + results.push(line); + } + } + + results +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn case_sensitive() { + let query = "duct"; + let contents = "\ +Rust: +safe, fast, productive. +Pick three. +Duct tape."; + + assert_eq!(vec!["safe, fast, productive."], search(query, contents)); + } + + #[test] + fn case_insensitive() { + let query = "rUsT"; + let contents = "\ +Rust: +safe, fast, productive. +Pick three. +Trust me."; + + assert_eq!( + vec!["Rust:", "Trust me."], + search_case_insensitive(query, contents) + ); + } +} diff --git a/src/doc/book/listings/ch13-functional-features/listing-12-24-reproduced/src/main.rs b/src/doc/book/listings/ch13-functional-features/listing-12-24-reproduced/src/main.rs new file mode 100644 index 000000000..f9d179c8c --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-12-24-reproduced/src/main.rs @@ -0,0 +1,24 @@ +use std::env; +use std::process; + +use minigrep::Config; + +// ANCHOR: ch13 +fn main() { + let args: Vec = env::args().collect(); + + let config = Config::build(&args).unwrap_or_else(|err| { + eprintln!("Problem parsing arguments: {err}"); + process::exit(1); + }); + + // --snip-- + // ANCHOR_END: ch13 + + if let Err(e) = minigrep::run(config) { + eprintln!("Application error: {e}"); + process::exit(1); + } + // ANCHOR: ch13 +} +// ANCHOR_END: ch13 diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-01/Cargo.lock b/src/doc/book/listings/ch13-functional-features/listing-13-01/Cargo.lock new file mode 100644 index 000000000..6f974d1ba --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-01/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "shirt-company" +version = "0.1.0" diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-01/Cargo.toml b/src/doc/book/listings/ch13-functional-features/listing-13-01/Cargo.toml new file mode 100644 index 000000000..1eb392dfa --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-01/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "shirt-company" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-01/output.txt b/src/doc/book/listings/ch13-functional-features/listing-13-01/output.txt new file mode 100644 index 000000000..b64a4d8dc --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-01/output.txt @@ -0,0 +1,6 @@ +$ cargo run + Compiling shirt-company v0.1.0 (file:///projects/shirt-company) + Finished dev [unoptimized + debuginfo] target(s) in 0.27s + Running `target/debug/shirt-company` +The user with preference Some(Red) gets Red +The user with preference None gets Blue diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-01/src/main.rs b/src/doc/book/listings/ch13-functional-features/listing-13-01/src/main.rs new file mode 100644 index 000000000..2c87d6965 --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-01/src/main.rs @@ -0,0 +1,52 @@ +#[derive(Debug, PartialEq, Copy, Clone)] +enum ShirtColor { + Red, + Blue, +} + +struct Inventory { + shirts: Vec, +} + +impl Inventory { + fn giveaway(&self, user_preference: Option) -> ShirtColor { + user_preference.unwrap_or_else(|| self.most_stocked()) + } + + fn most_stocked(&self) -> ShirtColor { + let mut num_red = 0; + let mut num_blue = 0; + + for color in &self.shirts { + match color { + ShirtColor::Red => num_red += 1, + ShirtColor::Blue => num_blue += 1, + } + } + if num_red > num_blue { + ShirtColor::Red + } else { + ShirtColor::Blue + } + } +} + +fn main() { + let store = Inventory { + shirts: vec![ShirtColor::Blue, ShirtColor::Red, ShirtColor::Blue], + }; + + let user_pref1 = Some(ShirtColor::Red); + let giveaway1 = store.giveaway(user_pref1); + println!( + "The user with preference {:?} gets {:?}", + user_pref1, giveaway1 + ); + + let user_pref2 = None; + let giveaway2 = store.giveaway(user_pref2); + println!( + "The user with preference {:?} gets {:?}", + user_pref2, giveaway2 + ); +} diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-02/Cargo.lock b/src/doc/book/listings/ch13-functional-features/listing-13-02/Cargo.lock new file mode 100644 index 000000000..75ff09e51 --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-02/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "workout-app" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-02/Cargo.toml b/src/doc/book/listings/ch13-functional-features/listing-13-02/Cargo.toml new file mode 100644 index 000000000..f09a737d4 --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-02/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "workout-app" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-02/src/main.rs b/src/doc/book/listings/ch13-functional-features/listing-13-02/src/main.rs new file mode 100644 index 000000000..b3f4cc2c2 --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-02/src/main.rs @@ -0,0 +1,33 @@ +use std::thread; +use std::time::Duration; + +fn generate_workout(intensity: u32, random_number: u32) { + // ANCHOR: here + let expensive_closure = |num: u32| -> u32 { + println!("calculating slowly..."); + thread::sleep(Duration::from_secs(2)); + num + }; + // ANCHOR_END: here + + if intensity < 25 { + println!("Today, do {} pushups!", expensive_closure(intensity)); + println!("Next, do {} situps!", expensive_closure(intensity)); + } else { + if random_number == 3 { + println!("Take a break today! Remember to stay hydrated!"); + } else { + println!( + "Today, run for {} minutes!", + expensive_closure(intensity) + ); + } + } +} + +fn main() { + let simulated_user_specified_value = 10; + let simulated_random_number = 7; + + generate_workout(simulated_user_specified_value, simulated_random_number); +} diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-03/Cargo.lock b/src/doc/book/listings/ch13-functional-features/listing-13-03/Cargo.lock new file mode 100644 index 000000000..c190d3a41 --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-03/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "closure-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-03/Cargo.toml b/src/doc/book/listings/ch13-functional-features/listing-13-03/Cargo.toml new file mode 100644 index 000000000..914c4cfaa --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-03/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "closure-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-03/output.txt b/src/doc/book/listings/ch13-functional-features/listing-13-03/output.txt new file mode 100644 index 000000000..37d83618a --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-03/output.txt @@ -0,0 +1,12 @@ +$ cargo run + Compiling closure-example v0.1.0 (file:///projects/closure-example) +error[E0308]: mismatched types + --> src/main.rs:5:29 + | +5 | let n = example_closure(5); + | ^- help: try using a conversion method: `.to_string()` + | | + | expected struct `String`, found integer + +For more information about this error, try `rustc --explain E0308`. +error: could not compile `closure-example` due to previous error diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-03/src/main.rs b/src/doc/book/listings/ch13-functional-features/listing-13-03/src/main.rs new file mode 100644 index 000000000..ebb2489bf --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-03/src/main.rs @@ -0,0 +1,8 @@ +fn main() { + // ANCHOR: here + let example_closure = |x| x; + + let s = example_closure(String::from("hello")); + let n = example_closure(5); + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-04/Cargo.lock b/src/doc/book/listings/ch13-functional-features/listing-13-04/Cargo.lock new file mode 100644 index 000000000..75ff09e51 --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-04/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "workout-app" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-04/Cargo.toml b/src/doc/book/listings/ch13-functional-features/listing-13-04/Cargo.toml new file mode 100644 index 000000000..914c4cfaa --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-04/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "closure-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-04/output.txt b/src/doc/book/listings/ch13-functional-features/listing-13-04/output.txt new file mode 100644 index 000000000..64d763b51 --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-04/output.txt @@ -0,0 +1,8 @@ +$ cargo run + Compiling closure-example v0.1.0 (file:///projects/closure-example) + Finished dev [unoptimized + debuginfo] target(s) in 0.43s + Running `target/debug/closure-example` +Before defining closure: [1, 2, 3] +Before calling closure: [1, 2, 3] +From closure: [1, 2, 3] +After calling closure: [1, 2, 3] diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-04/src/main.rs b/src/doc/book/listings/ch13-functional-features/listing-13-04/src/main.rs new file mode 100644 index 000000000..43b91bb30 --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-04/src/main.rs @@ -0,0 +1,10 @@ +fn main() { + let list = vec![1, 2, 3]; + println!("Before defining closure: {:?}", list); + + let only_borrows = || println!("From closure: {:?}", list); + + println!("Before calling closure: {:?}", list); + only_borrows(); + println!("After calling closure: {:?}", list); +} diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-05/Cargo.lock b/src/doc/book/listings/ch13-functional-features/listing-13-05/Cargo.lock new file mode 100644 index 000000000..75ff09e51 --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-05/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "workout-app" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-05/Cargo.toml b/src/doc/book/listings/ch13-functional-features/listing-13-05/Cargo.toml new file mode 100644 index 000000000..914c4cfaa --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-05/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "closure-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-05/output.txt b/src/doc/book/listings/ch13-functional-features/listing-13-05/output.txt new file mode 100644 index 000000000..ce0ad5e37 --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-05/output.txt @@ -0,0 +1,6 @@ +$ cargo run + Compiling closure-example v0.1.0 (file:///projects/closure-example) + Finished dev [unoptimized + debuginfo] target(s) in 0.43s + Running `target/debug/closure-example` +Before defining closure: [1, 2, 3] +After calling closure: [1, 2, 3, 7] diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-05/src/main.rs b/src/doc/book/listings/ch13-functional-features/listing-13-05/src/main.rs new file mode 100644 index 000000000..37f8130e2 --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-05/src/main.rs @@ -0,0 +1,9 @@ +fn main() { + let mut list = vec![1, 2, 3]; + println!("Before defining closure: {:?}", list); + + let mut borrows_mutably = || list.push(7); + + borrows_mutably(); + println!("After calling closure: {:?}", list); +} diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-06/Cargo.toml b/src/doc/book/listings/ch13-functional-features/listing-13-06/Cargo.toml new file mode 100644 index 000000000..8085ade0f --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-06/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "closure-example" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-06/src/main.rs b/src/doc/book/listings/ch13-functional-features/listing-13-06/src/main.rs new file mode 100644 index 000000000..2c8e18c9f --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-06/src/main.rs @@ -0,0 +1,10 @@ +use std::thread; + +fn main() { + let list = vec![1, 2, 3]; + println!("Before defining closure: {:?}", list); + + thread::spawn(move || println!("From thread: {:?}", list)) + .join() + .unwrap(); +} diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-07/.rustfmt.toml b/src/doc/book/listings/ch13-functional-features/listing-13-07/.rustfmt.toml new file mode 100644 index 000000000..ee10c634b --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-07/.rustfmt.toml @@ -0,0 +1,2 @@ +struct_lit_width = 50 + diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-07/Cargo.toml b/src/doc/book/listings/ch13-functional-features/listing-13-07/Cargo.toml new file mode 100644 index 000000000..4a279a450 --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-07/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "rectangles" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-07/output.txt b/src/doc/book/listings/ch13-functional-features/listing-13-07/output.txt new file mode 100644 index 000000000..f18fce46e --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-07/output.txt @@ -0,0 +1,18 @@ +$ cargo run + Compiling rectangles v0.1.0 (file:///projects/rectangles) + Finished dev [unoptimized + debuginfo] target(s) in 0.41s + Running `target/debug/rectangles` +[ + Rectangle { + width: 3, + height: 5, + }, + Rectangle { + width: 7, + height: 12, + }, + Rectangle { + width: 10, + height: 1, + }, +] diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-07/src/main.rs b/src/doc/book/listings/ch13-functional-features/listing-13-07/src/main.rs new file mode 100644 index 000000000..73a25e5f9 --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-07/src/main.rs @@ -0,0 +1,16 @@ +#[derive(Debug)] +struct Rectangle { + width: u32, + height: u32, +} + +fn main() { + let mut list = [ + Rectangle { width: 10, height: 1 }, + Rectangle { width: 3, height: 5 }, + Rectangle { width: 7, height: 12 }, + ]; + + list.sort_by_key(|r| r.width); + println!("{:#?}", list); +} diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-08/.rustfmt.toml b/src/doc/book/listings/ch13-functional-features/listing-13-08/.rustfmt.toml new file mode 100644 index 000000000..ee10c634b --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-08/.rustfmt.toml @@ -0,0 +1,2 @@ +struct_lit_width = 50 + diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-08/Cargo.toml b/src/doc/book/listings/ch13-functional-features/listing-13-08/Cargo.toml new file mode 100644 index 000000000..703c9d977 --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-08/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "rectangles" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-08/output.txt b/src/doc/book/listings/ch13-functional-features/listing-13-08/output.txt new file mode 100644 index 000000000..7a39ac618 --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-08/output.txt @@ -0,0 +1,18 @@ +$ cargo run + Compiling rectangles v0.1.0 (file:///projects/rectangles) +error[E0507]: cannot move out of `value`, a captured variable in an `FnMut` closure + --> src/main.rs:18:30 + | +15 | let value = String::from("by key called"); + | ----- captured outer variable +16 | +17 | list.sort_by_key(|r| { + | ______________________- +18 | | sort_operations.push(value); + | | ^^^^^ move occurs because `value` has type `String`, which does not implement the `Copy` trait +19 | | r.width +20 | | }); + | |_____- captured by this `FnMut` closure + +For more information about this error, try `rustc --explain E0507`. +error: could not compile `rectangles` due to previous error diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-08/src/main.rs b/src/doc/book/listings/ch13-functional-features/listing-13-08/src/main.rs new file mode 100644 index 000000000..3b9c9cbdf --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-08/src/main.rs @@ -0,0 +1,22 @@ +#[derive(Debug)] +struct Rectangle { + width: u32, + height: u32, +} + +fn main() { + let mut list = [ + Rectangle { width: 10, height: 1 }, + Rectangle { width: 3, height: 5 }, + Rectangle { width: 7, height: 12 }, + ]; + + let mut sort_operations = vec![]; + let value = String::from("by key called"); + + list.sort_by_key(|r| { + sort_operations.push(value); + r.width + }); + println!("{:#?}", list); +} diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-09/.rustfmt.toml b/src/doc/book/listings/ch13-functional-features/listing-13-09/.rustfmt.toml new file mode 100644 index 000000000..ee10c634b --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-09/.rustfmt.toml @@ -0,0 +1,2 @@ +struct_lit_width = 50 + diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-09/Cargo.lock b/src/doc/book/listings/ch13-functional-features/listing-13-09/Cargo.lock new file mode 100644 index 000000000..e090432bc --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-09/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "cacher" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-09/Cargo.toml b/src/doc/book/listings/ch13-functional-features/listing-13-09/Cargo.toml new file mode 100644 index 000000000..4a279a450 --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-09/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "rectangles" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-09/src/main.rs b/src/doc/book/listings/ch13-functional-features/listing-13-09/src/main.rs new file mode 100644 index 000000000..a60d6fd3f --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-09/src/main.rs @@ -0,0 +1,20 @@ +#[derive(Debug)] +struct Rectangle { + width: u32, + height: u32, +} + +fn main() { + let mut list = [ + Rectangle { width: 10, height: 1 }, + Rectangle { width: 3, height: 5 }, + Rectangle { width: 7, height: 12 }, + ]; + + let mut num_sort_operations = 0; + list.sort_by_key(|r| { + num_sort_operations += 1; + r.width + }); + println!("{:#?}, sorted in {num_sort_operations} operations", list); +} diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-10/Cargo.lock b/src/doc/book/listings/ch13-functional-features/listing-13-10/Cargo.lock new file mode 100644 index 000000000..e91eaa8d4 --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-10/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "iterators" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-10/Cargo.toml b/src/doc/book/listings/ch13-functional-features/listing-13-10/Cargo.toml new file mode 100644 index 000000000..2652a8a1a --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-10/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "iterators" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-10/src/main.rs b/src/doc/book/listings/ch13-functional-features/listing-13-10/src/main.rs new file mode 100644 index 000000000..55a0dd37e --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-10/src/main.rs @@ -0,0 +1,7 @@ +fn main() { + // ANCHOR: here + let v1 = vec![1, 2, 3]; + + let v1_iter = v1.iter(); + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-11/Cargo.lock b/src/doc/book/listings/ch13-functional-features/listing-13-11/Cargo.lock new file mode 100644 index 000000000..e91eaa8d4 --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-11/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "iterators" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-11/Cargo.toml b/src/doc/book/listings/ch13-functional-features/listing-13-11/Cargo.toml new file mode 100644 index 000000000..2652a8a1a --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-11/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "iterators" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-11/src/main.rs b/src/doc/book/listings/ch13-functional-features/listing-13-11/src/main.rs new file mode 100644 index 000000000..712aff408 --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-11/src/main.rs @@ -0,0 +1,11 @@ +fn main() { + // ANCHOR: here + let v1 = vec![1, 2, 3]; + + let v1_iter = v1.iter(); + + for val in v1_iter { + println!("Got: {}", val); + } + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-12/Cargo.lock b/src/doc/book/listings/ch13-functional-features/listing-13-12/Cargo.lock new file mode 100644 index 000000000..e91eaa8d4 --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-12/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "iterators" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-12/Cargo.toml b/src/doc/book/listings/ch13-functional-features/listing-13-12/Cargo.toml new file mode 100644 index 000000000..2652a8a1a --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-12/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "iterators" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-12/src/lib.rs b/src/doc/book/listings/ch13-functional-features/listing-13-12/src/lib.rs new file mode 100644 index 000000000..758284044 --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-12/src/lib.rs @@ -0,0 +1,16 @@ +#[cfg(test)] +mod tests { + // ANCHOR: here + #[test] + fn iterator_demonstration() { + let v1 = vec![1, 2, 3]; + + let mut v1_iter = v1.iter(); + + assert_eq!(v1_iter.next(), Some(&1)); + assert_eq!(v1_iter.next(), Some(&2)); + assert_eq!(v1_iter.next(), Some(&3)); + assert_eq!(v1_iter.next(), None); + } + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-13/Cargo.lock b/src/doc/book/listings/ch13-functional-features/listing-13-13/Cargo.lock new file mode 100644 index 000000000..e91eaa8d4 --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-13/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "iterators" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-13/Cargo.toml b/src/doc/book/listings/ch13-functional-features/listing-13-13/Cargo.toml new file mode 100644 index 000000000..2652a8a1a --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-13/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "iterators" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-13/src/lib.rs b/src/doc/book/listings/ch13-functional-features/listing-13-13/src/lib.rs new file mode 100644 index 000000000..d1cb54d0a --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-13/src/lib.rs @@ -0,0 +1,15 @@ +#[cfg(test)] +mod tests { + // ANCHOR: here + #[test] + fn iterator_sum() { + let v1 = vec![1, 2, 3]; + + let v1_iter = v1.iter(); + + let total: i32 = v1_iter.sum(); + + assert_eq!(total, 6); + } + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-14/Cargo.lock b/src/doc/book/listings/ch13-functional-features/listing-13-14/Cargo.lock new file mode 100644 index 000000000..e91eaa8d4 --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-14/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "iterators" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-14/Cargo.toml b/src/doc/book/listings/ch13-functional-features/listing-13-14/Cargo.toml new file mode 100644 index 000000000..2652a8a1a --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-14/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "iterators" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-14/output.txt b/src/doc/book/listings/ch13-functional-features/listing-13-14/output.txt new file mode 100644 index 000000000..228c764ed --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-14/output.txt @@ -0,0 +1,14 @@ +$ cargo run + Compiling iterators v0.1.0 (file:///projects/iterators) +warning: unused `Map` that must be used + --> src/main.rs:4:5 + | +4 | v1.iter().map(|x| x + 1); + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `#[warn(unused_must_use)]` on by default + = note: iterators are lazy and do nothing unless consumed + +warning: `iterators` (bin "iterators") generated 1 warning + Finished dev [unoptimized + debuginfo] target(s) in 0.47s + Running `target/debug/iterators` diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-14/src/main.rs b/src/doc/book/listings/ch13-functional-features/listing-13-14/src/main.rs new file mode 100644 index 000000000..62a68be9b --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-14/src/main.rs @@ -0,0 +1,7 @@ +fn main() { + // ANCHOR: here + let v1: Vec = vec![1, 2, 3]; + + v1.iter().map(|x| x + 1); + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-15/Cargo.lock b/src/doc/book/listings/ch13-functional-features/listing-13-15/Cargo.lock new file mode 100644 index 000000000..e91eaa8d4 --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-15/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "iterators" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-15/Cargo.toml b/src/doc/book/listings/ch13-functional-features/listing-13-15/Cargo.toml new file mode 100644 index 000000000..2652a8a1a --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-15/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "iterators" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-15/src/main.rs b/src/doc/book/listings/ch13-functional-features/listing-13-15/src/main.rs new file mode 100644 index 000000000..db9025d6f --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-15/src/main.rs @@ -0,0 +1,9 @@ +fn main() { + // ANCHOR: here + let v1: Vec = vec![1, 2, 3]; + + let v2: Vec<_> = v1.iter().map(|x| x + 1).collect(); + + assert_eq!(v2, vec![2, 3, 4]); + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-16/Cargo.lock b/src/doc/book/listings/ch13-functional-features/listing-13-16/Cargo.lock new file mode 100644 index 000000000..0b15e2157 --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-16/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "shoe_size" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-16/Cargo.toml b/src/doc/book/listings/ch13-functional-features/listing-13-16/Cargo.toml new file mode 100644 index 000000000..cc803776b --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-16/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "shoe_size" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-16/src/lib.rs b/src/doc/book/listings/ch13-functional-features/listing-13-16/src/lib.rs new file mode 100644 index 000000000..281c3c9e4 --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-16/src/lib.rs @@ -0,0 +1,48 @@ +#[derive(PartialEq, Debug)] +struct Shoe { + size: u32, + style: String, +} + +fn shoes_in_size(shoes: Vec, shoe_size: u32) -> Vec { + shoes.into_iter().filter(|s| s.size == shoe_size).collect() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn filters_by_size() { + let shoes = vec![ + Shoe { + size: 10, + style: String::from("sneaker"), + }, + Shoe { + size: 13, + style: String::from("sandal"), + }, + Shoe { + size: 10, + style: String::from("boot"), + }, + ]; + + let in_my_size = shoes_in_size(shoes, 10); + + assert_eq!( + in_my_size, + vec![ + Shoe { + size: 10, + style: String::from("sneaker") + }, + Shoe { + size: 10, + style: String::from("boot") + }, + ] + ); + } +} diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-18/Cargo.lock b/src/doc/book/listings/ch13-functional-features/listing-13-18/Cargo.lock new file mode 100644 index 000000000..88bf82d16 --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-18/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "minigrep" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-18/Cargo.toml b/src/doc/book/listings/ch13-functional-features/listing-13-18/Cargo.toml new file mode 100644 index 000000000..64c2a3f52 --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-18/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "minigrep" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-18/poem.txt b/src/doc/book/listings/ch13-functional-features/listing-13-18/poem.txt new file mode 100644 index 000000000..870752731 --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-18/poem.txt @@ -0,0 +1,9 @@ +I'm nobody! Who are you? +Are you nobody, too? +Then there's a pair of us - don't tell! +They'd banish us, you know. + +How dreary to be somebody! +How public, like a frog +To tell your name the livelong day +To an admiring bog! diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-18/src/lib.rs b/src/doc/book/listings/ch13-functional-features/listing-13-18/src/lib.rs new file mode 100644 index 000000000..292b09789 --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-18/src/lib.rs @@ -0,0 +1,104 @@ +use std::env; +use std::error::Error; +use std::fs; + +pub struct Config { + pub query: String, + pub file_path: String, + pub ignore_case: bool, +} + +impl Config { + pub fn build(args: &[String]) -> Result { + if args.len() < 3 { + return Err("not enough arguments"); + } + + let query = args[1].clone(); + let file_path = args[2].clone(); + + let ignore_case = env::var("IGNORE_CASE").is_ok(); + + Ok(Config { + query, + file_path, + ignore_case, + }) + } +} + +pub fn run(config: Config) -> Result<(), Box> { + let contents = fs::read_to_string(config.file_path)?; + + let results = if config.ignore_case { + search_case_insensitive(&config.query, &contents) + } else { + search(&config.query, &contents) + }; + + for line in results { + println!("{line}"); + } + + Ok(()) +} + +pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { + let mut results = Vec::new(); + + for line in contents.lines() { + if line.contains(query) { + results.push(line); + } + } + + results +} + +pub fn search_case_insensitive<'a>( + query: &str, + contents: &'a str, +) -> Vec<&'a str> { + let query = query.to_lowercase(); + let mut results = Vec::new(); + + for line in contents.lines() { + if line.to_lowercase().contains(&query) { + results.push(line); + } + } + + results +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn case_sensitive() { + let query = "duct"; + let contents = "\ +Rust: +safe, fast, productive. +Pick three. +Duct tape."; + + assert_eq!(vec!["safe, fast, productive."], search(query, contents)); + } + + #[test] + fn case_insensitive() { + let query = "rUsT"; + let contents = "\ +Rust: +safe, fast, productive. +Pick three. +Trust me."; + + assert_eq!( + vec!["Rust:", "Trust me."], + search_case_insensitive(query, contents) + ); + } +} diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-18/src/main.rs b/src/doc/book/listings/ch13-functional-features/listing-13-18/src/main.rs new file mode 100644 index 000000000..40109ef63 --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-18/src/main.rs @@ -0,0 +1,22 @@ +use std::env; +use std::process; + +use minigrep::Config; + +// ANCHOR: here +fn main() { + let config = Config::build(env::args()).unwrap_or_else(|err| { + eprintln!("Problem parsing arguments: {err}"); + process::exit(1); + }); + + // --snip-- + // ANCHOR_END: here + + if let Err(e) = minigrep::run(config) { + eprintln!("Application error: {e}"); + process::exit(1); + } + // ANCHOR: here +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-19/Cargo.lock b/src/doc/book/listings/ch13-functional-features/listing-13-19/Cargo.lock new file mode 100644 index 000000000..88bf82d16 --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-19/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "minigrep" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-19/Cargo.toml b/src/doc/book/listings/ch13-functional-features/listing-13-19/Cargo.toml new file mode 100644 index 000000000..64c2a3f52 --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-19/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "minigrep" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-19/poem.txt b/src/doc/book/listings/ch13-functional-features/listing-13-19/poem.txt new file mode 100644 index 000000000..870752731 --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-19/poem.txt @@ -0,0 +1,9 @@ +I'm nobody! Who are you? +Are you nobody, too? +Then there's a pair of us - don't tell! +They'd banish us, you know. + +How dreary to be somebody! +How public, like a frog +To tell your name the livelong day +To an admiring bog! diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-19/src/lib.rs b/src/doc/book/listings/ch13-functional-features/listing-13-19/src/lib.rs new file mode 100644 index 000000000..79ae2b8f6 --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-19/src/lib.rs @@ -0,0 +1,109 @@ +use std::env; +use std::error::Error; +use std::fs; + +pub struct Config { + pub query: String, + pub file_path: String, + pub ignore_case: bool, +} + +// ANCHOR: here +impl Config { + pub fn build( + mut args: impl Iterator, + ) -> Result { + // --snip-- + // ANCHOR_END: here + if args.len() < 3 { + return Err("not enough arguments"); + } + + let query = args[1].clone(); + let file_path = args[2].clone(); + + let ignore_case = env::var("IGNORE_CASE").is_ok(); + + Ok(Config { + query, + file_path, + ignore_case, + }) + } +} + +pub fn run(config: Config) -> Result<(), Box> { + let contents = fs::read_to_string(config.file_path)?; + + let results = if config.ignore_case { + search_case_insensitive(&config.query, &contents) + } else { + search(&config.query, &contents) + }; + + for line in results { + println!("{line}"); + } + + Ok(()) +} + +pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { + let mut results = Vec::new(); + + for line in contents.lines() { + if line.contains(query) { + results.push(line); + } + } + + results +} + +pub fn search_case_insensitive<'a>( + query: &str, + contents: &'a str, +) -> Vec<&'a str> { + let query = query.to_lowercase(); + let mut results = Vec::new(); + + for line in contents.lines() { + if line.to_lowercase().contains(&query) { + results.push(line); + } + } + + results +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn case_sensitive() { + let query = "duct"; + let contents = "\ +Rust: +safe, fast, productive. +Pick three. +Duct tape."; + + assert_eq!(vec!["safe, fast, productive."], search(query, contents)); + } + + #[test] + fn case_insensitive() { + let query = "rUsT"; + let contents = "\ +Rust: +safe, fast, productive. +Pick three. +Trust me."; + + assert_eq!( + vec!["Rust:", "Trust me."], + search_case_insensitive(query, contents) + ); + } +} diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-19/src/main.rs b/src/doc/book/listings/ch13-functional-features/listing-13-19/src/main.rs new file mode 100644 index 000000000..9ac022545 --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-19/src/main.rs @@ -0,0 +1,16 @@ +use std::env; +use std::process; + +use minigrep::Config; + +fn main() { + let config = Config::build(env::args()).unwrap_or_else(|err| { + eprintln!("Problem parsing arguments: {err}"); + process::exit(1); + }); + + if let Err(e) = minigrep::run(config) { + eprintln!("Application error: {e}"); + process::exit(1); + } +} diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-20/Cargo.lock b/src/doc/book/listings/ch13-functional-features/listing-13-20/Cargo.lock new file mode 100644 index 000000000..88bf82d16 --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-20/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "minigrep" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-20/Cargo.toml b/src/doc/book/listings/ch13-functional-features/listing-13-20/Cargo.toml new file mode 100644 index 000000000..64c2a3f52 --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-20/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "minigrep" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-20/poem.txt b/src/doc/book/listings/ch13-functional-features/listing-13-20/poem.txt new file mode 100644 index 000000000..870752731 --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-20/poem.txt @@ -0,0 +1,9 @@ +I'm nobody! Who are you? +Are you nobody, too? +Then there's a pair of us - don't tell! +They'd banish us, you know. + +How dreary to be somebody! +How public, like a frog +To tell your name the livelong day +To an admiring bog! diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-20/src/lib.rs b/src/doc/book/listings/ch13-functional-features/listing-13-20/src/lib.rs new file mode 100644 index 000000000..4410964a7 --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-20/src/lib.rs @@ -0,0 +1,113 @@ +use std::env; +use std::error::Error; +use std::fs; + +pub struct Config { + pub query: String, + pub file_path: String, + pub ignore_case: bool, +} + +// ANCHOR: here +impl Config { + pub fn build( + mut args: impl Iterator, + ) -> Result { + args.next(); + + let query = match args.next() { + Some(arg) => arg, + None => return Err("Didn't get a query string"), + }; + + let file_path = match args.next() { + Some(arg) => arg, + None => return Err("Didn't get a file path"), + }; + + let ignore_case = env::var("IGNORE_CASE").is_ok(); + + Ok(Config { + query, + file_path, + ignore_case, + }) + } +} +// ANCHOR_END: here + +pub fn run(config: Config) -> Result<(), Box> { + let contents = fs::read_to_string(config.file_path)?; + + let results = if config.ignore_case { + search_case_insensitive(&config.query, &contents) + } else { + search(&config.query, &contents) + }; + + for line in results { + println!("{line}"); + } + + Ok(()) +} + +pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { + let mut results = Vec::new(); + + for line in contents.lines() { + if line.contains(query) { + results.push(line); + } + } + + results +} + +pub fn search_case_insensitive<'a>( + query: &str, + contents: &'a str, +) -> Vec<&'a str> { + let query = query.to_lowercase(); + let mut results = Vec::new(); + + for line in contents.lines() { + if line.to_lowercase().contains(&query) { + results.push(line); + } + } + + results +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn case_sensitive() { + let query = "duct"; + let contents = "\ +Rust: +safe, fast, productive. +Pick three. +Duct tape."; + + assert_eq!(vec!["safe, fast, productive."], search(query, contents)); + } + + #[test] + fn case_insensitive() { + let query = "rUsT"; + let contents = "\ +Rust: +safe, fast, productive. +Pick three. +Trust me."; + + assert_eq!( + vec!["Rust:", "Trust me."], + search_case_insensitive(query, contents) + ); + } +} diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-20/src/main.rs b/src/doc/book/listings/ch13-functional-features/listing-13-20/src/main.rs new file mode 100644 index 000000000..9ac022545 --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-20/src/main.rs @@ -0,0 +1,16 @@ +use std::env; +use std::process; + +use minigrep::Config; + +fn main() { + let config = Config::build(env::args()).unwrap_or_else(|err| { + eprintln!("Problem parsing arguments: {err}"); + process::exit(1); + }); + + if let Err(e) = minigrep::run(config) { + eprintln!("Application error: {e}"); + process::exit(1); + } +} diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-22/Cargo.lock b/src/doc/book/listings/ch13-functional-features/listing-13-22/Cargo.lock new file mode 100644 index 000000000..88bf82d16 --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-22/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "minigrep" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-22/Cargo.toml b/src/doc/book/listings/ch13-functional-features/listing-13-22/Cargo.toml new file mode 100644 index 000000000..64c2a3f52 --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-22/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "minigrep" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-22/poem.txt b/src/doc/book/listings/ch13-functional-features/listing-13-22/poem.txt new file mode 100644 index 000000000..870752731 --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-22/poem.txt @@ -0,0 +1,9 @@ +I'm nobody! Who are you? +Are you nobody, too? +Then there's a pair of us - don't tell! +They'd banish us, you know. + +How dreary to be somebody! +How public, like a frog +To tell your name the livelong day +To an admiring bog! diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-22/src/lib.rs b/src/doc/book/listings/ch13-functional-features/listing-13-22/src/lib.rs new file mode 100644 index 000000000..d694669b4 --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-22/src/lib.rs @@ -0,0 +1,108 @@ +use std::env; +use std::error::Error; +use std::fs; + +pub struct Config { + pub query: String, + pub file_path: String, + pub ignore_case: bool, +} + +impl Config { + pub fn build( + mut args: impl Iterator, + ) -> Result { + args.next(); + + let query = match args.next() { + Some(arg) => arg, + None => return Err("Didn't get a query string"), + }; + + let file_path = match args.next() { + Some(arg) => arg, + None => return Err("Didn't get a file path"), + }; + + let ignore_case = env::var("IGNORE_CASE").is_ok(); + + Ok(Config { + query, + file_path, + ignore_case, + }) + } +} + +pub fn run(config: Config) -> Result<(), Box> { + let contents = fs::read_to_string(config.file_path)?; + + let results = if config.ignore_case { + search_case_insensitive(&config.query, &contents) + } else { + search(&config.query, &contents) + }; + + for line in results { + println!("{line}"); + } + + Ok(()) +} + +// ANCHOR: here +pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { + contents + .lines() + .filter(|line| line.contains(query)) + .collect() +} +// ANCHOR_END: here + +pub fn search_case_insensitive<'a>( + query: &str, + contents: &'a str, +) -> Vec<&'a str> { + let query = query.to_lowercase(); + let mut results = Vec::new(); + + for line in contents.lines() { + if line.to_lowercase().contains(&query) { + results.push(line); + } + } + + results +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn case_sensitive() { + let query = "duct"; + let contents = "\ +Rust: +safe, fast, productive. +Pick three. +Duct tape."; + + assert_eq!(vec!["safe, fast, productive."], search(query, contents)); + } + + #[test] + fn case_insensitive() { + let query = "rUsT"; + let contents = "\ +Rust: +safe, fast, productive. +Pick three. +Trust me."; + + assert_eq!( + vec!["Rust:", "Trust me."], + search_case_insensitive(query, contents) + ); + } +} diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-22/src/main.rs b/src/doc/book/listings/ch13-functional-features/listing-13-22/src/main.rs new file mode 100644 index 000000000..9ac022545 --- /dev/null +++ b/src/doc/book/listings/ch13-functional-features/listing-13-22/src/main.rs @@ -0,0 +1,16 @@ +use std::env; +use std::process; + +use minigrep::Config; + +fn main() { + let config = Config::build(env::args()).unwrap_or_else(|err| { + eprintln!("Problem parsing arguments: {err}"); + process::exit(1); + }); + + if let Err(e) = minigrep::run(config) { + eprintln!("Application error: {e}"); + process::exit(1); + } +} diff --git a/src/doc/book/listings/ch14-more-about-cargo/listing-14-01/Cargo.lock b/src/doc/book/listings/ch14-more-about-cargo/listing-14-01/Cargo.lock new file mode 100644 index 000000000..b304dd7c7 --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/listing-14-01/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "my_crate" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch14-more-about-cargo/listing-14-01/Cargo.toml b/src/doc/book/listings/ch14-more-about-cargo/listing-14-01/Cargo.toml new file mode 100644 index 000000000..c52da0412 --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/listing-14-01/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "my_crate" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch14-more-about-cargo/listing-14-01/src/lib.rs b/src/doc/book/listings/ch14-more-about-cargo/listing-14-01/src/lib.rs new file mode 100644 index 000000000..ed7abb727 --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/listing-14-01/src/lib.rs @@ -0,0 +1,13 @@ +/// Adds one to the number given. +/// +/// # Examples +/// +/// ``` +/// let arg = 5; +/// let answer = my_crate::add_one(arg); +/// +/// assert_eq!(6, answer); +/// ``` +pub fn add_one(x: i32) -> i32 { + x + 1 +} diff --git a/src/doc/book/listings/ch14-more-about-cargo/listing-14-02/Cargo.lock b/src/doc/book/listings/ch14-more-about-cargo/listing-14-02/Cargo.lock new file mode 100644 index 000000000..b304dd7c7 --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/listing-14-02/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "my_crate" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch14-more-about-cargo/listing-14-02/Cargo.toml b/src/doc/book/listings/ch14-more-about-cargo/listing-14-02/Cargo.toml new file mode 100644 index 000000000..c52da0412 --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/listing-14-02/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "my_crate" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch14-more-about-cargo/listing-14-02/src/lib.rs b/src/doc/book/listings/ch14-more-about-cargo/listing-14-02/src/lib.rs new file mode 100644 index 000000000..64c9c439c --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/listing-14-02/src/lib.rs @@ -0,0 +1,21 @@ +// ANCHOR: here +//! # My Crate +//! +//! `my_crate` is a collection of utilities to make performing certain +//! calculations more convenient. + +/// Adds one to the number given. +// --snip-- +// ANCHOR_END: here +/// +/// # Examples +/// +/// ``` +/// let arg = 5; +/// let answer = my_crate::add_one(arg); +/// +/// assert_eq!(6, answer); +/// ``` +pub fn add_one(x: i32) -> i32 { + x + 1 +} diff --git a/src/doc/book/listings/ch14-more-about-cargo/listing-14-03/Cargo.lock b/src/doc/book/listings/ch14-more-about-cargo/listing-14-03/Cargo.lock new file mode 100644 index 000000000..df19c247b --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/listing-14-03/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "art" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch14-more-about-cargo/listing-14-03/Cargo.toml b/src/doc/book/listings/ch14-more-about-cargo/listing-14-03/Cargo.toml new file mode 100644 index 000000000..66ef4b532 --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/listing-14-03/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "art" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch14-more-about-cargo/listing-14-03/src/lib.rs b/src/doc/book/listings/ch14-more-about-cargo/listing-14-03/src/lib.rs new file mode 100644 index 000000000..bffbe058d --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/listing-14-03/src/lib.rs @@ -0,0 +1,34 @@ +// ANCHOR: here +//! # Art +//! +//! A library for modeling artistic concepts. + +pub mod kinds { + /// The primary colors according to the RYB color model. + pub enum PrimaryColor { + Red, + Yellow, + Blue, + } + + /// The secondary colors according to the RYB color model. + pub enum SecondaryColor { + Orange, + Green, + Purple, + } +} + +pub mod utils { + use crate::kinds::*; + + /// Combines two primary colors in equal amounts to create + /// a secondary color. + pub fn mix(c1: PrimaryColor, c2: PrimaryColor) -> SecondaryColor { + // --snip-- + // ANCHOR_END: here + unimplemented!(); + // ANCHOR: here + } +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch14-more-about-cargo/listing-14-04/Cargo.lock b/src/doc/book/listings/ch14-more-about-cargo/listing-14-04/Cargo.lock new file mode 100644 index 000000000..df19c247b --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/listing-14-04/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "art" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch14-more-about-cargo/listing-14-04/Cargo.toml b/src/doc/book/listings/ch14-more-about-cargo/listing-14-04/Cargo.toml new file mode 100644 index 000000000..66ef4b532 --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/listing-14-04/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "art" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch14-more-about-cargo/listing-14-04/src/lib.rs b/src/doc/book/listings/ch14-more-about-cargo/listing-14-04/src/lib.rs new file mode 100644 index 000000000..b077a9a71 --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/listing-14-04/src/lib.rs @@ -0,0 +1,29 @@ +//! # Art +//! +//! A library for modeling artistic concepts. + +pub mod kinds { + /// The primary colors according to the RYB color model. + pub enum PrimaryColor { + Red, + Yellow, + Blue, + } + + /// The secondary colors according to the RYB color model. + pub enum SecondaryColor { + Orange, + Green, + Purple, + } +} + +pub mod utils { + use crate::kinds::*; + + /// Combines two primary colors in equal amounts to create + /// a secondary color. + pub fn mix(c1: PrimaryColor, c2: PrimaryColor) -> SecondaryColor { + SecondaryColor::Orange + } +} diff --git a/src/doc/book/listings/ch14-more-about-cargo/listing-14-04/src/main.rs b/src/doc/book/listings/ch14-more-about-cargo/listing-14-04/src/main.rs new file mode 100644 index 000000000..b1a4bf792 --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/listing-14-04/src/main.rs @@ -0,0 +1,8 @@ +use art::kinds::PrimaryColor; +use art::utils::mix; + +fn main() { + let red = PrimaryColor::Red; + let yellow = PrimaryColor::Yellow; + mix(red, yellow); +} diff --git a/src/doc/book/listings/ch14-more-about-cargo/listing-14-05/Cargo.lock b/src/doc/book/listings/ch14-more-about-cargo/listing-14-05/Cargo.lock new file mode 100644 index 000000000..df19c247b --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/listing-14-05/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "art" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch14-more-about-cargo/listing-14-05/Cargo.toml b/src/doc/book/listings/ch14-more-about-cargo/listing-14-05/Cargo.toml new file mode 100644 index 000000000..66ef4b532 --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/listing-14-05/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "art" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch14-more-about-cargo/listing-14-05/src/lib.rs b/src/doc/book/listings/ch14-more-about-cargo/listing-14-05/src/lib.rs new file mode 100644 index 000000000..c5aa9e7b0 --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/listing-14-05/src/lib.rs @@ -0,0 +1,41 @@ +// ANCHOR: here +//! # Art +//! +//! A library for modeling artistic concepts. + +pub use self::kinds::PrimaryColor; +pub use self::kinds::SecondaryColor; +pub use self::utils::mix; + +pub mod kinds { + // --snip-- + // ANCHOR_END: here + /// The primary colors according to the RYB color model. + pub enum PrimaryColor { + Red, + Yellow, + Blue, + } + + /// The secondary colors according to the RYB color model. + pub enum SecondaryColor { + Orange, + Green, + Purple, + } + // ANCHOR: here +} + +pub mod utils { + // --snip-- + // ANCHOR_END: here + use crate::kinds::*; + + /// Combines two primary colors in equal amounts to create + /// a secondary color. + pub fn mix(c1: PrimaryColor, c2: PrimaryColor) -> SecondaryColor { + SecondaryColor::Orange + } + // ANCHOR: here +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch14-more-about-cargo/listing-14-06/Cargo.lock b/src/doc/book/listings/ch14-more-about-cargo/listing-14-06/Cargo.lock new file mode 100644 index 000000000..df19c247b --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/listing-14-06/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "art" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch14-more-about-cargo/listing-14-06/Cargo.toml b/src/doc/book/listings/ch14-more-about-cargo/listing-14-06/Cargo.toml new file mode 100644 index 000000000..66ef4b532 --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/listing-14-06/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "art" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch14-more-about-cargo/listing-14-06/src/lib.rs b/src/doc/book/listings/ch14-more-about-cargo/listing-14-06/src/lib.rs new file mode 100644 index 000000000..daabd006d --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/listing-14-06/src/lib.rs @@ -0,0 +1,33 @@ +//! # Art +//! +//! A library for modeling artistic concepts. + +pub use self::kinds::PrimaryColor; +pub use self::kinds::SecondaryColor; +pub use self::utils::mix; + +pub mod kinds { + /// The primary colors according to the RYB color model. + pub enum PrimaryColor { + Red, + Yellow, + Blue, + } + + /// The secondary colors according to the RYB color model. + pub enum SecondaryColor { + Orange, + Green, + Purple, + } +} + +pub mod utils { + use crate::kinds::*; + + /// Combines two primary colors in equal amounts to create + /// a secondary color. + pub fn mix(c1: PrimaryColor, c2: PrimaryColor) -> SecondaryColor { + SecondaryColor::Orange + } +} diff --git a/src/doc/book/listings/ch14-more-about-cargo/listing-14-06/src/main.rs b/src/doc/book/listings/ch14-more-about-cargo/listing-14-06/src/main.rs new file mode 100644 index 000000000..51f3b761d --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/listing-14-06/src/main.rs @@ -0,0 +1,13 @@ +// ANCHOR: here +use art::mix; +use art::PrimaryColor; + +fn main() { + // --snip-- + // ANCHOR_END: here + let red = PrimaryColor::Red; + let yellow = PrimaryColor::Yellow; + mix(red, yellow); + // ANCHOR: here +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch14-more-about-cargo/listing-14-07/add/Cargo.lock b/src/doc/book/listings/ch14-more-about-cargo/listing-14-07/add/Cargo.lock new file mode 100644 index 000000000..a456055c9 --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/listing-14-07/add/Cargo.lock @@ -0,0 +1,13 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "add_one" +version = "0.1.0" + +[[package]] +name = "adder" +version = "0.1.0" +dependencies = [ + "add_one 0.1.0", +] + diff --git a/src/doc/book/listings/ch14-more-about-cargo/listing-14-07/add/Cargo.toml b/src/doc/book/listings/ch14-more-about-cargo/listing-14-07/add/Cargo.toml new file mode 100644 index 000000000..1448801d5 --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/listing-14-07/add/Cargo.toml @@ -0,0 +1,6 @@ +[workspace] + +members = [ + "adder", + "add_one", +] diff --git a/src/doc/book/listings/ch14-more-about-cargo/listing-14-07/add/add_one/Cargo.toml b/src/doc/book/listings/ch14-more-about-cargo/listing-14-07/add/add_one/Cargo.toml new file mode 100644 index 000000000..8af4ab816 --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/listing-14-07/add/add_one/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "add_one" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch14-more-about-cargo/listing-14-07/add/add_one/src/lib.rs b/src/doc/book/listings/ch14-more-about-cargo/listing-14-07/add/add_one/src/lib.rs new file mode 100644 index 000000000..b0bb86943 --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/listing-14-07/add/add_one/src/lib.rs @@ -0,0 +1,3 @@ +pub fn add_one(x: i32) -> i32 { + x + 1 +} diff --git a/src/doc/book/listings/ch14-more-about-cargo/listing-14-07/add/adder/Cargo.toml b/src/doc/book/listings/ch14-more-about-cargo/listing-14-07/add/adder/Cargo.toml new file mode 100644 index 000000000..feb3d956e --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/listing-14-07/add/adder/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "adder" +version = "0.1.0" +edition = "2021" + +[dependencies] + +add_one = { path = "../add_one" } diff --git a/src/doc/book/listings/ch14-more-about-cargo/listing-14-07/add/adder/src/main.rs b/src/doc/book/listings/ch14-more-about-cargo/listing-14-07/add/adder/src/main.rs new file mode 100644 index 000000000..131629490 --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/listing-14-07/add/adder/src/main.rs @@ -0,0 +1,6 @@ +use add_one; + +fn main() { + let num = 10; + println!("Hello, world! {num} plus one is {}!", add_one::add_one(num)); +} diff --git a/src/doc/book/listings/ch14-more-about-cargo/no-listing-01-workspace-with-adder-crate/add/Cargo.lock b/src/doc/book/listings/ch14-more-about-cargo/no-listing-01-workspace-with-adder-crate/add/Cargo.lock new file mode 100644 index 000000000..d37189b33 --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/no-listing-01-workspace-with-adder-crate/add/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "adder" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch14-more-about-cargo/no-listing-01-workspace-with-adder-crate/add/Cargo.toml b/src/doc/book/listings/ch14-more-about-cargo/no-listing-01-workspace-with-adder-crate/add/Cargo.toml new file mode 100644 index 000000000..c5ea8e510 --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/no-listing-01-workspace-with-adder-crate/add/Cargo.toml @@ -0,0 +1,5 @@ +[workspace] + +members = [ + "adder", +] diff --git a/src/doc/book/listings/ch14-more-about-cargo/no-listing-01-workspace-with-adder-crate/add/adder/Cargo.toml b/src/doc/book/listings/ch14-more-about-cargo/no-listing-01-workspace-with-adder-crate/add/adder/Cargo.toml new file mode 100644 index 000000000..e61cb12e3 --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/no-listing-01-workspace-with-adder-crate/add/adder/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "adder" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch14-more-about-cargo/no-listing-01-workspace-with-adder-crate/add/adder/src/main.rs b/src/doc/book/listings/ch14-more-about-cargo/no-listing-01-workspace-with-adder-crate/add/adder/src/main.rs new file mode 100644 index 000000000..e7a11a969 --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/no-listing-01-workspace-with-adder-crate/add/adder/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/src/doc/book/listings/ch14-more-about-cargo/no-listing-02-workspace-with-two-crates/add/Cargo.lock b/src/doc/book/listings/ch14-more-about-cargo/no-listing-02-workspace-with-two-crates/add/Cargo.lock new file mode 100644 index 000000000..a456055c9 --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/no-listing-02-workspace-with-two-crates/add/Cargo.lock @@ -0,0 +1,13 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "add_one" +version = "0.1.0" + +[[package]] +name = "adder" +version = "0.1.0" +dependencies = [ + "add_one 0.1.0", +] + diff --git a/src/doc/book/listings/ch14-more-about-cargo/no-listing-02-workspace-with-two-crates/add/Cargo.toml b/src/doc/book/listings/ch14-more-about-cargo/no-listing-02-workspace-with-two-crates/add/Cargo.toml new file mode 100644 index 000000000..1448801d5 --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/no-listing-02-workspace-with-two-crates/add/Cargo.toml @@ -0,0 +1,6 @@ +[workspace] + +members = [ + "adder", + "add_one", +] diff --git a/src/doc/book/listings/ch14-more-about-cargo/no-listing-02-workspace-with-two-crates/add/add_one/Cargo.toml b/src/doc/book/listings/ch14-more-about-cargo/no-listing-02-workspace-with-two-crates/add/add_one/Cargo.toml new file mode 100644 index 000000000..8af4ab816 --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/no-listing-02-workspace-with-two-crates/add/add_one/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "add_one" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch14-more-about-cargo/no-listing-02-workspace-with-two-crates/add/add_one/src/lib.rs b/src/doc/book/listings/ch14-more-about-cargo/no-listing-02-workspace-with-two-crates/add/add_one/src/lib.rs new file mode 100644 index 000000000..b0bb86943 --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/no-listing-02-workspace-with-two-crates/add/add_one/src/lib.rs @@ -0,0 +1,3 @@ +pub fn add_one(x: i32) -> i32 { + x + 1 +} diff --git a/src/doc/book/listings/ch14-more-about-cargo/no-listing-02-workspace-with-two-crates/add/adder/Cargo.toml b/src/doc/book/listings/ch14-more-about-cargo/no-listing-02-workspace-with-two-crates/add/adder/Cargo.toml new file mode 100644 index 000000000..55c02036c --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/no-listing-02-workspace-with-two-crates/add/adder/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "adder" +version = "0.1.0" +edition = "2021" + +[dependencies] +add_one = { path = "../add_one" } diff --git a/src/doc/book/listings/ch14-more-about-cargo/no-listing-02-workspace-with-two-crates/add/adder/src/main.rs b/src/doc/book/listings/ch14-more-about-cargo/no-listing-02-workspace-with-two-crates/add/adder/src/main.rs new file mode 100644 index 000000000..e7a11a969 --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/no-listing-02-workspace-with-two-crates/add/adder/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/src/doc/book/listings/ch14-more-about-cargo/no-listing-03-workspace-with-external-dependency/add/Cargo.lock b/src/doc/book/listings/ch14-more-about-cargo/no-listing-03-workspace-with-external-dependency/add/Cargo.lock new file mode 100644 index 000000000..eec3a9e4b --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/no-listing-03-workspace-with-external-dependency/add/Cargo.lock @@ -0,0 +1,90 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "add_one" +version = "0.1.0" +dependencies = [ + "rand", +] + +[[package]] +name = "adder" +version = "0.1.0" +dependencies = [ + "add_one", +] + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "getrandom" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "libc" +version = "0.2.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7282d924be3275cec7f6756ff4121987bc6481325397dde6ba3e7802b1a8b1c" + +[[package]] +name = "ppv-lite86" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" + +[[package]] +name = "rand" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", + "rand_hc", +] + +[[package]] +name = "rand_chacha" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34cf66eb183df1c5876e2dcf6b13d57340741e8dc255b48e40a26de954d06ae7" +dependencies = [ + "getrandom", +] + +[[package]] +name = "rand_hc" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73" +dependencies = [ + "rand_core", +] + +[[package]] +name = "wasi" +version = "0.10.2+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" diff --git a/src/doc/book/listings/ch14-more-about-cargo/no-listing-03-workspace-with-external-dependency/add/Cargo.toml b/src/doc/book/listings/ch14-more-about-cargo/no-listing-03-workspace-with-external-dependency/add/Cargo.toml new file mode 100644 index 000000000..1448801d5 --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/no-listing-03-workspace-with-external-dependency/add/Cargo.toml @@ -0,0 +1,6 @@ +[workspace] + +members = [ + "adder", + "add_one", +] diff --git a/src/doc/book/listings/ch14-more-about-cargo/no-listing-03-workspace-with-external-dependency/add/add_one/Cargo.toml b/src/doc/book/listings/ch14-more-about-cargo/no-listing-03-workspace-with-external-dependency/add/add_one/Cargo.toml new file mode 100644 index 000000000..fd4942ace --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/no-listing-03-workspace-with-external-dependency/add/add_one/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "add_one" +version = "0.1.0" +edition = "2021" + +[dependencies] +rand = "0.8.3" diff --git a/src/doc/book/listings/ch14-more-about-cargo/no-listing-03-workspace-with-external-dependency/add/add_one/src/lib.rs b/src/doc/book/listings/ch14-more-about-cargo/no-listing-03-workspace-with-external-dependency/add/add_one/src/lib.rs new file mode 100644 index 000000000..7b61b40a4 --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/no-listing-03-workspace-with-external-dependency/add/add_one/src/lib.rs @@ -0,0 +1,5 @@ +use rand; + +pub fn add_one(x: i32) -> i32 { + x + 1 +} diff --git a/src/doc/book/listings/ch14-more-about-cargo/no-listing-03-workspace-with-external-dependency/add/adder/Cargo.toml b/src/doc/book/listings/ch14-more-about-cargo/no-listing-03-workspace-with-external-dependency/add/adder/Cargo.toml new file mode 100644 index 000000000..feb3d956e --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/no-listing-03-workspace-with-external-dependency/add/adder/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "adder" +version = "0.1.0" +edition = "2021" + +[dependencies] + +add_one = { path = "../add_one" } diff --git a/src/doc/book/listings/ch14-more-about-cargo/no-listing-03-workspace-with-external-dependency/add/adder/src/main.rs b/src/doc/book/listings/ch14-more-about-cargo/no-listing-03-workspace-with-external-dependency/add/adder/src/main.rs new file mode 100644 index 000000000..7deb7962f --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/no-listing-03-workspace-with-external-dependency/add/adder/src/main.rs @@ -0,0 +1,10 @@ +use add_one; + +fn main() { + let num = 10; + println!( + "Hello, world! {} plus one is {}!", + num, + add_one::add_one(num) + ); +} diff --git a/src/doc/book/listings/ch14-more-about-cargo/no-listing-04-workspace-with-tests/add/Cargo.lock b/src/doc/book/listings/ch14-more-about-cargo/no-listing-04-workspace-with-tests/add/Cargo.lock new file mode 100644 index 000000000..a456055c9 --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/no-listing-04-workspace-with-tests/add/Cargo.lock @@ -0,0 +1,13 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "add_one" +version = "0.1.0" + +[[package]] +name = "adder" +version = "0.1.0" +dependencies = [ + "add_one 0.1.0", +] + diff --git a/src/doc/book/listings/ch14-more-about-cargo/no-listing-04-workspace-with-tests/add/Cargo.toml b/src/doc/book/listings/ch14-more-about-cargo/no-listing-04-workspace-with-tests/add/Cargo.toml new file mode 100644 index 000000000..1448801d5 --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/no-listing-04-workspace-with-tests/add/Cargo.toml @@ -0,0 +1,6 @@ +[workspace] + +members = [ + "adder", + "add_one", +] diff --git a/src/doc/book/listings/ch14-more-about-cargo/no-listing-04-workspace-with-tests/add/add_one/Cargo.toml b/src/doc/book/listings/ch14-more-about-cargo/no-listing-04-workspace-with-tests/add/add_one/Cargo.toml new file mode 100644 index 000000000..8af4ab816 --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/no-listing-04-workspace-with-tests/add/add_one/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "add_one" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch14-more-about-cargo/no-listing-04-workspace-with-tests/add/add_one/src/lib.rs b/src/doc/book/listings/ch14-more-about-cargo/no-listing-04-workspace-with-tests/add/add_one/src/lib.rs new file mode 100644 index 000000000..40ceb1285 --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/no-listing-04-workspace-with-tests/add/add_one/src/lib.rs @@ -0,0 +1,13 @@ +pub fn add_one(x: i32) -> i32 { + x + 1 +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn it_works() { + assert_eq!(3, add_one(2)); + } +} diff --git a/src/doc/book/listings/ch14-more-about-cargo/no-listing-04-workspace-with-tests/add/adder/Cargo.toml b/src/doc/book/listings/ch14-more-about-cargo/no-listing-04-workspace-with-tests/add/adder/Cargo.toml new file mode 100644 index 000000000..feb3d956e --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/no-listing-04-workspace-with-tests/add/adder/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "adder" +version = "0.1.0" +edition = "2021" + +[dependencies] + +add_one = { path = "../add_one" } diff --git a/src/doc/book/listings/ch14-more-about-cargo/no-listing-04-workspace-with-tests/add/adder/src/main.rs b/src/doc/book/listings/ch14-more-about-cargo/no-listing-04-workspace-with-tests/add/adder/src/main.rs new file mode 100644 index 000000000..7deb7962f --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/no-listing-04-workspace-with-tests/add/adder/src/main.rs @@ -0,0 +1,10 @@ +use add_one; + +fn main() { + let num = 10; + println!( + "Hello, world! {} plus one is {}!", + num, + add_one::add_one(num) + ); +} diff --git a/src/doc/book/listings/ch14-more-about-cargo/output-only-01-adder-crate/add/Cargo.lock b/src/doc/book/listings/ch14-more-about-cargo/output-only-01-adder-crate/add/Cargo.lock new file mode 100644 index 000000000..d98623d6e --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/output-only-01-adder-crate/add/Cargo.lock @@ -0,0 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "adder" +version = "0.1.0" diff --git a/src/doc/book/listings/ch14-more-about-cargo/output-only-01-adder-crate/add/Cargo.toml b/src/doc/book/listings/ch14-more-about-cargo/output-only-01-adder-crate/add/Cargo.toml new file mode 100644 index 000000000..c5ea8e510 --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/output-only-01-adder-crate/add/Cargo.toml @@ -0,0 +1,5 @@ +[workspace] + +members = [ + "adder", +] diff --git a/src/doc/book/listings/ch14-more-about-cargo/output-only-01-adder-crate/add/rustfmt-ignore b/src/doc/book/listings/ch14-more-about-cargo/output-only-01-adder-crate/add/rustfmt-ignore new file mode 100644 index 000000000..958e56837 --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/output-only-01-adder-crate/add/rustfmt-ignore @@ -0,0 +1,2 @@ +This listing is used for demonstrating how to set up a workspace, but the workspace isn't +completely set up yet, so rustfmt complains the crate mentioned in Cargo.toml doesn't exist yet. diff --git a/src/doc/book/listings/ch14-more-about-cargo/output-only-02-add-one/add/Cargo.lock b/src/doc/book/listings/ch14-more-about-cargo/output-only-02-add-one/add/Cargo.lock new file mode 100644 index 000000000..a456055c9 --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/output-only-02-add-one/add/Cargo.lock @@ -0,0 +1,13 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "add_one" +version = "0.1.0" + +[[package]] +name = "adder" +version = "0.1.0" +dependencies = [ + "add_one 0.1.0", +] + diff --git a/src/doc/book/listings/ch14-more-about-cargo/output-only-02-add-one/add/Cargo.toml b/src/doc/book/listings/ch14-more-about-cargo/output-only-02-add-one/add/Cargo.toml new file mode 100644 index 000000000..1448801d5 --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/output-only-02-add-one/add/Cargo.toml @@ -0,0 +1,6 @@ +[workspace] + +members = [ + "adder", + "add_one", +] diff --git a/src/doc/book/listings/ch14-more-about-cargo/output-only-02-add-one/add/add_one/Cargo.toml b/src/doc/book/listings/ch14-more-about-cargo/output-only-02-add-one/add/add_one/Cargo.toml new file mode 100644 index 000000000..900018470 --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/output-only-02-add-one/add/add_one/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "add_one" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/src/doc/book/listings/ch14-more-about-cargo/output-only-02-add-one/add/add_one/src/lib.rs b/src/doc/book/listings/ch14-more-about-cargo/output-only-02-add-one/add/add_one/src/lib.rs new file mode 100644 index 000000000..1b4a90c93 --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/output-only-02-add-one/add/add_one/src/lib.rs @@ -0,0 +1,8 @@ +#[cfg(test)] +mod tests { + #[test] + fn it_works() { + let result = 2 + 2; + assert_eq!(result, 4); + } +} diff --git a/src/doc/book/listings/ch14-more-about-cargo/output-only-02-add-one/add/adder/Cargo.toml b/src/doc/book/listings/ch14-more-about-cargo/output-only-02-add-one/add/adder/Cargo.toml new file mode 100644 index 000000000..feb3d956e --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/output-only-02-add-one/add/adder/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "adder" +version = "0.1.0" +edition = "2021" + +[dependencies] + +add_one = { path = "../add_one" } diff --git a/src/doc/book/listings/ch14-more-about-cargo/output-only-02-add-one/add/adder/src/main.rs b/src/doc/book/listings/ch14-more-about-cargo/output-only-02-add-one/add/adder/src/main.rs new file mode 100644 index 000000000..e7a11a969 --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/output-only-02-add-one/add/adder/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/src/doc/book/listings/ch14-more-about-cargo/output-only-03-use-rand/add/Cargo.lock b/src/doc/book/listings/ch14-more-about-cargo/output-only-03-use-rand/add/Cargo.lock new file mode 100644 index 000000000..eec3a9e4b --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/output-only-03-use-rand/add/Cargo.lock @@ -0,0 +1,90 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "add_one" +version = "0.1.0" +dependencies = [ + "rand", +] + +[[package]] +name = "adder" +version = "0.1.0" +dependencies = [ + "add_one", +] + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "getrandom" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "libc" +version = "0.2.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7282d924be3275cec7f6756ff4121987bc6481325397dde6ba3e7802b1a8b1c" + +[[package]] +name = "ppv-lite86" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" + +[[package]] +name = "rand" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", + "rand_hc", +] + +[[package]] +name = "rand_chacha" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34cf66eb183df1c5876e2dcf6b13d57340741e8dc255b48e40a26de954d06ae7" +dependencies = [ + "getrandom", +] + +[[package]] +name = "rand_hc" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73" +dependencies = [ + "rand_core", +] + +[[package]] +name = "wasi" +version = "0.10.2+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" diff --git a/src/doc/book/listings/ch14-more-about-cargo/output-only-03-use-rand/add/Cargo.toml b/src/doc/book/listings/ch14-more-about-cargo/output-only-03-use-rand/add/Cargo.toml new file mode 100644 index 000000000..1448801d5 --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/output-only-03-use-rand/add/Cargo.toml @@ -0,0 +1,6 @@ +[workspace] + +members = [ + "adder", + "add_one", +] diff --git a/src/doc/book/listings/ch14-more-about-cargo/output-only-03-use-rand/add/add_one/Cargo.toml b/src/doc/book/listings/ch14-more-about-cargo/output-only-03-use-rand/add/add_one/Cargo.toml new file mode 100644 index 000000000..fd4942ace --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/output-only-03-use-rand/add/add_one/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "add_one" +version = "0.1.0" +edition = "2021" + +[dependencies] +rand = "0.8.3" diff --git a/src/doc/book/listings/ch14-more-about-cargo/output-only-03-use-rand/add/add_one/src/lib.rs b/src/doc/book/listings/ch14-more-about-cargo/output-only-03-use-rand/add/add_one/src/lib.rs new file mode 100644 index 000000000..7b61b40a4 --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/output-only-03-use-rand/add/add_one/src/lib.rs @@ -0,0 +1,5 @@ +use rand; + +pub fn add_one(x: i32) -> i32 { + x + 1 +} diff --git a/src/doc/book/listings/ch14-more-about-cargo/output-only-03-use-rand/add/adder/Cargo.toml b/src/doc/book/listings/ch14-more-about-cargo/output-only-03-use-rand/add/adder/Cargo.toml new file mode 100644 index 000000000..feb3d956e --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/output-only-03-use-rand/add/adder/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "adder" +version = "0.1.0" +edition = "2021" + +[dependencies] + +add_one = { path = "../add_one" } diff --git a/src/doc/book/listings/ch14-more-about-cargo/output-only-03-use-rand/add/adder/src/main.rs b/src/doc/book/listings/ch14-more-about-cargo/output-only-03-use-rand/add/adder/src/main.rs new file mode 100644 index 000000000..eb4050dc3 --- /dev/null +++ b/src/doc/book/listings/ch14-more-about-cargo/output-only-03-use-rand/add/adder/src/main.rs @@ -0,0 +1,11 @@ +use add_one; +use rand; + +fn main() { + let num = 10; + println!( + "Hello, world! {} plus one is {}!", + num, + add_one::add_one(num) + ); +} diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-01/Cargo.lock b/src/doc/book/listings/ch15-smart-pointers/listing-15-01/Cargo.lock new file mode 100644 index 000000000..8c125fc84 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-01/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "box-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-01/Cargo.toml b/src/doc/book/listings/ch15-smart-pointers/listing-15-01/Cargo.toml new file mode 100644 index 000000000..690385c7a --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-01/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "box-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-01/src/main.rs b/src/doc/book/listings/ch15-smart-pointers/listing-15-01/src/main.rs new file mode 100644 index 000000000..8da1d905d --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-01/src/main.rs @@ -0,0 +1,4 @@ +fn main() { + let b = Box::new(5); + println!("b = {}", b); +} diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-02/Cargo.lock b/src/doc/book/listings/ch15-smart-pointers/listing-15-02/Cargo.lock new file mode 100644 index 000000000..a792c49aa --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-02/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "cons-list" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-02/Cargo.toml b/src/doc/book/listings/ch15-smart-pointers/listing-15-02/Cargo.toml new file mode 100644 index 000000000..dce1515c3 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-02/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "cons-list" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-02/src/main.rs b/src/doc/book/listings/ch15-smart-pointers/listing-15-02/src/main.rs new file mode 100644 index 000000000..84640b9b9 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-02/src/main.rs @@ -0,0 +1,8 @@ +// ANCHOR: here +enum List { + Cons(i32, List), + Nil, +} +// ANCHOR_END: here + +fn main() {} diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-03/Cargo.lock b/src/doc/book/listings/ch15-smart-pointers/listing-15-03/Cargo.lock new file mode 100644 index 000000000..a792c49aa --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-03/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "cons-list" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-03/Cargo.toml b/src/doc/book/listings/ch15-smart-pointers/listing-15-03/Cargo.toml new file mode 100644 index 000000000..dce1515c3 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-03/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "cons-list" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-03/output.txt b/src/doc/book/listings/ch15-smart-pointers/listing-15-03/output.txt new file mode 100644 index 000000000..437d74b5c --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-03/output.txt @@ -0,0 +1,27 @@ +$ cargo run + Compiling cons-list v0.1.0 (file:///projects/cons-list) +error[E0072]: recursive type `List` has infinite size + --> src/main.rs:1:1 + | +1 | enum List { + | ^^^^^^^^^ recursive type has infinite size +2 | Cons(i32, List), + | ---- recursive without indirection + | +help: insert some indirection (e.g., a `Box`, `Rc`, or `&`) to make `List` representable + | +2 | Cons(i32, Box), + | ++++ + + +error[E0391]: cycle detected when computing drop-check constraints for `List` + --> src/main.rs:1:1 + | +1 | enum List { + | ^^^^^^^^^ + | + = note: ...which immediately requires computing drop-check constraints for `List` again + = note: cycle used when computing dropck types for `Canonical { max_universe: U0, variables: [], value: ParamEnvAnd { param_env: ParamEnv { caller_bounds: [], reveal: UserFacing, constness: NotConst }, value: List } }` + +Some errors have detailed explanations: E0072, E0391. +For more information about an error, try `rustc --explain E0072`. +error: could not compile `cons-list` due to 2 previous errors diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-03/src/main.rs b/src/doc/book/listings/ch15-smart-pointers/listing-15-03/src/main.rs new file mode 100644 index 000000000..a96f3d7b1 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-03/src/main.rs @@ -0,0 +1,12 @@ +enum List { + Cons(i32, List), + Nil, +} + +// ANCHOR: here +use crate::List::{Cons, Nil}; + +fn main() { + let list = Cons(1, Cons(2, Cons(3, Nil))); +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-05/Cargo.lock b/src/doc/book/listings/ch15-smart-pointers/listing-15-05/Cargo.lock new file mode 100644 index 000000000..a792c49aa --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-05/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "cons-list" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-05/Cargo.toml b/src/doc/book/listings/ch15-smart-pointers/listing-15-05/Cargo.toml new file mode 100644 index 000000000..dce1515c3 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-05/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "cons-list" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-05/src/main.rs b/src/doc/book/listings/ch15-smart-pointers/listing-15-05/src/main.rs new file mode 100644 index 000000000..22f7d8338 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-05/src/main.rs @@ -0,0 +1,10 @@ +enum List { + Cons(i32, Box), + Nil, +} + +use crate::List::{Cons, Nil}; + +fn main() { + let list = Cons(1, Box::new(Cons(2, Box::new(Cons(3, Box::new(Nil)))))); +} diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-06/Cargo.lock b/src/doc/book/listings/ch15-smart-pointers/listing-15-06/Cargo.lock new file mode 100644 index 000000000..4297c6733 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-06/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "deref-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-06/Cargo.toml b/src/doc/book/listings/ch15-smart-pointers/listing-15-06/Cargo.toml new file mode 100644 index 000000000..67ec198f7 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-06/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "deref-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-06/src/main.rs b/src/doc/book/listings/ch15-smart-pointers/listing-15-06/src/main.rs new file mode 100644 index 000000000..174b620cf --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-06/src/main.rs @@ -0,0 +1,7 @@ +fn main() { + let x = 5; + let y = &x; + + assert_eq!(5, x); + assert_eq!(5, *y); +} diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-07/Cargo.lock b/src/doc/book/listings/ch15-smart-pointers/listing-15-07/Cargo.lock new file mode 100644 index 000000000..4297c6733 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-07/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "deref-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-07/Cargo.toml b/src/doc/book/listings/ch15-smart-pointers/listing-15-07/Cargo.toml new file mode 100644 index 000000000..67ec198f7 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-07/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "deref-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-07/src/main.rs b/src/doc/book/listings/ch15-smart-pointers/listing-15-07/src/main.rs new file mode 100644 index 000000000..4933a416b --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-07/src/main.rs @@ -0,0 +1,7 @@ +fn main() { + let x = 5; + let y = Box::new(x); + + assert_eq!(5, x); + assert_eq!(5, *y); +} diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-08/Cargo.lock b/src/doc/book/listings/ch15-smart-pointers/listing-15-08/Cargo.lock new file mode 100644 index 000000000..4297c6733 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-08/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "deref-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-08/Cargo.toml b/src/doc/book/listings/ch15-smart-pointers/listing-15-08/Cargo.toml new file mode 100644 index 000000000..67ec198f7 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-08/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "deref-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-08/src/main.rs b/src/doc/book/listings/ch15-smart-pointers/listing-15-08/src/main.rs new file mode 100644 index 000000000..f48594673 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-08/src/main.rs @@ -0,0 +1,11 @@ +// ANCHOR: here +struct MyBox(T); + +impl MyBox { + fn new(x: T) -> MyBox { + MyBox(x) + } +} +// ANCHOR_END: here + +fn main() {} diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-09/Cargo.lock b/src/doc/book/listings/ch15-smart-pointers/listing-15-09/Cargo.lock new file mode 100644 index 000000000..4297c6733 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-09/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "deref-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-09/Cargo.toml b/src/doc/book/listings/ch15-smart-pointers/listing-15-09/Cargo.toml new file mode 100644 index 000000000..67ec198f7 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-09/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "deref-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-09/output.txt b/src/doc/book/listings/ch15-smart-pointers/listing-15-09/output.txt new file mode 100644 index 000000000..75e5f1c8c --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-09/output.txt @@ -0,0 +1,10 @@ +$ cargo run + Compiling deref-example v0.1.0 (file:///projects/deref-example) +error[E0614]: type `MyBox<{integer}>` cannot be dereferenced + --> src/main.rs:14:19 + | +14 | assert_eq!(5, *y); + | ^^ + +For more information about this error, try `rustc --explain E0614`. +error: could not compile `deref-example` due to previous error diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-09/src/main.rs b/src/doc/book/listings/ch15-smart-pointers/listing-15-09/src/main.rs new file mode 100644 index 000000000..d07f2d78a --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-09/src/main.rs @@ -0,0 +1,17 @@ +struct MyBox(T); + +impl MyBox { + fn new(x: T) -> MyBox { + MyBox(x) + } +} + +// ANCHOR: here +fn main() { + let x = 5; + let y = MyBox::new(x); + + assert_eq!(5, x); + assert_eq!(5, *y); +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-10/Cargo.lock b/src/doc/book/listings/ch15-smart-pointers/listing-15-10/Cargo.lock new file mode 100644 index 000000000..4297c6733 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-10/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "deref-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-10/Cargo.toml b/src/doc/book/listings/ch15-smart-pointers/listing-15-10/Cargo.toml new file mode 100644 index 000000000..67ec198f7 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-10/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "deref-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-10/src/main.rs b/src/doc/book/listings/ch15-smart-pointers/listing-15-10/src/main.rs new file mode 100644 index 000000000..cce754d08 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-10/src/main.rs @@ -0,0 +1,27 @@ +// ANCHOR: here +use std::ops::Deref; + +impl Deref for MyBox { + type Target = T; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} +// ANCHOR_END: here + +struct MyBox(T); + +impl MyBox { + fn new(x: T) -> MyBox { + MyBox(x) + } +} + +fn main() { + let x = 5; + let y = MyBox::new(x); + + assert_eq!(5, x); + assert_eq!(5, *y); +} diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-11/Cargo.lock b/src/doc/book/listings/ch15-smart-pointers/listing-15-11/Cargo.lock new file mode 100644 index 000000000..4297c6733 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-11/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "deref-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-11/Cargo.toml b/src/doc/book/listings/ch15-smart-pointers/listing-15-11/Cargo.toml new file mode 100644 index 000000000..67ec198f7 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-11/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "deref-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-11/src/main.rs b/src/doc/book/listings/ch15-smart-pointers/listing-15-11/src/main.rs new file mode 100644 index 000000000..77a88c91f --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-11/src/main.rs @@ -0,0 +1,7 @@ +// ANCHOR: here +fn hello(name: &str) { + println!("Hello, {name}!"); +} +// ANCHOR_END: here + +fn main() {} diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-12/Cargo.lock b/src/doc/book/listings/ch15-smart-pointers/listing-15-12/Cargo.lock new file mode 100644 index 000000000..4297c6733 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-12/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "deref-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-12/Cargo.toml b/src/doc/book/listings/ch15-smart-pointers/listing-15-12/Cargo.toml new file mode 100644 index 000000000..67ec198f7 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-12/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "deref-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-12/src/main.rs b/src/doc/book/listings/ch15-smart-pointers/listing-15-12/src/main.rs new file mode 100644 index 000000000..8cd3893cb --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-12/src/main.rs @@ -0,0 +1,28 @@ +use std::ops::Deref; + +impl Deref for MyBox { + type Target = T; + + fn deref(&self) -> &T { + &self.0 + } +} + +struct MyBox(T); + +impl MyBox { + fn new(x: T) -> MyBox { + MyBox(x) + } +} + +fn hello(name: &str) { + println!("Hello, {name}!"); +} + +// ANCHOR: here +fn main() { + let m = MyBox::new(String::from("Rust")); + hello(&m); +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-13/Cargo.lock b/src/doc/book/listings/ch15-smart-pointers/listing-15-13/Cargo.lock new file mode 100644 index 000000000..4297c6733 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-13/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "deref-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-13/Cargo.toml b/src/doc/book/listings/ch15-smart-pointers/listing-15-13/Cargo.toml new file mode 100644 index 000000000..67ec198f7 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-13/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "deref-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-13/src/main.rs b/src/doc/book/listings/ch15-smart-pointers/listing-15-13/src/main.rs new file mode 100644 index 000000000..9debe2a31 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-13/src/main.rs @@ -0,0 +1,28 @@ +use std::ops::Deref; + +impl Deref for MyBox { + type Target = T; + + fn deref(&self) -> &T { + &self.0 + } +} + +struct MyBox(T); + +impl MyBox { + fn new(x: T) -> MyBox { + MyBox(x) + } +} + +fn hello(name: &str) { + println!("Hello, {name}!"); +} + +// ANCHOR: here +fn main() { + let m = MyBox::new(String::from("Rust")); + hello(&(*m)[..]); +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-14/Cargo.lock b/src/doc/book/listings/ch15-smart-pointers/listing-15-14/Cargo.lock new file mode 100644 index 000000000..eb8a2817a --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-14/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "drop-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-14/Cargo.toml b/src/doc/book/listings/ch15-smart-pointers/listing-15-14/Cargo.toml new file mode 100644 index 000000000..1e4c99481 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-14/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "drop-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-14/output.txt b/src/doc/book/listings/ch15-smart-pointers/listing-15-14/output.txt new file mode 100644 index 000000000..4e795949a --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-14/output.txt @@ -0,0 +1,7 @@ +$ cargo run + Compiling drop-example v0.1.0 (file:///projects/drop-example) + Finished dev [unoptimized + debuginfo] target(s) in 0.60s + Running `target/debug/drop-example` +CustomSmartPointers created. +Dropping CustomSmartPointer with data `other stuff`! +Dropping CustomSmartPointer with data `my stuff`! diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-14/src/main.rs b/src/doc/book/listings/ch15-smart-pointers/listing-15-14/src/main.rs new file mode 100644 index 000000000..231612ae6 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-14/src/main.rs @@ -0,0 +1,19 @@ +struct CustomSmartPointer { + data: String, +} + +impl Drop for CustomSmartPointer { + fn drop(&mut self) { + println!("Dropping CustomSmartPointer with data `{}`!", self.data); + } +} + +fn main() { + let c = CustomSmartPointer { + data: String::from("my stuff"), + }; + let d = CustomSmartPointer { + data: String::from("other stuff"), + }; + println!("CustomSmartPointers created."); +} diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-15/Cargo.lock b/src/doc/book/listings/ch15-smart-pointers/listing-15-15/Cargo.lock new file mode 100644 index 000000000..eb8a2817a --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-15/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "drop-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-15/Cargo.toml b/src/doc/book/listings/ch15-smart-pointers/listing-15-15/Cargo.toml new file mode 100644 index 000000000..1e4c99481 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-15/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "drop-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-15/output.txt b/src/doc/book/listings/ch15-smart-pointers/listing-15-15/output.txt new file mode 100644 index 000000000..a38c9ccb7 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-15/output.txt @@ -0,0 +1,13 @@ +$ cargo run + Compiling drop-example v0.1.0 (file:///projects/drop-example) +error[E0040]: explicit use of destructor method + --> src/main.rs:16:7 + | +16 | c.drop(); + | --^^^^-- + | | | + | | explicit destructor calls not allowed + | help: consider using `drop` function: `drop(c)` + +For more information about this error, try `rustc --explain E0040`. +error: could not compile `drop-example` due to previous error diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-15/src/main.rs b/src/doc/book/listings/ch15-smart-pointers/listing-15-15/src/main.rs new file mode 100644 index 000000000..ff3b391a9 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-15/src/main.rs @@ -0,0 +1,20 @@ +struct CustomSmartPointer { + data: String, +} + +impl Drop for CustomSmartPointer { + fn drop(&mut self) { + println!("Dropping CustomSmartPointer with data `{}`!", self.data); + } +} + +// ANCHOR: here +fn main() { + let c = CustomSmartPointer { + data: String::from("some data"), + }; + println!("CustomSmartPointer created."); + c.drop(); + println!("CustomSmartPointer dropped before the end of main."); +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-16/Cargo.lock b/src/doc/book/listings/ch15-smart-pointers/listing-15-16/Cargo.lock new file mode 100644 index 000000000..eb8a2817a --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-16/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "drop-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-16/Cargo.toml b/src/doc/book/listings/ch15-smart-pointers/listing-15-16/Cargo.toml new file mode 100644 index 000000000..1e4c99481 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-16/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "drop-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-16/output.txt b/src/doc/book/listings/ch15-smart-pointers/listing-15-16/output.txt new file mode 100644 index 000000000..e960cd89a --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-16/output.txt @@ -0,0 +1,7 @@ +$ cargo run + Compiling drop-example v0.1.0 (file:///projects/drop-example) + Finished dev [unoptimized + debuginfo] target(s) in 0.73s + Running `target/debug/drop-example` +CustomSmartPointer created. +Dropping CustomSmartPointer with data `some data`! +CustomSmartPointer dropped before the end of main. diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-16/src/main.rs b/src/doc/book/listings/ch15-smart-pointers/listing-15-16/src/main.rs new file mode 100644 index 000000000..f11715c45 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-16/src/main.rs @@ -0,0 +1,20 @@ +struct CustomSmartPointer { + data: String, +} + +impl Drop for CustomSmartPointer { + fn drop(&mut self) { + println!("Dropping CustomSmartPointer with data `{}`!", self.data); + } +} + +// ANCHOR: here +fn main() { + let c = CustomSmartPointer { + data: String::from("some data"), + }; + println!("CustomSmartPointer created."); + drop(c); + println!("CustomSmartPointer dropped before the end of main."); +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-17/Cargo.lock b/src/doc/book/listings/ch15-smart-pointers/listing-15-17/Cargo.lock new file mode 100644 index 000000000..a792c49aa --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-17/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "cons-list" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-17/Cargo.toml b/src/doc/book/listings/ch15-smart-pointers/listing-15-17/Cargo.toml new file mode 100644 index 000000000..dce1515c3 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-17/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "cons-list" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-17/output.txt b/src/doc/book/listings/ch15-smart-pointers/listing-15-17/output.txt new file mode 100644 index 000000000..ab314d883 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-17/output.txt @@ -0,0 +1,14 @@ +$ cargo run + Compiling cons-list v0.1.0 (file:///projects/cons-list) +error[E0382]: use of moved value: `a` + --> src/main.rs:11:30 + | +9 | let a = Cons(5, Box::new(Cons(10, Box::new(Nil)))); + | - move occurs because `a` has type `List`, which does not implement the `Copy` trait +10 | let b = Cons(3, Box::new(a)); + | - value moved here +11 | let c = Cons(4, Box::new(a)); + | ^ value used here after move + +For more information about this error, try `rustc --explain E0382`. +error: could not compile `cons-list` due to previous error diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-17/src/main.rs b/src/doc/book/listings/ch15-smart-pointers/listing-15-17/src/main.rs new file mode 100644 index 000000000..47c33e4c4 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-17/src/main.rs @@ -0,0 +1,12 @@ +enum List { + Cons(i32, Box), + Nil, +} + +use crate::List::{Cons, Nil}; + +fn main() { + let a = Cons(5, Box::new(Cons(10, Box::new(Nil)))); + let b = Cons(3, Box::new(a)); + let c = Cons(4, Box::new(a)); +} diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-18/Cargo.lock b/src/doc/book/listings/ch15-smart-pointers/listing-15-18/Cargo.lock new file mode 100644 index 000000000..a792c49aa --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-18/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "cons-list" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-18/Cargo.toml b/src/doc/book/listings/ch15-smart-pointers/listing-15-18/Cargo.toml new file mode 100644 index 000000000..dce1515c3 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-18/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "cons-list" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-18/src/main.rs b/src/doc/book/listings/ch15-smart-pointers/listing-15-18/src/main.rs new file mode 100644 index 000000000..602f7de40 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-18/src/main.rs @@ -0,0 +1,13 @@ +enum List { + Cons(i32, Rc), + Nil, +} + +use crate::List::{Cons, Nil}; +use std::rc::Rc; + +fn main() { + let a = Rc::new(Cons(5, Rc::new(Cons(10, Rc::new(Nil))))); + let b = Cons(3, Rc::clone(&a)); + let c = Cons(4, Rc::clone(&a)); +} diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-19/Cargo.lock b/src/doc/book/listings/ch15-smart-pointers/listing-15-19/Cargo.lock new file mode 100644 index 000000000..a792c49aa --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-19/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "cons-list" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-19/Cargo.toml b/src/doc/book/listings/ch15-smart-pointers/listing-15-19/Cargo.toml new file mode 100644 index 000000000..dce1515c3 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-19/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "cons-list" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-19/output.txt b/src/doc/book/listings/ch15-smart-pointers/listing-15-19/output.txt new file mode 100644 index 000000000..6a8cc8efe --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-19/output.txt @@ -0,0 +1,8 @@ +$ cargo run + Compiling cons-list v0.1.0 (file:///projects/cons-list) + Finished dev [unoptimized + debuginfo] target(s) in 0.45s + Running `target/debug/cons-list` +count after creating a = 1 +count after creating b = 2 +count after creating c = 3 +count after c goes out of scope = 2 diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-19/src/main.rs b/src/doc/book/listings/ch15-smart-pointers/listing-15-19/src/main.rs new file mode 100644 index 000000000..1bd7bc533 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-19/src/main.rs @@ -0,0 +1,21 @@ +enum List { + Cons(i32, Rc), + Nil, +} + +use crate::List::{Cons, Nil}; +use std::rc::Rc; + +// ANCHOR: here +fn main() { + let a = Rc::new(Cons(5, Rc::new(Cons(10, Rc::new(Nil))))); + println!("count after creating a = {}", Rc::strong_count(&a)); + let b = Cons(3, Rc::clone(&a)); + println!("count after creating b = {}", Rc::strong_count(&a)); + { + let c = Cons(4, Rc::clone(&a)); + println!("count after creating c = {}", Rc::strong_count(&a)); + } + println!("count after c goes out of scope = {}", Rc::strong_count(&a)); +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-20/Cargo.lock b/src/doc/book/listings/ch15-smart-pointers/listing-15-20/Cargo.lock new file mode 100644 index 000000000..4dc2226a5 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-20/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "limit-tracker" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-20/Cargo.toml b/src/doc/book/listings/ch15-smart-pointers/listing-15-20/Cargo.toml new file mode 100644 index 000000000..98c3f537c --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-20/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "limit-tracker" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-20/src/lib.rs b/src/doc/book/listings/ch15-smart-pointers/listing-15-20/src/lib.rs new file mode 100644 index 000000000..a5181834c --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-20/src/lib.rs @@ -0,0 +1,38 @@ +pub trait Messenger { + fn send(&self, msg: &str); +} + +pub struct LimitTracker<'a, T: Messenger> { + messenger: &'a T, + value: usize, + max: usize, +} + +impl<'a, T> LimitTracker<'a, T> +where + T: Messenger, +{ + pub fn new(messenger: &'a T, max: usize) -> LimitTracker<'a, T> { + LimitTracker { + messenger, + value: 0, + max, + } + } + + pub fn set_value(&mut self, value: usize) { + self.value = value; + + let percentage_of_max = self.value as f64 / self.max as f64; + + if percentage_of_max >= 1.0 { + self.messenger.send("Error: You are over your quota!"); + } else if percentage_of_max >= 0.9 { + self.messenger + .send("Urgent warning: You've used up over 90% of your quota!"); + } else if percentage_of_max >= 0.75 { + self.messenger + .send("Warning: You've used up over 75% of your quota!"); + } + } +} diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-21/Cargo.lock b/src/doc/book/listings/ch15-smart-pointers/listing-15-21/Cargo.lock new file mode 100644 index 000000000..4dc2226a5 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-21/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "limit-tracker" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-21/Cargo.toml b/src/doc/book/listings/ch15-smart-pointers/listing-15-21/Cargo.toml new file mode 100644 index 000000000..98c3f537c --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-21/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "limit-tracker" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-21/output.txt b/src/doc/book/listings/ch15-smart-pointers/listing-15-21/output.txt new file mode 100644 index 000000000..6b07b66ea --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-21/output.txt @@ -0,0 +1,14 @@ +$ cargo test + Compiling limit-tracker v0.1.0 (file:///projects/limit-tracker) +error[E0596]: cannot borrow `self.sent_messages` as mutable, as it is behind a `&` reference + --> src/lib.rs:58:13 + | +2 | fn send(&self, msg: &str); + | ----- help: consider changing that to be a mutable reference: `&mut self` +... +58 | self.sent_messages.push(String::from(message)); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `self` is a `&` reference, so the data it refers to cannot be borrowed as mutable + +For more information about this error, try `rustc --explain E0596`. +error: could not compile `limit-tracker` due to previous error +warning: build failed, waiting for other jobs to finish... diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-21/src/lib.rs b/src/doc/book/listings/ch15-smart-pointers/listing-15-21/src/lib.rs new file mode 100644 index 000000000..4572d4313 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-21/src/lib.rs @@ -0,0 +1,73 @@ +pub trait Messenger { + fn send(&self, msg: &str); +} + +pub struct LimitTracker<'a, T: Messenger> { + messenger: &'a T, + value: usize, + max: usize, +} + +impl<'a, T> LimitTracker<'a, T> +where + T: Messenger, +{ + pub fn new(messenger: &'a T, max: usize) -> LimitTracker<'a, T> { + LimitTracker { + messenger, + value: 0, + max, + } + } + + pub fn set_value(&mut self, value: usize) { + self.value = value; + + let percentage_of_max = self.value as f64 / self.max as f64; + + if percentage_of_max >= 1.0 { + self.messenger.send("Error: You are over your quota!"); + } else if percentage_of_max >= 0.9 { + self.messenger + .send("Urgent warning: You've used up over 90% of your quota!"); + } else if percentage_of_max >= 0.75 { + self.messenger + .send("Warning: You've used up over 75% of your quota!"); + } + } +} + +// ANCHOR: here +#[cfg(test)] +mod tests { + use super::*; + + struct MockMessenger { + sent_messages: Vec, + } + + impl MockMessenger { + fn new() -> MockMessenger { + MockMessenger { + sent_messages: vec![], + } + } + } + + impl Messenger for MockMessenger { + fn send(&self, message: &str) { + self.sent_messages.push(String::from(message)); + } + } + + #[test] + fn it_sends_an_over_75_percent_warning_message() { + let mock_messenger = MockMessenger::new(); + let mut limit_tracker = LimitTracker::new(&mock_messenger, 100); + + limit_tracker.set_value(80); + + assert_eq!(mock_messenger.sent_messages.len(), 1); + } +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-22/Cargo.lock b/src/doc/book/listings/ch15-smart-pointers/listing-15-22/Cargo.lock new file mode 100644 index 000000000..4dc2226a5 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-22/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "limit-tracker" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-22/Cargo.toml b/src/doc/book/listings/ch15-smart-pointers/listing-15-22/Cargo.toml new file mode 100644 index 000000000..98c3f537c --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-22/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "limit-tracker" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-22/src/lib.rs b/src/doc/book/listings/ch15-smart-pointers/listing-15-22/src/lib.rs new file mode 100644 index 000000000..a77ffa41c --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-22/src/lib.rs @@ -0,0 +1,78 @@ +pub trait Messenger { + fn send(&self, msg: &str); +} + +pub struct LimitTracker<'a, T: Messenger> { + messenger: &'a T, + value: usize, + max: usize, +} + +impl<'a, T> LimitTracker<'a, T> +where + T: Messenger, +{ + pub fn new(messenger: &'a T, max: usize) -> LimitTracker<'a, T> { + LimitTracker { + messenger, + value: 0, + max, + } + } + + pub fn set_value(&mut self, value: usize) { + self.value = value; + + let percentage_of_max = self.value as f64 / self.max as f64; + + if percentage_of_max >= 1.0 { + self.messenger.send("Error: You are over your quota!"); + } else if percentage_of_max >= 0.9 { + self.messenger + .send("Urgent warning: You've used up over 90% of your quota!"); + } else if percentage_of_max >= 0.75 { + self.messenger + .send("Warning: You've used up over 75% of your quota!"); + } + } +} + +// ANCHOR: here +#[cfg(test)] +mod tests { + use super::*; + use std::cell::RefCell; + + struct MockMessenger { + sent_messages: RefCell>, + } + + impl MockMessenger { + fn new() -> MockMessenger { + MockMessenger { + sent_messages: RefCell::new(vec![]), + } + } + } + + impl Messenger for MockMessenger { + fn send(&self, message: &str) { + self.sent_messages.borrow_mut().push(String::from(message)); + } + } + + #[test] + fn it_sends_an_over_75_percent_warning_message() { + // --snip-- + // ANCHOR_END: here + let mock_messenger = MockMessenger::new(); + let mut limit_tracker = LimitTracker::new(&mock_messenger, 100); + + limit_tracker.set_value(80); + // ANCHOR: here + + // ANCHOR: here + assert_eq!(mock_messenger.sent_messages.borrow().len(), 1); + } +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-23/Cargo.lock b/src/doc/book/listings/ch15-smart-pointers/listing-15-23/Cargo.lock new file mode 100644 index 000000000..4dc2226a5 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-23/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "limit-tracker" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-23/Cargo.toml b/src/doc/book/listings/ch15-smart-pointers/listing-15-23/Cargo.toml new file mode 100644 index 000000000..98c3f537c --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-23/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "limit-tracker" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-23/output.txt b/src/doc/book/listings/ch15-smart-pointers/listing-15-23/output.txt new file mode 100644 index 000000000..c44e69f13 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-23/output.txt @@ -0,0 +1,21 @@ +$ cargo test + Compiling limit-tracker v0.1.0 (file:///projects/limit-tracker) + Finished test [unoptimized + debuginfo] target(s) in 0.91s + Running unittests src/lib.rs (target/debug/deps/limit_tracker-e599811fa246dbde) + +running 1 test +test tests::it_sends_an_over_75_percent_warning_message ... FAILED + +failures: + +---- tests::it_sends_an_over_75_percent_warning_message stdout ---- +thread 'main' panicked at 'already borrowed: BorrowMutError', src/lib.rs:60:53 +note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace + + +failures: + tests::it_sends_an_over_75_percent_warning_message + +test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s + +error: test failed, to rerun pass '--lib' diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-23/src/lib.rs b/src/doc/book/listings/ch15-smart-pointers/listing-15-23/src/lib.rs new file mode 100644 index 000000000..7d288e680 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-23/src/lib.rs @@ -0,0 +1,78 @@ +pub trait Messenger { + fn send(&self, msg: &str); +} + +pub struct LimitTracker<'a, T: Messenger> { + messenger: &'a T, + value: usize, + max: usize, +} + +impl<'a, T> LimitTracker<'a, T> +where + T: Messenger, +{ + pub fn new(messenger: &'a T, max: usize) -> LimitTracker<'a, T> { + LimitTracker { + messenger, + value: 0, + max, + } + } + + pub fn set_value(&mut self, value: usize) { + self.value = value; + + let percentage_of_max = self.value as f64 / self.max as f64; + + if percentage_of_max >= 1.0 { + self.messenger.send("Error: You are over your quota!"); + } else if percentage_of_max >= 0.9 { + self.messenger + .send("Urgent warning: You've used up over 90% of your quota!"); + } else if percentage_of_max >= 0.75 { + self.messenger + .send("Warning: You've used up over 75% of your quota!"); + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use std::cell::RefCell; + + struct MockMessenger { + sent_messages: RefCell>, + } + + impl MockMessenger { + fn new() -> MockMessenger { + MockMessenger { + sent_messages: RefCell::new(vec![]), + } + } + } + + // ANCHOR: here + impl Messenger for MockMessenger { + fn send(&self, message: &str) { + let mut one_borrow = self.sent_messages.borrow_mut(); + let mut two_borrow = self.sent_messages.borrow_mut(); + + one_borrow.push(String::from(message)); + two_borrow.push(String::from(message)); + } + } + // ANCHOR_END: here + + #[test] + fn it_sends_an_over_75_percent_warning_message() { + let mock_messenger = MockMessenger::new(); + let mut limit_tracker = LimitTracker::new(&mock_messenger, 100); + + limit_tracker.set_value(80); + + assert_eq!(mock_messenger.sent_messages.borrow().len(), 1); + } +} diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-24/Cargo.lock b/src/doc/book/listings/ch15-smart-pointers/listing-15-24/Cargo.lock new file mode 100644 index 000000000..a792c49aa --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-24/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "cons-list" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-24/Cargo.toml b/src/doc/book/listings/ch15-smart-pointers/listing-15-24/Cargo.toml new file mode 100644 index 000000000..dce1515c3 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-24/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "cons-list" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-24/output.txt b/src/doc/book/listings/ch15-smart-pointers/listing-15-24/output.txt new file mode 100644 index 000000000..21b3530d9 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-24/output.txt @@ -0,0 +1,7 @@ +$ cargo run + Compiling cons-list v0.1.0 (file:///projects/cons-list) + Finished dev [unoptimized + debuginfo] target(s) in 0.63s + Running `target/debug/cons-list` +a after = Cons(RefCell { value: 15 }, Nil) +b after = Cons(RefCell { value: 3 }, Cons(RefCell { value: 15 }, Nil)) +c after = Cons(RefCell { value: 4 }, Cons(RefCell { value: 15 }, Nil)) diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-24/src/main.rs b/src/doc/book/listings/ch15-smart-pointers/listing-15-24/src/main.rs new file mode 100644 index 000000000..e225bd862 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-24/src/main.rs @@ -0,0 +1,24 @@ +#[derive(Debug)] +enum List { + Cons(Rc>, Rc), + Nil, +} + +use crate::List::{Cons, Nil}; +use std::cell::RefCell; +use std::rc::Rc; + +fn main() { + let value = Rc::new(RefCell::new(5)); + + let a = Rc::new(Cons(Rc::clone(&value), Rc::new(Nil))); + + let b = Cons(Rc::new(RefCell::new(3)), Rc::clone(&a)); + let c = Cons(Rc::new(RefCell::new(4)), Rc::clone(&a)); + + *value.borrow_mut() += 10; + + println!("a after = {:?}", a); + println!("b after = {:?}", b); + println!("c after = {:?}", c); +} diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-25/Cargo.lock b/src/doc/book/listings/ch15-smart-pointers/listing-15-25/Cargo.lock new file mode 100644 index 000000000..a792c49aa --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-25/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "cons-list" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-25/Cargo.toml b/src/doc/book/listings/ch15-smart-pointers/listing-15-25/Cargo.toml new file mode 100644 index 000000000..dce1515c3 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-25/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "cons-list" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-25/src/main.rs b/src/doc/book/listings/ch15-smart-pointers/listing-15-25/src/main.rs new file mode 100644 index 000000000..f36c7fd06 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-25/src/main.rs @@ -0,0 +1,20 @@ +use crate::List::{Cons, Nil}; +use std::cell::RefCell; +use std::rc::Rc; + +#[derive(Debug)] +enum List { + Cons(i32, RefCell>), + Nil, +} + +impl List { + fn tail(&self) -> Option<&RefCell>> { + match self { + Cons(_, item) => Some(item), + Nil => None, + } + } +} + +fn main() {} diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-26/Cargo.lock b/src/doc/book/listings/ch15-smart-pointers/listing-15-26/Cargo.lock new file mode 100644 index 000000000..a792c49aa --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-26/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "cons-list" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-26/Cargo.toml b/src/doc/book/listings/ch15-smart-pointers/listing-15-26/Cargo.toml new file mode 100644 index 000000000..dce1515c3 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-26/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "cons-list" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-26/output.txt b/src/doc/book/listings/ch15-smart-pointers/listing-15-26/output.txt new file mode 100644 index 000000000..8b8eb40b6 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-26/output.txt @@ -0,0 +1,11 @@ +$ cargo run + Compiling cons-list v0.1.0 (file:///projects/cons-list) + Finished dev [unoptimized + debuginfo] target(s) in 0.53s + Running `target/debug/cons-list` +a initial rc count = 1 +a next item = Some(RefCell { value: Nil }) +a rc count after b creation = 2 +b initial rc count = 1 +b next item = Some(RefCell { value: Cons(5, RefCell { value: Nil }) }) +b rc count after changing a = 2 +a rc count after changing a = 2 diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-26/src/main.rs b/src/doc/book/listings/ch15-smart-pointers/listing-15-26/src/main.rs new file mode 100644 index 000000000..08963aaa5 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-26/src/main.rs @@ -0,0 +1,44 @@ +use crate::List::{Cons, Nil}; +use std::cell::RefCell; +use std::rc::Rc; + +#[derive(Debug)] +enum List { + Cons(i32, RefCell>), + Nil, +} + +impl List { + fn tail(&self) -> Option<&RefCell>> { + match self { + Cons(_, item) => Some(item), + Nil => None, + } + } +} + +// ANCHOR: here +fn main() { + let a = Rc::new(Cons(5, RefCell::new(Rc::new(Nil)))); + + println!("a initial rc count = {}", Rc::strong_count(&a)); + println!("a next item = {:?}", a.tail()); + + let b = Rc::new(Cons(10, RefCell::new(Rc::clone(&a)))); + + println!("a rc count after b creation = {}", Rc::strong_count(&a)); + println!("b initial rc count = {}", Rc::strong_count(&b)); + println!("b next item = {:?}", b.tail()); + + if let Some(link) = a.tail() { + *link.borrow_mut() = Rc::clone(&b); + } + + println!("b rc count after changing a = {}", Rc::strong_count(&b)); + println!("a rc count after changing a = {}", Rc::strong_count(&a)); + + // Uncomment the next line to see that we have a cycle; + // it will overflow the stack + // println!("a next item = {:?}", a.tail()); +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-27/Cargo.lock b/src/doc/book/listings/ch15-smart-pointers/listing-15-27/Cargo.lock new file mode 100644 index 000000000..dd1f00a87 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-27/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "tree" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-27/Cargo.toml b/src/doc/book/listings/ch15-smart-pointers/listing-15-27/Cargo.toml new file mode 100644 index 000000000..0bbf897d0 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-27/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "tree" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-27/src/main.rs b/src/doc/book/listings/ch15-smart-pointers/listing-15-27/src/main.rs new file mode 100644 index 000000000..335d154dd --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-27/src/main.rs @@ -0,0 +1,24 @@ +// ANCHOR: here +use std::cell::RefCell; +use std::rc::Rc; + +#[derive(Debug)] +struct Node { + value: i32, + children: RefCell>>, +} +// ANCHOR_END: here + +// ANCHOR: there +fn main() { + let leaf = Rc::new(Node { + value: 3, + children: RefCell::new(vec![]), + }); + + let branch = Rc::new(Node { + value: 5, + children: RefCell::new(vec![Rc::clone(&leaf)]), + }); +} +// ANCHOR_END: there diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-28/Cargo.lock b/src/doc/book/listings/ch15-smart-pointers/listing-15-28/Cargo.lock new file mode 100644 index 000000000..dd1f00a87 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-28/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "tree" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-28/Cargo.toml b/src/doc/book/listings/ch15-smart-pointers/listing-15-28/Cargo.toml new file mode 100644 index 000000000..0bbf897d0 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-28/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "tree" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-28/src/main.rs b/src/doc/book/listings/ch15-smart-pointers/listing-15-28/src/main.rs new file mode 100644 index 000000000..fabd1cbce --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-28/src/main.rs @@ -0,0 +1,33 @@ +// ANCHOR: here +use std::cell::RefCell; +use std::rc::{Rc, Weak}; + +#[derive(Debug)] +struct Node { + value: i32, + parent: RefCell>, + children: RefCell>>, +} +// ANCHOR_END: here + +// ANCHOR: there +fn main() { + let leaf = Rc::new(Node { + value: 3, + parent: RefCell::new(Weak::new()), + children: RefCell::new(vec![]), + }); + + println!("leaf parent = {:?}", leaf.parent.borrow().upgrade()); + + let branch = Rc::new(Node { + value: 5, + parent: RefCell::new(Weak::new()), + children: RefCell::new(vec![Rc::clone(&leaf)]), + }); + + *leaf.parent.borrow_mut() = Rc::downgrade(&branch); + + println!("leaf parent = {:?}", leaf.parent.borrow().upgrade()); +} +// ANCHOR_END: there diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-29/Cargo.lock b/src/doc/book/listings/ch15-smart-pointers/listing-15-29/Cargo.lock new file mode 100644 index 000000000..dd1f00a87 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-29/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "tree" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-29/Cargo.toml b/src/doc/book/listings/ch15-smart-pointers/listing-15-29/Cargo.toml new file mode 100644 index 000000000..0bbf897d0 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-29/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "tree" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-29/src/main.rs b/src/doc/book/listings/ch15-smart-pointers/listing-15-29/src/main.rs new file mode 100644 index 000000000..ea13df0eb --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-29/src/main.rs @@ -0,0 +1,54 @@ +use std::cell::RefCell; +use std::rc::{Rc, Weak}; + +#[derive(Debug)] +struct Node { + value: i32, + parent: RefCell>, + children: RefCell>>, +} + +// ANCHOR: here +fn main() { + let leaf = Rc::new(Node { + value: 3, + parent: RefCell::new(Weak::new()), + children: RefCell::new(vec![]), + }); + + println!( + "leaf strong = {}, weak = {}", + Rc::strong_count(&leaf), + Rc::weak_count(&leaf), + ); + + { + let branch = Rc::new(Node { + value: 5, + parent: RefCell::new(Weak::new()), + children: RefCell::new(vec![Rc::clone(&leaf)]), + }); + + *leaf.parent.borrow_mut() = Rc::downgrade(&branch); + + println!( + "branch strong = {}, weak = {}", + Rc::strong_count(&branch), + Rc::weak_count(&branch), + ); + + println!( + "leaf strong = {}, weak = {}", + Rc::strong_count(&leaf), + Rc::weak_count(&leaf), + ); + } + + println!("leaf parent = {:?}", leaf.parent.borrow().upgrade()); + println!( + "leaf strong = {}, weak = {}", + Rc::strong_count(&leaf), + Rc::weak_count(&leaf), + ); +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch15-smart-pointers/no-listing-01-cant-borrow-immutable-as-mutable/Cargo.lock b/src/doc/book/listings/ch15-smart-pointers/no-listing-01-cant-borrow-immutable-as-mutable/Cargo.lock new file mode 100644 index 000000000..340f6604a --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/no-listing-01-cant-borrow-immutable-as-mutable/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "borrowing" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch15-smart-pointers/no-listing-01-cant-borrow-immutable-as-mutable/Cargo.toml b/src/doc/book/listings/ch15-smart-pointers/no-listing-01-cant-borrow-immutable-as-mutable/Cargo.toml new file mode 100644 index 000000000..16f92447f --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/no-listing-01-cant-borrow-immutable-as-mutable/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "borrowing" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch15-smart-pointers/no-listing-01-cant-borrow-immutable-as-mutable/output.txt b/src/doc/book/listings/ch15-smart-pointers/no-listing-01-cant-borrow-immutable-as-mutable/output.txt new file mode 100644 index 000000000..8e84746ee --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/no-listing-01-cant-borrow-immutable-as-mutable/output.txt @@ -0,0 +1,12 @@ +$ cargo run + Compiling borrowing v0.1.0 (file:///projects/borrowing) +error[E0596]: cannot borrow `x` as mutable, as it is not declared as mutable + --> src/main.rs:3:13 + | +2 | let x = 5; + | - help: consider changing this to be mutable: `mut x` +3 | let y = &mut x; + | ^^^^^^ cannot borrow as mutable + +For more information about this error, try `rustc --explain E0596`. +error: could not compile `borrowing` due to previous error diff --git a/src/doc/book/listings/ch15-smart-pointers/no-listing-01-cant-borrow-immutable-as-mutable/src/main.rs b/src/doc/book/listings/ch15-smart-pointers/no-listing-01-cant-borrow-immutable-as-mutable/src/main.rs new file mode 100644 index 000000000..8f48d41c1 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/no-listing-01-cant-borrow-immutable-as-mutable/src/main.rs @@ -0,0 +1,4 @@ +fn main() { + let x = 5; + let y = &mut x; +} diff --git a/src/doc/book/listings/ch15-smart-pointers/output-only-01-comparing-to-reference/Cargo.lock b/src/doc/book/listings/ch15-smart-pointers/output-only-01-comparing-to-reference/Cargo.lock new file mode 100644 index 000000000..4297c6733 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/output-only-01-comparing-to-reference/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "deref-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch15-smart-pointers/output-only-01-comparing-to-reference/Cargo.toml b/src/doc/book/listings/ch15-smart-pointers/output-only-01-comparing-to-reference/Cargo.toml new file mode 100644 index 000000000..67ec198f7 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/output-only-01-comparing-to-reference/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "deref-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch15-smart-pointers/output-only-01-comparing-to-reference/output.txt b/src/doc/book/listings/ch15-smart-pointers/output-only-01-comparing-to-reference/output.txt new file mode 100644 index 000000000..a03cc34e2 --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/output-only-01-comparing-to-reference/output.txt @@ -0,0 +1,23 @@ +$ cargo run + Compiling deref-example v0.1.0 (file:///projects/deref-example) +error[E0277]: can't compare `{integer}` with `&{integer}` + --> src/main.rs:6:5 + | +6 | assert_eq!(5, y); + | ^^^^^^^^^^^^^^^^ no implementation for `{integer} == &{integer}` + | + = help: the trait `PartialEq<&{integer}>` is not implemented for `{integer}` + = help: the following other types implement trait `PartialEq`: + f32 + f64 + i128 + i16 + i32 + i64 + i8 + isize + and 6 others + = note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info) + +For more information about this error, try `rustc --explain E0277`. +error: could not compile `deref-example` due to previous error diff --git a/src/doc/book/listings/ch15-smart-pointers/output-only-01-comparing-to-reference/src/main.rs b/src/doc/book/listings/ch15-smart-pointers/output-only-01-comparing-to-reference/src/main.rs new file mode 100644 index 000000000..4e20cae0b --- /dev/null +++ b/src/doc/book/listings/ch15-smart-pointers/output-only-01-comparing-to-reference/src/main.rs @@ -0,0 +1,7 @@ +fn main() { + let x = 5; + let y = &x; + + assert_eq!(5, x); + assert_eq!(5, y); +} diff --git a/src/doc/book/listings/ch16-fearless-concurrency/listing-16-01/Cargo.lock b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-01/Cargo.lock new file mode 100644 index 000000000..8ecc3ae17 --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-01/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "threads" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch16-fearless-concurrency/listing-16-01/Cargo.toml b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-01/Cargo.toml new file mode 100644 index 000000000..bd4edf762 --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-01/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "threads" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch16-fearless-concurrency/listing-16-01/src/main.rs b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-01/src/main.rs new file mode 100644 index 000000000..6305a98e3 --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-01/src/main.rs @@ -0,0 +1,16 @@ +use std::thread; +use std::time::Duration; + +fn main() { + thread::spawn(|| { + for i in 1..10 { + println!("hi number {} from the spawned thread!", i); + thread::sleep(Duration::from_millis(1)); + } + }); + + for i in 1..5 { + println!("hi number {} from the main thread!", i); + thread::sleep(Duration::from_millis(1)); + } +} diff --git a/src/doc/book/listings/ch16-fearless-concurrency/listing-16-02/Cargo.lock b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-02/Cargo.lock new file mode 100644 index 000000000..8ecc3ae17 --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-02/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "threads" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch16-fearless-concurrency/listing-16-02/Cargo.toml b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-02/Cargo.toml new file mode 100644 index 000000000..bd4edf762 --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-02/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "threads" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch16-fearless-concurrency/listing-16-02/src/main.rs b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-02/src/main.rs new file mode 100644 index 000000000..e37607f1d --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-02/src/main.rs @@ -0,0 +1,18 @@ +use std::thread; +use std::time::Duration; + +fn main() { + let handle = thread::spawn(|| { + for i in 1..10 { + println!("hi number {} from the spawned thread!", i); + thread::sleep(Duration::from_millis(1)); + } + }); + + for i in 1..5 { + println!("hi number {} from the main thread!", i); + thread::sleep(Duration::from_millis(1)); + } + + handle.join().unwrap(); +} diff --git a/src/doc/book/listings/ch16-fearless-concurrency/listing-16-03/Cargo.lock b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-03/Cargo.lock new file mode 100644 index 000000000..8ecc3ae17 --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-03/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "threads" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch16-fearless-concurrency/listing-16-03/Cargo.toml b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-03/Cargo.toml new file mode 100644 index 000000000..bd4edf762 --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-03/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "threads" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch16-fearless-concurrency/listing-16-03/output.txt b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-03/output.txt new file mode 100644 index 000000000..321bf59d7 --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-03/output.txt @@ -0,0 +1,25 @@ +$ cargo run + Compiling threads v0.1.0 (file:///projects/threads) +error[E0373]: closure may outlive the current function, but it borrows `v`, which is owned by the current function + --> src/main.rs:6:32 + | +6 | let handle = thread::spawn(|| { + | ^^ may outlive borrowed value `v` +7 | println!("Here's a vector: {:?}", v); + | - `v` is borrowed here + | +note: function requires argument type to outlive `'static` + --> src/main.rs:6:18 + | +6 | let handle = thread::spawn(|| { + | __________________^ +7 | | println!("Here's a vector: {:?}", v); +8 | | }); + | |______^ +help: to force the closure to take ownership of `v` (and any other referenced variables), use the `move` keyword + | +6 | let handle = thread::spawn(move || { + | ++++ + +For more information about this error, try `rustc --explain E0373`. +error: could not compile `threads` due to previous error diff --git a/src/doc/book/listings/ch16-fearless-concurrency/listing-16-03/src/main.rs b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-03/src/main.rs new file mode 100644 index 000000000..defc87648 --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-03/src/main.rs @@ -0,0 +1,11 @@ +use std::thread; + +fn main() { + let v = vec![1, 2, 3]; + + let handle = thread::spawn(|| { + println!("Here's a vector: {:?}", v); + }); + + handle.join().unwrap(); +} diff --git a/src/doc/book/listings/ch16-fearless-concurrency/listing-16-04/Cargo.lock b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-04/Cargo.lock new file mode 100644 index 000000000..8ecc3ae17 --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-04/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "threads" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch16-fearless-concurrency/listing-16-04/Cargo.toml b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-04/Cargo.toml new file mode 100644 index 000000000..bd4edf762 --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-04/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "threads" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch16-fearless-concurrency/listing-16-04/src/main.rs b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-04/src/main.rs new file mode 100644 index 000000000..0bccc5f56 --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-04/src/main.rs @@ -0,0 +1,13 @@ +use std::thread; + +fn main() { + let v = vec![1, 2, 3]; + + let handle = thread::spawn(|| { + println!("Here's a vector: {:?}", v); + }); + + drop(v); // oh no! + + handle.join().unwrap(); +} diff --git a/src/doc/book/listings/ch16-fearless-concurrency/listing-16-05/Cargo.lock b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-05/Cargo.lock new file mode 100644 index 000000000..8ecc3ae17 --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-05/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "threads" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch16-fearless-concurrency/listing-16-05/Cargo.toml b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-05/Cargo.toml new file mode 100644 index 000000000..bd4edf762 --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-05/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "threads" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch16-fearless-concurrency/listing-16-05/src/main.rs b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-05/src/main.rs new file mode 100644 index 000000000..a6547dc4c --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-05/src/main.rs @@ -0,0 +1,11 @@ +use std::thread; + +fn main() { + let v = vec![1, 2, 3]; + + let handle = thread::spawn(move || { + println!("Here's a vector: {:?}", v); + }); + + handle.join().unwrap(); +} diff --git a/src/doc/book/listings/ch16-fearless-concurrency/listing-16-06/Cargo.lock b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-06/Cargo.lock new file mode 100644 index 000000000..55d2252da --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-06/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "message-passing" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch16-fearless-concurrency/listing-16-06/Cargo.toml b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-06/Cargo.toml new file mode 100644 index 000000000..24bd2ee7b --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-06/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "message-passing" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch16-fearless-concurrency/listing-16-06/src/main.rs b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-06/src/main.rs new file mode 100644 index 000000000..d80dac4a0 --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-06/src/main.rs @@ -0,0 +1,5 @@ +use std::sync::mpsc; + +fn main() { + let (tx, rx) = mpsc::channel(); +} diff --git a/src/doc/book/listings/ch16-fearless-concurrency/listing-16-07/Cargo.lock b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-07/Cargo.lock new file mode 100644 index 000000000..55d2252da --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-07/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "message-passing" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch16-fearless-concurrency/listing-16-07/Cargo.toml b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-07/Cargo.toml new file mode 100644 index 000000000..24bd2ee7b --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-07/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "message-passing" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch16-fearless-concurrency/listing-16-07/src/main.rs b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-07/src/main.rs new file mode 100644 index 000000000..7859b64da --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-07/src/main.rs @@ -0,0 +1,11 @@ +use std::sync::mpsc; +use std::thread; + +fn main() { + let (tx, rx) = mpsc::channel(); + + thread::spawn(move || { + let val = String::from("hi"); + tx.send(val).unwrap(); + }); +} diff --git a/src/doc/book/listings/ch16-fearless-concurrency/listing-16-08/Cargo.lock b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-08/Cargo.lock new file mode 100644 index 000000000..55d2252da --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-08/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "message-passing" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch16-fearless-concurrency/listing-16-08/Cargo.toml b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-08/Cargo.toml new file mode 100644 index 000000000..24bd2ee7b --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-08/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "message-passing" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch16-fearless-concurrency/listing-16-08/src/main.rs b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-08/src/main.rs new file mode 100644 index 000000000..fbba9167d --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-08/src/main.rs @@ -0,0 +1,14 @@ +use std::sync::mpsc; +use std::thread; + +fn main() { + let (tx, rx) = mpsc::channel(); + + thread::spawn(move || { + let val = String::from("hi"); + tx.send(val).unwrap(); + }); + + let received = rx.recv().unwrap(); + println!("Got: {}", received); +} diff --git a/src/doc/book/listings/ch16-fearless-concurrency/listing-16-09/Cargo.lock b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-09/Cargo.lock new file mode 100644 index 000000000..55d2252da --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-09/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "message-passing" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch16-fearless-concurrency/listing-16-09/Cargo.toml b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-09/Cargo.toml new file mode 100644 index 000000000..24bd2ee7b --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-09/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "message-passing" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch16-fearless-concurrency/listing-16-09/output.txt b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-09/output.txt new file mode 100644 index 000000000..0795a3a4f --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-09/output.txt @@ -0,0 +1,16 @@ +$ cargo run + Compiling message-passing v0.1.0 (file:///projects/message-passing) +error[E0382]: borrow of moved value: `val` + --> src/main.rs:10:31 + | +8 | let val = String::from("hi"); + | --- move occurs because `val` has type `String`, which does not implement the `Copy` trait +9 | tx.send(val).unwrap(); + | --- value moved here +10 | println!("val is {}", val); + | ^^^ value borrowed here after move + | + = note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info) + +For more information about this error, try `rustc --explain E0382`. +error: could not compile `message-passing` due to previous error diff --git a/src/doc/book/listings/ch16-fearless-concurrency/listing-16-09/src/main.rs b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-09/src/main.rs new file mode 100644 index 000000000..98a8129ab --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-09/src/main.rs @@ -0,0 +1,15 @@ +use std::sync::mpsc; +use std::thread; + +fn main() { + let (tx, rx) = mpsc::channel(); + + thread::spawn(move || { + let val = String::from("hi"); + tx.send(val).unwrap(); + println!("val is {}", val); + }); + + let received = rx.recv().unwrap(); + println!("Got: {}", received); +} diff --git a/src/doc/book/listings/ch16-fearless-concurrency/listing-16-10/Cargo.lock b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-10/Cargo.lock new file mode 100644 index 000000000..55d2252da --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-10/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "message-passing" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch16-fearless-concurrency/listing-16-10/Cargo.toml b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-10/Cargo.toml new file mode 100644 index 000000000..24bd2ee7b --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-10/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "message-passing" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch16-fearless-concurrency/listing-16-10/src/main.rs b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-10/src/main.rs new file mode 100644 index 000000000..82b220de4 --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-10/src/main.rs @@ -0,0 +1,25 @@ +use std::sync::mpsc; +use std::thread; +use std::time::Duration; + +fn main() { + let (tx, rx) = mpsc::channel(); + + thread::spawn(move || { + let vals = vec![ + String::from("hi"), + String::from("from"), + String::from("the"), + String::from("thread"), + ]; + + for val in vals { + tx.send(val).unwrap(); + thread::sleep(Duration::from_secs(1)); + } + }); + + for received in rx { + println!("Got: {}", received); + } +} diff --git a/src/doc/book/listings/ch16-fearless-concurrency/listing-16-11/Cargo.lock b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-11/Cargo.lock new file mode 100644 index 000000000..55d2252da --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-11/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "message-passing" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch16-fearless-concurrency/listing-16-11/Cargo.toml b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-11/Cargo.toml new file mode 100644 index 000000000..24bd2ee7b --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-11/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "message-passing" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch16-fearless-concurrency/listing-16-11/src/main.rs b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-11/src/main.rs new file mode 100644 index 000000000..d92deab5c --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-11/src/main.rs @@ -0,0 +1,46 @@ +use std::sync::mpsc; +use std::thread; +use std::time::Duration; + +fn main() { + // ANCHOR: here + // --snip-- + + let (tx, rx) = mpsc::channel(); + + let tx1 = tx.clone(); + thread::spawn(move || { + let vals = vec![ + String::from("hi"), + String::from("from"), + String::from("the"), + String::from("thread"), + ]; + + for val in vals { + tx1.send(val).unwrap(); + thread::sleep(Duration::from_secs(1)); + } + }); + + thread::spawn(move || { + let vals = vec![ + String::from("more"), + String::from("messages"), + String::from("for"), + String::from("you"), + ]; + + for val in vals { + tx.send(val).unwrap(); + thread::sleep(Duration::from_secs(1)); + } + }); + + for received in rx { + println!("Got: {}", received); + } + + // --snip-- + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch16-fearless-concurrency/listing-16-12/Cargo.lock b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-12/Cargo.lock new file mode 100644 index 000000000..8e7ba9cf5 --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-12/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "shared-state" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch16-fearless-concurrency/listing-16-12/Cargo.toml b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-12/Cargo.toml new file mode 100644 index 000000000..da297eaba --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-12/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "shared-state" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch16-fearless-concurrency/listing-16-12/src/main.rs b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-12/src/main.rs new file mode 100644 index 000000000..0c0d6767a --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-12/src/main.rs @@ -0,0 +1,12 @@ +use std::sync::Mutex; + +fn main() { + let m = Mutex::new(5); + + { + let mut num = m.lock().unwrap(); + *num = 6; + } + + println!("m = {:?}", m); +} diff --git a/src/doc/book/listings/ch16-fearless-concurrency/listing-16-13/Cargo.lock b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-13/Cargo.lock new file mode 100644 index 000000000..8e7ba9cf5 --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-13/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "shared-state" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch16-fearless-concurrency/listing-16-13/Cargo.toml b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-13/Cargo.toml new file mode 100644 index 000000000..da297eaba --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-13/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "shared-state" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch16-fearless-concurrency/listing-16-13/output.txt b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-13/output.txt new file mode 100644 index 000000000..ea6963903 --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-13/output.txt @@ -0,0 +1,15 @@ +$ cargo run + Compiling shared-state v0.1.0 (file:///projects/shared-state) +error[E0382]: use of moved value: `counter` + --> src/main.rs:9:36 + | +5 | let counter = Mutex::new(0); + | ------- move occurs because `counter` has type `Mutex`, which does not implement the `Copy` trait +... +9 | let handle = thread::spawn(move || { + | ^^^^^^^ value moved into closure here, in previous iteration of loop +10 | let mut num = counter.lock().unwrap(); + | ------- use occurs due to use in closure + +For more information about this error, try `rustc --explain E0382`. +error: could not compile `shared-state` due to previous error diff --git a/src/doc/book/listings/ch16-fearless-concurrency/listing-16-13/src/main.rs b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-13/src/main.rs new file mode 100644 index 000000000..4e380a59a --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-13/src/main.rs @@ -0,0 +1,22 @@ +use std::sync::Mutex; +use std::thread; + +fn main() { + let counter = Mutex::new(0); + let mut handles = vec![]; + + for _ in 0..10 { + let handle = thread::spawn(move || { + let mut num = counter.lock().unwrap(); + + *num += 1; + }); + handles.push(handle); + } + + for handle in handles { + handle.join().unwrap(); + } + + println!("Result: {}", *counter.lock().unwrap()); +} diff --git a/src/doc/book/listings/ch16-fearless-concurrency/listing-16-14/Cargo.lock b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-14/Cargo.lock new file mode 100644 index 000000000..8e7ba9cf5 --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-14/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "shared-state" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch16-fearless-concurrency/listing-16-14/Cargo.toml b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-14/Cargo.toml new file mode 100644 index 000000000..da297eaba --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-14/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "shared-state" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch16-fearless-concurrency/listing-16-14/output.txt b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-14/output.txt new file mode 100644 index 000000000..9546e1e48 --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-14/output.txt @@ -0,0 +1,21 @@ +$ cargo run + Compiling shared-state v0.1.0 (file:///projects/shared-state) +error[E0277]: `Rc>` cannot be sent between threads safely + --> src/main.rs:11:22 + | +11 | let handle = thread::spawn(move || { + | ______________________^^^^^^^^^^^^^_- + | | | + | | `Rc>` cannot be sent between threads safely +12 | | let mut num = counter.lock().unwrap(); +13 | | +14 | | *num += 1; +15 | | }); + | |_________- within this `[closure@src/main.rs:11:36: 15:10]` + | + = help: within `[closure@src/main.rs:11:36: 15:10]`, the trait `Send` is not implemented for `Rc>` + = note: required because it appears within the type `[closure@src/main.rs:11:36: 15:10]` +note: required by a bound in `spawn` + +For more information about this error, try `rustc --explain E0277`. +error: could not compile `shared-state` due to previous error diff --git a/src/doc/book/listings/ch16-fearless-concurrency/listing-16-14/src/main.rs b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-14/src/main.rs new file mode 100644 index 000000000..d940b1a34 --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-14/src/main.rs @@ -0,0 +1,24 @@ +use std::rc::Rc; +use std::sync::Mutex; +use std::thread; + +fn main() { + let counter = Rc::new(Mutex::new(0)); + let mut handles = vec![]; + + for _ in 0..10 { + let counter = Rc::clone(&counter); + let handle = thread::spawn(move || { + let mut num = counter.lock().unwrap(); + + *num += 1; + }); + handles.push(handle); + } + + for handle in handles { + handle.join().unwrap(); + } + + println!("Result: {}", *counter.lock().unwrap()); +} diff --git a/src/doc/book/listings/ch16-fearless-concurrency/listing-16-15/Cargo.lock b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-15/Cargo.lock new file mode 100644 index 000000000..8e7ba9cf5 --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-15/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "shared-state" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch16-fearless-concurrency/listing-16-15/Cargo.toml b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-15/Cargo.toml new file mode 100644 index 000000000..da297eaba --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-15/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "shared-state" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch16-fearless-concurrency/listing-16-15/src/main.rs b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-15/src/main.rs new file mode 100644 index 000000000..30247dd52 --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-15/src/main.rs @@ -0,0 +1,23 @@ +use std::sync::{Arc, Mutex}; +use std::thread; + +fn main() { + let counter = Arc::new(Mutex::new(0)); + let mut handles = vec![]; + + for _ in 0..10 { + let counter = Arc::clone(&counter); + let handle = thread::spawn(move || { + let mut num = counter.lock().unwrap(); + + *num += 1; + }); + handles.push(handle); + } + + for handle in handles { + handle.join().unwrap(); + } + + println!("Result: {}", *counter.lock().unwrap()); +} diff --git a/src/doc/book/listings/ch16-fearless-concurrency/no-listing-01-join-too-early/Cargo.lock b/src/doc/book/listings/ch16-fearless-concurrency/no-listing-01-join-too-early/Cargo.lock new file mode 100644 index 000000000..8ecc3ae17 --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/no-listing-01-join-too-early/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "threads" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch16-fearless-concurrency/no-listing-01-join-too-early/Cargo.toml b/src/doc/book/listings/ch16-fearless-concurrency/no-listing-01-join-too-early/Cargo.toml new file mode 100644 index 000000000..bd4edf762 --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/no-listing-01-join-too-early/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "threads" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch16-fearless-concurrency/no-listing-01-join-too-early/src/main.rs b/src/doc/book/listings/ch16-fearless-concurrency/no-listing-01-join-too-early/src/main.rs new file mode 100644 index 000000000..6205e57d3 --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/no-listing-01-join-too-early/src/main.rs @@ -0,0 +1,18 @@ +use std::thread; +use std::time::Duration; + +fn main() { + let handle = thread::spawn(|| { + for i in 1..10 { + println!("hi number {} from the spawned thread!", i); + thread::sleep(Duration::from_millis(1)); + } + }); + + handle.join().unwrap(); + + for i in 1..5 { + println!("hi number {} from the main thread!", i); + thread::sleep(Duration::from_millis(1)); + } +} diff --git a/src/doc/book/listings/ch16-fearless-concurrency/no-listing-02-no-loop-to-understand-error/Cargo.lock b/src/doc/book/listings/ch16-fearless-concurrency/no-listing-02-no-loop-to-understand-error/Cargo.lock new file mode 100644 index 000000000..8e7ba9cf5 --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/no-listing-02-no-loop-to-understand-error/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "shared-state" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch16-fearless-concurrency/no-listing-02-no-loop-to-understand-error/Cargo.toml b/src/doc/book/listings/ch16-fearless-concurrency/no-listing-02-no-loop-to-understand-error/Cargo.toml new file mode 100644 index 000000000..da297eaba --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/no-listing-02-no-loop-to-understand-error/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "shared-state" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch16-fearless-concurrency/no-listing-02-no-loop-to-understand-error/src/main.rs b/src/doc/book/listings/ch16-fearless-concurrency/no-listing-02-no-loop-to-understand-error/src/main.rs new file mode 100644 index 000000000..dbb139771 --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/no-listing-02-no-loop-to-understand-error/src/main.rs @@ -0,0 +1,27 @@ +use std::sync::Mutex; +use std::thread; + +fn main() { + let counter = Mutex::new(0); + let mut handles = vec![]; + + let handle = thread::spawn(move || { + let mut num = counter.lock().unwrap(); + + *num += 1; + }); + handles.push(handle); + + let handle2 = thread::spawn(move || { + let mut num2 = counter.lock().unwrap(); + + *num2 += 1; + }); + handles.push(handle2); + + for handle in handles { + handle.join().unwrap(); + } + + println!("Result: {}", *counter.lock().unwrap()); +} diff --git a/src/doc/book/listings/ch16-fearless-concurrency/output-only-01-move-drop/Cargo.lock b/src/doc/book/listings/ch16-fearless-concurrency/output-only-01-move-drop/Cargo.lock new file mode 100644 index 000000000..8ecc3ae17 --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/output-only-01-move-drop/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "threads" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch16-fearless-concurrency/output-only-01-move-drop/Cargo.toml b/src/doc/book/listings/ch16-fearless-concurrency/output-only-01-move-drop/Cargo.toml new file mode 100644 index 000000000..bd4edf762 --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/output-only-01-move-drop/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "threads" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch16-fearless-concurrency/output-only-01-move-drop/output.txt b/src/doc/book/listings/ch16-fearless-concurrency/output-only-01-move-drop/output.txt new file mode 100644 index 000000000..f7be53b9a --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/output-only-01-move-drop/output.txt @@ -0,0 +1,18 @@ +$ cargo run + Compiling threads v0.1.0 (file:///projects/threads) +error[E0382]: use of moved value: `v` + --> src/main.rs:10:10 + | +4 | let v = vec![1, 2, 3]; + | - move occurs because `v` has type `Vec`, which does not implement the `Copy` trait +5 | +6 | let handle = thread::spawn(move || { + | ------- value moved into closure here +7 | println!("Here's a vector: {:?}", v); + | - variable moved due to use in closure +... +10 | drop(v); // oh no! + | ^ value used here after move + +For more information about this error, try `rustc --explain E0382`. +error: could not compile `threads` due to previous error diff --git a/src/doc/book/listings/ch16-fearless-concurrency/output-only-01-move-drop/src/main.rs b/src/doc/book/listings/ch16-fearless-concurrency/output-only-01-move-drop/src/main.rs new file mode 100644 index 000000000..70f659c5f --- /dev/null +++ b/src/doc/book/listings/ch16-fearless-concurrency/output-only-01-move-drop/src/main.rs @@ -0,0 +1,13 @@ +use std::thread; + +fn main() { + let v = vec![1, 2, 3]; + + let handle = thread::spawn(move || { + println!("Here's a vector: {:?}", v); + }); + + drop(v); // oh no! + + handle.join().unwrap(); +} diff --git a/src/doc/book/listings/ch17-oop/listing-17-01/Cargo.lock b/src/doc/book/listings/ch17-oop/listing-17-01/Cargo.lock new file mode 100644 index 000000000..471d8dfc3 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-01/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "averaged-collection" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch17-oop/listing-17-01/Cargo.toml b/src/doc/book/listings/ch17-oop/listing-17-01/Cargo.toml new file mode 100644 index 000000000..aed614e93 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-01/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "averaged-collection" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch17-oop/listing-17-01/src/lib.rs b/src/doc/book/listings/ch17-oop/listing-17-01/src/lib.rs new file mode 100644 index 000000000..b5ce2ab64 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-01/src/lib.rs @@ -0,0 +1,4 @@ +pub struct AveragedCollection { + list: Vec, + average: f64, +} diff --git a/src/doc/book/listings/ch17-oop/listing-17-02/Cargo.lock b/src/doc/book/listings/ch17-oop/listing-17-02/Cargo.lock new file mode 100644 index 000000000..471d8dfc3 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-02/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "averaged-collection" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch17-oop/listing-17-02/Cargo.toml b/src/doc/book/listings/ch17-oop/listing-17-02/Cargo.toml new file mode 100644 index 000000000..aed614e93 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-02/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "averaged-collection" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch17-oop/listing-17-02/src/lib.rs b/src/doc/book/listings/ch17-oop/listing-17-02/src/lib.rs new file mode 100644 index 000000000..bb407ec5f --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-02/src/lib.rs @@ -0,0 +1,33 @@ +pub struct AveragedCollection { + list: Vec, + average: f64, +} + +// ANCHOR: here +impl AveragedCollection { + pub fn add(&mut self, value: i32) { + self.list.push(value); + self.update_average(); + } + + pub fn remove(&mut self) -> Option { + let result = self.list.pop(); + match result { + Some(value) => { + self.update_average(); + Some(value) + } + None => None, + } + } + + pub fn average(&self) -> f64 { + self.average + } + + fn update_average(&mut self) { + let total: i32 = self.list.iter().sum(); + self.average = total as f64 / self.list.len() as f64; + } +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch17-oop/listing-17-03/Cargo.lock b/src/doc/book/listings/ch17-oop/listing-17-03/Cargo.lock new file mode 100644 index 000000000..00d7b2182 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-03/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "gui" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch17-oop/listing-17-03/Cargo.toml b/src/doc/book/listings/ch17-oop/listing-17-03/Cargo.toml new file mode 100644 index 000000000..9b816e766 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-03/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "gui" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch17-oop/listing-17-03/src/lib.rs b/src/doc/book/listings/ch17-oop/listing-17-03/src/lib.rs new file mode 100644 index 000000000..3a5cb779c --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-03/src/lib.rs @@ -0,0 +1,3 @@ +pub trait Draw { + fn draw(&self); +} diff --git a/src/doc/book/listings/ch17-oop/listing-17-04/Cargo.lock b/src/doc/book/listings/ch17-oop/listing-17-04/Cargo.lock new file mode 100644 index 000000000..00d7b2182 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-04/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "gui" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch17-oop/listing-17-04/Cargo.toml b/src/doc/book/listings/ch17-oop/listing-17-04/Cargo.toml new file mode 100644 index 000000000..9b816e766 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-04/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "gui" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch17-oop/listing-17-04/src/lib.rs b/src/doc/book/listings/ch17-oop/listing-17-04/src/lib.rs new file mode 100644 index 000000000..0c45e2a62 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-04/src/lib.rs @@ -0,0 +1,9 @@ +pub trait Draw { + fn draw(&self); +} + +// ANCHOR: here +pub struct Screen { + pub components: Vec>, +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch17-oop/listing-17-05/Cargo.lock b/src/doc/book/listings/ch17-oop/listing-17-05/Cargo.lock new file mode 100644 index 000000000..00d7b2182 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-05/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "gui" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch17-oop/listing-17-05/Cargo.toml b/src/doc/book/listings/ch17-oop/listing-17-05/Cargo.toml new file mode 100644 index 000000000..9b816e766 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-05/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "gui" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch17-oop/listing-17-05/src/lib.rs b/src/doc/book/listings/ch17-oop/listing-17-05/src/lib.rs new file mode 100644 index 000000000..57ebb5782 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-05/src/lib.rs @@ -0,0 +1,17 @@ +pub trait Draw { + fn draw(&self); +} + +pub struct Screen { + pub components: Vec>, +} + +// ANCHOR: here +impl Screen { + pub fn run(&self) { + for component in self.components.iter() { + component.draw(); + } + } +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch17-oop/listing-17-06/Cargo.lock b/src/doc/book/listings/ch17-oop/listing-17-06/Cargo.lock new file mode 100644 index 000000000..00d7b2182 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-06/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "gui" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch17-oop/listing-17-06/Cargo.toml b/src/doc/book/listings/ch17-oop/listing-17-06/Cargo.toml new file mode 100644 index 000000000..9b816e766 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-06/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "gui" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch17-oop/listing-17-06/src/lib.rs b/src/doc/book/listings/ch17-oop/listing-17-06/src/lib.rs new file mode 100644 index 000000000..63a8907d3 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-06/src/lib.rs @@ -0,0 +1,20 @@ +pub trait Draw { + fn draw(&self); +} + +// ANCHOR: here +pub struct Screen { + pub components: Vec, +} + +impl Screen +where + T: Draw, +{ + pub fn run(&self) { + for component in self.components.iter() { + component.draw(); + } + } +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch17-oop/listing-17-07/Cargo.lock b/src/doc/book/listings/ch17-oop/listing-17-07/Cargo.lock new file mode 100644 index 000000000..00d7b2182 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-07/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "gui" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch17-oop/listing-17-07/Cargo.toml b/src/doc/book/listings/ch17-oop/listing-17-07/Cargo.toml new file mode 100644 index 000000000..9b816e766 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-07/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "gui" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch17-oop/listing-17-07/src/lib.rs b/src/doc/book/listings/ch17-oop/listing-17-07/src/lib.rs new file mode 100644 index 000000000..b16cd0155 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-07/src/lib.rs @@ -0,0 +1,29 @@ +pub trait Draw { + fn draw(&self); +} + +pub struct Screen { + pub components: Vec>, +} + +impl Screen { + pub fn run(&self) { + for component in self.components.iter() { + component.draw(); + } + } +} + +// ANCHOR: here +pub struct Button { + pub width: u32, + pub height: u32, + pub label: String, +} + +impl Draw for Button { + fn draw(&self) { + // code to actually draw a button + } +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch17-oop/listing-17-08/Cargo.lock b/src/doc/book/listings/ch17-oop/listing-17-08/Cargo.lock new file mode 100644 index 000000000..00d7b2182 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-08/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "gui" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch17-oop/listing-17-08/Cargo.toml b/src/doc/book/listings/ch17-oop/listing-17-08/Cargo.toml new file mode 100644 index 000000000..9b816e766 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-08/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "gui" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch17-oop/listing-17-08/src/lib.rs b/src/doc/book/listings/ch17-oop/listing-17-08/src/lib.rs new file mode 100644 index 000000000..960fee23d --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-08/src/lib.rs @@ -0,0 +1,27 @@ +pub trait Draw { + fn draw(&self); +} + +pub struct Screen { + pub components: Vec>, +} + +impl Screen { + pub fn run(&self) { + for component in self.components.iter() { + component.draw(); + } + } +} + +pub struct Button { + pub width: u32, + pub height: u32, + pub label: String, +} + +impl Draw for Button { + fn draw(&self) { + // code to actually draw a button + } +} diff --git a/src/doc/book/listings/ch17-oop/listing-17-08/src/main.rs b/src/doc/book/listings/ch17-oop/listing-17-08/src/main.rs new file mode 100644 index 000000000..9575d407e --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-08/src/main.rs @@ -0,0 +1,17 @@ +// ANCHOR: here +use gui::Draw; + +struct SelectBox { + width: u32, + height: u32, + options: Vec, +} + +impl Draw for SelectBox { + fn draw(&self) { + // code to actually draw a select box + } +} +// ANCHOR_END: here + +fn main() {} diff --git a/src/doc/book/listings/ch17-oop/listing-17-09/Cargo.lock b/src/doc/book/listings/ch17-oop/listing-17-09/Cargo.lock new file mode 100644 index 000000000..00d7b2182 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-09/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "gui" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch17-oop/listing-17-09/Cargo.toml b/src/doc/book/listings/ch17-oop/listing-17-09/Cargo.toml new file mode 100644 index 000000000..9b816e766 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-09/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "gui" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch17-oop/listing-17-09/src/lib.rs b/src/doc/book/listings/ch17-oop/listing-17-09/src/lib.rs new file mode 100644 index 000000000..960fee23d --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-09/src/lib.rs @@ -0,0 +1,27 @@ +pub trait Draw { + fn draw(&self); +} + +pub struct Screen { + pub components: Vec>, +} + +impl Screen { + pub fn run(&self) { + for component in self.components.iter() { + component.draw(); + } + } +} + +pub struct Button { + pub width: u32, + pub height: u32, + pub label: String, +} + +impl Draw for Button { + fn draw(&self) { + // code to actually draw a button + } +} diff --git a/src/doc/book/listings/ch17-oop/listing-17-09/src/main.rs b/src/doc/book/listings/ch17-oop/listing-17-09/src/main.rs new file mode 100644 index 000000000..4eb13f6b7 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-09/src/main.rs @@ -0,0 +1,40 @@ +use gui::Draw; + +struct SelectBox { + width: u32, + height: u32, + options: Vec, +} + +impl Draw for SelectBox { + fn draw(&self) { + // code to actually draw a select box + } +} + +// ANCHOR: here +use gui::{Button, Screen}; + +fn main() { + let screen = Screen { + components: vec![ + Box::new(SelectBox { + width: 75, + height: 10, + options: vec![ + String::from("Yes"), + String::from("Maybe"), + String::from("No"), + ], + }), + Box::new(Button { + width: 50, + height: 10, + label: String::from("OK"), + }), + ], + }; + + screen.run(); +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch17-oop/listing-17-10/Cargo.lock b/src/doc/book/listings/ch17-oop/listing-17-10/Cargo.lock new file mode 100644 index 000000000..00d7b2182 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-10/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "gui" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch17-oop/listing-17-10/Cargo.toml b/src/doc/book/listings/ch17-oop/listing-17-10/Cargo.toml new file mode 100644 index 000000000..9b816e766 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-10/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "gui" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch17-oop/listing-17-10/output.txt b/src/doc/book/listings/ch17-oop/listing-17-10/output.txt new file mode 100644 index 000000000..74330fa0a --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-10/output.txt @@ -0,0 +1,13 @@ +$ cargo run + Compiling gui v0.1.0 (file:///projects/gui) +error[E0277]: the trait bound `String: Draw` is not satisfied + --> src/main.rs:5:26 + | +5 | components: vec![Box::new(String::from("Hi"))], + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Draw` is not implemented for `String` + | + = help: the trait `Draw` is implemented for `Button` + = note: required for the cast to the object type `dyn Draw` + +For more information about this error, try `rustc --explain E0277`. +error: could not compile `gui` due to previous error diff --git a/src/doc/book/listings/ch17-oop/listing-17-10/src/lib.rs b/src/doc/book/listings/ch17-oop/listing-17-10/src/lib.rs new file mode 100644 index 000000000..960fee23d --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-10/src/lib.rs @@ -0,0 +1,27 @@ +pub trait Draw { + fn draw(&self); +} + +pub struct Screen { + pub components: Vec>, +} + +impl Screen { + pub fn run(&self) { + for component in self.components.iter() { + component.draw(); + } + } +} + +pub struct Button { + pub width: u32, + pub height: u32, + pub label: String, +} + +impl Draw for Button { + fn draw(&self) { + // code to actually draw a button + } +} diff --git a/src/doc/book/listings/ch17-oop/listing-17-10/src/main.rs b/src/doc/book/listings/ch17-oop/listing-17-10/src/main.rs new file mode 100644 index 000000000..2ede87ab7 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-10/src/main.rs @@ -0,0 +1,9 @@ +use gui::Screen; + +fn main() { + let screen = Screen { + components: vec![Box::new(String::from("Hi"))], + }; + + screen.run(); +} diff --git a/src/doc/book/listings/ch17-oop/listing-17-11/Cargo.lock b/src/doc/book/listings/ch17-oop/listing-17-11/Cargo.lock new file mode 100644 index 000000000..b6f4232c6 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-11/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "blog" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch17-oop/listing-17-11/Cargo.toml b/src/doc/book/listings/ch17-oop/listing-17-11/Cargo.toml new file mode 100644 index 000000000..1619af5c6 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-11/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "blog" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch17-oop/listing-17-11/src/main.rs b/src/doc/book/listings/ch17-oop/listing-17-11/src/main.rs new file mode 100644 index 000000000..d99170a97 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-11/src/main.rs @@ -0,0 +1,20 @@ +// ANCHOR: all +use blog::Post; + +// ANCHOR: here +fn main() { + let mut post = Post::new(); + + post.add_text("I ate a salad for lunch today"); + assert_eq!("", post.content()); + // ANCHOR_END: here + + post.request_review(); + assert_eq!("", post.content()); + + post.approve(); + assert_eq!("I ate a salad for lunch today", post.content()); + // ANCHOR: here +} +// ANCHOR_END: here +// ANCHOR_END: all diff --git a/src/doc/book/listings/ch17-oop/listing-17-12/Cargo.lock b/src/doc/book/listings/ch17-oop/listing-17-12/Cargo.lock new file mode 100644 index 000000000..b6f4232c6 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-12/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "blog" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch17-oop/listing-17-12/Cargo.toml b/src/doc/book/listings/ch17-oop/listing-17-12/Cargo.toml new file mode 100644 index 000000000..1619af5c6 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-12/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "blog" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch17-oop/listing-17-12/src/lib.rs b/src/doc/book/listings/ch17-oop/listing-17-12/src/lib.rs new file mode 100644 index 000000000..b8156c39d --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-12/src/lib.rs @@ -0,0 +1,19 @@ +pub struct Post { + state: Option>, + content: String, +} + +impl Post { + pub fn new() -> Post { + Post { + state: Some(Box::new(Draft {})), + content: String::new(), + } + } +} + +trait State {} + +struct Draft {} + +impl State for Draft {} diff --git a/src/doc/book/listings/ch17-oop/listing-17-12/src/main.rs b/src/doc/book/listings/ch17-oop/listing-17-12/src/main.rs new file mode 100644 index 000000000..14b4c0824 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-12/src/main.rs @@ -0,0 +1,14 @@ +use blog::Post; + +fn main() { + let mut post = Post::new(); + + post.add_text("I ate a salad for lunch today"); + assert_eq!("", post.content()); + + post.request_review(); + assert_eq!("", post.content()); + + post.approve(); + assert_eq!("I ate a salad for lunch today", post.content()); +} diff --git a/src/doc/book/listings/ch17-oop/listing-17-13/Cargo.lock b/src/doc/book/listings/ch17-oop/listing-17-13/Cargo.lock new file mode 100644 index 000000000..b6f4232c6 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-13/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "blog" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch17-oop/listing-17-13/Cargo.toml b/src/doc/book/listings/ch17-oop/listing-17-13/Cargo.toml new file mode 100644 index 000000000..1619af5c6 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-13/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "blog" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch17-oop/listing-17-13/src/lib.rs b/src/doc/book/listings/ch17-oop/listing-17-13/src/lib.rs new file mode 100644 index 000000000..bd68557a0 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-13/src/lib.rs @@ -0,0 +1,28 @@ +pub struct Post { + state: Option>, + content: String, +} + +// ANCHOR: here +impl Post { + // --snip-- + // ANCHOR_END: here + pub fn new() -> Post { + Post { + state: Some(Box::new(Draft {})), + content: String::new(), + } + } + + // ANCHOR: here + pub fn add_text(&mut self, text: &str) { + self.content.push_str(text); + } +} +// ANCHOR_END: here + +trait State {} + +struct Draft {} + +impl State for Draft {} diff --git a/src/doc/book/listings/ch17-oop/listing-17-13/src/main.rs b/src/doc/book/listings/ch17-oop/listing-17-13/src/main.rs new file mode 100644 index 000000000..14b4c0824 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-13/src/main.rs @@ -0,0 +1,14 @@ +use blog::Post; + +fn main() { + let mut post = Post::new(); + + post.add_text("I ate a salad for lunch today"); + assert_eq!("", post.content()); + + post.request_review(); + assert_eq!("", post.content()); + + post.approve(); + assert_eq!("I ate a salad for lunch today", post.content()); +} diff --git a/src/doc/book/listings/ch17-oop/listing-17-14/Cargo.lock b/src/doc/book/listings/ch17-oop/listing-17-14/Cargo.lock new file mode 100644 index 000000000..b6f4232c6 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-14/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "blog" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch17-oop/listing-17-14/Cargo.toml b/src/doc/book/listings/ch17-oop/listing-17-14/Cargo.toml new file mode 100644 index 000000000..1619af5c6 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-14/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "blog" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch17-oop/listing-17-14/src/lib.rs b/src/doc/book/listings/ch17-oop/listing-17-14/src/lib.rs new file mode 100644 index 000000000..09cf0c466 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-14/src/lib.rs @@ -0,0 +1,32 @@ +pub struct Post { + state: Option>, + content: String, +} + +// ANCHOR: here +impl Post { + // --snip-- + // ANCHOR_END: here + pub fn new() -> Post { + Post { + state: Some(Box::new(Draft {})), + content: String::new(), + } + } + + pub fn add_text(&mut self, text: &str) { + self.content.push_str(text); + } + + // ANCHOR: here + pub fn content(&self) -> &str { + "" + } +} +// ANCHOR_END: here + +trait State {} + +struct Draft {} + +impl State for Draft {} diff --git a/src/doc/book/listings/ch17-oop/listing-17-14/src/main.rs b/src/doc/book/listings/ch17-oop/listing-17-14/src/main.rs new file mode 100644 index 000000000..14b4c0824 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-14/src/main.rs @@ -0,0 +1,14 @@ +use blog::Post; + +fn main() { + let mut post = Post::new(); + + post.add_text("I ate a salad for lunch today"); + assert_eq!("", post.content()); + + post.request_review(); + assert_eq!("", post.content()); + + post.approve(); + assert_eq!("I ate a salad for lunch today", post.content()); +} diff --git a/src/doc/book/listings/ch17-oop/listing-17-15/Cargo.lock b/src/doc/book/listings/ch17-oop/listing-17-15/Cargo.lock new file mode 100644 index 000000000..b6f4232c6 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-15/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "blog" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch17-oop/listing-17-15/Cargo.toml b/src/doc/book/listings/ch17-oop/listing-17-15/Cargo.toml new file mode 100644 index 000000000..1619af5c6 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-15/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "blog" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch17-oop/listing-17-15/src/lib.rs b/src/doc/book/listings/ch17-oop/listing-17-15/src/lib.rs new file mode 100644 index 000000000..909dd5274 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-15/src/lib.rs @@ -0,0 +1,52 @@ +pub struct Post { + state: Option>, + content: String, +} + +// ANCHOR: here +impl Post { + // --snip-- + // ANCHOR_END: here + pub fn new() -> Post { + Post { + state: Some(Box::new(Draft {})), + content: String::new(), + } + } + + pub fn add_text(&mut self, text: &str) { + self.content.push_str(text); + } + + pub fn content(&self) -> &str { + "" + } + + // ANCHOR: here + pub fn request_review(&mut self) { + if let Some(s) = self.state.take() { + self.state = Some(s.request_review()) + } + } +} + +trait State { + fn request_review(self: Box) -> Box; +} + +struct Draft {} + +impl State for Draft { + fn request_review(self: Box) -> Box { + Box::new(PendingReview {}) + } +} + +struct PendingReview {} + +impl State for PendingReview { + fn request_review(self: Box) -> Box { + self + } +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch17-oop/listing-17-15/src/main.rs b/src/doc/book/listings/ch17-oop/listing-17-15/src/main.rs new file mode 100644 index 000000000..14b4c0824 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-15/src/main.rs @@ -0,0 +1,14 @@ +use blog::Post; + +fn main() { + let mut post = Post::new(); + + post.add_text("I ate a salad for lunch today"); + assert_eq!("", post.content()); + + post.request_review(); + assert_eq!("", post.content()); + + post.approve(); + assert_eq!("I ate a salad for lunch today", post.content()); +} diff --git a/src/doc/book/listings/ch17-oop/listing-17-16/Cargo.lock b/src/doc/book/listings/ch17-oop/listing-17-16/Cargo.lock new file mode 100644 index 000000000..b6f4232c6 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-16/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "blog" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch17-oop/listing-17-16/Cargo.toml b/src/doc/book/listings/ch17-oop/listing-17-16/Cargo.toml new file mode 100644 index 000000000..1619af5c6 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-16/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "blog" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch17-oop/listing-17-16/src/lib.rs b/src/doc/book/listings/ch17-oop/listing-17-16/src/lib.rs new file mode 100644 index 000000000..92cb29813 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-16/src/lib.rs @@ -0,0 +1,85 @@ +pub struct Post { + state: Option>, + content: String, +} + +// ANCHOR: here +impl Post { + // --snip-- + // ANCHOR_END: here + pub fn new() -> Post { + Post { + state: Some(Box::new(Draft {})), + content: String::new(), + } + } + + pub fn add_text(&mut self, text: &str) { + self.content.push_str(text); + } + + pub fn content(&self) -> &str { + "" + } + + pub fn request_review(&mut self) { + if let Some(s) = self.state.take() { + self.state = Some(s.request_review()) + } + } + + // ANCHOR: here + pub fn approve(&mut self) { + if let Some(s) = self.state.take() { + self.state = Some(s.approve()) + } + } +} + +trait State { + fn request_review(self: Box) -> Box; + fn approve(self: Box) -> Box; +} + +struct Draft {} + +impl State for Draft { + // --snip-- + // ANCHOR_END: here + fn request_review(self: Box) -> Box { + Box::new(PendingReview {}) + } + + // ANCHOR: here + fn approve(self: Box) -> Box { + self + } +} + +struct PendingReview {} + +impl State for PendingReview { + // --snip-- + // ANCHOR_END: here + fn request_review(self: Box) -> Box { + self + } + + // ANCHOR: here + fn approve(self: Box) -> Box { + Box::new(Published {}) + } +} + +struct Published {} + +impl State for Published { + fn request_review(self: Box) -> Box { + self + } + + fn approve(self: Box) -> Box { + self + } +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch17-oop/listing-17-16/src/main.rs b/src/doc/book/listings/ch17-oop/listing-17-16/src/main.rs new file mode 100644 index 000000000..14b4c0824 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-16/src/main.rs @@ -0,0 +1,14 @@ +use blog::Post; + +fn main() { + let mut post = Post::new(); + + post.add_text("I ate a salad for lunch today"); + assert_eq!("", post.content()); + + post.request_review(); + assert_eq!("", post.content()); + + post.approve(); + assert_eq!("I ate a salad for lunch today", post.content()); +} diff --git a/src/doc/book/listings/ch17-oop/listing-17-17/Cargo.lock b/src/doc/book/listings/ch17-oop/listing-17-17/Cargo.lock new file mode 100644 index 000000000..b6f4232c6 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-17/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "blog" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch17-oop/listing-17-17/Cargo.toml b/src/doc/book/listings/ch17-oop/listing-17-17/Cargo.toml new file mode 100644 index 000000000..1619af5c6 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-17/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "blog" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch17-oop/listing-17-17/src/lib.rs b/src/doc/book/listings/ch17-oop/listing-17-17/src/lib.rs new file mode 100644 index 000000000..0beee7b8d --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-17/src/lib.rs @@ -0,0 +1,82 @@ +pub struct Post { + state: Option>, + content: String, +} + +// ANCHOR: here +impl Post { + // --snip-- + // ANCHOR_END: here + pub fn new() -> Post { + Post { + state: Some(Box::new(Draft {})), + content: String::new(), + } + } + + pub fn add_text(&mut self, text: &str) { + self.content.push_str(text); + } + + // ANCHOR: here + pub fn content(&self) -> &str { + self.state.as_ref().unwrap().content(self) + } + // --snip-- + // ANCHOR_END: here + + pub fn request_review(&mut self) { + if let Some(s) = self.state.take() { + self.state = Some(s.request_review()) + } + } + + pub fn approve(&mut self) { + if let Some(s) = self.state.take() { + self.state = Some(s.approve()) + } + } + // ANCHOR: here +} +// ANCHOR_END: here + +trait State { + fn request_review(self: Box) -> Box; + fn approve(self: Box) -> Box; +} + +struct Draft {} + +impl State for Draft { + fn request_review(self: Box) -> Box { + Box::new(PendingReview {}) + } + + fn approve(self: Box) -> Box { + self + } +} + +struct PendingReview {} + +impl State for PendingReview { + fn request_review(self: Box) -> Box { + self + } + + fn approve(self: Box) -> Box { + Box::new(Published {}) + } +} + +struct Published {} + +impl State for Published { + fn request_review(self: Box) -> Box { + self + } + + fn approve(self: Box) -> Box { + self + } +} diff --git a/src/doc/book/listings/ch17-oop/listing-17-17/src/main.rs b/src/doc/book/listings/ch17-oop/listing-17-17/src/main.rs new file mode 100644 index 000000000..14b4c0824 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-17/src/main.rs @@ -0,0 +1,14 @@ +use blog::Post; + +fn main() { + let mut post = Post::new(); + + post.add_text("I ate a salad for lunch today"); + assert_eq!("", post.content()); + + post.request_review(); + assert_eq!("", post.content()); + + post.approve(); + assert_eq!("I ate a salad for lunch today", post.content()); +} diff --git a/src/doc/book/listings/ch17-oop/listing-17-18/Cargo.lock b/src/doc/book/listings/ch17-oop/listing-17-18/Cargo.lock new file mode 100644 index 000000000..b6f4232c6 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-18/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "blog" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch17-oop/listing-17-18/Cargo.toml b/src/doc/book/listings/ch17-oop/listing-17-18/Cargo.toml new file mode 100644 index 000000000..1619af5c6 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-18/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "blog" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch17-oop/listing-17-18/src/lib.rs b/src/doc/book/listings/ch17-oop/listing-17-18/src/lib.rs new file mode 100644 index 000000000..1bac8a87a --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-18/src/lib.rs @@ -0,0 +1,94 @@ +pub struct Post { + state: Option>, + content: String, +} + +impl Post { + pub fn new() -> Post { + Post { + state: Some(Box::new(Draft {})), + content: String::new(), + } + } + + pub fn add_text(&mut self, text: &str) { + self.content.push_str(text); + } + + pub fn content(&self) -> &str { + self.state.as_ref().unwrap().content(self) + } + + pub fn request_review(&mut self) { + if let Some(s) = self.state.take() { + self.state = Some(s.request_review()) + } + } + + pub fn approve(&mut self) { + if let Some(s) = self.state.take() { + self.state = Some(s.approve()) + } + } +} + +// ANCHOR: here +trait State { + // --snip-- + // ANCHOR_END: here + fn request_review(self: Box) -> Box; + fn approve(self: Box) -> Box; + + // ANCHOR: here + fn content<'a>(&self, post: &'a Post) -> &'a str { + "" + } +} + +// --snip-- +// ANCHOR_END: here + +struct Draft {} + +impl State for Draft { + fn request_review(self: Box) -> Box { + Box::new(PendingReview {}) + } + + fn approve(self: Box) -> Box { + self + } +} + +struct PendingReview {} + +impl State for PendingReview { + fn request_review(self: Box) -> Box { + self + } + + fn approve(self: Box) -> Box { + Box::new(Published {}) + } +} + +// ANCHOR: here +struct Published {} + +impl State for Published { + // --snip-- + // ANCHOR_END: here + fn request_review(self: Box) -> Box { + self + } + + fn approve(self: Box) -> Box { + self + } + + // ANCHOR: here + fn content<'a>(&self, post: &'a Post) -> &'a str { + &post.content + } +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch17-oop/listing-17-18/src/main.rs b/src/doc/book/listings/ch17-oop/listing-17-18/src/main.rs new file mode 100644 index 000000000..14b4c0824 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-18/src/main.rs @@ -0,0 +1,14 @@ +use blog::Post; + +fn main() { + let mut post = Post::new(); + + post.add_text("I ate a salad for lunch today"); + assert_eq!("", post.content()); + + post.request_review(); + assert_eq!("", post.content()); + + post.approve(); + assert_eq!("I ate a salad for lunch today", post.content()); +} diff --git a/src/doc/book/listings/ch17-oop/listing-17-19/Cargo.lock b/src/doc/book/listings/ch17-oop/listing-17-19/Cargo.lock new file mode 100644 index 000000000..b6f4232c6 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-19/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "blog" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch17-oop/listing-17-19/Cargo.toml b/src/doc/book/listings/ch17-oop/listing-17-19/Cargo.toml new file mode 100644 index 000000000..1619af5c6 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-19/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "blog" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch17-oop/listing-17-19/src/lib.rs b/src/doc/book/listings/ch17-oop/listing-17-19/src/lib.rs new file mode 100644 index 000000000..bfe034eaf --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-19/src/lib.rs @@ -0,0 +1,25 @@ +pub struct Post { + content: String, +} + +pub struct DraftPost { + content: String, +} + +impl Post { + pub fn new() -> DraftPost { + DraftPost { + content: String::new(), + } + } + + pub fn content(&self) -> &str { + &self.content + } +} + +impl DraftPost { + pub fn add_text(&mut self, text: &str) { + self.content.push_str(text); + } +} diff --git a/src/doc/book/listings/ch17-oop/listing-17-20/Cargo.lock b/src/doc/book/listings/ch17-oop/listing-17-20/Cargo.lock new file mode 100644 index 000000000..b6f4232c6 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-20/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "blog" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch17-oop/listing-17-20/Cargo.toml b/src/doc/book/listings/ch17-oop/listing-17-20/Cargo.toml new file mode 100644 index 000000000..1619af5c6 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-20/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "blog" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch17-oop/listing-17-20/src/lib.rs b/src/doc/book/listings/ch17-oop/listing-17-20/src/lib.rs new file mode 100644 index 000000000..3b82ec05d --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-20/src/lib.rs @@ -0,0 +1,48 @@ +pub struct Post { + content: String, +} + +pub struct DraftPost { + content: String, +} + +impl Post { + pub fn new() -> DraftPost { + DraftPost { + content: String::new(), + } + } + + pub fn content(&self) -> &str { + &self.content + } +} + +// ANCHOR: here +impl DraftPost { + // --snip-- + // ANCHOR_END: here + pub fn add_text(&mut self, text: &str) { + self.content.push_str(text); + } + + // ANCHOR: here + pub fn request_review(self) -> PendingReviewPost { + PendingReviewPost { + content: self.content, + } + } +} + +pub struct PendingReviewPost { + content: String, +} + +impl PendingReviewPost { + pub fn approve(self) -> Post { + Post { + content: self.content, + } + } +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch17-oop/listing-17-21/Cargo.lock b/src/doc/book/listings/ch17-oop/listing-17-21/Cargo.lock new file mode 100644 index 000000000..b6f4232c6 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-21/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "blog" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch17-oop/listing-17-21/Cargo.toml b/src/doc/book/listings/ch17-oop/listing-17-21/Cargo.toml new file mode 100644 index 000000000..1619af5c6 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-21/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "blog" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch17-oop/listing-17-21/src/lib.rs b/src/doc/book/listings/ch17-oop/listing-17-21/src/lib.rs new file mode 100644 index 000000000..38500a651 --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-21/src/lib.rs @@ -0,0 +1,43 @@ +pub struct Post { + content: String, +} + +pub struct DraftPost { + content: String, +} + +impl Post { + pub fn new() -> DraftPost { + DraftPost { + content: String::new(), + } + } + + pub fn content(&self) -> &str { + &self.content + } +} + +impl DraftPost { + pub fn add_text(&mut self, text: &str) { + self.content.push_str(text); + } + + pub fn request_review(self) -> PendingReviewPost { + PendingReviewPost { + content: self.content, + } + } +} + +pub struct PendingReviewPost { + content: String, +} + +impl PendingReviewPost { + pub fn approve(self) -> Post { + Post { + content: self.content, + } + } +} diff --git a/src/doc/book/listings/ch17-oop/listing-17-21/src/main.rs b/src/doc/book/listings/ch17-oop/listing-17-21/src/main.rs new file mode 100644 index 000000000..720c55e6a --- /dev/null +++ b/src/doc/book/listings/ch17-oop/listing-17-21/src/main.rs @@ -0,0 +1,13 @@ +use blog::Post; + +fn main() { + let mut post = Post::new(); + + post.add_text("I ate a salad for lunch today"); + + let post = post.request_review(); + + let post = post.approve(); + + assert_eq!("I ate a salad for lunch today", post.content()); +} diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-01/Cargo.lock b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-01/Cargo.lock new file mode 100644 index 000000000..2b4fa2903 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-01/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "patterns" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-01/Cargo.toml b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-01/Cargo.toml new file mode 100644 index 000000000..82fe057bb --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-01/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "patterns" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-01/src/main.rs b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-01/src/main.rs new file mode 100644 index 000000000..fc87768fb --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-01/src/main.rs @@ -0,0 +1,19 @@ +fn main() { + let favorite_color: Option<&str> = None; + let is_tuesday = false; + let age: Result = "34".parse(); + + if let Some(color) = favorite_color { + println!("Using your favorite color, {color}, as the background"); + } else if is_tuesday { + println!("Tuesday is green day!"); + } else if let Ok(age) = age { + if age > 30 { + println!("Using purple as the background color"); + } else { + println!("Using orange as the background color"); + } + } else { + println!("Using blue as the background color"); + } +} diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-02/Cargo.lock b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-02/Cargo.lock new file mode 100644 index 000000000..2b4fa2903 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-02/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "patterns" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-02/Cargo.toml b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-02/Cargo.toml new file mode 100644 index 000000000..82fe057bb --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-02/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "patterns" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-02/src/main.rs b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-02/src/main.rs new file mode 100644 index 000000000..5f75a4f2d --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-02/src/main.rs @@ -0,0 +1,13 @@ +fn main() { + // ANCHOR: here + let mut stack = Vec::new(); + + stack.push(1); + stack.push(2); + stack.push(3); + + while let Some(top) = stack.pop() { + println!("{}", top); + } + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-03/Cargo.lock b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-03/Cargo.lock new file mode 100644 index 000000000..2b4fa2903 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-03/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "patterns" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-03/Cargo.toml b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-03/Cargo.toml new file mode 100644 index 000000000..82fe057bb --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-03/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "patterns" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-03/output.txt b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-03/output.txt new file mode 100644 index 000000000..02fdecbf5 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-03/output.txt @@ -0,0 +1,7 @@ +$ cargo run + Compiling patterns v0.1.0 (file:///projects/patterns) + Finished dev [unoptimized + debuginfo] target(s) in 0.52s + Running `target/debug/patterns` +a is at index 0 +b is at index 1 +c is at index 2 diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-03/src/main.rs b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-03/src/main.rs new file mode 100644 index 000000000..eb922d62c --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-03/src/main.rs @@ -0,0 +1,9 @@ +fn main() { + // ANCHOR: here + let v = vec!['a', 'b', 'c']; + + for (index, value) in v.iter().enumerate() { + println!("{} is at index {}", value, index); + } + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-04/Cargo.lock b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-04/Cargo.lock new file mode 100644 index 000000000..2b4fa2903 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-04/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "patterns" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-04/Cargo.toml b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-04/Cargo.toml new file mode 100644 index 000000000..82fe057bb --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-04/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "patterns" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-04/src/main.rs b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-04/src/main.rs new file mode 100644 index 000000000..27b0c3fbd --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-04/src/main.rs @@ -0,0 +1,5 @@ +fn main() { + // ANCHOR: here + let (x, y, z) = (1, 2, 3); + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-05/Cargo.lock b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-05/Cargo.lock new file mode 100644 index 000000000..2b4fa2903 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-05/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "patterns" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-05/Cargo.toml b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-05/Cargo.toml new file mode 100644 index 000000000..82fe057bb --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-05/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "patterns" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-05/output.txt b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-05/output.txt new file mode 100644 index 000000000..57916a1f1 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-05/output.txt @@ -0,0 +1,15 @@ +$ cargo run + Compiling patterns v0.1.0 (file:///projects/patterns) +error[E0308]: mismatched types + --> src/main.rs:2:9 + | +2 | let (x, y) = (1, 2, 3); + | ^^^^^^ --------- this expression has type `({integer}, {integer}, {integer})` + | | + | expected a tuple with 3 elements, found one with 2 elements + | + = note: expected tuple `({integer}, {integer}, {integer})` + found tuple `(_, _)` + +For more information about this error, try `rustc --explain E0308`. +error: could not compile `patterns` due to previous error diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-05/src/main.rs b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-05/src/main.rs new file mode 100644 index 000000000..39f768e29 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-05/src/main.rs @@ -0,0 +1,5 @@ +fn main() { + // ANCHOR: here + let (x, y) = (1, 2, 3); + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-06/Cargo.lock b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-06/Cargo.lock new file mode 100644 index 000000000..2b4fa2903 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-06/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "patterns" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-06/Cargo.toml b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-06/Cargo.toml new file mode 100644 index 000000000..82fe057bb --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-06/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "patterns" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-06/src/main.rs b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-06/src/main.rs new file mode 100644 index 000000000..c5d71e6c1 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-06/src/main.rs @@ -0,0 +1,7 @@ +// ANCHOR: here +fn foo(x: i32) { + // code goes here +} +// ANCHOR_END: here + +fn main() {} diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-07/Cargo.lock b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-07/Cargo.lock new file mode 100644 index 000000000..2b4fa2903 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-07/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "patterns" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-07/Cargo.toml b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-07/Cargo.toml new file mode 100644 index 000000000..82fe057bb --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-07/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "patterns" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-07/src/main.rs b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-07/src/main.rs new file mode 100644 index 000000000..4eccb8088 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-07/src/main.rs @@ -0,0 +1,8 @@ +fn print_coordinates(&(x, y): &(i32, i32)) { + println!("Current location: ({}, {})", x, y); +} + +fn main() { + let point = (3, 5); + print_coordinates(&point); +} diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-08/Cargo.lock b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-08/Cargo.lock new file mode 100644 index 000000000..2b4fa2903 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-08/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "patterns" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-08/Cargo.toml b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-08/Cargo.toml new file mode 100644 index 000000000..82fe057bb --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-08/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "patterns" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-08/output.txt b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-08/output.txt new file mode 100644 index 000000000..72274d07c --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-08/output.txt @@ -0,0 +1,19 @@ +$ cargo run + Compiling patterns v0.1.0 (file:///projects/patterns) +error[E0005]: refutable pattern in local binding: `None` not covered + --> src/main.rs:3:9 + | +3 | let Some(x) = some_option_value; + | ^^^^^^^ pattern `None` not covered + | + = note: `let` bindings require an "irrefutable pattern", like a `struct` or an `enum` with only one variant + = note: for more information, visit https://doc.rust-lang.org/book/ch18-02-refutability.html +note: `Option` defined here + = note: the matched value is of type `Option` +help: you might want to use `if let` to ignore the variant that isn't matched + | +3 | let x = if let Some(x) = some_option_value { x } else { todo!() }; + | ++++++++++ ++++++++++++++++++++++ + +For more information about this error, try `rustc --explain E0005`. +error: could not compile `patterns` due to previous error diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-08/src/main.rs b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-08/src/main.rs new file mode 100644 index 000000000..7baa02a49 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-08/src/main.rs @@ -0,0 +1,6 @@ +fn main() { + let some_option_value: Option = None; + // ANCHOR: here + let Some(x) = some_option_value; + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-09/Cargo.lock b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-09/Cargo.lock new file mode 100644 index 000000000..2b4fa2903 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-09/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "patterns" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-09/Cargo.toml b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-09/Cargo.toml new file mode 100644 index 000000000..82fe057bb --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-09/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "patterns" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-09/src/main.rs b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-09/src/main.rs new file mode 100644 index 000000000..d6274fc0e --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-09/src/main.rs @@ -0,0 +1,8 @@ +fn main() { + let some_option_value: Option = None; + // ANCHOR: here + if let Some(x) = some_option_value { + println!("{}", x); + } + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-10/Cargo.lock b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-10/Cargo.lock new file mode 100644 index 000000000..2b4fa2903 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-10/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "patterns" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-10/Cargo.toml b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-10/Cargo.toml new file mode 100644 index 000000000..82fe057bb --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-10/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "patterns" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-10/output.txt b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-10/output.txt new file mode 100644 index 000000000..702d10a23 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-10/output.txt @@ -0,0 +1,16 @@ +$ cargo run + Compiling patterns v0.1.0 (file:///projects/patterns) +warning: irrefutable `if let` pattern + --> src/main.rs:2:8 + | +2 | if let x = 5 { + | ^^^^^^^^^ + | + = note: `#[warn(irrefutable_let_patterns)]` on by default + = note: this pattern will always match, so the `if let` is useless + = help: consider replacing the `if let` with a `let` + +warning: `patterns` (bin "patterns") generated 1 warning + Finished dev [unoptimized + debuginfo] target(s) in 0.39s + Running `target/debug/patterns` +5 diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-10/src/main.rs b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-10/src/main.rs new file mode 100644 index 000000000..cb81772e0 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-10/src/main.rs @@ -0,0 +1,7 @@ +fn main() { + // ANCHOR: here + if let x = 5 { + println!("{}", x); + }; + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-11/Cargo.lock b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-11/Cargo.lock new file mode 100644 index 000000000..2b4fa2903 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-11/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "patterns" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-11/Cargo.toml b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-11/Cargo.toml new file mode 100644 index 000000000..82fe057bb --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-11/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "patterns" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-11/src/main.rs b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-11/src/main.rs new file mode 100644 index 000000000..db942b7ac --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-11/src/main.rs @@ -0,0 +1,14 @@ +fn main() { + // ANCHOR: here + let x = Some(5); + let y = 10; + + match x { + Some(50) => println!("Got 50"), + Some(y) => println!("Matched, y = {y}"), + _ => println!("Default case, x = {:?}", x), + } + + println!("at the end: x = {:?}, y = {y}", x); + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-12/Cargo.lock b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-12/Cargo.lock new file mode 100644 index 000000000..2b4fa2903 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-12/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "patterns" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-12/Cargo.toml b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-12/Cargo.toml new file mode 100644 index 000000000..82fe057bb --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-12/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "patterns" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-12/src/main.rs b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-12/src/main.rs new file mode 100644 index 000000000..62f4ccbf1 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-12/src/main.rs @@ -0,0 +1,12 @@ +struct Point { + x: i32, + y: i32, +} + +fn main() { + let p = Point { x: 0, y: 7 }; + + let Point { x: a, y: b } = p; + assert_eq!(0, a); + assert_eq!(7, b); +} diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-13/Cargo.lock b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-13/Cargo.lock new file mode 100644 index 000000000..2b4fa2903 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-13/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "patterns" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-13/Cargo.toml b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-13/Cargo.toml new file mode 100644 index 000000000..82fe057bb --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-13/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "patterns" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-13/src/main.rs b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-13/src/main.rs new file mode 100644 index 000000000..5badc1594 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-13/src/main.rs @@ -0,0 +1,12 @@ +struct Point { + x: i32, + y: i32, +} + +fn main() { + let p = Point { x: 0, y: 7 }; + + let Point { x, y } = p; + assert_eq!(0, x); + assert_eq!(7, y); +} diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-14/Cargo.lock b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-14/Cargo.lock new file mode 100644 index 000000000..2b4fa2903 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-14/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "patterns" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-14/Cargo.toml b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-14/Cargo.toml new file mode 100644 index 000000000..82fe057bb --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-14/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "patterns" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-14/src/main.rs b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-14/src/main.rs new file mode 100644 index 000000000..8d445d9b9 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-14/src/main.rs @@ -0,0 +1,16 @@ +struct Point { + x: i32, + y: i32, +} + +// ANCHOR: here +fn main() { + let p = Point { x: 0, y: 7 }; + + match p { + Point { x, y: 0 } => println!("On the x axis at {}", x), + Point { x: 0, y } => println!("On the y axis at {}", y), + Point { x, y } => println!("On neither axis: ({}, {})", x, y), + } +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-15/Cargo.lock b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-15/Cargo.lock new file mode 100644 index 000000000..2b4fa2903 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-15/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "patterns" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-15/Cargo.toml b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-15/Cargo.toml new file mode 100644 index 000000000..82fe057bb --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-15/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "patterns" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-15/src/main.rs b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-15/src/main.rs new file mode 100644 index 000000000..9b8dac193 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-15/src/main.rs @@ -0,0 +1,27 @@ +enum Message { + Quit, + Move { x: i32, y: i32 }, + Write(String), + ChangeColor(i32, i32, i32), +} + +fn main() { + let msg = Message::ChangeColor(0, 160, 255); + + match msg { + Message::Quit => { + println!("The Quit variant has no data to destructure.") + } + Message::Move { x, y } => { + println!( + "Move in the x direction {} and in the y direction {}", + x, y + ); + } + Message::Write(text) => println!("Text message: {}", text), + Message::ChangeColor(r, g, b) => println!( + "Change the color to red {}, green {}, and blue {}", + r, g, b + ), + } +} diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-16/Cargo.lock b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-16/Cargo.lock new file mode 100644 index 000000000..2b4fa2903 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-16/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "patterns" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-16/Cargo.toml b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-16/Cargo.toml new file mode 100644 index 000000000..82fe057bb --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-16/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "patterns" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-16/src/main.rs b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-16/src/main.rs new file mode 100644 index 000000000..ed6a20bf4 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-16/src/main.rs @@ -0,0 +1,27 @@ +enum Color { + Rgb(i32, i32, i32), + Hsv(i32, i32, i32), +} + +enum Message { + Quit, + Move { x: i32, y: i32 }, + Write(String), + ChangeColor(Color), +} + +fn main() { + let msg = Message::ChangeColor(Color::Hsv(0, 160, 255)); + + match msg { + Message::ChangeColor(Color::Rgb(r, g, b)) => println!( + "Change the color to red {}, green {}, and blue {}", + r, g, b + ), + Message::ChangeColor(Color::Hsv(h, s, v)) => println!( + "Change the color to hue {}, saturation {}, and value {}", + h, s, v + ), + _ => (), + } +} diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-17/Cargo.lock b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-17/Cargo.lock new file mode 100644 index 000000000..2b4fa2903 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-17/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "patterns" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-17/Cargo.toml b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-17/Cargo.toml new file mode 100644 index 000000000..82fe057bb --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-17/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "patterns" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-17/src/main.rs b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-17/src/main.rs new file mode 100644 index 000000000..cf1fbe072 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-17/src/main.rs @@ -0,0 +1,7 @@ +fn foo(_: i32, y: i32) { + println!("This code only uses the y parameter: {}", y); +} + +fn main() { + foo(3, 4); +} diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-18/Cargo.lock b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-18/Cargo.lock new file mode 100644 index 000000000..2b4fa2903 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-18/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "patterns" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-18/Cargo.toml b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-18/Cargo.toml new file mode 100644 index 000000000..82fe057bb --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-18/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "patterns" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-18/src/main.rs b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-18/src/main.rs new file mode 100644 index 000000000..b776c64c4 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-18/src/main.rs @@ -0,0 +1,17 @@ +fn main() { + // ANCHOR: here + let mut setting_value = Some(5); + let new_setting_value = Some(10); + + match (setting_value, new_setting_value) { + (Some(_), Some(_)) => { + println!("Can't overwrite an existing customized value"); + } + _ => { + setting_value = new_setting_value; + } + } + + println!("setting is {:?}", setting_value); + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-19/Cargo.lock b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-19/Cargo.lock new file mode 100644 index 000000000..2b4fa2903 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-19/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "patterns" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-19/Cargo.toml b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-19/Cargo.toml new file mode 100644 index 000000000..82fe057bb --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-19/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "patterns" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-19/src/main.rs b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-19/src/main.rs new file mode 100644 index 000000000..e28dab111 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-19/src/main.rs @@ -0,0 +1,11 @@ +fn main() { + // ANCHOR: here + let numbers = (2, 4, 8, 16, 32); + + match numbers { + (first, _, third, _, fifth) => { + println!("Some numbers: {first}, {third}, {fifth}") + } + } + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-20/Cargo.lock b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-20/Cargo.lock new file mode 100644 index 000000000..2b4fa2903 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-20/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "patterns" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-20/Cargo.toml b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-20/Cargo.toml new file mode 100644 index 000000000..82fe057bb --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-20/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "patterns" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-20/src/main.rs b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-20/src/main.rs new file mode 100644 index 000000000..1ffc46bad --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-20/src/main.rs @@ -0,0 +1,4 @@ +fn main() { + let _x = 5; + let y = 10; +} diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-21/Cargo.lock b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-21/Cargo.lock new file mode 100644 index 000000000..2b4fa2903 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-21/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "patterns" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-21/Cargo.toml b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-21/Cargo.toml new file mode 100644 index 000000000..82fe057bb --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-21/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "patterns" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-21/src/main.rs b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-21/src/main.rs new file mode 100644 index 000000000..980610503 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-21/src/main.rs @@ -0,0 +1,11 @@ +fn main() { + // ANCHOR: here + let s = Some(String::from("Hello!")); + + if let Some(_s) = s { + println!("found a string"); + } + + println!("{:?}", s); + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-22/Cargo.lock b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-22/Cargo.lock new file mode 100644 index 000000000..2b4fa2903 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-22/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "patterns" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-22/Cargo.toml b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-22/Cargo.toml new file mode 100644 index 000000000..82fe057bb --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-22/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "patterns" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-22/src/main.rs b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-22/src/main.rs new file mode 100644 index 000000000..e2faa345b --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-22/src/main.rs @@ -0,0 +1,11 @@ +fn main() { + // ANCHOR: here + let s = Some(String::from("Hello!")); + + if let Some(_) = s { + println!("found a string"); + } + + println!("{:?}", s); + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-23/Cargo.lock b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-23/Cargo.lock new file mode 100644 index 000000000..2b4fa2903 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-23/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "patterns" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-23/Cargo.toml b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-23/Cargo.toml new file mode 100644 index 000000000..82fe057bb --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-23/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "patterns" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-23/src/main.rs b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-23/src/main.rs new file mode 100644 index 000000000..7a9d9bb36 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-23/src/main.rs @@ -0,0 +1,15 @@ +fn main() { + // ANCHOR: here + struct Point { + x: i32, + y: i32, + z: i32, + } + + let origin = Point { x: 0, y: 0, z: 0 }; + + match origin { + Point { x, .. } => println!("x is {}", x), + } + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-24/Cargo.lock b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-24/Cargo.lock new file mode 100644 index 000000000..2b4fa2903 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-24/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "patterns" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-24/Cargo.toml b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-24/Cargo.toml new file mode 100644 index 000000000..82fe057bb --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-24/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "patterns" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-24/src/main.rs b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-24/src/main.rs new file mode 100644 index 000000000..3f9aaafc2 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-24/src/main.rs @@ -0,0 +1,9 @@ +fn main() { + let numbers = (2, 4, 8, 16, 32); + + match numbers { + (first, .., last) => { + println!("Some numbers: {first}, {last}"); + } + } +} diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-25/Cargo.lock b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-25/Cargo.lock new file mode 100644 index 000000000..a233623e5 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-25/Cargo.lock @@ -0,0 +1,4 @@ +[[package]] +name = "patterns" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-25/Cargo.toml b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-25/Cargo.toml new file mode 100644 index 000000000..82fe057bb --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-25/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "patterns" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-25/output.txt b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-25/output.txt new file mode 100644 index 000000000..7e0357eac --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-25/output.txt @@ -0,0 +1,11 @@ +$ cargo run + Compiling patterns v0.1.0 (file:///projects/patterns) +error: `..` can only be used once per tuple pattern + --> src/main.rs:5:22 + | +5 | (.., second, ..) => { + | -- ^^ can only be used once per tuple pattern + | | + | previously used here + +error: could not compile `patterns` due to previous error diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-25/rustfmt-ignore b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-25/rustfmt-ignore new file mode 100644 index 000000000..06a976dd4 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-25/rustfmt-ignore @@ -0,0 +1 @@ +This listing deliberately doesn't parse so rustfmt fails. diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-25/src/main.rs b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-25/src/main.rs new file mode 100644 index 000000000..b90884eb9 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-25/src/main.rs @@ -0,0 +1,9 @@ +fn main() { + let numbers = (2, 4, 8, 16, 32); + + match numbers { + (.., second, ..) => { + println!("Some numbers: {}", second) + }, + } +} diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-26/Cargo.lock b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-26/Cargo.lock new file mode 100644 index 000000000..2b4fa2903 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-26/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "patterns" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-26/Cargo.toml b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-26/Cargo.toml new file mode 100644 index 000000000..82fe057bb --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-26/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "patterns" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-26/src/main.rs b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-26/src/main.rs new file mode 100644 index 000000000..41fce9795 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-26/src/main.rs @@ -0,0 +1,11 @@ +fn main() { + // ANCHOR: here + let num = Some(4); + + match num { + Some(x) if x % 2 == 0 => println!("The number {} is even", x), + Some(x) => println!("The number {} is odd", x), + None => (), + } + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-27/Cargo.lock b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-27/Cargo.lock new file mode 100644 index 000000000..2b4fa2903 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-27/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "patterns" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-27/Cargo.toml b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-27/Cargo.toml new file mode 100644 index 000000000..82fe057bb --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-27/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "patterns" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-27/src/main.rs b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-27/src/main.rs new file mode 100644 index 000000000..8386a0ab8 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-27/src/main.rs @@ -0,0 +1,12 @@ +fn main() { + let x = Some(5); + let y = 10; + + match x { + Some(50) => println!("Got 50"), + Some(n) if n == y => println!("Matched, n = {n}"), + _ => println!("Default case, x = {:?}", x), + } + + println!("at the end: x = {:?}, y = {y}", x); +} diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-28/Cargo.lock b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-28/Cargo.lock new file mode 100644 index 000000000..2b4fa2903 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-28/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "patterns" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-28/Cargo.toml b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-28/Cargo.toml new file mode 100644 index 000000000..82fe057bb --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-28/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "patterns" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-28/src/main.rs b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-28/src/main.rs new file mode 100644 index 000000000..15804553f --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-28/src/main.rs @@ -0,0 +1,11 @@ +fn main() { + // ANCHOR: here + let x = 4; + let y = false; + + match x { + 4 | 5 | 6 if y => println!("yes"), + _ => println!("no"), + } + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-29/Cargo.lock b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-29/Cargo.lock new file mode 100644 index 000000000..2b4fa2903 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-29/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "patterns" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-29/Cargo.toml b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-29/Cargo.toml new file mode 100644 index 000000000..82fe057bb --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-29/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "patterns" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch18-patterns-and-matching/listing-18-29/src/main.rs b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-29/src/main.rs new file mode 100644 index 000000000..3514deb63 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/listing-18-29/src/main.rs @@ -0,0 +1,19 @@ +fn main() { + // ANCHOR: here + enum Message { + Hello { id: i32 }, + } + + let msg = Message::Hello { id: 5 }; + + match msg { + Message::Hello { + id: id_variable @ 3..=7, + } => println!("Found an id in range: {}", id_variable), + Message::Hello { id: 10..=12 } => { + println!("Found an id in another range") + } + Message::Hello { id } => println!("Found some other id: {}", id), + } + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch18-patterns-and-matching/no-listing-01-literals/Cargo.lock b/src/doc/book/listings/ch18-patterns-and-matching/no-listing-01-literals/Cargo.lock new file mode 100644 index 000000000..2b4fa2903 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/no-listing-01-literals/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "patterns" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch18-patterns-and-matching/no-listing-01-literals/Cargo.toml b/src/doc/book/listings/ch18-patterns-and-matching/no-listing-01-literals/Cargo.toml new file mode 100644 index 000000000..82fe057bb --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/no-listing-01-literals/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "patterns" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch18-patterns-and-matching/no-listing-01-literals/src/main.rs b/src/doc/book/listings/ch18-patterns-and-matching/no-listing-01-literals/src/main.rs new file mode 100644 index 000000000..7978e1ade --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/no-listing-01-literals/src/main.rs @@ -0,0 +1,12 @@ +fn main() { + // ANCHOR: here + let x = 1; + + match x { + 1 => println!("one"), + 2 => println!("two"), + 3 => println!("three"), + _ => println!("anything"), + } + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch18-patterns-and-matching/no-listing-02-multiple-patterns/Cargo.lock b/src/doc/book/listings/ch18-patterns-and-matching/no-listing-02-multiple-patterns/Cargo.lock new file mode 100644 index 000000000..2b4fa2903 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/no-listing-02-multiple-patterns/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "patterns" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch18-patterns-and-matching/no-listing-02-multiple-patterns/Cargo.toml b/src/doc/book/listings/ch18-patterns-and-matching/no-listing-02-multiple-patterns/Cargo.toml new file mode 100644 index 000000000..82fe057bb --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/no-listing-02-multiple-patterns/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "patterns" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch18-patterns-and-matching/no-listing-02-multiple-patterns/src/main.rs b/src/doc/book/listings/ch18-patterns-and-matching/no-listing-02-multiple-patterns/src/main.rs new file mode 100644 index 000000000..e52d815d8 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/no-listing-02-multiple-patterns/src/main.rs @@ -0,0 +1,11 @@ +fn main() { + // ANCHOR: here + let x = 1; + + match x { + 1 | 2 => println!("one or two"), + 3 => println!("three"), + _ => println!("anything"), + } + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch18-patterns-and-matching/no-listing-03-ranges/Cargo.lock b/src/doc/book/listings/ch18-patterns-and-matching/no-listing-03-ranges/Cargo.lock new file mode 100644 index 000000000..2b4fa2903 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/no-listing-03-ranges/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "patterns" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch18-patterns-and-matching/no-listing-03-ranges/Cargo.toml b/src/doc/book/listings/ch18-patterns-and-matching/no-listing-03-ranges/Cargo.toml new file mode 100644 index 000000000..82fe057bb --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/no-listing-03-ranges/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "patterns" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch18-patterns-and-matching/no-listing-03-ranges/src/main.rs b/src/doc/book/listings/ch18-patterns-and-matching/no-listing-03-ranges/src/main.rs new file mode 100644 index 000000000..a3ebe7af0 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/no-listing-03-ranges/src/main.rs @@ -0,0 +1,10 @@ +fn main() { + // ANCHOR: here + let x = 5; + + match x { + 1..=5 => println!("one through five"), + _ => println!("something else"), + } + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch18-patterns-and-matching/no-listing-04-ranges-of-char/Cargo.lock b/src/doc/book/listings/ch18-patterns-and-matching/no-listing-04-ranges-of-char/Cargo.lock new file mode 100644 index 000000000..2b4fa2903 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/no-listing-04-ranges-of-char/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "patterns" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch18-patterns-and-matching/no-listing-04-ranges-of-char/Cargo.toml b/src/doc/book/listings/ch18-patterns-and-matching/no-listing-04-ranges-of-char/Cargo.toml new file mode 100644 index 000000000..82fe057bb --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/no-listing-04-ranges-of-char/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "patterns" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch18-patterns-and-matching/no-listing-04-ranges-of-char/src/main.rs b/src/doc/book/listings/ch18-patterns-and-matching/no-listing-04-ranges-of-char/src/main.rs new file mode 100644 index 000000000..8cebfef5e --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/no-listing-04-ranges-of-char/src/main.rs @@ -0,0 +1,11 @@ +fn main() { + // ANCHOR: here + let x = 'c'; + + match x { + 'a'..='j' => println!("early ASCII letter"), + 'k'..='z' => println!("late ASCII letter"), + _ => println!("something else"), + } + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch18-patterns-and-matching/no-listing-05-destructuring-structs-and-tuples/Cargo.lock b/src/doc/book/listings/ch18-patterns-and-matching/no-listing-05-destructuring-structs-and-tuples/Cargo.lock new file mode 100644 index 000000000..2b4fa2903 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/no-listing-05-destructuring-structs-and-tuples/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "patterns" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch18-patterns-and-matching/no-listing-05-destructuring-structs-and-tuples/Cargo.toml b/src/doc/book/listings/ch18-patterns-and-matching/no-listing-05-destructuring-structs-and-tuples/Cargo.toml new file mode 100644 index 000000000..82fe057bb --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/no-listing-05-destructuring-structs-and-tuples/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "patterns" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch18-patterns-and-matching/no-listing-05-destructuring-structs-and-tuples/src/main.rs b/src/doc/book/listings/ch18-patterns-and-matching/no-listing-05-destructuring-structs-and-tuples/src/main.rs new file mode 100644 index 000000000..962d09349 --- /dev/null +++ b/src/doc/book/listings/ch18-patterns-and-matching/no-listing-05-destructuring-structs-and-tuples/src/main.rs @@ -0,0 +1,10 @@ +fn main() { + struct Point { + x: i32, + y: i32, + } + + // ANCHOR: here + let ((feet, inches), Point { x, y }) = ((3, 10), Point { x: 3, y: -10 }); + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-01/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/listing-19-01/Cargo.lock new file mode 100644 index 000000000..497817bf2 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-01/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "unsafe-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-01/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/listing-19-01/Cargo.toml new file mode 100644 index 000000000..3e8a29201 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-01/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "unsafe-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-01/src/main.rs b/src/doc/book/listings/ch19-advanced-features/listing-19-01/src/main.rs new file mode 100644 index 000000000..893f57890 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-01/src/main.rs @@ -0,0 +1,8 @@ +fn main() { + // ANCHOR: here + let mut num = 5; + + let r1 = &num as *const i32; + let r2 = &mut num as *mut i32; + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-02/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/listing-19-02/Cargo.lock new file mode 100644 index 000000000..497817bf2 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-02/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "unsafe-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-02/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/listing-19-02/Cargo.toml new file mode 100644 index 000000000..3e8a29201 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-02/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "unsafe-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-02/src/main.rs b/src/doc/book/listings/ch19-advanced-features/listing-19-02/src/main.rs new file mode 100644 index 000000000..849629a7c --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-02/src/main.rs @@ -0,0 +1,6 @@ +fn main() { + // ANCHOR: here + let address = 0x012345usize; + let r = address as *const i32; + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-03/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/listing-19-03/Cargo.lock new file mode 100644 index 000000000..497817bf2 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-03/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "unsafe-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-03/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/listing-19-03/Cargo.toml new file mode 100644 index 000000000..3e8a29201 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-03/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "unsafe-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-03/src/main.rs b/src/doc/book/listings/ch19-advanced-features/listing-19-03/src/main.rs new file mode 100644 index 000000000..02a0be6b0 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-03/src/main.rs @@ -0,0 +1,13 @@ +fn main() { + // ANCHOR: here + let mut num = 5; + + let r1 = &num as *const i32; + let r2 = &mut num as *mut i32; + + unsafe { + println!("r1 is: {}", *r1); + println!("r2 is: {}", *r2); + } + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-04/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/listing-19-04/Cargo.lock new file mode 100644 index 000000000..497817bf2 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-04/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "unsafe-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-04/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/listing-19-04/Cargo.toml new file mode 100644 index 000000000..3e8a29201 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-04/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "unsafe-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-04/src/main.rs b/src/doc/book/listings/ch19-advanced-features/listing-19-04/src/main.rs new file mode 100644 index 000000000..6ac58442d --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-04/src/main.rs @@ -0,0 +1,12 @@ +fn main() { + // ANCHOR: here + let mut v = vec![1, 2, 3, 4, 5, 6]; + + let r = &mut v[..]; + + let (a, b) = r.split_at_mut(3); + + assert_eq!(a, &mut [1, 2, 3]); + assert_eq!(b, &mut [4, 5, 6]); + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-05/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/listing-19-05/Cargo.lock new file mode 100644 index 000000000..497817bf2 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-05/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "unsafe-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-05/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/listing-19-05/Cargo.toml new file mode 100644 index 000000000..3e8a29201 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-05/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "unsafe-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-05/output.txt b/src/doc/book/listings/ch19-advanced-features/listing-19-05/output.txt new file mode 100644 index 000000000..f4b7582ac --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-05/output.txt @@ -0,0 +1,17 @@ +$ cargo run + Compiling unsafe-example v0.1.0 (file:///projects/unsafe-example) +error[E0499]: cannot borrow `*values` as mutable more than once at a time + --> src/main.rs:6:31 + | +1 | fn split_at_mut(values: &mut [i32], mid: usize) -> (&mut [i32], &mut [i32]) { + | - let's call the lifetime of this reference `'1` +... +6 | (&mut values[..mid], &mut values[mid..]) + | --------------------------^^^^^^-------- + | | | | + | | | second mutable borrow occurs here + | | first mutable borrow occurs here + | returning this value requires that `*values` is borrowed for `'1` + +For more information about this error, try `rustc --explain E0499`. +error: could not compile `unsafe-example` due to previous error diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-05/src/main.rs b/src/doc/book/listings/ch19-advanced-features/listing-19-05/src/main.rs new file mode 100644 index 000000000..dabf63de1 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-05/src/main.rs @@ -0,0 +1,14 @@ +// ANCHOR: here +fn split_at_mut(values: &mut [i32], mid: usize) -> (&mut [i32], &mut [i32]) { + let len = values.len(); + + assert!(mid <= len); + + (&mut values[..mid], &mut values[mid..]) +} +// ANCHOR_END: here + +fn main() { + let mut vector = vec![1, 2, 3, 4, 5, 6]; + let (left, right) = split_at_mut(&mut vector, 3); +} diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-06/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/listing-19-06/Cargo.lock new file mode 100644 index 000000000..497817bf2 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-06/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "unsafe-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-06/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/listing-19-06/Cargo.toml new file mode 100644 index 000000000..3e8a29201 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-06/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "unsafe-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-06/src/main.rs b/src/doc/book/listings/ch19-advanced-features/listing-19-06/src/main.rs new file mode 100644 index 000000000..3af21f761 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-06/src/main.rs @@ -0,0 +1,22 @@ +// ANCHOR: here +use std::slice; + +fn split_at_mut(values: &mut [i32], mid: usize) -> (&mut [i32], &mut [i32]) { + let len = values.len(); + let ptr = values.as_mut_ptr(); + + assert!(mid <= len); + + unsafe { + ( + slice::from_raw_parts_mut(ptr, mid), + slice::from_raw_parts_mut(ptr.add(mid), len - mid), + ) + } +} +// ANCHOR_END: here + +fn main() { + let mut vector = vec![1, 2, 3, 4, 5, 6]; + let (left, right) = split_at_mut(&mut vector, 3); +} diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-07/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/listing-19-07/Cargo.lock new file mode 100644 index 000000000..497817bf2 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-07/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "unsafe-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-07/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/listing-19-07/Cargo.toml new file mode 100644 index 000000000..3e8a29201 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-07/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "unsafe-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-07/src/main.rs b/src/doc/book/listings/ch19-advanced-features/listing-19-07/src/main.rs new file mode 100644 index 000000000..b4d6cdb7c --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-07/src/main.rs @@ -0,0 +1,10 @@ +fn main() { + // ANCHOR: here + use std::slice; + + let address = 0x01234usize; + let r = address as *mut i32; + + let values: &[i32] = unsafe { slice::from_raw_parts_mut(r, 10000) }; + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-08/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/listing-19-08/Cargo.lock new file mode 100644 index 000000000..497817bf2 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-08/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "unsafe-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-08/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/listing-19-08/Cargo.toml new file mode 100644 index 000000000..3e8a29201 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-08/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "unsafe-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-08/src/main.rs b/src/doc/book/listings/ch19-advanced-features/listing-19-08/src/main.rs new file mode 100644 index 000000000..8b56630c9 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-08/src/main.rs @@ -0,0 +1,9 @@ +extern "C" { + fn abs(input: i32) -> i32; +} + +fn main() { + unsafe { + println!("Absolute value of -3 according to C: {}", abs(-3)); + } +} diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-09/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/listing-19-09/Cargo.lock new file mode 100644 index 000000000..497817bf2 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-09/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "unsafe-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-09/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/listing-19-09/Cargo.toml new file mode 100644 index 000000000..3e8a29201 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-09/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "unsafe-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-09/src/main.rs b/src/doc/book/listings/ch19-advanced-features/listing-19-09/src/main.rs new file mode 100644 index 000000000..82a4b4219 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-09/src/main.rs @@ -0,0 +1,5 @@ +static HELLO_WORLD: &str = "Hello, world!"; + +fn main() { + println!("name is: {}", HELLO_WORLD); +} diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-10/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/listing-19-10/Cargo.lock new file mode 100644 index 000000000..497817bf2 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-10/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "unsafe-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-10/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/listing-19-10/Cargo.toml new file mode 100644 index 000000000..3e8a29201 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-10/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "unsafe-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-10/src/main.rs b/src/doc/book/listings/ch19-advanced-features/listing-19-10/src/main.rs new file mode 100644 index 000000000..e8dab68e0 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-10/src/main.rs @@ -0,0 +1,15 @@ +static mut COUNTER: u32 = 0; + +fn add_to_count(inc: u32) { + unsafe { + COUNTER += inc; + } +} + +fn main() { + add_to_count(3); + + unsafe { + println!("COUNTER: {}", COUNTER); + } +} diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-11/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/listing-19-11/Cargo.lock new file mode 100644 index 000000000..497817bf2 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-11/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "unsafe-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-11/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/listing-19-11/Cargo.toml new file mode 100644 index 000000000..3e8a29201 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-11/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "unsafe-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-11/src/main.rs b/src/doc/book/listings/ch19-advanced-features/listing-19-11/src/main.rs new file mode 100644 index 000000000..885c1aa1d --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-11/src/main.rs @@ -0,0 +1,9 @@ +unsafe trait Foo { + // methods go here +} + +unsafe impl Foo for i32 { + // method implementations go here +} + +fn main() {} diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-12/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/listing-19-12/Cargo.lock new file mode 100644 index 000000000..b1977d01e --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-12/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "traits-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-12/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/listing-19-12/Cargo.toml new file mode 100644 index 000000000..52395a587 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-12/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "traits-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-12/src/lib.rs b/src/doc/book/listings/ch19-advanced-features/listing-19-12/src/lib.rs new file mode 100644 index 000000000..dbe04620e --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-12/src/lib.rs @@ -0,0 +1,5 @@ +pub trait Iterator { + type Item; + + fn next(&mut self) -> Option; +} diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-13/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/listing-19-13/Cargo.lock new file mode 100644 index 000000000..b1977d01e --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-13/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "traits-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-13/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/listing-19-13/Cargo.toml new file mode 100644 index 000000000..52395a587 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-13/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "traits-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-13/src/lib.rs b/src/doc/book/listings/ch19-advanced-features/listing-19-13/src/lib.rs new file mode 100644 index 000000000..7c9479c5b --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-13/src/lib.rs @@ -0,0 +1,3 @@ +pub trait Iterator { + fn next(&mut self) -> Option; +} diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-14/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/listing-19-14/Cargo.lock new file mode 100644 index 000000000..b1977d01e --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-14/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "traits-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-14/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/listing-19-14/Cargo.toml new file mode 100644 index 000000000..52395a587 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-14/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "traits-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-14/src/main.rs b/src/doc/book/listings/ch19-advanced-features/listing-19-14/src/main.rs new file mode 100644 index 000000000..9111fbc55 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-14/src/main.rs @@ -0,0 +1,25 @@ +use std::ops::Add; + +#[derive(Debug, Copy, Clone, PartialEq)] +struct Point { + x: i32, + y: i32, +} + +impl Add for Point { + type Output = Point; + + fn add(self, other: Point) -> Point { + Point { + x: self.x + other.x, + y: self.y + other.y, + } + } +} + +fn main() { + assert_eq!( + Point { x: 1, y: 0 } + Point { x: 2, y: 3 }, + Point { x: 3, y: 3 } + ); +} diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-15/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/listing-19-15/Cargo.lock new file mode 100644 index 000000000..b1977d01e --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-15/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "traits-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-15/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/listing-19-15/Cargo.toml new file mode 100644 index 000000000..52395a587 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-15/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "traits-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-15/src/lib.rs b/src/doc/book/listings/ch19-advanced-features/listing-19-15/src/lib.rs new file mode 100644 index 000000000..f38bf475c --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-15/src/lib.rs @@ -0,0 +1,12 @@ +use std::ops::Add; + +struct Millimeters(u32); +struct Meters(u32); + +impl Add for Millimeters { + type Output = Millimeters; + + fn add(self, other: Meters) -> Millimeters { + Millimeters(self.0 + (other.0 * 1000)) + } +} diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-16/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/listing-19-16/Cargo.lock new file mode 100644 index 000000000..b1977d01e --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-16/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "traits-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-16/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/listing-19-16/Cargo.toml new file mode 100644 index 000000000..52395a587 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-16/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "traits-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-16/src/main.rs b/src/doc/book/listings/ch19-advanced-features/listing-19-16/src/main.rs new file mode 100644 index 000000000..d854e287d --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-16/src/main.rs @@ -0,0 +1,31 @@ +// ANCHOR: here +trait Pilot { + fn fly(&self); +} + +trait Wizard { + fn fly(&self); +} + +struct Human; + +impl Pilot for Human { + fn fly(&self) { + println!("This is your captain speaking."); + } +} + +impl Wizard for Human { + fn fly(&self) { + println!("Up!"); + } +} + +impl Human { + fn fly(&self) { + println!("*waving arms furiously*"); + } +} +// ANCHOR_END: here + +fn main() {} diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-17/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/listing-19-17/Cargo.lock new file mode 100644 index 000000000..b1977d01e --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-17/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "traits-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-17/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/listing-19-17/Cargo.toml new file mode 100644 index 000000000..52395a587 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-17/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "traits-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-17/src/main.rs b/src/doc/book/listings/ch19-advanced-features/listing-19-17/src/main.rs new file mode 100644 index 000000000..3df65a7ce --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-17/src/main.rs @@ -0,0 +1,34 @@ +trait Pilot { + fn fly(&self); +} + +trait Wizard { + fn fly(&self); +} + +struct Human; + +impl Pilot for Human { + fn fly(&self) { + println!("This is your captain speaking."); + } +} + +impl Wizard for Human { + fn fly(&self) { + println!("Up!"); + } +} + +impl Human { + fn fly(&self) { + println!("*waving arms furiously*"); + } +} + +// ANCHOR: here +fn main() { + let person = Human; + person.fly(); +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-18/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/listing-19-18/Cargo.lock new file mode 100644 index 000000000..b1977d01e --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-18/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "traits-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-18/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/listing-19-18/Cargo.toml new file mode 100644 index 000000000..52395a587 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-18/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "traits-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-18/output.txt b/src/doc/book/listings/ch19-advanced-features/listing-19-18/output.txt new file mode 100644 index 000000000..2e9da17d6 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-18/output.txt @@ -0,0 +1,7 @@ +$ cargo run + Compiling traits-example v0.1.0 (file:///projects/traits-example) + Finished dev [unoptimized + debuginfo] target(s) in 0.46s + Running `target/debug/traits-example` +This is your captain speaking. +Up! +*waving arms furiously* diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-18/src/main.rs b/src/doc/book/listings/ch19-advanced-features/listing-19-18/src/main.rs new file mode 100644 index 000000000..fa01c09cc --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-18/src/main.rs @@ -0,0 +1,36 @@ +trait Pilot { + fn fly(&self); +} + +trait Wizard { + fn fly(&self); +} + +struct Human; + +impl Pilot for Human { + fn fly(&self) { + println!("This is your captain speaking."); + } +} + +impl Wizard for Human { + fn fly(&self) { + println!("Up!"); + } +} + +impl Human { + fn fly(&self) { + println!("*waving arms furiously*"); + } +} + +// ANCHOR: here +fn main() { + let person = Human; + Pilot::fly(&person); + Wizard::fly(&person); + person.fly(); +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-19/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/listing-19-19/Cargo.lock new file mode 100644 index 000000000..b1977d01e --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-19/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "traits-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-19/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/listing-19-19/Cargo.toml new file mode 100644 index 000000000..52395a587 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-19/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "traits-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-19/output.txt b/src/doc/book/listings/ch19-advanced-features/listing-19-19/output.txt new file mode 100644 index 000000000..087e802b1 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-19/output.txt @@ -0,0 +1,5 @@ +$ cargo run + Compiling traits-example v0.1.0 (file:///projects/traits-example) + Finished dev [unoptimized + debuginfo] target(s) in 0.54s + Running `target/debug/traits-example` +A baby dog is called a Spot diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-19/src/main.rs b/src/doc/book/listings/ch19-advanced-features/listing-19-19/src/main.rs new file mode 100644 index 000000000..44affe0ee --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-19/src/main.rs @@ -0,0 +1,21 @@ +trait Animal { + fn baby_name() -> String; +} + +struct Dog; + +impl Dog { + fn baby_name() -> String { + String::from("Spot") + } +} + +impl Animal for Dog { + fn baby_name() -> String { + String::from("puppy") + } +} + +fn main() { + println!("A baby dog is called a {}", Dog::baby_name()); +} diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-20/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/listing-19-20/Cargo.lock new file mode 100644 index 000000000..b1977d01e --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-20/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "traits-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-20/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/listing-19-20/Cargo.toml new file mode 100644 index 000000000..52395a587 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-20/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "traits-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-20/output.txt b/src/doc/book/listings/ch19-advanced-features/listing-19-20/output.txt new file mode 100644 index 000000000..684508245 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-20/output.txt @@ -0,0 +1,12 @@ +$ cargo run + Compiling traits-example v0.1.0 (file:///projects/traits-example) +error[E0283]: type annotations needed + --> src/main.rs:20:43 + | +20 | println!("A baby dog is called a {}", Animal::baby_name()); + | ^^^^^^^^^^^^^^^^^ cannot infer type + | + = note: cannot satisfy `_: Animal` + +For more information about this error, try `rustc --explain E0283`. +error: could not compile `traits-example` due to previous error diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-20/src/main.rs b/src/doc/book/listings/ch19-advanced-features/listing-19-20/src/main.rs new file mode 100644 index 000000000..8e295c9b6 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-20/src/main.rs @@ -0,0 +1,23 @@ +trait Animal { + fn baby_name() -> String; +} + +struct Dog; + +impl Dog { + fn baby_name() -> String { + String::from("Spot") + } +} + +impl Animal for Dog { + fn baby_name() -> String { + String::from("puppy") + } +} + +// ANCHOR: here +fn main() { + println!("A baby dog is called a {}", Animal::baby_name()); +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-21/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/listing-19-21/Cargo.lock new file mode 100644 index 000000000..b1977d01e --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-21/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "traits-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-21/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/listing-19-21/Cargo.toml new file mode 100644 index 000000000..52395a587 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-21/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "traits-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-21/output.txt b/src/doc/book/listings/ch19-advanced-features/listing-19-21/output.txt new file mode 100644 index 000000000..4d1ee5ab4 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-21/output.txt @@ -0,0 +1,5 @@ +$ cargo run + Compiling traits-example v0.1.0 (file:///projects/traits-example) + Finished dev [unoptimized + debuginfo] target(s) in 0.48s + Running `target/debug/traits-example` +A baby dog is called a puppy diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-21/src/main.rs b/src/doc/book/listings/ch19-advanced-features/listing-19-21/src/main.rs new file mode 100644 index 000000000..b1df72895 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-21/src/main.rs @@ -0,0 +1,23 @@ +trait Animal { + fn baby_name() -> String; +} + +struct Dog; + +impl Dog { + fn baby_name() -> String { + String::from("Spot") + } +} + +impl Animal for Dog { + fn baby_name() -> String { + String::from("puppy") + } +} + +// ANCHOR: here +fn main() { + println!("A baby dog is called a {}", ::baby_name()); +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-22/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/listing-19-22/Cargo.lock new file mode 100644 index 000000000..b1977d01e --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-22/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "traits-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-22/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/listing-19-22/Cargo.toml new file mode 100644 index 000000000..52395a587 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-22/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "traits-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-22/src/main.rs b/src/doc/book/listings/ch19-advanced-features/listing-19-22/src/main.rs new file mode 100644 index 000000000..febe58b0c --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-22/src/main.rs @@ -0,0 +1,17 @@ +// ANCHOR: here +use std::fmt; + +trait OutlinePrint: fmt::Display { + fn outline_print(&self) { + let output = self.to_string(); + let len = output.len(); + println!("{}", "*".repeat(len + 4)); + println!("*{}*", " ".repeat(len + 2)); + println!("* {} *", output); + println!("*{}*", " ".repeat(len + 2)); + println!("{}", "*".repeat(len + 4)); + } +} +// ANCHOR_END: here + +fn main() {} diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-23/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/listing-19-23/Cargo.lock new file mode 100644 index 000000000..b1977d01e --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-23/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "traits-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-23/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/listing-19-23/Cargo.toml new file mode 100644 index 000000000..52395a587 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-23/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "traits-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-23/src/main.rs b/src/doc/book/listings/ch19-advanced-features/listing-19-23/src/main.rs new file mode 100644 index 000000000..eae46c92f --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-23/src/main.rs @@ -0,0 +1,14 @@ +use std::fmt; + +struct Wrapper(Vec); + +impl fmt::Display for Wrapper { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "[{}]", self.0.join(", ")) + } +} + +fn main() { + let w = Wrapper(vec![String::from("hello"), String::from("world")]); + println!("w = {}", w); +} diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-24/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/listing-19-24/Cargo.lock new file mode 100644 index 000000000..c0c98a79c --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-24/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "types-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-24/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/listing-19-24/Cargo.toml new file mode 100644 index 000000000..a2ae20c77 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-24/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "types-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-24/src/main.rs b/src/doc/book/listings/ch19-advanced-features/listing-19-24/src/main.rs new file mode 100644 index 000000000..d604ae8d6 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-24/src/main.rs @@ -0,0 +1,16 @@ +fn main() { + // ANCHOR: here + let f: Box = Box::new(|| println!("hi")); + + fn takes_long_type(f: Box) { + // --snip-- + } + + fn returns_long_type() -> Box { + // --snip-- + // ANCHOR_END: here + Box::new(|| ()) + // ANCHOR: here + } + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-25/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/listing-19-25/Cargo.lock new file mode 100644 index 000000000..c0c98a79c --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-25/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "types-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-25/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/listing-19-25/Cargo.toml new file mode 100644 index 000000000..a2ae20c77 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-25/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "types-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-25/src/main.rs b/src/doc/book/listings/ch19-advanced-features/listing-19-25/src/main.rs new file mode 100644 index 000000000..af35bed2c --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-25/src/main.rs @@ -0,0 +1,18 @@ +fn main() { + // ANCHOR: here + type Thunk = Box; + + let f: Thunk = Box::new(|| println!("hi")); + + fn takes_long_type(f: Thunk) { + // --snip-- + } + + fn returns_long_type() -> Thunk { + // --snip-- + // ANCHOR_END: here + Box::new(|| ()) + // ANCHOR: here + } + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-27/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/listing-19-27/Cargo.lock new file mode 100644 index 000000000..b2327c755 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-27/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "functions-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-27/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/listing-19-27/Cargo.toml new file mode 100644 index 000000000..b196f35b5 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-27/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "functions-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-27/src/main.rs b/src/doc/book/listings/ch19-advanced-features/listing-19-27/src/main.rs new file mode 100644 index 000000000..91b2cf04b --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-27/src/main.rs @@ -0,0 +1,13 @@ +fn add_one(x: i32) -> i32 { + x + 1 +} + +fn do_twice(f: fn(i32) -> i32, arg: i32) -> i32 { + f(arg) + f(arg) +} + +fn main() { + let answer = do_twice(add_one, 5); + + println!("The answer is: {}", answer); +} diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-28/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/listing-19-28/Cargo.lock new file mode 100644 index 000000000..b2d925754 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-28/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "macros-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-28/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/listing-19-28/Cargo.toml new file mode 100644 index 000000000..9218091c8 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-28/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "macros-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-28/src/lib.rs b/src/doc/book/listings/ch19-advanced-features/listing-19-28/src/lib.rs new file mode 100644 index 000000000..7c7c4756c --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-28/src/lib.rs @@ -0,0 +1,12 @@ +#[macro_export] +macro_rules! vec { + ( $( $x:expr ),* ) => { + { + let mut temp_vec = Vec::new(); + $( + temp_vec.push($x); + )* + temp_vec + } + }; +} diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-30/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/listing-19-30/Cargo.lock new file mode 100644 index 000000000..39afcf282 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-30/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "hello_macro" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-30/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/listing-19-30/Cargo.toml new file mode 100644 index 000000000..c6fb92087 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-30/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "hello_macro" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-30/src/main.rs b/src/doc/book/listings/ch19-advanced-features/listing-19-30/src/main.rs new file mode 100644 index 000000000..468c30aa4 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-30/src/main.rs @@ -0,0 +1,9 @@ +use hello_macro::HelloMacro; +use hello_macro_derive::HelloMacro; + +#[derive(HelloMacro)] +struct Pancakes; + +fn main() { + Pancakes::hello_macro(); +} diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-31/hello_macro/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/listing-19-31/hello_macro/Cargo.lock new file mode 100644 index 000000000..39afcf282 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-31/hello_macro/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "hello_macro" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-31/hello_macro/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/listing-19-31/hello_macro/Cargo.toml new file mode 100644 index 000000000..c6fb92087 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-31/hello_macro/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "hello_macro" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-31/hello_macro/hello_macro_derive/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/listing-19-31/hello_macro/hello_macro_derive/Cargo.lock new file mode 100644 index 000000000..9a38c8ac2 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-31/hello_macro/hello_macro_derive/Cargo.lock @@ -0,0 +1,46 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "hello_macro_derive" +version = "0.1.0" +dependencies = [ + "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 1.0.14 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "proc-macro2" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "quote" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "syn" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "unicode-xid" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[metadata] +"checksum proc-macro2 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)" = "3acb317c6ff86a4e579dfa00fc5e6cca91ecbb4e7eb2df0468805b674eb88548" +"checksum quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "053a8c8bcc71fcce321828dc897a98ab9760bef03a4fc36693c231e5b3216cfe" +"checksum syn 1.0.14 (registry+https://github.com/rust-lang/crates.io-index)" = "af6f3550d8dff9ef7dc34d384ac6f107e5d31c8f57d9f28e0081503f547ac8f5" +"checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c" diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-31/hello_macro/hello_macro_derive/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/listing-19-31/hello_macro/hello_macro_derive/Cargo.toml new file mode 100644 index 000000000..aa076ac48 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-31/hello_macro/hello_macro_derive/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "hello_macro_derive" +version = "0.1.0" +edition = "2021" + +[lib] +proc-macro = true + +[dependencies] +syn = "1.0" +quote = "1.0" diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-31/hello_macro/hello_macro_derive/src/lib.rs b/src/doc/book/listings/ch19-advanced-features/listing-19-31/hello_macro/hello_macro_derive/src/lib.rs new file mode 100644 index 000000000..11643a8d6 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-31/hello_macro/hello_macro_derive/src/lib.rs @@ -0,0 +1,13 @@ +use proc_macro::TokenStream; +use quote::quote; +use syn; + +#[proc_macro_derive(HelloMacro)] +pub fn hello_macro_derive(input: TokenStream) -> TokenStream { + // Construct a representation of Rust code as a syntax tree + // that we can manipulate + let ast = syn::parse(input).unwrap(); + + // Build the trait implementation + impl_hello_macro(&ast) +} diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-31/hello_macro/src/lib.rs b/src/doc/book/listings/ch19-advanced-features/listing-19-31/hello_macro/src/lib.rs new file mode 100644 index 000000000..e74793184 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-31/hello_macro/src/lib.rs @@ -0,0 +1,3 @@ +pub trait HelloMacro { + fn hello_macro(); +} diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-31/hello_macro/src/main.rs b/src/doc/book/listings/ch19-advanced-features/listing-19-31/hello_macro/src/main.rs new file mode 100644 index 000000000..10b028b2d --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-31/hello_macro/src/main.rs @@ -0,0 +1,13 @@ +use hello_macro::HelloMacro; + +struct Pancakes; + +impl HelloMacro for Pancakes { + fn hello_macro() { + println!("Hello, Macro! My name is Pancakes!"); + } +} + +fn main() { + Pancakes::hello_macro(); +} diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-33/hello_macro/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/listing-19-33/hello_macro/Cargo.lock new file mode 100644 index 000000000..39afcf282 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-33/hello_macro/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "hello_macro" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-33/hello_macro/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/listing-19-33/hello_macro/Cargo.toml new file mode 100644 index 000000000..c6fb92087 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-33/hello_macro/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "hello_macro" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-33/hello_macro/hello_macro_derive/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/listing-19-33/hello_macro/hello_macro_derive/Cargo.lock new file mode 100644 index 000000000..9a38c8ac2 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-33/hello_macro/hello_macro_derive/Cargo.lock @@ -0,0 +1,46 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "hello_macro_derive" +version = "0.1.0" +dependencies = [ + "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 1.0.14 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "proc-macro2" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "quote" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "syn" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "unicode-xid" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[metadata] +"checksum proc-macro2 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)" = "3acb317c6ff86a4e579dfa00fc5e6cca91ecbb4e7eb2df0468805b674eb88548" +"checksum quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "053a8c8bcc71fcce321828dc897a98ab9760bef03a4fc36693c231e5b3216cfe" +"checksum syn 1.0.14 (registry+https://github.com/rust-lang/crates.io-index)" = "af6f3550d8dff9ef7dc34d384ac6f107e5d31c8f57d9f28e0081503f547ac8f5" +"checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c" diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-33/hello_macro/hello_macro_derive/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/listing-19-33/hello_macro/hello_macro_derive/Cargo.toml new file mode 100644 index 000000000..aa076ac48 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-33/hello_macro/hello_macro_derive/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "hello_macro_derive" +version = "0.1.0" +edition = "2021" + +[lib] +proc-macro = true + +[dependencies] +syn = "1.0" +quote = "1.0" diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-33/hello_macro/hello_macro_derive/src/lib.rs b/src/doc/book/listings/ch19-advanced-features/listing-19-33/hello_macro/hello_macro_derive/src/lib.rs new file mode 100644 index 000000000..dac6c98f6 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-33/hello_macro/hello_macro_derive/src/lib.rs @@ -0,0 +1,27 @@ +use proc_macro::TokenStream; +use quote::quote; +use syn; + +#[proc_macro_derive(HelloMacro)] +pub fn hello_macro_derive(input: TokenStream) -> TokenStream { + // Construct a representation of Rust code as a syntax tree + // that we can manipulate + let ast = syn::parse(input).unwrap(); + + // Build the trait implementation + impl_hello_macro(&ast) +} + +// ANCHOR: here +fn impl_hello_macro(ast: &syn::DeriveInput) -> TokenStream { + let name = &ast.ident; + let gen = quote! { + impl HelloMacro for #name { + fn hello_macro() { + println!("Hello, Macro! My name is {}!", stringify!(#name)); + } + } + }; + gen.into() +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-33/hello_macro/src/lib.rs b/src/doc/book/listings/ch19-advanced-features/listing-19-33/hello_macro/src/lib.rs new file mode 100644 index 000000000..e74793184 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-33/hello_macro/src/lib.rs @@ -0,0 +1,3 @@ +pub trait HelloMacro { + fn hello_macro(); +} diff --git a/src/doc/book/listings/ch19-advanced-features/listing-19-33/hello_macro/src/main.rs b/src/doc/book/listings/ch19-advanced-features/listing-19-33/hello_macro/src/main.rs new file mode 100644 index 000000000..10b028b2d --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/listing-19-33/hello_macro/src/main.rs @@ -0,0 +1,13 @@ +use hello_macro::HelloMacro; + +struct Pancakes; + +impl HelloMacro for Pancakes { + fn hello_macro() { + println!("Hello, Macro! My name is Pancakes!"); + } +} + +fn main() { + Pancakes::hello_macro(); +} diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-01-unsafe-fn/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/no-listing-01-unsafe-fn/Cargo.lock new file mode 100644 index 000000000..497817bf2 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-01-unsafe-fn/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "unsafe-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-01-unsafe-fn/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/no-listing-01-unsafe-fn/Cargo.toml new file mode 100644 index 000000000..3e8a29201 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-01-unsafe-fn/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "unsafe-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-01-unsafe-fn/src/main.rs b/src/doc/book/listings/ch19-advanced-features/no-listing-01-unsafe-fn/src/main.rs new file mode 100644 index 000000000..21ecdbe5a --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-01-unsafe-fn/src/main.rs @@ -0,0 +1,9 @@ +fn main() { + // ANCHOR: here + unsafe fn dangerous() {} + + unsafe { + dangerous(); + } + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-02-impl-outlineprint-for-point/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/no-listing-02-impl-outlineprint-for-point/Cargo.lock new file mode 100644 index 000000000..b1977d01e --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-02-impl-outlineprint-for-point/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "traits-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-02-impl-outlineprint-for-point/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/no-listing-02-impl-outlineprint-for-point/Cargo.toml new file mode 100644 index 000000000..52395a587 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-02-impl-outlineprint-for-point/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "traits-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-02-impl-outlineprint-for-point/output.txt b/src/doc/book/listings/ch19-advanced-features/no-listing-02-impl-outlineprint-for-point/output.txt new file mode 100644 index 000000000..0991f10fa --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-02-impl-outlineprint-for-point/output.txt @@ -0,0 +1,18 @@ +$ cargo run + Compiling traits-example v0.1.0 (file:///projects/traits-example) +error[E0277]: `Point` doesn't implement `std::fmt::Display` + --> src/main.rs:20:6 + | +20 | impl OutlinePrint for Point {} + | ^^^^^^^^^^^^ `Point` cannot be formatted with the default formatter + | + = help: the trait `std::fmt::Display` is not implemented for `Point` + = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead +note: required by a bound in `OutlinePrint` + --> src/main.rs:3:21 + | +3 | trait OutlinePrint: fmt::Display { + | ^^^^^^^^^^^^ required by this bound in `OutlinePrint` + +For more information about this error, try `rustc --explain E0277`. +error: could not compile `traits-example` due to previous error diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-02-impl-outlineprint-for-point/src/main.rs b/src/doc/book/listings/ch19-advanced-features/no-listing-02-impl-outlineprint-for-point/src/main.rs new file mode 100644 index 000000000..a1e2fe4c4 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-02-impl-outlineprint-for-point/src/main.rs @@ -0,0 +1,27 @@ +use std::fmt; + +trait OutlinePrint: fmt::Display { + fn outline_print(&self) { + let output = self.to_string(); + let len = output.len(); + println!("{}", "*".repeat(len + 4)); + println!("*{}*", " ".repeat(len + 2)); + println!("* {} *", output); + println!("*{}*", " ".repeat(len + 2)); + println!("{}", "*".repeat(len + 4)); + } +} + +// ANCHOR: here +struct Point { + x: i32, + y: i32, +} + +impl OutlinePrint for Point {} +// ANCHOR_END: here + +fn main() { + let p = Point { x: 1, y: 3 }; + p.outline_print(); +} diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-03-impl-display-for-point/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/no-listing-03-impl-display-for-point/Cargo.lock new file mode 100644 index 000000000..b1977d01e --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-03-impl-display-for-point/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "traits-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-03-impl-display-for-point/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/no-listing-03-impl-display-for-point/Cargo.toml new file mode 100644 index 000000000..52395a587 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-03-impl-display-for-point/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "traits-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-03-impl-display-for-point/src/main.rs b/src/doc/book/listings/ch19-advanced-features/no-listing-03-impl-display-for-point/src/main.rs new file mode 100644 index 000000000..c7bbb6a70 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-03-impl-display-for-point/src/main.rs @@ -0,0 +1,33 @@ +trait OutlinePrint: fmt::Display { + fn outline_print(&self) { + let output = self.to_string(); + let len = output.len(); + println!("{}", "*".repeat(len + 4)); + println!("*{}*", " ".repeat(len + 2)); + println!("* {} *", output); + println!("*{}*", " ".repeat(len + 2)); + println!("{}", "*".repeat(len + 4)); + } +} + +struct Point { + x: i32, + y: i32, +} + +impl OutlinePrint for Point {} + +// ANCHOR: here +use std::fmt; + +impl fmt::Display for Point { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "({}, {})", self.x, self.y) + } +} +// ANCHOR_END: here + +fn main() { + let p = Point { x: 1, y: 3 }; + p.outline_print(); +} diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-04-kilometers-alias/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/no-listing-04-kilometers-alias/Cargo.lock new file mode 100644 index 000000000..c0c98a79c --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-04-kilometers-alias/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "types-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-04-kilometers-alias/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/no-listing-04-kilometers-alias/Cargo.toml new file mode 100644 index 000000000..a2ae20c77 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-04-kilometers-alias/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "types-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-04-kilometers-alias/src/main.rs b/src/doc/book/listings/ch19-advanced-features/no-listing-04-kilometers-alias/src/main.rs new file mode 100644 index 000000000..d3fe32e2f --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-04-kilometers-alias/src/main.rs @@ -0,0 +1,12 @@ +fn main() { + // ANCHOR: there + // ANCHOR: here + type Kilometers = i32; + // ANCHOR_END: here + + let x: i32 = 5; + let y: Kilometers = 5; + + println!("x + y = {}", x + y); + // ANCHOR_END: there +} diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-05-write-trait/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/no-listing-05-write-trait/Cargo.lock new file mode 100644 index 000000000..b1977d01e --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-05-write-trait/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "traits-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-05-write-trait/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/no-listing-05-write-trait/Cargo.toml new file mode 100644 index 000000000..52395a587 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-05-write-trait/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "traits-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-05-write-trait/src/lib.rs b/src/doc/book/listings/ch19-advanced-features/no-listing-05-write-trait/src/lib.rs new file mode 100644 index 000000000..8300dccee --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-05-write-trait/src/lib.rs @@ -0,0 +1,10 @@ +use std::fmt; +use std::io::Error; + +pub trait Write { + fn write(&mut self, buf: &[u8]) -> Result; + fn flush(&mut self) -> Result<(), Error>; + + fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>; + fn write_fmt(&mut self, fmt: fmt::Arguments) -> Result<(), Error>; +} diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-06-result-alias/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/no-listing-06-result-alias/Cargo.lock new file mode 100644 index 000000000..b1977d01e --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-06-result-alias/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "traits-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-06-result-alias/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/no-listing-06-result-alias/Cargo.toml new file mode 100644 index 000000000..52395a587 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-06-result-alias/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "traits-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-06-result-alias/src/lib.rs b/src/doc/book/listings/ch19-advanced-features/no-listing-06-result-alias/src/lib.rs new file mode 100644 index 000000000..573559934 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-06-result-alias/src/lib.rs @@ -0,0 +1,15 @@ +use std::fmt; + +// ANCHOR: here +type Result = std::result::Result; +// ANCHOR_END: here + +// ANCHOR: there +pub trait Write { + fn write(&mut self, buf: &[u8]) -> Result; + fn flush(&mut self) -> Result<()>; + + fn write_all(&mut self, buf: &[u8]) -> Result<()>; + fn write_fmt(&mut self, fmt: fmt::Arguments) -> Result<()>; +} +// ANCHOR_END: there diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-07-never-type/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/no-listing-07-never-type/Cargo.lock new file mode 100644 index 000000000..b1977d01e --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-07-never-type/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "traits-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-07-never-type/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/no-listing-07-never-type/Cargo.toml new file mode 100644 index 000000000..52395a587 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-07-never-type/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "traits-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-07-never-type/src/lib.rs b/src/doc/book/listings/ch19-advanced-features/no-listing-07-never-type/src/lib.rs new file mode 100644 index 000000000..f0f7acac2 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-07-never-type/src/lib.rs @@ -0,0 +1,8 @@ +// ANCHOR: here +fn bar() -> ! { + // --snip-- + // ANCHOR_END: here + panic!(); + // ANCHOR: here +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-08-match-arms-different-types/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/no-listing-08-match-arms-different-types/Cargo.lock new file mode 100644 index 000000000..c0c98a79c --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-08-match-arms-different-types/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "types-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-08-match-arms-different-types/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/no-listing-08-match-arms-different-types/Cargo.toml new file mode 100644 index 000000000..a2ae20c77 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-08-match-arms-different-types/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "types-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-08-match-arms-different-types/src/main.rs b/src/doc/book/listings/ch19-advanced-features/no-listing-08-match-arms-different-types/src/main.rs new file mode 100644 index 000000000..6d56008ad --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-08-match-arms-different-types/src/main.rs @@ -0,0 +1,9 @@ +fn main() { + let guess = "3"; + // ANCHOR: here + let guess = match guess.trim().parse() { + Ok(_) => 5, + Err(_) => "hello", + }; + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-09-unwrap-definition/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/no-listing-09-unwrap-definition/Cargo.lock new file mode 100644 index 000000000..c0c98a79c --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-09-unwrap-definition/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "types-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-09-unwrap-definition/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/no-listing-09-unwrap-definition/Cargo.toml new file mode 100644 index 000000000..a2ae20c77 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-09-unwrap-definition/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "types-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-09-unwrap-definition/src/lib.rs b/src/doc/book/listings/ch19-advanced-features/no-listing-09-unwrap-definition/src/lib.rs new file mode 100644 index 000000000..aa4f937ff --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-09-unwrap-definition/src/lib.rs @@ -0,0 +1,17 @@ +enum Option { + Some(T), + None, +} + +use crate::Option::*; + +// ANCHOR: here +impl Option { + pub fn unwrap(self) -> T { + match self { + Some(val) => val, + None => panic!("called `Option::unwrap()` on a `None` value"), + } + } +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-10-loop-returns-never/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/no-listing-10-loop-returns-never/Cargo.lock new file mode 100644 index 000000000..c0c98a79c --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-10-loop-returns-never/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "types-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-10-loop-returns-never/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/no-listing-10-loop-returns-never/Cargo.toml new file mode 100644 index 000000000..a2ae20c77 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-10-loop-returns-never/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "types-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-10-loop-returns-never/src/main.rs b/src/doc/book/listings/ch19-advanced-features/no-listing-10-loop-returns-never/src/main.rs new file mode 100644 index 000000000..e7768913b --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-10-loop-returns-never/src/main.rs @@ -0,0 +1,9 @@ +fn main() { + // ANCHOR: here + print!("forever "); + + loop { + print!("and ever "); + } + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-11-cant-create-str/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/no-listing-11-cant-create-str/Cargo.lock new file mode 100644 index 000000000..c0c98a79c --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-11-cant-create-str/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "types-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-11-cant-create-str/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/no-listing-11-cant-create-str/Cargo.toml new file mode 100644 index 000000000..a2ae20c77 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-11-cant-create-str/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "types-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-11-cant-create-str/src/main.rs b/src/doc/book/listings/ch19-advanced-features/no-listing-11-cant-create-str/src/main.rs new file mode 100644 index 000000000..075d5110c --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-11-cant-create-str/src/main.rs @@ -0,0 +1,6 @@ +fn main() { + // ANCHOR: here + let s1: str = "Hello there!"; + let s2: str = "How's it going?"; + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-12-generic-fn-definition/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/no-listing-12-generic-fn-definition/Cargo.lock new file mode 100644 index 000000000..c0c98a79c --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-12-generic-fn-definition/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "types-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-12-generic-fn-definition/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/no-listing-12-generic-fn-definition/Cargo.toml new file mode 100644 index 000000000..a2ae20c77 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-12-generic-fn-definition/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "types-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-12-generic-fn-definition/src/lib.rs b/src/doc/book/listings/ch19-advanced-features/no-listing-12-generic-fn-definition/src/lib.rs new file mode 100644 index 000000000..69186ddfb --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-12-generic-fn-definition/src/lib.rs @@ -0,0 +1,3 @@ +fn generic(t: T) { + // --snip-- +} diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-13-generic-implicit-sized-bound/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/no-listing-13-generic-implicit-sized-bound/Cargo.lock new file mode 100644 index 000000000..c0c98a79c --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-13-generic-implicit-sized-bound/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "types-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-13-generic-implicit-sized-bound/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/no-listing-13-generic-implicit-sized-bound/Cargo.toml new file mode 100644 index 000000000..a2ae20c77 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-13-generic-implicit-sized-bound/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "types-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-13-generic-implicit-sized-bound/src/lib.rs b/src/doc/book/listings/ch19-advanced-features/no-listing-13-generic-implicit-sized-bound/src/lib.rs new file mode 100644 index 000000000..c2d00e216 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-13-generic-implicit-sized-bound/src/lib.rs @@ -0,0 +1,3 @@ +fn generic(t: T) { + // --snip-- +} diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-14-generic-maybe-sized/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/no-listing-14-generic-maybe-sized/Cargo.lock new file mode 100644 index 000000000..c0c98a79c --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-14-generic-maybe-sized/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "types-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-14-generic-maybe-sized/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/no-listing-14-generic-maybe-sized/Cargo.toml new file mode 100644 index 000000000..a2ae20c77 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-14-generic-maybe-sized/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "types-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-14-generic-maybe-sized/src/lib.rs b/src/doc/book/listings/ch19-advanced-features/no-listing-14-generic-maybe-sized/src/lib.rs new file mode 100644 index 000000000..e4722267a --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-14-generic-maybe-sized/src/lib.rs @@ -0,0 +1,3 @@ +fn generic(t: &T) { + // --snip-- +} diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-15-map-closure/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/no-listing-15-map-closure/Cargo.lock new file mode 100644 index 000000000..b2327c755 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-15-map-closure/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "functions-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-15-map-closure/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/no-listing-15-map-closure/Cargo.toml new file mode 100644 index 000000000..b196f35b5 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-15-map-closure/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "functions-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-15-map-closure/src/main.rs b/src/doc/book/listings/ch19-advanced-features/no-listing-15-map-closure/src/main.rs new file mode 100644 index 000000000..b4fcf7eb8 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-15-map-closure/src/main.rs @@ -0,0 +1,7 @@ +fn main() { + // ANCHOR: here + let list_of_numbers = vec![1, 2, 3]; + let list_of_strings: Vec = + list_of_numbers.iter().map(|i| i.to_string()).collect(); + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-16-map-function/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/no-listing-16-map-function/Cargo.lock new file mode 100644 index 000000000..b2327c755 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-16-map-function/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "functions-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-16-map-function/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/no-listing-16-map-function/Cargo.toml new file mode 100644 index 000000000..b196f35b5 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-16-map-function/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "functions-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-16-map-function/src/main.rs b/src/doc/book/listings/ch19-advanced-features/no-listing-16-map-function/src/main.rs new file mode 100644 index 000000000..dff20fe71 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-16-map-function/src/main.rs @@ -0,0 +1,7 @@ +fn main() { + // ANCHOR: here + let list_of_numbers = vec![1, 2, 3]; + let list_of_strings: Vec = + list_of_numbers.iter().map(ToString::to_string).collect(); + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-17-map-initializer/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/no-listing-17-map-initializer/Cargo.lock new file mode 100644 index 000000000..b2327c755 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-17-map-initializer/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "functions-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-17-map-initializer/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/no-listing-17-map-initializer/Cargo.toml new file mode 100644 index 000000000..b196f35b5 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-17-map-initializer/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "functions-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-17-map-initializer/src/main.rs b/src/doc/book/listings/ch19-advanced-features/no-listing-17-map-initializer/src/main.rs new file mode 100644 index 000000000..60fb73005 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-17-map-initializer/src/main.rs @@ -0,0 +1,10 @@ +fn main() { + // ANCHOR: here + enum Status { + Value(u32), + Stop, + } + + let list_of_statuses: Vec = (0u32..20).map(Status::Value).collect(); + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-18-returns-closure/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/no-listing-18-returns-closure/Cargo.lock new file mode 100644 index 000000000..b2327c755 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-18-returns-closure/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "functions-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-18-returns-closure/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/no-listing-18-returns-closure/Cargo.toml new file mode 100644 index 000000000..b196f35b5 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-18-returns-closure/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "functions-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-18-returns-closure/output.txt b/src/doc/book/listings/ch19-advanced-features/no-listing-18-returns-closure/output.txt new file mode 100644 index 000000000..d6fffc967 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-18-returns-closure/output.txt @@ -0,0 +1,16 @@ +$ cargo build + Compiling functions-example v0.1.0 (file:///projects/functions-example) +error[E0746]: return type cannot have an unboxed trait object + --> src/lib.rs:1:25 + | +1 | fn returns_closure() -> dyn Fn(i32) -> i32 { + | ^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = note: for information on `impl Trait`, see +help: use `impl Fn(i32) -> i32` as the return type, as all return paths are of type `[closure@src/lib.rs:2:5: 2:14]`, which implements `Fn(i32) -> i32` + | +1 | fn returns_closure() -> impl Fn(i32) -> i32 { + | ~~~~~~~~~~~~~~~~~~~ + +For more information about this error, try `rustc --explain E0746`. +error: could not compile `functions-example` due to previous error diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-18-returns-closure/src/lib.rs b/src/doc/book/listings/ch19-advanced-features/no-listing-18-returns-closure/src/lib.rs new file mode 100644 index 000000000..d699ac34e --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-18-returns-closure/src/lib.rs @@ -0,0 +1,3 @@ +fn returns_closure() -> dyn Fn(i32) -> i32 { + |x| x + 1 +} diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-19-returns-closure-trait-object/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/no-listing-19-returns-closure-trait-object/Cargo.lock new file mode 100644 index 000000000..b2327c755 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-19-returns-closure-trait-object/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "functions-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-19-returns-closure-trait-object/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/no-listing-19-returns-closure-trait-object/Cargo.toml new file mode 100644 index 000000000..b196f35b5 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-19-returns-closure-trait-object/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "functions-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-19-returns-closure-trait-object/src/lib.rs b/src/doc/book/listings/ch19-advanced-features/no-listing-19-returns-closure-trait-object/src/lib.rs new file mode 100644 index 000000000..b11407747 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-19-returns-closure-trait-object/src/lib.rs @@ -0,0 +1,3 @@ +fn returns_closure() -> Box i32> { + Box::new(|x| x + 1) +} diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-20-impl-hellomacro-for-pancakes/hello_macro/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/no-listing-20-impl-hellomacro-for-pancakes/hello_macro/Cargo.lock new file mode 100644 index 000000000..39afcf282 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-20-impl-hellomacro-for-pancakes/hello_macro/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "hello_macro" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-20-impl-hellomacro-for-pancakes/hello_macro/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/no-listing-20-impl-hellomacro-for-pancakes/hello_macro/Cargo.toml new file mode 100644 index 000000000..c6fb92087 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-20-impl-hellomacro-for-pancakes/hello_macro/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "hello_macro" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-20-impl-hellomacro-for-pancakes/hello_macro/src/lib.rs b/src/doc/book/listings/ch19-advanced-features/no-listing-20-impl-hellomacro-for-pancakes/hello_macro/src/lib.rs new file mode 100644 index 000000000..e74793184 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-20-impl-hellomacro-for-pancakes/hello_macro/src/lib.rs @@ -0,0 +1,3 @@ +pub trait HelloMacro { + fn hello_macro(); +} diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-20-impl-hellomacro-for-pancakes/pancakes/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/no-listing-20-impl-hellomacro-for-pancakes/pancakes/Cargo.lock new file mode 100644 index 000000000..881cd3f6d --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-20-impl-hellomacro-for-pancakes/pancakes/Cargo.lock @@ -0,0 +1,13 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "hello_macro" +version = "0.1.0" + +[[package]] +name = "pancakes" +version = "0.1.0" +dependencies = [ + "hello_macro 0.1.0", +] + diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-20-impl-hellomacro-for-pancakes/pancakes/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/no-listing-20-impl-hellomacro-for-pancakes/pancakes/Cargo.toml new file mode 100644 index 000000000..3ad910862 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-20-impl-hellomacro-for-pancakes/pancakes/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "pancakes" +version = "0.1.0" +edition = "2021" + +[dependencies] +hello_macro = { path = "../hello_macro" } \ No newline at end of file diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-20-impl-hellomacro-for-pancakes/pancakes/src/main.rs b/src/doc/book/listings/ch19-advanced-features/no-listing-20-impl-hellomacro-for-pancakes/pancakes/src/main.rs new file mode 100644 index 000000000..10b028b2d --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-20-impl-hellomacro-for-pancakes/pancakes/src/main.rs @@ -0,0 +1,13 @@ +use hello_macro::HelloMacro; + +struct Pancakes; + +impl HelloMacro for Pancakes { + fn hello_macro() { + println!("Hello, Macro! My name is Pancakes!"); + } +} + +fn main() { + Pancakes::hello_macro(); +} diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-21-pancakes/hello_macro/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/no-listing-21-pancakes/hello_macro/Cargo.lock new file mode 100644 index 000000000..39afcf282 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-21-pancakes/hello_macro/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "hello_macro" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-21-pancakes/hello_macro/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/no-listing-21-pancakes/hello_macro/Cargo.toml new file mode 100644 index 000000000..c6fb92087 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-21-pancakes/hello_macro/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "hello_macro" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-21-pancakes/hello_macro/hello_macro_derive/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/no-listing-21-pancakes/hello_macro/hello_macro_derive/Cargo.lock new file mode 100644 index 000000000..9a38c8ac2 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-21-pancakes/hello_macro/hello_macro_derive/Cargo.lock @@ -0,0 +1,46 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "hello_macro_derive" +version = "0.1.0" +dependencies = [ + "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 1.0.14 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "proc-macro2" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "quote" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "syn" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "unicode-xid" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[metadata] +"checksum proc-macro2 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)" = "3acb317c6ff86a4e579dfa00fc5e6cca91ecbb4e7eb2df0468805b674eb88548" +"checksum quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "053a8c8bcc71fcce321828dc897a98ab9760bef03a4fc36693c231e5b3216cfe" +"checksum syn 1.0.14 (registry+https://github.com/rust-lang/crates.io-index)" = "af6f3550d8dff9ef7dc34d384ac6f107e5d31c8f57d9f28e0081503f547ac8f5" +"checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c" diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-21-pancakes/hello_macro/hello_macro_derive/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/no-listing-21-pancakes/hello_macro/hello_macro_derive/Cargo.toml new file mode 100644 index 000000000..aa076ac48 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-21-pancakes/hello_macro/hello_macro_derive/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "hello_macro_derive" +version = "0.1.0" +edition = "2021" + +[lib] +proc-macro = true + +[dependencies] +syn = "1.0" +quote = "1.0" diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-21-pancakes/hello_macro/hello_macro_derive/src/lib.rs b/src/doc/book/listings/ch19-advanced-features/no-listing-21-pancakes/hello_macro/hello_macro_derive/src/lib.rs new file mode 100644 index 000000000..5e0b96c27 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-21-pancakes/hello_macro/hello_macro_derive/src/lib.rs @@ -0,0 +1,25 @@ +use proc_macro::TokenStream; +use quote::quote; +use syn; + +#[proc_macro_derive(HelloMacro)] +pub fn hello_macro_derive(input: TokenStream) -> TokenStream { + // Construct a representation of Rust code as a syntax tree + // that we can manipulate + let ast = syn::parse(input).unwrap(); + + // Build the trait implementation + impl_hello_macro(&ast) +} + +fn impl_hello_macro(ast: &syn::DeriveInput) -> TokenStream { + let name = &ast.ident; + let gen = quote! { + impl HelloMacro for #name { + fn hello_macro() { + println!("Hello, Macro! My name is {}!", stringify!(#name)); + } + } + }; + gen.into() +} diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-21-pancakes/hello_macro/src/lib.rs b/src/doc/book/listings/ch19-advanced-features/no-listing-21-pancakes/hello_macro/src/lib.rs new file mode 100644 index 000000000..e74793184 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-21-pancakes/hello_macro/src/lib.rs @@ -0,0 +1,3 @@ +pub trait HelloMacro { + fn hello_macro(); +} diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-21-pancakes/hello_macro/src/main.rs b/src/doc/book/listings/ch19-advanced-features/no-listing-21-pancakes/hello_macro/src/main.rs new file mode 100644 index 000000000..10b028b2d --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-21-pancakes/hello_macro/src/main.rs @@ -0,0 +1,13 @@ +use hello_macro::HelloMacro; + +struct Pancakes; + +impl HelloMacro for Pancakes { + fn hello_macro() { + println!("Hello, Macro! My name is Pancakes!"); + } +} + +fn main() { + Pancakes::hello_macro(); +} diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-21-pancakes/pancakes/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/no-listing-21-pancakes/pancakes/Cargo.lock new file mode 100644 index 000000000..dee23ecf9 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-21-pancakes/pancakes/Cargo.lock @@ -0,0 +1,58 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "hello_macro" +version = "0.1.0" + +[[package]] +name = "hello_macro_derive" +version = "0.1.0" +dependencies = [ + "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 1.0.14 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "pancakes" +version = "0.1.0" +dependencies = [ + "hello_macro 0.1.0", + "hello_macro_derive 0.1.0", +] + +[[package]] +name = "proc-macro2" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "quote" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "syn" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "unicode-xid" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[metadata] +"checksum proc-macro2 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)" = "3acb317c6ff86a4e579dfa00fc5e6cca91ecbb4e7eb2df0468805b674eb88548" +"checksum quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "053a8c8bcc71fcce321828dc897a98ab9760bef03a4fc36693c231e5b3216cfe" +"checksum syn 1.0.14 (registry+https://github.com/rust-lang/crates.io-index)" = "af6f3550d8dff9ef7dc34d384ac6f107e5d31c8f57d9f28e0081503f547ac8f5" +"checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c" diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-21-pancakes/pancakes/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/no-listing-21-pancakes/pancakes/Cargo.toml new file mode 100644 index 000000000..cb3a98c3a --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-21-pancakes/pancakes/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "pancakes" +version = "0.1.0" +edition = "2021" + +[dependencies] +hello_macro = { path = "../hello_macro" } +hello_macro_derive = { path = "../hello_macro/hello_macro_derive" } diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-21-pancakes/pancakes/src/main.rs b/src/doc/book/listings/ch19-advanced-features/no-listing-21-pancakes/pancakes/src/main.rs new file mode 100644 index 000000000..468c30aa4 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-21-pancakes/pancakes/src/main.rs @@ -0,0 +1,9 @@ +use hello_macro::HelloMacro; +use hello_macro_derive::HelloMacro; + +#[derive(HelloMacro)] +struct Pancakes; + +fn main() { + Pancakes::hello_macro(); +} diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-22-iterator-on-counter/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/no-listing-22-iterator-on-counter/Cargo.lock new file mode 100644 index 000000000..58b70c5b7 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-22-iterator-on-counter/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "counter" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-22-iterator-on-counter/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/no-listing-22-iterator-on-counter/Cargo.toml new file mode 100644 index 000000000..9e103f3eb --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-22-iterator-on-counter/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "counter" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch19-advanced-features/no-listing-22-iterator-on-counter/src/lib.rs b/src/doc/book/listings/ch19-advanced-features/no-listing-22-iterator-on-counter/src/lib.rs new file mode 100644 index 000000000..04c7f38f5 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/no-listing-22-iterator-on-counter/src/lib.rs @@ -0,0 +1,25 @@ +struct Counter { + count: u32, +} + +impl Counter { + fn new() -> Counter { + Counter { count: 0 } + } +} + +// ANCHOR: ch19 +impl Iterator for Counter { + type Item = u32; + + fn next(&mut self) -> Option { + // --snip-- + // ANCHOR_END: ch19 + if self.count < 5 { + self.count += 1; + Some(self.count) + } else { + None + } + } +} diff --git a/src/doc/book/listings/ch19-advanced-features/output-only-01-missing-unsafe/Cargo.lock b/src/doc/book/listings/ch19-advanced-features/output-only-01-missing-unsafe/Cargo.lock new file mode 100644 index 000000000..497817bf2 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/output-only-01-missing-unsafe/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "unsafe-example" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch19-advanced-features/output-only-01-missing-unsafe/Cargo.toml b/src/doc/book/listings/ch19-advanced-features/output-only-01-missing-unsafe/Cargo.toml new file mode 100644 index 000000000..3e8a29201 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/output-only-01-missing-unsafe/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "unsafe-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch19-advanced-features/output-only-01-missing-unsafe/output.txt b/src/doc/book/listings/ch19-advanced-features/output-only-01-missing-unsafe/output.txt new file mode 100644 index 000000000..5886bc630 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/output-only-01-missing-unsafe/output.txt @@ -0,0 +1,12 @@ +$ cargo run + Compiling unsafe-example v0.1.0 (file:///projects/unsafe-example) +error[E0133]: call to unsafe function is unsafe and requires unsafe function or block + --> src/main.rs:4:5 + | +4 | dangerous(); + | ^^^^^^^^^^^ call to unsafe function + | + = note: consult the function's documentation for information on how to avoid undefined behavior + +For more information about this error, try `rustc --explain E0133`. +error: could not compile `unsafe-example` due to previous error diff --git a/src/doc/book/listings/ch19-advanced-features/output-only-01-missing-unsafe/src/main.rs b/src/doc/book/listings/ch19-advanced-features/output-only-01-missing-unsafe/src/main.rs new file mode 100644 index 000000000..01305be74 --- /dev/null +++ b/src/doc/book/listings/ch19-advanced-features/output-only-01-missing-unsafe/src/main.rs @@ -0,0 +1,7 @@ +fn main() { + // ANCHOR: here + unsafe fn dangerous() {} + + dangerous(); + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch20-web-server/listing-20-01/Cargo.lock b/src/doc/book/listings/ch20-web-server/listing-20-01/Cargo.lock new file mode 100644 index 000000000..f2d069f46 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-01/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "hello" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-01/Cargo.toml b/src/doc/book/listings/ch20-web-server/listing-20-01/Cargo.toml new file mode 100644 index 000000000..fe619478a --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-01/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "hello" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch20-web-server/listing-20-01/src/main.rs b/src/doc/book/listings/ch20-web-server/listing-20-01/src/main.rs new file mode 100644 index 000000000..d868c3ec1 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-01/src/main.rs @@ -0,0 +1,11 @@ +use std::net::TcpListener; + +fn main() { + let listener = TcpListener::bind("127.0.0.1:7878").unwrap(); + + for stream in listener.incoming() { + let stream = stream.unwrap(); + + println!("Connection established!"); + } +} diff --git a/src/doc/book/listings/ch20-web-server/listing-20-02/Cargo.lock b/src/doc/book/listings/ch20-web-server/listing-20-02/Cargo.lock new file mode 100644 index 000000000..f2d069f46 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-02/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "hello" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-02/Cargo.toml b/src/doc/book/listings/ch20-web-server/listing-20-02/Cargo.toml new file mode 100644 index 000000000..fe619478a --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-02/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "hello" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch20-web-server/listing-20-02/src/main.rs b/src/doc/book/listings/ch20-web-server/listing-20-02/src/main.rs new file mode 100644 index 000000000..7240c73c7 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-02/src/main.rs @@ -0,0 +1,25 @@ +use std::{ + io::{prelude::*, BufReader}, + net::{TcpListener, TcpStream}, +}; + +fn main() { + let listener = TcpListener::bind("127.0.0.1:7878").unwrap(); + + for stream in listener.incoming() { + let stream = stream.unwrap(); + + handle_connection(stream); + } +} + +fn handle_connection(mut stream: TcpStream) { + let buf_reader = BufReader::new(&mut stream); + let http_request: Vec<_> = buf_reader + .lines() + .map(|result| result.unwrap()) + .take_while(|line| !line.is_empty()) + .collect(); + + println!("Request: {:#?}", http_request); +} diff --git a/src/doc/book/listings/ch20-web-server/listing-20-03/Cargo.lock b/src/doc/book/listings/ch20-web-server/listing-20-03/Cargo.lock new file mode 100644 index 000000000..f2d069f46 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-03/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "hello" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-03/Cargo.toml b/src/doc/book/listings/ch20-web-server/listing-20-03/Cargo.toml new file mode 100644 index 000000000..fe619478a --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-03/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "hello" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch20-web-server/listing-20-03/src/main.rs b/src/doc/book/listings/ch20-web-server/listing-20-03/src/main.rs new file mode 100644 index 000000000..c72d4a9c6 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-03/src/main.rs @@ -0,0 +1,29 @@ +use std::{ + io::{prelude::*, BufReader}, + net::{TcpListener, TcpStream}, +}; + +fn main() { + let listener = TcpListener::bind("127.0.0.1:7878").unwrap(); + + for stream in listener.incoming() { + let stream = stream.unwrap(); + + handle_connection(stream); + } +} + +// ANCHOR: here +fn handle_connection(mut stream: TcpStream) { + let buf_reader = BufReader::new(&mut stream); + let http_request: Vec<_> = buf_reader + .lines() + .map(|result| result.unwrap()) + .take_while(|line| !line.is_empty()) + .collect(); + + let response = "HTTP/1.1 200 OK\r\n\r\n"; + + stream.write_all(response.as_bytes()).unwrap(); +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch20-web-server/listing-20-05/Cargo.lock b/src/doc/book/listings/ch20-web-server/listing-20-05/Cargo.lock new file mode 100644 index 000000000..f2d069f46 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-05/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "hello" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-05/Cargo.toml b/src/doc/book/listings/ch20-web-server/listing-20-05/Cargo.toml new file mode 100644 index 000000000..fe619478a --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-05/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "hello" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch20-web-server/listing-20-05/hello.html b/src/doc/book/listings/ch20-web-server/listing-20-05/hello.html new file mode 100644 index 000000000..fe442d6b9 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-05/hello.html @@ -0,0 +1,11 @@ + + + + + Hello! + + +

Hello!

+

Hi from Rust

+ + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-05/src/main.rs b/src/doc/book/listings/ch20-web-server/listing-20-05/src/main.rs new file mode 100644 index 000000000..d4b78b640 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-05/src/main.rs @@ -0,0 +1,38 @@ +// ANCHOR: here +use std::{ + fs, + io::{prelude::*, BufReader}, + net::{TcpListener, TcpStream}, +}; +// --snip-- + +// ANCHOR_END: here +fn main() { + let listener = TcpListener::bind("127.0.0.1:7878").unwrap(); + + for stream in listener.incoming() { + let stream = stream.unwrap(); + + handle_connection(stream); + } +} + +// ANCHOR: here +fn handle_connection(mut stream: TcpStream) { + let buf_reader = BufReader::new(&mut stream); + let http_request: Vec<_> = buf_reader + .lines() + .map(|result| result.unwrap()) + .take_while(|line| !line.is_empty()) + .collect(); + + let status_line = "HTTP/1.1 200 OK"; + let contents = fs::read_to_string("hello.html").unwrap(); + let length = contents.len(); + + let response = + format!("{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}"); + + stream.write_all(response.as_bytes()).unwrap(); +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch20-web-server/listing-20-06/Cargo.lock b/src/doc/book/listings/ch20-web-server/listing-20-06/Cargo.lock new file mode 100644 index 000000000..f2d069f46 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-06/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "hello" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-06/Cargo.toml b/src/doc/book/listings/ch20-web-server/listing-20-06/Cargo.toml new file mode 100644 index 000000000..fe619478a --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-06/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "hello" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch20-web-server/listing-20-06/hello.html b/src/doc/book/listings/ch20-web-server/listing-20-06/hello.html new file mode 100644 index 000000000..fe442d6b9 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-06/hello.html @@ -0,0 +1,11 @@ + + + + + Hello! + + +

Hello!

+

Hi from Rust

+ + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-06/src/main.rs b/src/doc/book/listings/ch20-web-server/listing-20-06/src/main.rs new file mode 100644 index 000000000..5523a42d7 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-06/src/main.rs @@ -0,0 +1,37 @@ +use std::{ + fs, + io::{prelude::*, BufReader}, + net::{TcpListener, TcpStream}, +}; + +fn main() { + let listener = TcpListener::bind("127.0.0.1:7878").unwrap(); + + for stream in listener.incoming() { + let stream = stream.unwrap(); + + handle_connection(stream); + } +} +// ANCHOR: here +// --snip-- + +fn handle_connection(mut stream: TcpStream) { + let buf_reader = BufReader::new(&mut stream); + let request_line = buf_reader.lines().next().unwrap().unwrap(); + + if request_line == "GET / HTTP/1.1" { + let status_line = "HTTP/1.1 200 OK"; + let contents = fs::read_to_string("hello.html").unwrap(); + let length = contents.len(); + + let response = format!( + "{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}" + ); + + stream.write_all(response.as_bytes()).unwrap(); + } else { + // some other request + } +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch20-web-server/listing-20-07/404.html b/src/doc/book/listings/ch20-web-server/listing-20-07/404.html new file mode 100644 index 000000000..88d8e9152 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-07/404.html @@ -0,0 +1,11 @@ + + + + + Hello! + + +

Oops!

+

Sorry, I don't know what you're asking for.

+ + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-07/Cargo.lock b/src/doc/book/listings/ch20-web-server/listing-20-07/Cargo.lock new file mode 100644 index 000000000..f2d069f46 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-07/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "hello" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-07/Cargo.toml b/src/doc/book/listings/ch20-web-server/listing-20-07/Cargo.toml new file mode 100644 index 000000000..fe619478a --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-07/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "hello" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch20-web-server/listing-20-07/hello.html b/src/doc/book/listings/ch20-web-server/listing-20-07/hello.html new file mode 100644 index 000000000..fe442d6b9 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-07/hello.html @@ -0,0 +1,11 @@ + + + + + Hello! + + +

Hello!

+

Hi from Rust

+ + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-07/src/main.rs b/src/doc/book/listings/ch20-web-server/listing-20-07/src/main.rs new file mode 100644 index 000000000..a14b7d538 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-07/src/main.rs @@ -0,0 +1,45 @@ +use std::{ + fs, + io::{prelude::*, BufReader}, + net::{TcpListener, TcpStream}, +}; + +fn main() { + let listener = TcpListener::bind("127.0.0.1:7878").unwrap(); + + for stream in listener.incoming() { + let stream = stream.unwrap(); + + handle_connection(stream); + } +} + +fn handle_connection(mut stream: TcpStream) { + let buf_reader = BufReader::new(&mut stream); + let request_line = buf_reader.lines().next().unwrap().unwrap(); + + if request_line == "GET / HTTP/1.1" { + let status_line = "HTTP/1.1 200 OK"; + let contents = fs::read_to_string("hello.html").unwrap(); + let length = contents.len(); + + let response = format!( + "{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}" + ); + + stream.write_all(response.as_bytes()).unwrap(); + // ANCHOR: here + // --snip-- + } else { + let status_line = "HTTP/1.1 404 NOT FOUND"; + let contents = fs::read_to_string("404.html").unwrap(); + let length = contents.len(); + + let response = format!( + "{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}" + ); + + stream.write_all(response.as_bytes()).unwrap(); + } + // ANCHOR_END: here +} diff --git a/src/doc/book/listings/ch20-web-server/listing-20-09/404.html b/src/doc/book/listings/ch20-web-server/listing-20-09/404.html new file mode 100644 index 000000000..88d8e9152 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-09/404.html @@ -0,0 +1,11 @@ + + + + + Hello! + + +

Oops!

+

Sorry, I don't know what you're asking for.

+ + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-09/Cargo.lock b/src/doc/book/listings/ch20-web-server/listing-20-09/Cargo.lock new file mode 100644 index 000000000..f2d069f46 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-09/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "hello" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-09/Cargo.toml b/src/doc/book/listings/ch20-web-server/listing-20-09/Cargo.toml new file mode 100644 index 000000000..fe619478a --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-09/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "hello" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch20-web-server/listing-20-09/hello.html b/src/doc/book/listings/ch20-web-server/listing-20-09/hello.html new file mode 100644 index 000000000..fe442d6b9 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-09/hello.html @@ -0,0 +1,11 @@ + + + + + Hello! + + +

Hello!

+

Hi from Rust

+ + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-09/src/main.rs b/src/doc/book/listings/ch20-web-server/listing-20-09/src/main.rs new file mode 100644 index 000000000..ffc51e803 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-09/src/main.rs @@ -0,0 +1,40 @@ +use std::{ + fs, + io::{prelude::*, BufReader}, + net::{TcpListener, TcpStream}, +}; + +fn main() { + let listener = TcpListener::bind("127.0.0.1:7878").unwrap(); + + for stream in listener.incoming() { + let stream = stream.unwrap(); + + handle_connection(stream); + } +} +// ANCHOR: here +// --snip-- + +fn handle_connection(mut stream: TcpStream) { + // --snip-- + // ANCHOR_END: here + let buf_reader = BufReader::new(&mut stream); + let request_line = buf_reader.lines().next().unwrap().unwrap(); + // ANCHOR: here + + let (status_line, filename) = if request_line == "GET / HTTP/1.1" { + ("HTTP/1.1 200 OK", "hello.html") + } else { + ("HTTP/1.1 404 NOT FOUND", "404.html") + }; + + let contents = fs::read_to_string(filename).unwrap(); + let length = contents.len(); + + let response = + format!("{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}"); + + stream.write_all(response.as_bytes()).unwrap(); +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch20-web-server/listing-20-10/404.html b/src/doc/book/listings/ch20-web-server/listing-20-10/404.html new file mode 100644 index 000000000..88d8e9152 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-10/404.html @@ -0,0 +1,11 @@ + + + + + Hello! + + +

Oops!

+

Sorry, I don't know what you're asking for.

+ + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-10/Cargo.lock b/src/doc/book/listings/ch20-web-server/listing-20-10/Cargo.lock new file mode 100644 index 000000000..f2d069f46 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-10/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "hello" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-10/Cargo.toml b/src/doc/book/listings/ch20-web-server/listing-20-10/Cargo.toml new file mode 100644 index 000000000..fe619478a --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-10/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "hello" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch20-web-server/listing-20-10/hello.html b/src/doc/book/listings/ch20-web-server/listing-20-10/hello.html new file mode 100644 index 000000000..fe442d6b9 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-10/hello.html @@ -0,0 +1,11 @@ + + + + + Hello! + + +

Hello!

+

Hi from Rust

+ + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-10/src/main.rs b/src/doc/book/listings/ch20-web-server/listing-20-10/src/main.rs new file mode 100644 index 000000000..5a18b45c0 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-10/src/main.rs @@ -0,0 +1,52 @@ +// ANCHOR: here +use std::{ + fs, + io::{prelude::*, BufReader}, + net::{TcpListener, TcpStream}, + thread, + time::Duration, +}; +// --snip-- +// ANCHOR_END: here + +fn main() { + let listener = TcpListener::bind("127.0.0.1:7878").unwrap(); + + for stream in listener.incoming() { + let stream = stream.unwrap(); + + handle_connection(stream); + } +} +// ANCHOR: here + +fn handle_connection(mut stream: TcpStream) { + // --snip-- + + // ANCHOR_END: here + let buf_reader = BufReader::new(&mut stream); + let request_line = buf_reader.lines().next().unwrap().unwrap(); + + // ANCHOR: here + let (status_line, filename) = match &request_line[..] { + "GET / HTTP/1.1" => ("HTTP/1.1 200 OK", "hello.html"), + "GET /sleep HTTP/1.1" => { + thread::sleep(Duration::from_secs(5)); + ("HTTP/1.1 200 OK", "hello.html") + } + _ => ("HTTP/1.1 404 NOT FOUND", "404.html"), + }; + + // --snip-- + // ANCHOR_END: here + + let contents = fs::read_to_string(filename).unwrap(); + let length = contents.len(); + + let response = + format!("{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}"); + + stream.write_all(response.as_bytes()).unwrap(); + // ANCHOR: here +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch20-web-server/listing-20-11/404.html b/src/doc/book/listings/ch20-web-server/listing-20-11/404.html new file mode 100644 index 000000000..88d8e9152 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-11/404.html @@ -0,0 +1,11 @@ + + + + + Hello! + + +

Oops!

+

Sorry, I don't know what you're asking for.

+ + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-11/Cargo.lock b/src/doc/book/listings/ch20-web-server/listing-20-11/Cargo.lock new file mode 100644 index 000000000..f2d069f46 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-11/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "hello" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-11/Cargo.toml b/src/doc/book/listings/ch20-web-server/listing-20-11/Cargo.toml new file mode 100644 index 000000000..fe619478a --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-11/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "hello" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch20-web-server/listing-20-11/hello.html b/src/doc/book/listings/ch20-web-server/listing-20-11/hello.html new file mode 100644 index 000000000..fe442d6b9 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-11/hello.html @@ -0,0 +1,11 @@ + + + + + Hello! + + +

Hello!

+

Hi from Rust

+ + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-11/src/main.rs b/src/doc/book/listings/ch20-web-server/listing-20-11/src/main.rs new file mode 100644 index 000000000..1181357b0 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-11/src/main.rs @@ -0,0 +1,43 @@ +use std::{ + fs, + io::{prelude::*, BufReader}, + net::{TcpListener, TcpStream}, + thread, + time::Duration, +}; + +// ANCHOR: here +fn main() { + let listener = TcpListener::bind("127.0.0.1:7878").unwrap(); + + for stream in listener.incoming() { + let stream = stream.unwrap(); + + thread::spawn(|| { + handle_connection(stream); + }); + } +} +// ANCHOR_END: here + +fn handle_connection(mut stream: TcpStream) { + let buf_reader = BufReader::new(&mut stream); + let request_line = buf_reader.lines().next().unwrap().unwrap(); + + let (status_line, filename) = match &request_line[..] { + "GET / HTTP/1.1" => ("HTTP/1.1 200 OK", "hello.html"), + "GET /sleep HTTP/1.1" => { + thread::sleep(Duration::from_secs(5)); + ("HTTP/1.1 200 OK", "hello.html") + } + _ => ("HTTP/1.1 404 NOT FOUND", "404.html"), + }; + + let contents = fs::read_to_string(filename).unwrap(); + let length = contents.len(); + + let response = + format!("{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}"); + + stream.write_all(response.as_bytes()).unwrap(); +} diff --git a/src/doc/book/listings/ch20-web-server/listing-20-12/404.html b/src/doc/book/listings/ch20-web-server/listing-20-12/404.html new file mode 100644 index 000000000..88d8e9152 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-12/404.html @@ -0,0 +1,11 @@ + + + + + Hello! + + +

Oops!

+

Sorry, I don't know what you're asking for.

+ + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-12/Cargo.lock b/src/doc/book/listings/ch20-web-server/listing-20-12/Cargo.lock new file mode 100644 index 000000000..f2d069f46 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-12/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "hello" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-12/Cargo.toml b/src/doc/book/listings/ch20-web-server/listing-20-12/Cargo.toml new file mode 100644 index 000000000..fe619478a --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-12/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "hello" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch20-web-server/listing-20-12/hello.html b/src/doc/book/listings/ch20-web-server/listing-20-12/hello.html new file mode 100644 index 000000000..fe442d6b9 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-12/hello.html @@ -0,0 +1,11 @@ + + + + + Hello! + + +

Hello!

+

Hi from Rust

+ + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-12/output.txt b/src/doc/book/listings/ch20-web-server/listing-20-12/output.txt new file mode 100644 index 000000000..57a58b960 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-12/output.txt @@ -0,0 +1,10 @@ +$ cargo check + Checking hello v0.1.0 (file:///projects/hello) +error[E0433]: failed to resolve: use of undeclared type `ThreadPool` + --> src/main.rs:11:16 + | +11 | let pool = ThreadPool::new(4); + | ^^^^^^^^^^ use of undeclared type `ThreadPool` + +For more information about this error, try `rustc --explain E0433`. +error: could not compile `hello` due to previous error diff --git a/src/doc/book/listings/ch20-web-server/listing-20-12/src/main.rs b/src/doc/book/listings/ch20-web-server/listing-20-12/src/main.rs new file mode 100644 index 000000000..21b9a80f1 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-12/src/main.rs @@ -0,0 +1,44 @@ +use std::{ + fs, + io::{prelude::*, BufReader}, + net::{TcpListener, TcpStream}, + thread, + time::Duration, +}; + +// ANCHOR: here +fn main() { + let listener = TcpListener::bind("127.0.0.1:7878").unwrap(); + let pool = ThreadPool::new(4); + + for stream in listener.incoming() { + let stream = stream.unwrap(); + + pool.execute(|| { + handle_connection(stream); + }); + } +} +// ANCHOR_END: here + +fn handle_connection(mut stream: TcpStream) { + let buf_reader = BufReader::new(&mut stream); + let request_line = buf_reader.lines().next().unwrap().unwrap(); + + let (status_line, filename) = match &request_line[..] { + "GET / HTTP/1.1" => ("HTTP/1.1 200 OK", "hello.html"), + "GET /sleep HTTP/1.1" => { + thread::sleep(Duration::from_secs(5)); + ("HTTP/1.1 200 OK", "hello.html") + } + _ => ("HTTP/1.1 404 NOT FOUND", "404.html"), + }; + + let contents = fs::read_to_string(filename).unwrap(); + let length = contents.len(); + + let response = + format!("{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}"); + + stream.write_all(response.as_bytes()).unwrap(); +} diff --git a/src/doc/book/listings/ch20-web-server/listing-20-13/404.html b/src/doc/book/listings/ch20-web-server/listing-20-13/404.html new file mode 100644 index 000000000..88d8e9152 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-13/404.html @@ -0,0 +1,11 @@ + + + + + Hello! + + +

Oops!

+

Sorry, I don't know what you're asking for.

+ + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-13/Cargo.lock b/src/doc/book/listings/ch20-web-server/listing-20-13/Cargo.lock new file mode 100644 index 000000000..f2d069f46 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-13/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "hello" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-13/Cargo.toml b/src/doc/book/listings/ch20-web-server/listing-20-13/Cargo.toml new file mode 100644 index 000000000..fe619478a --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-13/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "hello" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch20-web-server/listing-20-13/hello.html b/src/doc/book/listings/ch20-web-server/listing-20-13/hello.html new file mode 100644 index 000000000..fe442d6b9 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-13/hello.html @@ -0,0 +1,11 @@ + + + + + Hello! + + +

Hello!

+

Hi from Rust

+ + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-13/src/lib.rs b/src/doc/book/listings/ch20-web-server/listing-20-13/src/lib.rs new file mode 100644 index 000000000..35960e7c0 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-13/src/lib.rs @@ -0,0 +1,27 @@ +pub struct ThreadPool; + +// ANCHOR: here +impl ThreadPool { + /// Create a new ThreadPool. + /// + /// The size is the number of threads in the pool. + /// + /// # Panics + /// + /// The `new` function will panic if the size is zero. + pub fn new(size: usize) -> ThreadPool { + assert!(size > 0); + + ThreadPool + } + + // --snip-- + // ANCHOR_END: here + pub fn execute(&self, f: F) + where + F: FnOnce() + Send + 'static, + { + } + // ANCHOR: here +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch20-web-server/listing-20-13/src/main.rs b/src/doc/book/listings/ch20-web-server/listing-20-13/src/main.rs new file mode 100644 index 000000000..79efb28a2 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-13/src/main.rs @@ -0,0 +1,43 @@ +use hello::ThreadPool; +use std::{ + fs, + io::{prelude::*, BufReader}, + net::{TcpListener, TcpStream}, + thread, + time::Duration, +}; + +fn main() { + let listener = TcpListener::bind("127.0.0.1:7878").unwrap(); + let pool = ThreadPool::new(4); + + for stream in listener.incoming() { + let stream = stream.unwrap(); + + pool.execute(|| { + handle_connection(stream); + }); + } +} + +fn handle_connection(mut stream: TcpStream) { + let buf_reader = BufReader::new(&mut stream); + let request_line = buf_reader.lines().next().unwrap().unwrap(); + + let (status_line, filename) = match &request_line[..] { + "GET / HTTP/1.1" => ("HTTP/1.1 200 OK", "hello.html"), + "GET /sleep HTTP/1.1" => { + thread::sleep(Duration::from_secs(5)); + ("HTTP/1.1 200 OK", "hello.html") + } + _ => ("HTTP/1.1 404 NOT FOUND", "404.html"), + }; + + let contents = fs::read_to_string(filename).unwrap(); + let length = contents.len(); + + let response = + format!("{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}"); + + stream.write_all(response.as_bytes()).unwrap(); +} diff --git a/src/doc/book/listings/ch20-web-server/listing-20-14/404.html b/src/doc/book/listings/ch20-web-server/listing-20-14/404.html new file mode 100644 index 000000000..88d8e9152 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-14/404.html @@ -0,0 +1,11 @@ + + + + + Hello! + + +

Oops!

+

Sorry, I don't know what you're asking for.

+ + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-14/Cargo.lock b/src/doc/book/listings/ch20-web-server/listing-20-14/Cargo.lock new file mode 100644 index 000000000..f2d069f46 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-14/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "hello" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-14/Cargo.toml b/src/doc/book/listings/ch20-web-server/listing-20-14/Cargo.toml new file mode 100644 index 000000000..fe619478a --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-14/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "hello" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch20-web-server/listing-20-14/hello.html b/src/doc/book/listings/ch20-web-server/listing-20-14/hello.html new file mode 100644 index 000000000..fe442d6b9 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-14/hello.html @@ -0,0 +1,11 @@ + + + + + Hello! + + +

Hello!

+

Hi from Rust

+ + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-14/src/lib.rs b/src/doc/book/listings/ch20-web-server/listing-20-14/src/lib.rs new file mode 100644 index 000000000..c1fa1828b --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-14/src/lib.rs @@ -0,0 +1,40 @@ +// ANCHOR: here +use std::thread; + +pub struct ThreadPool { + threads: Vec>, +} + +impl ThreadPool { + // --snip-- + // ANCHOR_END: here + /// Create a new ThreadPool. + /// + /// The size is the number of threads in the pool. + /// + /// # Panics + /// + /// The `new` function will panic if the size is zero. + // ANCHOR: here + pub fn new(size: usize) -> ThreadPool { + assert!(size > 0); + + let mut threads = Vec::with_capacity(size); + + for _ in 0..size { + // create some threads and store them in the vector + } + + ThreadPool { threads } + } + // --snip-- + // ANCHOR_END: here + + pub fn execute(&self, f: F) + where + F: FnOnce() + Send + 'static, + { + } + // ANCHOR: here +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch20-web-server/listing-20-14/src/main.rs b/src/doc/book/listings/ch20-web-server/listing-20-14/src/main.rs new file mode 100644 index 000000000..79efb28a2 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-14/src/main.rs @@ -0,0 +1,43 @@ +use hello::ThreadPool; +use std::{ + fs, + io::{prelude::*, BufReader}, + net::{TcpListener, TcpStream}, + thread, + time::Duration, +}; + +fn main() { + let listener = TcpListener::bind("127.0.0.1:7878").unwrap(); + let pool = ThreadPool::new(4); + + for stream in listener.incoming() { + let stream = stream.unwrap(); + + pool.execute(|| { + handle_connection(stream); + }); + } +} + +fn handle_connection(mut stream: TcpStream) { + let buf_reader = BufReader::new(&mut stream); + let request_line = buf_reader.lines().next().unwrap().unwrap(); + + let (status_line, filename) = match &request_line[..] { + "GET / HTTP/1.1" => ("HTTP/1.1 200 OK", "hello.html"), + "GET /sleep HTTP/1.1" => { + thread::sleep(Duration::from_secs(5)); + ("HTTP/1.1 200 OK", "hello.html") + } + _ => ("HTTP/1.1 404 NOT FOUND", "404.html"), + }; + + let contents = fs::read_to_string(filename).unwrap(); + let length = contents.len(); + + let response = + format!("{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}"); + + stream.write_all(response.as_bytes()).unwrap(); +} diff --git a/src/doc/book/listings/ch20-web-server/listing-20-15/404.html b/src/doc/book/listings/ch20-web-server/listing-20-15/404.html new file mode 100644 index 000000000..88d8e9152 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-15/404.html @@ -0,0 +1,11 @@ + + + + + Hello! + + +

Oops!

+

Sorry, I don't know what you're asking for.

+ + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-15/Cargo.lock b/src/doc/book/listings/ch20-web-server/listing-20-15/Cargo.lock new file mode 100644 index 000000000..f2d069f46 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-15/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "hello" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-15/Cargo.toml b/src/doc/book/listings/ch20-web-server/listing-20-15/Cargo.toml new file mode 100644 index 000000000..fe619478a --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-15/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "hello" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch20-web-server/listing-20-15/hello.html b/src/doc/book/listings/ch20-web-server/listing-20-15/hello.html new file mode 100644 index 000000000..fe442d6b9 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-15/hello.html @@ -0,0 +1,11 @@ + + + + + Hello! + + +

Hello!

+

Hi from Rust

+ + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-15/src/lib.rs b/src/doc/book/listings/ch20-web-server/listing-20-15/src/lib.rs new file mode 100644 index 000000000..80a6eeeb3 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-15/src/lib.rs @@ -0,0 +1,53 @@ +// ANCHOR: here +use std::thread; + +pub struct ThreadPool { + workers: Vec, +} + +impl ThreadPool { + // --snip-- + // ANCHOR_END: here + /// Create a new ThreadPool. + /// + /// The size is the number of threads in the pool. + /// + /// # Panics + /// + /// The `new` function will panic if the size is zero. + // ANCHOR: here + pub fn new(size: usize) -> ThreadPool { + assert!(size > 0); + + let mut workers = Vec::with_capacity(size); + + for id in 0..size { + workers.push(Worker::new(id)); + } + + ThreadPool { workers } + } + // --snip-- + // ANCHOR_END: here + + pub fn execute(&self, f: F) + where + F: FnOnce() + Send + 'static, + { + } + // ANCHOR: here +} + +struct Worker { + id: usize, + thread: thread::JoinHandle<()>, +} + +impl Worker { + fn new(id: usize) -> Worker { + let thread = thread::spawn(|| {}); + + Worker { id, thread } + } +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch20-web-server/listing-20-15/src/main.rs b/src/doc/book/listings/ch20-web-server/listing-20-15/src/main.rs new file mode 100644 index 000000000..79efb28a2 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-15/src/main.rs @@ -0,0 +1,43 @@ +use hello::ThreadPool; +use std::{ + fs, + io::{prelude::*, BufReader}, + net::{TcpListener, TcpStream}, + thread, + time::Duration, +}; + +fn main() { + let listener = TcpListener::bind("127.0.0.1:7878").unwrap(); + let pool = ThreadPool::new(4); + + for stream in listener.incoming() { + let stream = stream.unwrap(); + + pool.execute(|| { + handle_connection(stream); + }); + } +} + +fn handle_connection(mut stream: TcpStream) { + let buf_reader = BufReader::new(&mut stream); + let request_line = buf_reader.lines().next().unwrap().unwrap(); + + let (status_line, filename) = match &request_line[..] { + "GET / HTTP/1.1" => ("HTTP/1.1 200 OK", "hello.html"), + "GET /sleep HTTP/1.1" => { + thread::sleep(Duration::from_secs(5)); + ("HTTP/1.1 200 OK", "hello.html") + } + _ => ("HTTP/1.1 404 NOT FOUND", "404.html"), + }; + + let contents = fs::read_to_string(filename).unwrap(); + let length = contents.len(); + + let response = + format!("{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}"); + + stream.write_all(response.as_bytes()).unwrap(); +} diff --git a/src/doc/book/listings/ch20-web-server/listing-20-16/404.html b/src/doc/book/listings/ch20-web-server/listing-20-16/404.html new file mode 100644 index 000000000..88d8e9152 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-16/404.html @@ -0,0 +1,11 @@ + + + + + Hello! + + +

Oops!

+

Sorry, I don't know what you're asking for.

+ + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-16/Cargo.lock b/src/doc/book/listings/ch20-web-server/listing-20-16/Cargo.lock new file mode 100644 index 000000000..f2d069f46 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-16/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "hello" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-16/Cargo.toml b/src/doc/book/listings/ch20-web-server/listing-20-16/Cargo.toml new file mode 100644 index 000000000..fe619478a --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-16/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "hello" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch20-web-server/listing-20-16/hello.html b/src/doc/book/listings/ch20-web-server/listing-20-16/hello.html new file mode 100644 index 000000000..fe442d6b9 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-16/hello.html @@ -0,0 +1,11 @@ + + + + + Hello! + + +

Hello!

+

Hi from Rust

+ + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-16/src/lib.rs b/src/doc/book/listings/ch20-web-server/listing-20-16/src/lib.rs new file mode 100644 index 000000000..411c1d003 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-16/src/lib.rs @@ -0,0 +1,58 @@ +// ANCHOR: here +use std::{sync::mpsc, thread}; + +pub struct ThreadPool { + workers: Vec, + sender: mpsc::Sender, +} + +struct Job; + +impl ThreadPool { + // --snip-- + // ANCHOR_END: here + /// Create a new ThreadPool. + /// + /// The size is the number of threads in the pool. + /// + /// # Panics + /// + /// The `new` function will panic if the size is zero. + // ANCHOR: here + pub fn new(size: usize) -> ThreadPool { + assert!(size > 0); + + let (sender, receiver) = mpsc::channel(); + + let mut workers = Vec::with_capacity(size); + + for id in 0..size { + workers.push(Worker::new(id)); + } + + ThreadPool { workers, sender } + } + // --snip-- + // ANCHOR_END: here + + pub fn execute(&self, f: F) + where + F: FnOnce() + Send + 'static, + { + } + // ANCHOR: here +} +// ANCHOR_END: here + +struct Worker { + id: usize, + thread: thread::JoinHandle<()>, +} + +impl Worker { + fn new(id: usize) -> Worker { + let thread = thread::spawn(|| {}); + + Worker { id, thread } + } +} diff --git a/src/doc/book/listings/ch20-web-server/listing-20-16/src/main.rs b/src/doc/book/listings/ch20-web-server/listing-20-16/src/main.rs new file mode 100644 index 000000000..79efb28a2 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-16/src/main.rs @@ -0,0 +1,43 @@ +use hello::ThreadPool; +use std::{ + fs, + io::{prelude::*, BufReader}, + net::{TcpListener, TcpStream}, + thread, + time::Duration, +}; + +fn main() { + let listener = TcpListener::bind("127.0.0.1:7878").unwrap(); + let pool = ThreadPool::new(4); + + for stream in listener.incoming() { + let stream = stream.unwrap(); + + pool.execute(|| { + handle_connection(stream); + }); + } +} + +fn handle_connection(mut stream: TcpStream) { + let buf_reader = BufReader::new(&mut stream); + let request_line = buf_reader.lines().next().unwrap().unwrap(); + + let (status_line, filename) = match &request_line[..] { + "GET / HTTP/1.1" => ("HTTP/1.1 200 OK", "hello.html"), + "GET /sleep HTTP/1.1" => { + thread::sleep(Duration::from_secs(5)); + ("HTTP/1.1 200 OK", "hello.html") + } + _ => ("HTTP/1.1 404 NOT FOUND", "404.html"), + }; + + let contents = fs::read_to_string(filename).unwrap(); + let length = contents.len(); + + let response = + format!("{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}"); + + stream.write_all(response.as_bytes()).unwrap(); +} diff --git a/src/doc/book/listings/ch20-web-server/listing-20-17/404.html b/src/doc/book/listings/ch20-web-server/listing-20-17/404.html new file mode 100644 index 000000000..88d8e9152 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-17/404.html @@ -0,0 +1,11 @@ + + + + + Hello! + + +

Oops!

+

Sorry, I don't know what you're asking for.

+ + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-17/Cargo.lock b/src/doc/book/listings/ch20-web-server/listing-20-17/Cargo.lock new file mode 100644 index 000000000..f2d069f46 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-17/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "hello" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-17/Cargo.toml b/src/doc/book/listings/ch20-web-server/listing-20-17/Cargo.toml new file mode 100644 index 000000000..fe619478a --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-17/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "hello" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch20-web-server/listing-20-17/hello.html b/src/doc/book/listings/ch20-web-server/listing-20-17/hello.html new file mode 100644 index 000000000..fe442d6b9 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-17/hello.html @@ -0,0 +1,11 @@ + + + + + Hello! + + +

Hello!

+

Hi from Rust

+ + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-17/output.txt b/src/doc/book/listings/ch20-web-server/listing-20-17/output.txt new file mode 100644 index 000000000..8bedfecfd --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-17/output.txt @@ -0,0 +1,13 @@ +$ cargo check + Checking hello v0.1.0 (file:///projects/hello) +error[E0382]: use of moved value: `receiver` + --> src/lib.rs:26:42 + | +21 | let (sender, receiver) = mpsc::channel(); + | -------- move occurs because `receiver` has type `std::sync::mpsc::Receiver`, which does not implement the `Copy` trait +... +26 | workers.push(Worker::new(id, receiver)); + | ^^^^^^^^ value moved here, in previous iteration of loop + +For more information about this error, try `rustc --explain E0382`. +error: could not compile `hello` due to previous error diff --git a/src/doc/book/listings/ch20-web-server/listing-20-17/src/lib.rs b/src/doc/book/listings/ch20-web-server/listing-20-17/src/lib.rs new file mode 100644 index 000000000..d764879e5 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-17/src/lib.rs @@ -0,0 +1,65 @@ +use std::{sync::mpsc, thread}; + +pub struct ThreadPool { + workers: Vec, + sender: mpsc::Sender, +} + +struct Job; + +// ANCHOR: here +impl ThreadPool { + // --snip-- + // ANCHOR_END: here + /// Create a new ThreadPool. + /// + /// The size is the number of threads in the pool. + /// + /// # Panics + /// + /// The `new` function will panic if the size is zero. + // ANCHOR: here + pub fn new(size: usize) -> ThreadPool { + assert!(size > 0); + + let (sender, receiver) = mpsc::channel(); + + let mut workers = Vec::with_capacity(size); + + for id in 0..size { + workers.push(Worker::new(id, receiver)); + } + + ThreadPool { workers, sender } + } + // --snip-- + // ANCHOR_END: here + + pub fn execute(&self, f: F) + where + F: FnOnce() + Send + 'static, + { + } + // ANCHOR: here +} + +// --snip-- + +// ANCHOR_END: here + +struct Worker { + id: usize, + thread: thread::JoinHandle<()>, +} + +// ANCHOR: here +impl Worker { + fn new(id: usize, receiver: mpsc::Receiver) -> Worker { + let thread = thread::spawn(|| { + receiver; + }); + + Worker { id, thread } + } +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch20-web-server/listing-20-17/src/main.rs b/src/doc/book/listings/ch20-web-server/listing-20-17/src/main.rs new file mode 100644 index 000000000..79efb28a2 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-17/src/main.rs @@ -0,0 +1,43 @@ +use hello::ThreadPool; +use std::{ + fs, + io::{prelude::*, BufReader}, + net::{TcpListener, TcpStream}, + thread, + time::Duration, +}; + +fn main() { + let listener = TcpListener::bind("127.0.0.1:7878").unwrap(); + let pool = ThreadPool::new(4); + + for stream in listener.incoming() { + let stream = stream.unwrap(); + + pool.execute(|| { + handle_connection(stream); + }); + } +} + +fn handle_connection(mut stream: TcpStream) { + let buf_reader = BufReader::new(&mut stream); + let request_line = buf_reader.lines().next().unwrap().unwrap(); + + let (status_line, filename) = match &request_line[..] { + "GET / HTTP/1.1" => ("HTTP/1.1 200 OK", "hello.html"), + "GET /sleep HTTP/1.1" => { + thread::sleep(Duration::from_secs(5)); + ("HTTP/1.1 200 OK", "hello.html") + } + _ => ("HTTP/1.1 404 NOT FOUND", "404.html"), + }; + + let contents = fs::read_to_string(filename).unwrap(); + let length = contents.len(); + + let response = + format!("{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}"); + + stream.write_all(response.as_bytes()).unwrap(); +} diff --git a/src/doc/book/listings/ch20-web-server/listing-20-18/404.html b/src/doc/book/listings/ch20-web-server/listing-20-18/404.html new file mode 100644 index 000000000..88d8e9152 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-18/404.html @@ -0,0 +1,11 @@ + + + + + Hello! + + +

Oops!

+

Sorry, I don't know what you're asking for.

+ + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-18/Cargo.lock b/src/doc/book/listings/ch20-web-server/listing-20-18/Cargo.lock new file mode 100644 index 000000000..f2d069f46 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-18/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "hello" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-18/Cargo.toml b/src/doc/book/listings/ch20-web-server/listing-20-18/Cargo.toml new file mode 100644 index 000000000..fe619478a --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-18/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "hello" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch20-web-server/listing-20-18/hello.html b/src/doc/book/listings/ch20-web-server/listing-20-18/hello.html new file mode 100644 index 000000000..fe442d6b9 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-18/hello.html @@ -0,0 +1,11 @@ + + + + + Hello! + + +

Hello!

+

Hi from Rust

+ + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-18/src/lib.rs b/src/doc/book/listings/ch20-web-server/listing-20-18/src/lib.rs new file mode 100644 index 000000000..4bff8acad --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-18/src/lib.rs @@ -0,0 +1,76 @@ +// ANCHOR: here +use std::{ + sync::{mpsc, Arc, Mutex}, + thread, +}; +// --snip-- + +// ANCHOR_END: here +pub struct ThreadPool { + workers: Vec, + sender: mpsc::Sender, +} + +struct Job; + +// ANCHOR: here +impl ThreadPool { + // --snip-- + // ANCHOR_END: here + /// Create a new ThreadPool. + /// + /// The size is the number of threads in the pool. + /// + /// # Panics + /// + /// The `new` function will panic if the size is zero. + // ANCHOR: here + pub fn new(size: usize) -> ThreadPool { + assert!(size > 0); + + let (sender, receiver) = mpsc::channel(); + + let receiver = Arc::new(Mutex::new(receiver)); + + let mut workers = Vec::with_capacity(size); + + for id in 0..size { + workers.push(Worker::new(id, Arc::clone(&receiver))); + } + + ThreadPool { workers, sender } + } + + // --snip-- + // ANCHOR_END: here + + pub fn execute(&self, f: F) + where + F: FnOnce() + Send + 'static, + { + } + // ANCHOR: here +} + +// --snip-- + +// ANCHOR_END: here +struct Worker { + id: usize, + thread: thread::JoinHandle<()>, +} + +// ANCHOR: here +impl Worker { + fn new(id: usize, receiver: Arc>>) -> Worker { + // --snip-- + // ANCHOR_END: here + let thread = thread::spawn(|| { + receiver; + }); + + Worker { id, thread } + // ANCHOR: here + } +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch20-web-server/listing-20-18/src/main.rs b/src/doc/book/listings/ch20-web-server/listing-20-18/src/main.rs new file mode 100644 index 000000000..79efb28a2 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-18/src/main.rs @@ -0,0 +1,43 @@ +use hello::ThreadPool; +use std::{ + fs, + io::{prelude::*, BufReader}, + net::{TcpListener, TcpStream}, + thread, + time::Duration, +}; + +fn main() { + let listener = TcpListener::bind("127.0.0.1:7878").unwrap(); + let pool = ThreadPool::new(4); + + for stream in listener.incoming() { + let stream = stream.unwrap(); + + pool.execute(|| { + handle_connection(stream); + }); + } +} + +fn handle_connection(mut stream: TcpStream) { + let buf_reader = BufReader::new(&mut stream); + let request_line = buf_reader.lines().next().unwrap().unwrap(); + + let (status_line, filename) = match &request_line[..] { + "GET / HTTP/1.1" => ("HTTP/1.1 200 OK", "hello.html"), + "GET /sleep HTTP/1.1" => { + thread::sleep(Duration::from_secs(5)); + ("HTTP/1.1 200 OK", "hello.html") + } + _ => ("HTTP/1.1 404 NOT FOUND", "404.html"), + }; + + let contents = fs::read_to_string(filename).unwrap(); + let length = contents.len(); + + let response = + format!("{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}"); + + stream.write_all(response.as_bytes()).unwrap(); +} diff --git a/src/doc/book/listings/ch20-web-server/listing-20-19/404.html b/src/doc/book/listings/ch20-web-server/listing-20-19/404.html new file mode 100644 index 000000000..88d8e9152 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-19/404.html @@ -0,0 +1,11 @@ + + + + + Hello! + + +

Oops!

+

Sorry, I don't know what you're asking for.

+ + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-19/Cargo.lock b/src/doc/book/listings/ch20-web-server/listing-20-19/Cargo.lock new file mode 100644 index 000000000..f2d069f46 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-19/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "hello" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-19/Cargo.toml b/src/doc/book/listings/ch20-web-server/listing-20-19/Cargo.toml new file mode 100644 index 000000000..fe619478a --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-19/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "hello" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch20-web-server/listing-20-19/hello.html b/src/doc/book/listings/ch20-web-server/listing-20-19/hello.html new file mode 100644 index 000000000..fe442d6b9 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-19/hello.html @@ -0,0 +1,11 @@ + + + + + Hello! + + +

Hello!

+

Hi from Rust

+ + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-19/src/lib.rs b/src/doc/book/listings/ch20-web-server/listing-20-19/src/lib.rs new file mode 100644 index 000000000..aeb1facd6 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-19/src/lib.rs @@ -0,0 +1,69 @@ +use std::{ + sync::{mpsc, Arc, Mutex}, + thread, +}; + +pub struct ThreadPool { + workers: Vec, + sender: mpsc::Sender, +} + +// ANCHOR: here +// --snip-- + +type Job = Box; + +impl ThreadPool { + // --snip-- + // ANCHOR_END: here + /// Create a new ThreadPool. + /// + /// The size is the number of threads in the pool. + /// + /// # Panics + /// + /// The `new` function will panic if the size is zero. + pub fn new(size: usize) -> ThreadPool { + assert!(size > 0); + + let (sender, receiver) = mpsc::channel(); + + let receiver = Arc::new(Mutex::new(receiver)); + + let mut workers = Vec::with_capacity(size); + + for id in 0..size { + workers.push(Worker::new(id, Arc::clone(&receiver))); + } + + ThreadPool { workers, sender } + } + // ANCHOR: here + + pub fn execute(&self, f: F) + where + F: FnOnce() + Send + 'static, + { + let job = Box::new(f); + + self.sender.send(job).unwrap(); + } +} + +// --snip-- +// ANCHOR_END: here + +struct Worker { + id: usize, + thread: thread::JoinHandle<()>, +} + +impl Worker { + fn new(id: usize, receiver: Arc>>) -> Worker { + let thread = thread::spawn(|| { + receiver; + }); + + Worker { id, thread } + } +} diff --git a/src/doc/book/listings/ch20-web-server/listing-20-19/src/main.rs b/src/doc/book/listings/ch20-web-server/listing-20-19/src/main.rs new file mode 100644 index 000000000..79efb28a2 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-19/src/main.rs @@ -0,0 +1,43 @@ +use hello::ThreadPool; +use std::{ + fs, + io::{prelude::*, BufReader}, + net::{TcpListener, TcpStream}, + thread, + time::Duration, +}; + +fn main() { + let listener = TcpListener::bind("127.0.0.1:7878").unwrap(); + let pool = ThreadPool::new(4); + + for stream in listener.incoming() { + let stream = stream.unwrap(); + + pool.execute(|| { + handle_connection(stream); + }); + } +} + +fn handle_connection(mut stream: TcpStream) { + let buf_reader = BufReader::new(&mut stream); + let request_line = buf_reader.lines().next().unwrap().unwrap(); + + let (status_line, filename) = match &request_line[..] { + "GET / HTTP/1.1" => ("HTTP/1.1 200 OK", "hello.html"), + "GET /sleep HTTP/1.1" => { + thread::sleep(Duration::from_secs(5)); + ("HTTP/1.1 200 OK", "hello.html") + } + _ => ("HTTP/1.1 404 NOT FOUND", "404.html"), + }; + + let contents = fs::read_to_string(filename).unwrap(); + let length = contents.len(); + + let response = + format!("{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}"); + + stream.write_all(response.as_bytes()).unwrap(); +} diff --git a/src/doc/book/listings/ch20-web-server/listing-20-20/404.html b/src/doc/book/listings/ch20-web-server/listing-20-20/404.html new file mode 100644 index 000000000..88d8e9152 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-20/404.html @@ -0,0 +1,11 @@ + + + + + Hello! + + +

Oops!

+

Sorry, I don't know what you're asking for.

+ + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-20/Cargo.lock b/src/doc/book/listings/ch20-web-server/listing-20-20/Cargo.lock new file mode 100644 index 000000000..f2d069f46 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-20/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "hello" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-20/Cargo.toml b/src/doc/book/listings/ch20-web-server/listing-20-20/Cargo.toml new file mode 100644 index 000000000..fe619478a --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-20/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "hello" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch20-web-server/listing-20-20/hello.html b/src/doc/book/listings/ch20-web-server/listing-20-20/hello.html new file mode 100644 index 000000000..fe442d6b9 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-20/hello.html @@ -0,0 +1,11 @@ + + + + + Hello! + + +

Hello!

+

Hi from Rust

+ + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-20/src/lib.rs b/src/doc/book/listings/ch20-web-server/listing-20-20/src/lib.rs new file mode 100644 index 000000000..86157c9e7 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-20/src/lib.rs @@ -0,0 +1,68 @@ +use std::{ + sync::{mpsc, Arc, Mutex}, + thread, +}; + +pub struct ThreadPool { + workers: Vec, + sender: mpsc::Sender, +} + +type Job = Box; + +impl ThreadPool { + /// Create a new ThreadPool. + /// + /// The size is the number of threads in the pool. + /// + /// # Panics + /// + /// The `new` function will panic if the size is zero. + pub fn new(size: usize) -> ThreadPool { + assert!(size > 0); + + let (sender, receiver) = mpsc::channel(); + + let receiver = Arc::new(Mutex::new(receiver)); + + let mut workers = Vec::with_capacity(size); + + for id in 0..size { + workers.push(Worker::new(id, Arc::clone(&receiver))); + } + + ThreadPool { workers, sender } + } + + pub fn execute(&self, f: F) + where + F: FnOnce() + Send + 'static, + { + let job = Box::new(f); + + self.sender.send(job).unwrap(); + } +} + +struct Worker { + id: usize, + thread: thread::JoinHandle<()>, +} + +// ANCHOR: here +// --snip-- + +impl Worker { + fn new(id: usize, receiver: Arc>>) -> Worker { + let thread = thread::spawn(move || loop { + let job = receiver.lock().unwrap().recv().unwrap(); + + println!("Worker {id} got a job; executing."); + + job(); + }); + + Worker { id, thread } + } +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch20-web-server/listing-20-20/src/main.rs b/src/doc/book/listings/ch20-web-server/listing-20-20/src/main.rs new file mode 100644 index 000000000..79efb28a2 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-20/src/main.rs @@ -0,0 +1,43 @@ +use hello::ThreadPool; +use std::{ + fs, + io::{prelude::*, BufReader}, + net::{TcpListener, TcpStream}, + thread, + time::Duration, +}; + +fn main() { + let listener = TcpListener::bind("127.0.0.1:7878").unwrap(); + let pool = ThreadPool::new(4); + + for stream in listener.incoming() { + let stream = stream.unwrap(); + + pool.execute(|| { + handle_connection(stream); + }); + } +} + +fn handle_connection(mut stream: TcpStream) { + let buf_reader = BufReader::new(&mut stream); + let request_line = buf_reader.lines().next().unwrap().unwrap(); + + let (status_line, filename) = match &request_line[..] { + "GET / HTTP/1.1" => ("HTTP/1.1 200 OK", "hello.html"), + "GET /sleep HTTP/1.1" => { + thread::sleep(Duration::from_secs(5)); + ("HTTP/1.1 200 OK", "hello.html") + } + _ => ("HTTP/1.1 404 NOT FOUND", "404.html"), + }; + + let contents = fs::read_to_string(filename).unwrap(); + let length = contents.len(); + + let response = + format!("{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}"); + + stream.write_all(response.as_bytes()).unwrap(); +} diff --git a/src/doc/book/listings/ch20-web-server/listing-20-21/404.html b/src/doc/book/listings/ch20-web-server/listing-20-21/404.html new file mode 100644 index 000000000..88d8e9152 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-21/404.html @@ -0,0 +1,11 @@ + + + + + Hello! + + +

Oops!

+

Sorry, I don't know what you're asking for.

+ + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-21/Cargo.lock b/src/doc/book/listings/ch20-web-server/listing-20-21/Cargo.lock new file mode 100644 index 000000000..f2d069f46 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-21/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "hello" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-21/Cargo.toml b/src/doc/book/listings/ch20-web-server/listing-20-21/Cargo.toml new file mode 100644 index 000000000..fe619478a --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-21/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "hello" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch20-web-server/listing-20-21/hello.html b/src/doc/book/listings/ch20-web-server/listing-20-21/hello.html new file mode 100644 index 000000000..fe442d6b9 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-21/hello.html @@ -0,0 +1,11 @@ + + + + + Hello! + + +

Hello!

+

Hi from Rust

+ + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-21/src/lib.rs b/src/doc/book/listings/ch20-web-server/listing-20-21/src/lib.rs new file mode 100644 index 000000000..17b37e77b --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-21/src/lib.rs @@ -0,0 +1,67 @@ +use std::{ + sync::{mpsc, Arc, Mutex}, + thread, +}; + +pub struct ThreadPool { + workers: Vec, + sender: mpsc::Sender, +} + +type Job = Box; + +impl ThreadPool { + /// Create a new ThreadPool. + /// + /// The size is the number of threads in the pool. + /// + /// # Panics + /// + /// The `new` function will panic if the size is zero. + pub fn new(size: usize) -> ThreadPool { + assert!(size > 0); + + let (sender, receiver) = mpsc::channel(); + + let receiver = Arc::new(Mutex::new(receiver)); + + let mut workers = Vec::with_capacity(size); + + for id in 0..size { + workers.push(Worker::new(id, Arc::clone(&receiver))); + } + + ThreadPool { workers, sender } + } + + pub fn execute(&self, f: F) + where + F: FnOnce() + Send + 'static, + { + let job = Box::new(f); + + self.sender.send(job).unwrap(); + } +} + +struct Worker { + id: usize, + thread: thread::JoinHandle<()>, +} +// ANCHOR: here +// --snip-- + +impl Worker { + fn new(id: usize, receiver: Arc>>) -> Worker { + let thread = thread::spawn(move || { + while let Ok(job) = receiver.lock().unwrap().recv() { + println!("Worker {id} got a job; executing."); + + job(); + } + }); + + Worker { id, thread } + } +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch20-web-server/listing-20-21/src/main.rs b/src/doc/book/listings/ch20-web-server/listing-20-21/src/main.rs new file mode 100644 index 000000000..79efb28a2 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-21/src/main.rs @@ -0,0 +1,43 @@ +use hello::ThreadPool; +use std::{ + fs, + io::{prelude::*, BufReader}, + net::{TcpListener, TcpStream}, + thread, + time::Duration, +}; + +fn main() { + let listener = TcpListener::bind("127.0.0.1:7878").unwrap(); + let pool = ThreadPool::new(4); + + for stream in listener.incoming() { + let stream = stream.unwrap(); + + pool.execute(|| { + handle_connection(stream); + }); + } +} + +fn handle_connection(mut stream: TcpStream) { + let buf_reader = BufReader::new(&mut stream); + let request_line = buf_reader.lines().next().unwrap().unwrap(); + + let (status_line, filename) = match &request_line[..] { + "GET / HTTP/1.1" => ("HTTP/1.1 200 OK", "hello.html"), + "GET /sleep HTTP/1.1" => { + thread::sleep(Duration::from_secs(5)); + ("HTTP/1.1 200 OK", "hello.html") + } + _ => ("HTTP/1.1 404 NOT FOUND", "404.html"), + }; + + let contents = fs::read_to_string(filename).unwrap(); + let length = contents.len(); + + let response = + format!("{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}"); + + stream.write_all(response.as_bytes()).unwrap(); +} diff --git a/src/doc/book/listings/ch20-web-server/listing-20-22/404.html b/src/doc/book/listings/ch20-web-server/listing-20-22/404.html new file mode 100644 index 000000000..88d8e9152 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-22/404.html @@ -0,0 +1,11 @@ + + + + + Hello! + + +

Oops!

+

Sorry, I don't know what you're asking for.

+ + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-22/Cargo.lock b/src/doc/book/listings/ch20-web-server/listing-20-22/Cargo.lock new file mode 100644 index 000000000..f2d069f46 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-22/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "hello" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-22/Cargo.toml b/src/doc/book/listings/ch20-web-server/listing-20-22/Cargo.toml new file mode 100644 index 000000000..fe619478a --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-22/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "hello" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch20-web-server/listing-20-22/hello.html b/src/doc/book/listings/ch20-web-server/listing-20-22/hello.html new file mode 100644 index 000000000..fe442d6b9 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-22/hello.html @@ -0,0 +1,11 @@ + + + + + Hello! + + +

Hello!

+

Hi from Rust

+ + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-22/output.txt b/src/doc/book/listings/ch20-web-server/listing-20-22/output.txt new file mode 100644 index 000000000..4402092e9 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-22/output.txt @@ -0,0 +1,14 @@ +$ cargo check + Checking hello v0.1.0 (file:///projects/hello) +error[E0507]: cannot move out of `worker.thread` which is behind a mutable reference + --> src/lib.rs:52:13 + | +52 | worker.thread.join().unwrap(); + | ^^^^^^^^^^^^^ ------ `worker.thread` moved due to this method call + | | + | move occurs because `worker.thread` has type `JoinHandle<()>`, which does not implement the `Copy` trait + | +note: this function takes ownership of the receiver `self`, which moves `worker.thread` + +For more information about this error, try `rustc --explain E0507`. +error: could not compile `hello` due to previous error diff --git a/src/doc/book/listings/ch20-web-server/listing-20-22/src/lib.rs b/src/doc/book/listings/ch20-web-server/listing-20-22/src/lib.rs new file mode 100644 index 000000000..72a8c4808 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-22/src/lib.rs @@ -0,0 +1,76 @@ +use std::{ + sync::{mpsc, Arc, Mutex}, + thread, +}; + +pub struct ThreadPool { + workers: Vec, + sender: mpsc::Sender, +} + +type Job = Box; + +impl ThreadPool { + /// Create a new ThreadPool. + /// + /// The size is the number of threads in the pool. + /// + /// # Panics + /// + /// The `new` function will panic if the size is zero. + pub fn new(size: usize) -> ThreadPool { + assert!(size > 0); + + let (sender, receiver) = mpsc::channel(); + + let receiver = Arc::new(Mutex::new(receiver)); + + let mut workers = Vec::with_capacity(size); + + for id in 0..size { + workers.push(Worker::new(id, Arc::clone(&receiver))); + } + + ThreadPool { workers, sender } + } + + pub fn execute(&self, f: F) + where + F: FnOnce() + Send + 'static, + { + let job = Box::new(f); + + self.sender.send(job).unwrap(); + } +} + +// ANCHOR: here +impl Drop for ThreadPool { + fn drop(&mut self) { + for worker in &mut self.workers { + println!("Shutting down worker {}", worker.id); + + worker.thread.join().unwrap(); + } + } +} +// ANCHOR_END: here + +struct Worker { + id: usize, + thread: thread::JoinHandle<()>, +} + +impl Worker { + fn new(id: usize, receiver: Arc>>) -> Worker { + let thread = thread::spawn(move || loop { + let job = receiver.lock().unwrap().recv().unwrap(); + + println!("Worker {id} got a job; executing."); + + job(); + }); + + Worker { id, thread } + } +} diff --git a/src/doc/book/listings/ch20-web-server/listing-20-22/src/main.rs b/src/doc/book/listings/ch20-web-server/listing-20-22/src/main.rs new file mode 100644 index 000000000..79efb28a2 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-22/src/main.rs @@ -0,0 +1,43 @@ +use hello::ThreadPool; +use std::{ + fs, + io::{prelude::*, BufReader}, + net::{TcpListener, TcpStream}, + thread, + time::Duration, +}; + +fn main() { + let listener = TcpListener::bind("127.0.0.1:7878").unwrap(); + let pool = ThreadPool::new(4); + + for stream in listener.incoming() { + let stream = stream.unwrap(); + + pool.execute(|| { + handle_connection(stream); + }); + } +} + +fn handle_connection(mut stream: TcpStream) { + let buf_reader = BufReader::new(&mut stream); + let request_line = buf_reader.lines().next().unwrap().unwrap(); + + let (status_line, filename) = match &request_line[..] { + "GET / HTTP/1.1" => ("HTTP/1.1 200 OK", "hello.html"), + "GET /sleep HTTP/1.1" => { + thread::sleep(Duration::from_secs(5)); + ("HTTP/1.1 200 OK", "hello.html") + } + _ => ("HTTP/1.1 404 NOT FOUND", "404.html"), + }; + + let contents = fs::read_to_string(filename).unwrap(); + let length = contents.len(); + + let response = + format!("{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}"); + + stream.write_all(response.as_bytes()).unwrap(); +} diff --git a/src/doc/book/listings/ch20-web-server/listing-20-23/404.html b/src/doc/book/listings/ch20-web-server/listing-20-23/404.html new file mode 100644 index 000000000..88d8e9152 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-23/404.html @@ -0,0 +1,11 @@ + + + + + Hello! + + +

Oops!

+

Sorry, I don't know what you're asking for.

+ + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-23/Cargo.lock b/src/doc/book/listings/ch20-web-server/listing-20-23/Cargo.lock new file mode 100644 index 000000000..f2d069f46 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-23/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "hello" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-23/Cargo.toml b/src/doc/book/listings/ch20-web-server/listing-20-23/Cargo.toml new file mode 100644 index 000000000..fe619478a --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-23/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "hello" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch20-web-server/listing-20-23/hello.html b/src/doc/book/listings/ch20-web-server/listing-20-23/hello.html new file mode 100644 index 000000000..fe442d6b9 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-23/hello.html @@ -0,0 +1,11 @@ + + + + + Hello! + + +

Hello!

+

Hi from Rust

+ + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-23/src/lib.rs b/src/doc/book/listings/ch20-web-server/listing-20-23/src/lib.rs new file mode 100644 index 000000000..eea339b02 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-23/src/lib.rs @@ -0,0 +1,95 @@ +use std::{ + sync::{mpsc, Arc, Mutex}, + thread, +}; + +// ANCHOR: here +pub struct ThreadPool { + workers: Vec, + sender: Option>, +} +// --snip-- +// ANCHOR_END: here + +type Job = Box; + +// ANCHOR: here +impl ThreadPool { + // ANCHOR_END: here + /// Create a new ThreadPool. + /// + /// The size is the number of threads in the pool. + /// + /// # Panics + /// + /// The `new` function will panic if the size is zero. + // ANCHOR: here + pub fn new(size: usize) -> ThreadPool { + // --snip-- + + // ANCHOR_END: here + assert!(size > 0); + + let (sender, receiver) = mpsc::channel(); + + let receiver = Arc::new(Mutex::new(receiver)); + + let mut workers = Vec::with_capacity(size); + + for id in 0..size { + workers.push(Worker::new(id, Arc::clone(&receiver))); + } + + // ANCHOR: here + ThreadPool { + workers, + sender: Some(sender), + } + } + + pub fn execute(&self, f: F) + where + F: FnOnce() + Send + 'static, + { + let job = Box::new(f); + + self.sender.as_ref().unwrap().send(job).unwrap(); + } +} + +impl Drop for ThreadPool { + fn drop(&mut self) { + drop(self.sender.take()); + + for worker in &mut self.workers { + println!("Shutting down worker {}", worker.id); + + if let Some(thread) = worker.thread.take() { + thread.join().unwrap(); + } + } + } +} +// ANCHOR_END: here + +struct Worker { + id: usize, + thread: Option>, +} + +impl Worker { + fn new(id: usize, receiver: Arc>>) -> Worker { + let thread = thread::spawn(move || loop { + let job = receiver.lock().unwrap().recv().unwrap(); + + println!("Worker {id} got a job; executing."); + + job(); + }); + + Worker { + id, + thread: Some(thread), + } + } +} diff --git a/src/doc/book/listings/ch20-web-server/listing-20-23/src/main.rs b/src/doc/book/listings/ch20-web-server/listing-20-23/src/main.rs new file mode 100644 index 000000000..b6aa046d1 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-23/src/main.rs @@ -0,0 +1,45 @@ +use hello::ThreadPool; +use std::{ + fs, + io::{prelude::*, BufReader}, + net::{TcpListener, TcpStream}, + thread, + time::Duration, +}; + +fn main() { + let listener = TcpListener::bind("127.0.0.1:7878").unwrap(); + let pool = ThreadPool::new(4); + + for stream in listener.incoming().take(2) { + let stream = stream.unwrap(); + + pool.execute(|| { + handle_connection(stream); + }); + } + + println!("Shutting down."); +} + +fn handle_connection(mut stream: TcpStream) { + let buf_reader = BufReader::new(&mut stream); + let request_line = buf_reader.lines().next().unwrap().unwrap(); + + let (status_line, filename) = match &request_line[..] { + "GET / HTTP/1.1" => ("HTTP/1.1 200 OK", "hello.html"), + "GET /sleep HTTP/1.1" => { + thread::sleep(Duration::from_secs(5)); + ("HTTP/1.1 200 OK", "hello.html") + } + _ => ("HTTP/1.1 404 NOT FOUND", "404.html"), + }; + + let contents = fs::read_to_string(filename).unwrap(); + let length = contents.len(); + + let response = + format!("{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}"); + + stream.write_all(response.as_bytes()).unwrap(); +} diff --git a/src/doc/book/listings/ch20-web-server/listing-20-24/404.html b/src/doc/book/listings/ch20-web-server/listing-20-24/404.html new file mode 100644 index 000000000..88d8e9152 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-24/404.html @@ -0,0 +1,11 @@ + + + + + Hello! + + +

Oops!

+

Sorry, I don't know what you're asking for.

+ + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-24/Cargo.lock b/src/doc/book/listings/ch20-web-server/listing-20-24/Cargo.lock new file mode 100644 index 000000000..f2d069f46 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-24/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "hello" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-24/Cargo.toml b/src/doc/book/listings/ch20-web-server/listing-20-24/Cargo.toml new file mode 100644 index 000000000..fe619478a --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-24/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "hello" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch20-web-server/listing-20-24/hello.html b/src/doc/book/listings/ch20-web-server/listing-20-24/hello.html new file mode 100644 index 000000000..fe442d6b9 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-24/hello.html @@ -0,0 +1,11 @@ + + + + + Hello! + + +

Hello!

+

Hi from Rust

+ + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-24/src/lib.rs b/src/doc/book/listings/ch20-web-server/listing-20-24/src/lib.rs new file mode 100644 index 000000000..55e8fef8f --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-24/src/lib.rs @@ -0,0 +1,92 @@ +use std::{ + sync::{mpsc, Arc, Mutex}, + thread, +}; + +pub struct ThreadPool { + workers: Vec, + sender: Option>, +} + +type Job = Box; + +impl ThreadPool { + /// Create a new ThreadPool. + /// + /// The size is the number of threads in the pool. + /// + /// # Panics + /// + /// The `new` function will panic if the size is zero. + pub fn new(size: usize) -> ThreadPool { + assert!(size > 0); + + let (sender, receiver) = mpsc::channel(); + + let receiver = Arc::new(Mutex::new(receiver)); + + let mut workers = Vec::with_capacity(size); + + for id in 0..size { + workers.push(Worker::new(id, Arc::clone(&receiver))); + } + + ThreadPool { + workers, + sender: Some(sender), + } + } + + pub fn execute(&self, f: F) + where + F: FnOnce() + Send + 'static, + { + let job = Box::new(f); + + self.sender.as_ref().unwrap().send(job).unwrap(); + } +} + +impl Drop for ThreadPool { + fn drop(&mut self) { + drop(self.sender.take()); + + for worker in &mut self.workers { + println!("Shutting down worker {}", worker.id); + + if let Some(thread) = worker.thread.take() { + thread.join().unwrap(); + } + } + } +} + +struct Worker { + id: usize, + thread: Option>, +} + +// ANCHOR: here +impl Worker { + fn new(id: usize, receiver: Arc>>) -> Worker { + let thread = thread::spawn(move || loop { + match receiver.lock().unwrap().recv() { + Ok(job) => { + println!("Worker {id} got a job; executing."); + + job(); + } + Err(_) => { + println!("Worker {id} disconnected; shutting down."); + break; + } + } + }); + + Worker { + id, + thread: Some(thread), + } + } +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch20-web-server/listing-20-24/src/main.rs b/src/doc/book/listings/ch20-web-server/listing-20-24/src/main.rs new file mode 100644 index 000000000..b6aa046d1 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-24/src/main.rs @@ -0,0 +1,45 @@ +use hello::ThreadPool; +use std::{ + fs, + io::{prelude::*, BufReader}, + net::{TcpListener, TcpStream}, + thread, + time::Duration, +}; + +fn main() { + let listener = TcpListener::bind("127.0.0.1:7878").unwrap(); + let pool = ThreadPool::new(4); + + for stream in listener.incoming().take(2) { + let stream = stream.unwrap(); + + pool.execute(|| { + handle_connection(stream); + }); + } + + println!("Shutting down."); +} + +fn handle_connection(mut stream: TcpStream) { + let buf_reader = BufReader::new(&mut stream); + let request_line = buf_reader.lines().next().unwrap().unwrap(); + + let (status_line, filename) = match &request_line[..] { + "GET / HTTP/1.1" => ("HTTP/1.1 200 OK", "hello.html"), + "GET /sleep HTTP/1.1" => { + thread::sleep(Duration::from_secs(5)); + ("HTTP/1.1 200 OK", "hello.html") + } + _ => ("HTTP/1.1 404 NOT FOUND", "404.html"), + }; + + let contents = fs::read_to_string(filename).unwrap(); + let length = contents.len(); + + let response = + format!("{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}"); + + stream.write_all(response.as_bytes()).unwrap(); +} diff --git a/src/doc/book/listings/ch20-web-server/listing-20-25/404.html b/src/doc/book/listings/ch20-web-server/listing-20-25/404.html new file mode 100644 index 000000000..88d8e9152 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-25/404.html @@ -0,0 +1,11 @@ + + + + + Hello! + + +

Oops!

+

Sorry, I don't know what you're asking for.

+ + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-25/Cargo.lock b/src/doc/book/listings/ch20-web-server/listing-20-25/Cargo.lock new file mode 100644 index 000000000..f2d069f46 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-25/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "hello" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-25/Cargo.toml b/src/doc/book/listings/ch20-web-server/listing-20-25/Cargo.toml new file mode 100644 index 000000000..fe619478a --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-25/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "hello" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch20-web-server/listing-20-25/hello.html b/src/doc/book/listings/ch20-web-server/listing-20-25/hello.html new file mode 100644 index 000000000..fe442d6b9 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-25/hello.html @@ -0,0 +1,11 @@ + + + + + Hello! + + +

Hello!

+

Hi from Rust

+ + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-25/src/lib.rs b/src/doc/book/listings/ch20-web-server/listing-20-25/src/lib.rs new file mode 100644 index 000000000..54c0489ab --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-25/src/lib.rs @@ -0,0 +1,92 @@ +use std::{ + sync::{mpsc, Arc, Mutex}, + thread, +}; + +pub struct ThreadPool { + workers: Vec, + sender: Option>, +} + +type Job = Box; + +impl ThreadPool { + /// Create a new ThreadPool. + /// + /// The size is the number of threads in the pool. + /// + /// # Panics + /// + /// The `new` function will panic if the size is zero. + pub fn new(size: usize) -> ThreadPool { + assert!(size > 0); + + let (sender, receiver) = mpsc::channel(); + + let receiver = Arc::new(Mutex::new(receiver)); + + let mut workers = Vec::with_capacity(size); + + for id in 0..size { + workers.push(Worker::new(id, Arc::clone(&receiver))); + } + + ThreadPool { + workers, + sender: Some(sender), + } + } + + pub fn execute(&self, f: F) + where + F: FnOnce() + Send + 'static, + { + let job = Box::new(f); + + self.sender.as_ref().unwrap().send(job).unwrap(); + } +} + +impl Drop for ThreadPool { + fn drop(&mut self) { + drop(self.sender.take()); + + for worker in &mut self.workers { + println!("Shutting down worker {}", worker.id); + + if let Some(thread) = worker.thread.take() { + thread.join().unwrap(); + } + } + } +} + +struct Worker { + id: usize, + thread: Option>, +} + +impl Worker { + fn new(id: usize, receiver: Arc>>) -> Worker { + let thread = thread::spawn(move || loop { + let message = receiver.lock().unwrap().recv(); + + match message { + Ok(job) => { + println!("Worker {id} got a job; executing."); + + job(); + } + Err(_) => { + println!("Worker {id} disconnected; shutting down."); + break; + } + } + }); + + Worker { + id, + thread: Some(thread), + } + } +} diff --git a/src/doc/book/listings/ch20-web-server/listing-20-25/src/main.rs b/src/doc/book/listings/ch20-web-server/listing-20-25/src/main.rs new file mode 100644 index 000000000..a649ff103 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-25/src/main.rs @@ -0,0 +1,53 @@ +use hello::ThreadPool; +use std::fs; +use std::io::prelude::*; +use std::net::TcpListener; +use std::net::TcpStream; +use std::thread; +use std::time::Duration; + +// ANCHOR: here +fn main() { + let listener = TcpListener::bind("127.0.0.1:7878").unwrap(); + let pool = ThreadPool::new(4); + + for stream in listener.incoming().take(2) { + let stream = stream.unwrap(); + + pool.execute(|| { + handle_connection(stream); + }); + } + + println!("Shutting down."); +} +// ANCHOR_END: here + +fn handle_connection(mut stream: TcpStream) { + let mut buffer = [0; 1024]; + stream.read(&mut buffer).unwrap(); + + let get = b"GET / HTTP/1.1\r\n"; + let sleep = b"GET /sleep HTTP/1.1\r\n"; + + let (status_line, filename) = if buffer.starts_with(get) { + ("HTTP/1.1 200 OK", "hello.html") + } else if buffer.starts_with(sleep) { + thread::sleep(Duration::from_secs(5)); + ("HTTP/1.1 200 OK", "hello.html") + } else { + ("HTTP/1.1 404 NOT FOUND", "404.html") + }; + + let contents = fs::read_to_string(filename).unwrap(); + + let response = format!( + "{}\r\nContent-Length: {}\r\n\r\n{}", + status_line, + contents.len(), + contents + ); + + stream.write_all(response.as_bytes()).unwrap(); + stream.flush().unwrap(); +} diff --git a/src/doc/book/listings/ch20-web-server/no-listing-01-define-threadpool-struct/404.html b/src/doc/book/listings/ch20-web-server/no-listing-01-define-threadpool-struct/404.html new file mode 100644 index 000000000..88d8e9152 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/no-listing-01-define-threadpool-struct/404.html @@ -0,0 +1,11 @@ + + + + + Hello! + + +

Oops!

+

Sorry, I don't know what you're asking for.

+ + diff --git a/src/doc/book/listings/ch20-web-server/no-listing-01-define-threadpool-struct/Cargo.lock b/src/doc/book/listings/ch20-web-server/no-listing-01-define-threadpool-struct/Cargo.lock new file mode 100644 index 000000000..f2d069f46 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/no-listing-01-define-threadpool-struct/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "hello" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch20-web-server/no-listing-01-define-threadpool-struct/Cargo.toml b/src/doc/book/listings/ch20-web-server/no-listing-01-define-threadpool-struct/Cargo.toml new file mode 100644 index 000000000..fe619478a --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/no-listing-01-define-threadpool-struct/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "hello" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch20-web-server/no-listing-01-define-threadpool-struct/hello.html b/src/doc/book/listings/ch20-web-server/no-listing-01-define-threadpool-struct/hello.html new file mode 100644 index 000000000..fe442d6b9 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/no-listing-01-define-threadpool-struct/hello.html @@ -0,0 +1,11 @@ + + + + + Hello! + + +

Hello!

+

Hi from Rust

+ + diff --git a/src/doc/book/listings/ch20-web-server/no-listing-01-define-threadpool-struct/output.txt b/src/doc/book/listings/ch20-web-server/no-listing-01-define-threadpool-struct/output.txt new file mode 100644 index 000000000..fa337b8a8 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/no-listing-01-define-threadpool-struct/output.txt @@ -0,0 +1,10 @@ +$ cargo check + Checking hello v0.1.0 (file:///projects/hello) +error[E0599]: no function or associated item named `new` found for struct `ThreadPool` in the current scope + --> src/main.rs:12:28 + | +12 | let pool = ThreadPool::new(4); + | ^^^ function or associated item not found in `ThreadPool` + +For more information about this error, try `rustc --explain E0599`. +error: could not compile `hello` due to previous error diff --git a/src/doc/book/listings/ch20-web-server/no-listing-01-define-threadpool-struct/src/lib.rs b/src/doc/book/listings/ch20-web-server/no-listing-01-define-threadpool-struct/src/lib.rs new file mode 100644 index 000000000..7312e293d --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/no-listing-01-define-threadpool-struct/src/lib.rs @@ -0,0 +1 @@ +pub struct ThreadPool; diff --git a/src/doc/book/listings/ch20-web-server/no-listing-01-define-threadpool-struct/src/main.rs b/src/doc/book/listings/ch20-web-server/no-listing-01-define-threadpool-struct/src/main.rs new file mode 100644 index 000000000..f7b42167f --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/no-listing-01-define-threadpool-struct/src/main.rs @@ -0,0 +1,45 @@ +// ANCHOR: here +use hello::ThreadPool; +// ANCHOR_END: here +use std::{ + fs, + io::{prelude::*, BufReader}, + net::{TcpListener, TcpStream}, + thread, + time::Duration, +}; + +fn main() { + let listener = TcpListener::bind("127.0.0.1:7878").unwrap(); + let pool = ThreadPool::new(4); + + for stream in listener.incoming() { + let stream = stream.unwrap(); + + pool.execute(|| { + handle_connection(stream); + }); + } +} + +fn handle_connection(mut stream: TcpStream) { + let buf_reader = BufReader::new(&mut stream); + let request_line = buf_reader.lines().next().unwrap().unwrap(); + + let (status_line, filename) = match &request_line[..] { + "GET / HTTP/1.1" => ("HTTP/1.1 200 OK", "hello.html"), + "GET /sleep HTTP/1.1" => { + thread::sleep(Duration::from_secs(5)); + ("HTTP/1.1 200 OK", "hello.html") + } + _ => ("HTTP/1.1 404 NOT FOUND", "404.html"), + }; + + let contents = fs::read_to_string(filename).unwrap(); + let length = contents.len(); + + let response = + format!("{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}"); + + stream.write_all(response.as_bytes()).unwrap(); +} diff --git a/src/doc/book/listings/ch20-web-server/no-listing-02-impl-threadpool-new/404.html b/src/doc/book/listings/ch20-web-server/no-listing-02-impl-threadpool-new/404.html new file mode 100644 index 000000000..88d8e9152 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/no-listing-02-impl-threadpool-new/404.html @@ -0,0 +1,11 @@ + + + + + Hello! + + +

Oops!

+

Sorry, I don't know what you're asking for.

+ + diff --git a/src/doc/book/listings/ch20-web-server/no-listing-02-impl-threadpool-new/Cargo.lock b/src/doc/book/listings/ch20-web-server/no-listing-02-impl-threadpool-new/Cargo.lock new file mode 100644 index 000000000..f2d069f46 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/no-listing-02-impl-threadpool-new/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "hello" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch20-web-server/no-listing-02-impl-threadpool-new/Cargo.toml b/src/doc/book/listings/ch20-web-server/no-listing-02-impl-threadpool-new/Cargo.toml new file mode 100644 index 000000000..fe619478a --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/no-listing-02-impl-threadpool-new/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "hello" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch20-web-server/no-listing-02-impl-threadpool-new/hello.html b/src/doc/book/listings/ch20-web-server/no-listing-02-impl-threadpool-new/hello.html new file mode 100644 index 000000000..fe442d6b9 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/no-listing-02-impl-threadpool-new/hello.html @@ -0,0 +1,11 @@ + + + + + Hello! + + +

Hello!

+

Hi from Rust

+ + diff --git a/src/doc/book/listings/ch20-web-server/no-listing-02-impl-threadpool-new/output.txt b/src/doc/book/listings/ch20-web-server/no-listing-02-impl-threadpool-new/output.txt new file mode 100644 index 000000000..44c8f3953 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/no-listing-02-impl-threadpool-new/output.txt @@ -0,0 +1,10 @@ +$ cargo check + Checking hello v0.1.0 (file:///projects/hello) +error[E0599]: no method named `execute` found for struct `ThreadPool` in the current scope + --> src/main.rs:17:14 + | +17 | pool.execute(|| { + | ^^^^^^^ method not found in `ThreadPool` + +For more information about this error, try `rustc --explain E0599`. +error: could not compile `hello` due to previous error diff --git a/src/doc/book/listings/ch20-web-server/no-listing-02-impl-threadpool-new/src/lib.rs b/src/doc/book/listings/ch20-web-server/no-listing-02-impl-threadpool-new/src/lib.rs new file mode 100644 index 000000000..f0e1890bb --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/no-listing-02-impl-threadpool-new/src/lib.rs @@ -0,0 +1,7 @@ +pub struct ThreadPool; + +impl ThreadPool { + pub fn new(size: usize) -> ThreadPool { + ThreadPool + } +} diff --git a/src/doc/book/listings/ch20-web-server/no-listing-02-impl-threadpool-new/src/main.rs b/src/doc/book/listings/ch20-web-server/no-listing-02-impl-threadpool-new/src/main.rs new file mode 100644 index 000000000..79efb28a2 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/no-listing-02-impl-threadpool-new/src/main.rs @@ -0,0 +1,43 @@ +use hello::ThreadPool; +use std::{ + fs, + io::{prelude::*, BufReader}, + net::{TcpListener, TcpStream}, + thread, + time::Duration, +}; + +fn main() { + let listener = TcpListener::bind("127.0.0.1:7878").unwrap(); + let pool = ThreadPool::new(4); + + for stream in listener.incoming() { + let stream = stream.unwrap(); + + pool.execute(|| { + handle_connection(stream); + }); + } +} + +fn handle_connection(mut stream: TcpStream) { + let buf_reader = BufReader::new(&mut stream); + let request_line = buf_reader.lines().next().unwrap().unwrap(); + + let (status_line, filename) = match &request_line[..] { + "GET / HTTP/1.1" => ("HTTP/1.1 200 OK", "hello.html"), + "GET /sleep HTTP/1.1" => { + thread::sleep(Duration::from_secs(5)); + ("HTTP/1.1 200 OK", "hello.html") + } + _ => ("HTTP/1.1 404 NOT FOUND", "404.html"), + }; + + let contents = fs::read_to_string(filename).unwrap(); + let length = contents.len(); + + let response = + format!("{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}"); + + stream.write_all(response.as_bytes()).unwrap(); +} diff --git a/src/doc/book/listings/ch20-web-server/no-listing-03-define-execute/404.html b/src/doc/book/listings/ch20-web-server/no-listing-03-define-execute/404.html new file mode 100644 index 000000000..88d8e9152 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/no-listing-03-define-execute/404.html @@ -0,0 +1,11 @@ + + + + + Hello! + + +

Oops!

+

Sorry, I don't know what you're asking for.

+ + diff --git a/src/doc/book/listings/ch20-web-server/no-listing-03-define-execute/Cargo.lock b/src/doc/book/listings/ch20-web-server/no-listing-03-define-execute/Cargo.lock new file mode 100644 index 000000000..f2d069f46 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/no-listing-03-define-execute/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "hello" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch20-web-server/no-listing-03-define-execute/Cargo.toml b/src/doc/book/listings/ch20-web-server/no-listing-03-define-execute/Cargo.toml new file mode 100644 index 000000000..fe619478a --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/no-listing-03-define-execute/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "hello" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch20-web-server/no-listing-03-define-execute/hello.html b/src/doc/book/listings/ch20-web-server/no-listing-03-define-execute/hello.html new file mode 100644 index 000000000..fe442d6b9 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/no-listing-03-define-execute/hello.html @@ -0,0 +1,11 @@ + + + + + Hello! + + +

Hello!

+

Hi from Rust

+ + diff --git a/src/doc/book/listings/ch20-web-server/no-listing-03-define-execute/output.txt b/src/doc/book/listings/ch20-web-server/no-listing-03-define-execute/output.txt new file mode 100644 index 000000000..dc76c43d6 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/no-listing-03-define-execute/output.txt @@ -0,0 +1,3 @@ +$ cargo check + Checking hello v0.1.0 (file:///projects/hello) + Finished dev [unoptimized + debuginfo] target(s) in 0.24s diff --git a/src/doc/book/listings/ch20-web-server/no-listing-03-define-execute/src/lib.rs b/src/doc/book/listings/ch20-web-server/no-listing-03-define-execute/src/lib.rs new file mode 100644 index 000000000..1321ab873 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/no-listing-03-define-execute/src/lib.rs @@ -0,0 +1,18 @@ +pub struct ThreadPool; + +// ANCHOR: here +impl ThreadPool { + // --snip-- + // ANCHOR_END: here + pub fn new(size: usize) -> ThreadPool { + ThreadPool + } + + // ANCHOR: here + pub fn execute(&self, f: F) + where + F: FnOnce() + Send + 'static, + { + } +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch20-web-server/no-listing-03-define-execute/src/main.rs b/src/doc/book/listings/ch20-web-server/no-listing-03-define-execute/src/main.rs new file mode 100644 index 000000000..79efb28a2 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/no-listing-03-define-execute/src/main.rs @@ -0,0 +1,43 @@ +use hello::ThreadPool; +use std::{ + fs, + io::{prelude::*, BufReader}, + net::{TcpListener, TcpStream}, + thread, + time::Duration, +}; + +fn main() { + let listener = TcpListener::bind("127.0.0.1:7878").unwrap(); + let pool = ThreadPool::new(4); + + for stream in listener.incoming() { + let stream = stream.unwrap(); + + pool.execute(|| { + handle_connection(stream); + }); + } +} + +fn handle_connection(mut stream: TcpStream) { + let buf_reader = BufReader::new(&mut stream); + let request_line = buf_reader.lines().next().unwrap().unwrap(); + + let (status_line, filename) = match &request_line[..] { + "GET / HTTP/1.1" => ("HTTP/1.1 200 OK", "hello.html"), + "GET /sleep HTTP/1.1" => { + thread::sleep(Duration::from_secs(5)); + ("HTTP/1.1 200 OK", "hello.html") + } + _ => ("HTTP/1.1 404 NOT FOUND", "404.html"), + }; + + let contents = fs::read_to_string(filename).unwrap(); + let length = contents.len(); + + let response = + format!("{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}"); + + stream.write_all(response.as_bytes()).unwrap(); +} diff --git a/src/doc/book/listings/ch20-web-server/no-listing-04-update-worker-definition/404.html b/src/doc/book/listings/ch20-web-server/no-listing-04-update-worker-definition/404.html new file mode 100644 index 000000000..88d8e9152 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/no-listing-04-update-worker-definition/404.html @@ -0,0 +1,11 @@ + + + + + Hello! + + +

Oops!

+

Sorry, I don't know what you're asking for.

+ + diff --git a/src/doc/book/listings/ch20-web-server/no-listing-04-update-worker-definition/Cargo.lock b/src/doc/book/listings/ch20-web-server/no-listing-04-update-worker-definition/Cargo.lock new file mode 100644 index 000000000..f2d069f46 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/no-listing-04-update-worker-definition/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "hello" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch20-web-server/no-listing-04-update-worker-definition/Cargo.toml b/src/doc/book/listings/ch20-web-server/no-listing-04-update-worker-definition/Cargo.toml new file mode 100644 index 000000000..fe619478a --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/no-listing-04-update-worker-definition/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "hello" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch20-web-server/no-listing-04-update-worker-definition/hello.html b/src/doc/book/listings/ch20-web-server/no-listing-04-update-worker-definition/hello.html new file mode 100644 index 000000000..fe442d6b9 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/no-listing-04-update-worker-definition/hello.html @@ -0,0 +1,11 @@ + + + + + Hello! + + +

Hello!

+

Hi from Rust

+ + diff --git a/src/doc/book/listings/ch20-web-server/no-listing-04-update-worker-definition/output.txt b/src/doc/book/listings/ch20-web-server/no-listing-04-update-worker-definition/output.txt new file mode 100644 index 000000000..34d30fe05 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/no-listing-04-update-worker-definition/output.txt @@ -0,0 +1,24 @@ +$ cargo check + Checking hello v0.1.0 (file:///projects/hello) +error[E0599]: no method named `join` found for enum `Option` in the current scope + --> src/lib.rs:52:27 + | +52 | worker.thread.join().unwrap(); + | ^^^^ method not found in `Option>` + +error[E0308]: mismatched types + --> src/lib.rs:72:22 + | +72 | Worker { id, thread } + | ^^^^^^ expected enum `Option`, found struct `JoinHandle` + | + = note: expected enum `Option>` + found struct `JoinHandle<_>` +help: try wrapping the expression in `Some` + | +72 | Worker { id, thread: Some(thread) } + | +++++++++++++ + + +Some errors have detailed explanations: E0308, E0599. +For more information about an error, try `rustc --explain E0308`. +error: could not compile `hello` due to 2 previous errors diff --git a/src/doc/book/listings/ch20-web-server/no-listing-04-update-worker-definition/src/lib.rs b/src/doc/book/listings/ch20-web-server/no-listing-04-update-worker-definition/src/lib.rs new file mode 100644 index 000000000..6ef710a26 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/no-listing-04-update-worker-definition/src/lib.rs @@ -0,0 +1,76 @@ +use std::{ + sync::{mpsc, Arc, Mutex}, + thread, +}; + +pub struct ThreadPool { + workers: Vec, + sender: mpsc::Sender, +} + +type Job = Box; + +impl ThreadPool { + /// Create a new ThreadPool. + /// + /// The size is the number of threads in the pool. + /// + /// # Panics + /// + /// The `new` function will panic if the size is zero. + pub fn new(size: usize) -> ThreadPool { + assert!(size > 0); + + let (sender, receiver) = mpsc::channel(); + + let receiver = Arc::new(Mutex::new(receiver)); + + let mut workers = Vec::with_capacity(size); + + for id in 0..size { + workers.push(Worker::new(id, Arc::clone(&receiver))); + } + + ThreadPool { workers, sender } + } + + pub fn execute(&self, f: F) + where + F: FnOnce() + Send + 'static, + { + let job = Box::new(f); + + self.sender.send(job).unwrap(); + } +} + +impl Drop for ThreadPool { + fn drop(&mut self) { + for worker in &mut self.workers { + println!("Shutting down worker {}", worker.id); + + worker.thread.join().unwrap(); + } + } +} + +// ANCHOR: here +struct Worker { + id: usize, + thread: Option>, +} +// ANCHOR_END: here + +impl Worker { + fn new(id: usize, receiver: Arc>>) -> Worker { + let thread = thread::spawn(move || loop { + let job = receiver.lock().unwrap().recv().unwrap(); + + println!("Worker {id} got a job; executing."); + + job(); + }); + + Worker { id, thread } + } +} diff --git a/src/doc/book/listings/ch20-web-server/no-listing-04-update-worker-definition/src/main.rs b/src/doc/book/listings/ch20-web-server/no-listing-04-update-worker-definition/src/main.rs new file mode 100644 index 000000000..79efb28a2 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/no-listing-04-update-worker-definition/src/main.rs @@ -0,0 +1,43 @@ +use hello::ThreadPool; +use std::{ + fs, + io::{prelude::*, BufReader}, + net::{TcpListener, TcpStream}, + thread, + time::Duration, +}; + +fn main() { + let listener = TcpListener::bind("127.0.0.1:7878").unwrap(); + let pool = ThreadPool::new(4); + + for stream in listener.incoming() { + let stream = stream.unwrap(); + + pool.execute(|| { + handle_connection(stream); + }); + } +} + +fn handle_connection(mut stream: TcpStream) { + let buf_reader = BufReader::new(&mut stream); + let request_line = buf_reader.lines().next().unwrap().unwrap(); + + let (status_line, filename) = match &request_line[..] { + "GET / HTTP/1.1" => ("HTTP/1.1 200 OK", "hello.html"), + "GET /sleep HTTP/1.1" => { + thread::sleep(Duration::from_secs(5)); + ("HTTP/1.1 200 OK", "hello.html") + } + _ => ("HTTP/1.1 404 NOT FOUND", "404.html"), + }; + + let contents = fs::read_to_string(filename).unwrap(); + let length = contents.len(); + + let response = + format!("{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}"); + + stream.write_all(response.as_bytes()).unwrap(); +} diff --git a/src/doc/book/listings/ch20-web-server/no-listing-05-fix-worker-new/404.html b/src/doc/book/listings/ch20-web-server/no-listing-05-fix-worker-new/404.html new file mode 100644 index 000000000..88d8e9152 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/no-listing-05-fix-worker-new/404.html @@ -0,0 +1,11 @@ + + + + + Hello! + + +

Oops!

+

Sorry, I don't know what you're asking for.

+ + diff --git a/src/doc/book/listings/ch20-web-server/no-listing-05-fix-worker-new/Cargo.lock b/src/doc/book/listings/ch20-web-server/no-listing-05-fix-worker-new/Cargo.lock new file mode 100644 index 000000000..f2d069f46 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/no-listing-05-fix-worker-new/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "hello" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch20-web-server/no-listing-05-fix-worker-new/Cargo.toml b/src/doc/book/listings/ch20-web-server/no-listing-05-fix-worker-new/Cargo.toml new file mode 100644 index 000000000..fe619478a --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/no-listing-05-fix-worker-new/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "hello" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch20-web-server/no-listing-05-fix-worker-new/hello.html b/src/doc/book/listings/ch20-web-server/no-listing-05-fix-worker-new/hello.html new file mode 100644 index 000000000..fe442d6b9 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/no-listing-05-fix-worker-new/hello.html @@ -0,0 +1,11 @@ + + + + + Hello! + + +

Hello!

+

Hi from Rust

+ + diff --git a/src/doc/book/listings/ch20-web-server/no-listing-05-fix-worker-new/src/lib.rs b/src/doc/book/listings/ch20-web-server/no-listing-05-fix-worker-new/src/lib.rs new file mode 100644 index 000000000..ede3750a1 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/no-listing-05-fix-worker-new/src/lib.rs @@ -0,0 +1,83 @@ +use std::{ + sync::{mpsc, Arc, Mutex}, + thread, +}; + +pub struct ThreadPool { + workers: Vec, + sender: mpsc::Sender, +} + +type Job = Box; + +impl ThreadPool { + /// Create a new ThreadPool. + /// + /// The size is the number of threads in the pool. + /// + /// # Panics + /// + /// The `new` function will panic if the size is zero. + pub fn new(size: usize) -> ThreadPool { + assert!(size > 0); + + let (sender, receiver) = mpsc::channel(); + + let receiver = Arc::new(Mutex::new(receiver)); + + let mut workers = Vec::with_capacity(size); + + for id in 0..size { + workers.push(Worker::new(id, Arc::clone(&receiver))); + } + + ThreadPool { workers, sender } + } + + pub fn execute(&self, f: F) + where + F: FnOnce() + Send + 'static, + { + let job = Box::new(f); + + self.sender.send(job).unwrap(); + } +} + +impl Drop for ThreadPool { + fn drop(&mut self) { + for worker in &mut self.workers { + println!("Shutting down worker {}", worker.id); + + worker.thread.join().unwrap(); + } + } +} + +struct Worker { + id: usize, + thread: Option>, +} + +// ANCHOR: here +impl Worker { + fn new(id: usize, receiver: Arc>>) -> Worker { + // --snip-- + + // ANCHOR_END: here + let thread = thread::spawn(move || loop { + let job = receiver.lock().unwrap().recv().unwrap(); + + println!("Worker {id} got a job; executing."); + + job(); + }); + + // ANCHOR: here + Worker { + id, + thread: Some(thread), + } + } +} +// ANCHOR_END: here diff --git a/src/doc/book/listings/ch20-web-server/no-listing-05-fix-worker-new/src/main.rs b/src/doc/book/listings/ch20-web-server/no-listing-05-fix-worker-new/src/main.rs new file mode 100644 index 000000000..79efb28a2 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/no-listing-05-fix-worker-new/src/main.rs @@ -0,0 +1,43 @@ +use hello::ThreadPool; +use std::{ + fs, + io::{prelude::*, BufReader}, + net::{TcpListener, TcpStream}, + thread, + time::Duration, +}; + +fn main() { + let listener = TcpListener::bind("127.0.0.1:7878").unwrap(); + let pool = ThreadPool::new(4); + + for stream in listener.incoming() { + let stream = stream.unwrap(); + + pool.execute(|| { + handle_connection(stream); + }); + } +} + +fn handle_connection(mut stream: TcpStream) { + let buf_reader = BufReader::new(&mut stream); + let request_line = buf_reader.lines().next().unwrap().unwrap(); + + let (status_line, filename) = match &request_line[..] { + "GET / HTTP/1.1" => ("HTTP/1.1 200 OK", "hello.html"), + "GET /sleep HTTP/1.1" => { + thread::sleep(Duration::from_secs(5)); + ("HTTP/1.1 200 OK", "hello.html") + } + _ => ("HTTP/1.1 404 NOT FOUND", "404.html"), + }; + + let contents = fs::read_to_string(filename).unwrap(); + let length = contents.len(); + + let response = + format!("{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}"); + + stream.write_all(response.as_bytes()).unwrap(); +} diff --git a/src/doc/book/listings/ch20-web-server/no-listing-06-fix-threadpool-drop/404.html b/src/doc/book/listings/ch20-web-server/no-listing-06-fix-threadpool-drop/404.html new file mode 100644 index 000000000..88d8e9152 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/no-listing-06-fix-threadpool-drop/404.html @@ -0,0 +1,11 @@ + + + + + Hello! + + +

Oops!

+

Sorry, I don't know what you're asking for.

+ + diff --git a/src/doc/book/listings/ch20-web-server/no-listing-06-fix-threadpool-drop/Cargo.lock b/src/doc/book/listings/ch20-web-server/no-listing-06-fix-threadpool-drop/Cargo.lock new file mode 100644 index 000000000..f2d069f46 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/no-listing-06-fix-threadpool-drop/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "hello" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch20-web-server/no-listing-06-fix-threadpool-drop/Cargo.toml b/src/doc/book/listings/ch20-web-server/no-listing-06-fix-threadpool-drop/Cargo.toml new file mode 100644 index 000000000..fe619478a --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/no-listing-06-fix-threadpool-drop/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "hello" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch20-web-server/no-listing-06-fix-threadpool-drop/hello.html b/src/doc/book/listings/ch20-web-server/no-listing-06-fix-threadpool-drop/hello.html new file mode 100644 index 000000000..fe442d6b9 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/no-listing-06-fix-threadpool-drop/hello.html @@ -0,0 +1,11 @@ + + + + + Hello! + + +

Hello!

+

Hi from Rust

+ + diff --git a/src/doc/book/listings/ch20-web-server/no-listing-06-fix-threadpool-drop/src/lib.rs b/src/doc/book/listings/ch20-web-server/no-listing-06-fix-threadpool-drop/src/lib.rs new file mode 100644 index 000000000..b795ea53b --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/no-listing-06-fix-threadpool-drop/src/lib.rs @@ -0,0 +1,81 @@ +use std::{ + sync::{mpsc, Arc, Mutex}, + thread, +}; + +pub struct ThreadPool { + workers: Vec, + sender: mpsc::Sender, +} + +type Job = Box; + +impl ThreadPool { + /// Create a new ThreadPool. + /// + /// The size is the number of threads in the pool. + /// + /// # Panics + /// + /// The `new` function will panic if the size is zero. + pub fn new(size: usize) -> ThreadPool { + assert!(size > 0); + + let (sender, receiver) = mpsc::channel(); + + let receiver = Arc::new(Mutex::new(receiver)); + + let mut workers = Vec::with_capacity(size); + + for id in 0..size { + workers.push(Worker::new(id, Arc::clone(&receiver))); + } + + ThreadPool { workers, sender } + } + + pub fn execute(&self, f: F) + where + F: FnOnce() + Send + 'static, + { + let job = Box::new(f); + + self.sender.send(job).unwrap(); + } +} + +// ANCHOR: here +impl Drop for ThreadPool { + fn drop(&mut self) { + for worker in &mut self.workers { + println!("Shutting down worker {}", worker.id); + + if let Some(thread) = worker.thread.take() { + thread.join().unwrap(); + } + } + } +} +// ANCHOR_END: here + +struct Worker { + id: usize, + thread: Option>, +} + +impl Worker { + fn new(id: usize, receiver: Arc>>) -> Worker { + let thread = thread::spawn(move || loop { + let job = receiver.lock().unwrap().recv().unwrap(); + + println!("Worker {id} got a job; executing."); + + job(); + }); + + Worker { + id, + thread: Some(thread), + } + } +} diff --git a/src/doc/book/listings/ch20-web-server/no-listing-06-fix-threadpool-drop/src/main.rs b/src/doc/book/listings/ch20-web-server/no-listing-06-fix-threadpool-drop/src/main.rs new file mode 100644 index 000000000..b6aa046d1 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/no-listing-06-fix-threadpool-drop/src/main.rs @@ -0,0 +1,45 @@ +use hello::ThreadPool; +use std::{ + fs, + io::{prelude::*, BufReader}, + net::{TcpListener, TcpStream}, + thread, + time::Duration, +}; + +fn main() { + let listener = TcpListener::bind("127.0.0.1:7878").unwrap(); + let pool = ThreadPool::new(4); + + for stream in listener.incoming().take(2) { + let stream = stream.unwrap(); + + pool.execute(|| { + handle_connection(stream); + }); + } + + println!("Shutting down."); +} + +fn handle_connection(mut stream: TcpStream) { + let buf_reader = BufReader::new(&mut stream); + let request_line = buf_reader.lines().next().unwrap().unwrap(); + + let (status_line, filename) = match &request_line[..] { + "GET / HTTP/1.1" => ("HTTP/1.1 200 OK", "hello.html"), + "GET /sleep HTTP/1.1" => { + thread::sleep(Duration::from_secs(5)); + ("HTTP/1.1 200 OK", "hello.html") + } + _ => ("HTTP/1.1 404 NOT FOUND", "404.html"), + }; + + let contents = fs::read_to_string(filename).unwrap(); + let length = contents.len(); + + let response = + format!("{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}"); + + stream.write_all(response.as_bytes()).unwrap(); +} diff --git a/src/doc/book/listings/ch20-web-server/no-listing-07-final-code/404.html b/src/doc/book/listings/ch20-web-server/no-listing-07-final-code/404.html new file mode 100644 index 000000000..88d8e9152 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/no-listing-07-final-code/404.html @@ -0,0 +1,11 @@ + + + + + Hello! + + +

Oops!

+

Sorry, I don't know what you're asking for.

+ + diff --git a/src/doc/book/listings/ch20-web-server/no-listing-07-final-code/Cargo.lock b/src/doc/book/listings/ch20-web-server/no-listing-07-final-code/Cargo.lock new file mode 100644 index 000000000..f2d069f46 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/no-listing-07-final-code/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "hello" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch20-web-server/no-listing-07-final-code/Cargo.toml b/src/doc/book/listings/ch20-web-server/no-listing-07-final-code/Cargo.toml new file mode 100644 index 000000000..fe619478a --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/no-listing-07-final-code/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "hello" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch20-web-server/no-listing-07-final-code/hello.html b/src/doc/book/listings/ch20-web-server/no-listing-07-final-code/hello.html new file mode 100644 index 000000000..fe442d6b9 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/no-listing-07-final-code/hello.html @@ -0,0 +1,11 @@ + + + + + Hello! + + +

Hello!

+

Hi from Rust

+ + diff --git a/src/doc/book/listings/ch20-web-server/no-listing-07-final-code/src/lib.rs b/src/doc/book/listings/ch20-web-server/no-listing-07-final-code/src/lib.rs new file mode 100644 index 000000000..54c0489ab --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/no-listing-07-final-code/src/lib.rs @@ -0,0 +1,92 @@ +use std::{ + sync::{mpsc, Arc, Mutex}, + thread, +}; + +pub struct ThreadPool { + workers: Vec, + sender: Option>, +} + +type Job = Box; + +impl ThreadPool { + /// Create a new ThreadPool. + /// + /// The size is the number of threads in the pool. + /// + /// # Panics + /// + /// The `new` function will panic if the size is zero. + pub fn new(size: usize) -> ThreadPool { + assert!(size > 0); + + let (sender, receiver) = mpsc::channel(); + + let receiver = Arc::new(Mutex::new(receiver)); + + let mut workers = Vec::with_capacity(size); + + for id in 0..size { + workers.push(Worker::new(id, Arc::clone(&receiver))); + } + + ThreadPool { + workers, + sender: Some(sender), + } + } + + pub fn execute(&self, f: F) + where + F: FnOnce() + Send + 'static, + { + let job = Box::new(f); + + self.sender.as_ref().unwrap().send(job).unwrap(); + } +} + +impl Drop for ThreadPool { + fn drop(&mut self) { + drop(self.sender.take()); + + for worker in &mut self.workers { + println!("Shutting down worker {}", worker.id); + + if let Some(thread) = worker.thread.take() { + thread.join().unwrap(); + } + } + } +} + +struct Worker { + id: usize, + thread: Option>, +} + +impl Worker { + fn new(id: usize, receiver: Arc>>) -> Worker { + let thread = thread::spawn(move || loop { + let message = receiver.lock().unwrap().recv(); + + match message { + Ok(job) => { + println!("Worker {id} got a job; executing."); + + job(); + } + Err(_) => { + println!("Worker {id} disconnected; shutting down."); + break; + } + } + }); + + Worker { + id, + thread: Some(thread), + } + } +} diff --git a/src/doc/book/listings/ch20-web-server/no-listing-07-final-code/src/main.rs b/src/doc/book/listings/ch20-web-server/no-listing-07-final-code/src/main.rs new file mode 100644 index 000000000..3161c2ee5 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/no-listing-07-final-code/src/main.rs @@ -0,0 +1,51 @@ +use hello::ThreadPool; +use std::fs; +use std::io::prelude::*; +use std::net::TcpListener; +use std::net::TcpStream; +use std::thread; +use std::time::Duration; + +fn main() { + let listener = TcpListener::bind("127.0.0.1:7878").unwrap(); + let pool = ThreadPool::new(4); + + for stream in listener.incoming().take(2) { + let stream = stream.unwrap(); + + pool.execute(|| { + handle_connection(stream); + }); + } + + println!("Shutting down."); +} + +fn handle_connection(mut stream: TcpStream) { + let mut buffer = [0; 1024]; + stream.read(&mut buffer).unwrap(); + + let get = b"GET / HTTP/1.1\r\n"; + let sleep = b"GET /sleep HTTP/1.1\r\n"; + + let (status_line, filename) = if buffer.starts_with(get) { + ("HTTP/1.1 200 OK", "hello.html") + } else if buffer.starts_with(sleep) { + thread::sleep(Duration::from_secs(5)); + ("HTTP/1.1 200 OK", "hello.html") + } else { + ("HTTP/1.1 404 NOT FOUND", "404.html") + }; + + let contents = fs::read_to_string(filename).unwrap(); + + let response = format!( + "{}\r\nContent-Length: {}\r\n\r\n{}", + status_line, + contents.len(), + contents + ); + + stream.write_all(response.as_bytes()).unwrap(); + stream.flush().unwrap(); +} -- cgit v1.2.3