Skip to content

Commit 0a639d7

Browse files
authored
Merge branch 'master' into master
2 parents 84e80f7 + 8e75749 commit 0a639d7

File tree

13 files changed

+49
-42
lines changed

13 files changed

+49
-42
lines changed

src/custom_types/enum/testcase_linked_list.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ impl List {
3232
// depends on the variant of `self`
3333
// `self` has type `&List`, and `*self` has type `List`, matching on a
3434
// concrete type `T` is preferred over a match on a reference `&T`
35+
// after Rust 2018 you can use self here and tail (with no ref) below as well,
36+
// rust will infer &s and ref tail.
37+
// See https://doc.rust-lang.org/edition-guide/rust-2018/ownership-and-lifetimes/default-match-bindings.html
3538
match *self {
3639
// Can't take ownership of the tail, because `self` is borrowed;
3740
// instead take a reference to the tail

src/custom_types/structs.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
There are three types of structures ("structs") that can be created using the
44
`struct` keyword:
55

6-
* Tuple structs, which are, basically, named tuples.
7-
* The classic [C structs][c_struct]
6+
* Tuple structs, which are, basically, named tuples. The classic [C structs][c_struct]
87
* Unit structs, which are field-less, are useful for generics.
98

109
```rust,editable
@@ -86,7 +85,7 @@ fn main() {
8685

8786
### Activity
8887

89-
1. Add a function `rect_area` which calculates the area of a rectangle (try
88+
1. Add a function `rect_area` which calculates the area of a `Rectangle` (try
9089
using nested destructuring).
9190
2. Add a function `square` which takes a `Point` and a `f32` as arguments, and
9291
returns a `Rectangle` with its lower left corner on the point, and a width and

src/error/option_unwrap.md

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# `Option` & `unwrap`
22

33
In the last example, we showed that we can induce program failure at will.
4-
We told our program to `panic` if the royal received an inappropriate
5-
gift - a snake. But what if the royal expected a gift and didn't receive
6-
one? That case would be just as bad, so it needs to be handled!
4+
We told our program to `panic` if we drink a sugary lemonade.
5+
But what if we expect _some_ drink but don't receive one?
6+
That case would be just as bad, so it needs to be handled!
77

8-
We *could* test this against the null string (`""`) as we do with a snake.
8+
We *could* test this against the null string (`""`) as we do with a lemonade.
99
Since we're using Rust, let's instead have the compiler point out cases
10-
where there's no gift.
10+
where there's no drink.
1111

1212
An `enum` called `Option<T>` in the `std` library is used when absence is a
1313
possibility. It manifests itself as one of two "options":
@@ -24,41 +24,41 @@ handling. In the following example, explicit handling yields a more
2424
controlled result while retaining the option to `panic` if desired.
2525

2626
```rust,editable,ignore,mdbook-runnable
27-
// The commoner has seen it all, and can handle any gift well.
28-
// All gifts are handled explicitly using `match`.
29-
fn give_commoner(gift: Option<&str>) {
27+
// The adult has seen it all, and can handle any drink well.
28+
// All drinks are handled explicitly using `match`.
29+
fn give_adult(drink: Option<&str>) {
3030
// Specify a course of action for each case.
31-
match gift {
32-
Some("snake") => println!("Yuck! I'm putting this snake back in the forest."),
31+
match drink {
32+
Some("lemonade") => println!("Yuck! Too sugary."),
3333
Some(inner) => println!("{}? How nice.", inner),
34-
None => println!("No gift? Oh well."),
34+
None => println!("No drink? Oh well."),
3535
}
3636
}
3737
38-
// Our sheltered royal will `panic` at the sight of snakes.
39-
// All gifts are handled implicitly using `unwrap`.
40-
fn give_royal(gift: Option<&str>) {
38+
// Others will `panic` before drinking sugary drinks.
39+
// All drinks are handled implicitly using `unwrap`.
40+
fn drink(drink: Option<&str>) {
4141
// `unwrap` returns a `panic` when it receives a `None`.
42-
let inside = gift.unwrap();
43-
if inside == "snake" { panic!("AAAaaaaa!!!!"); }
42+
let inside = drink.unwrap();
43+
if inside == "lemonade" { panic!("AAAaaaaa!!!!"); }
4444
4545
println!("I love {}s!!!!!", inside);
4646
}
4747
4848
fn main() {
49-
let food = Some("cabbage");
50-
let snake = Some("snake");
49+
let water = Some("water");
50+
let lemonade = Some("lemonade");
5151
let void = None;
5252
53-
give_commoner(food);
54-
give_commoner(snake);
55-
give_commoner(void);
53+
give_adult(water);
54+
give_adult(lemonade);
55+
give_adult(void);
5656
57-
let bird = Some("robin");
57+
let coffee = Some("coffee");
5858
let nothing = None;
5959
60-
give_royal(bird);
61-
give_royal(nothing);
60+
drink(coffee);
61+
drink(nothing);
6262
}
6363
```
6464

src/expression.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
A Rust program is (mostly) made up of a series of statements:
44

5-
```
5+
```rust,editable
66
fn main() {
77
// statement
88
// statement
@@ -13,7 +13,7 @@ fn main() {
1313
There are a few kinds of statements in Rust. The most common two are declaring
1414
a variable binding, and using a `;` with an expression:
1515

16-
```
16+
```rust,editable
1717
fn main() {
1818
// variable binding
1919
let x = 5;

src/generics/multi_bounds.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Multiple bounds
22

3-
Multiple bounds can be applied with a `+`. Like normal, different types are
3+
Multiple bounds for a single type can be applied with a `+`. Like normal, different types are
44
separated with `,`.
55

66
```rust,editable

src/generics/new_types.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,14 @@ fn main() {
4040

4141
Uncomment the last print statement to observe that the type supplied must be `Years`.
4242

43-
To obtain the `newtype`'s value as the base type, you may use tuple syntax like so:
43+
To obtain the `newtype`'s value as the base type, you may use the tuple or destructuring syntax like so:
4444
```rust, editable
4545
struct Years(i64);
4646
4747
fn main() {
4848
let years = Years(42);
49-
let years_as_primitive: i64 = years.0;
49+
let years_as_primitive_1: i64 = years.0; // Tuple
50+
let Years(years_as_primitive_2) = years; // Destructuring
5051
}
5152
```
5253

src/hello/print/print_display.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ each requires its own implementation. This is detailed further in
108108
### Activity
109109

110110
After checking the output of the above example, use the `Point2D` struct as a
111-
guide to add a Complex struct to the example. When printed in the same
111+
guide to add a `Complex` struct to the example. When printed in the same
112112
way, the output should be:
113113

114114
```txt
@@ -125,5 +125,5 @@ Debug: Complex { real: 3.3, imag: 7.2 }
125125
[fmt]: https://doc.rust-lang.org/std/fmt/
126126
[macros]: ../../macros.md
127127
[structs]: ../../custom_types/structs.md
128-
[traits]: ../../trait.md
128+
[traits]: https://doc.rust-lang.org/std/fmt/#formatting-traits
129129
[use]: ../../mod/use.md

src/primitives/tuples.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ fn main() {
6464

6565
### Activity
6666

67-
1. *Recap*: Add the `fmt::Display` trait to the Matrix `struct` in the above example,
67+
1. *Recap*: Add the `fmt::Display` trait to the `Matrix` struct in the above example,
6868
so that if you switch from printing the debug format `{:?}` to the display
6969
format `{}`, you see the following output:
7070

src/scope/lifetime/static_lifetime.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ fn main() {
8585
print_it(i);
8686
8787
// oops, &i only has the lifetime defined by the scope of
88-
// use_it(), so it's not 'static:
88+
// main(), so it's not 'static:
8989
print_it(&i);
9090
}
9191
```

src/scope/lifetime/trait.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ Note that `impl` may have annotation of lifetimes too.
66
```rust,editable
77
// A struct with annotation of lifetimes.
88
#[derive(Debug)]
9-
struct Borrowed<'a> {
10-
x: &'a i32,
11-
}
9+
struct Borrowed<'a> {
10+
x: &'a i32,
11+
}
1212
1313
// Annotate lifetimes to impl.
1414
impl<'a> Default for Borrowed<'a> {

0 commit comments

Comments
 (0)