Skip to content

Commit dbcdccf

Browse files
authored
Merge pull request #1254 from funkill/patch-1
Inline code in some new/changed chapters
2 parents 127e7dc + ce28351 commit dbcdccf

File tree

4 files changed

+31
-19
lines changed

4 files changed

+31
-19
lines changed

src/SUMMARY.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@
137137
- [Operator Overloading](trait/ops.md)
138138
- [Drop](trait/drop.md)
139139
- [Iterators](trait/iter.md)
140-
- [impl Trait](trait/impl_trait.md)
140+
- [`impl Trait`](trait/impl_trait.md)
141141
- [Clone](trait/clone.md)
142142

143143
- [macro_rules!](macros.md)
@@ -152,7 +152,7 @@
152152
- [Error handling](error.md)
153153
- [`panic`](error/panic.md)
154154
- [`Option` & `unwrap`](error/option_unwrap.md)
155-
- [Unpacking options with ?](error/option_unwrap/question_mark.md)
155+
- [Unpacking options with `?`](error/option_unwrap/question_mark.md)
156156
- [Combinators: `map`](error/option_unwrap/map.md)
157157
- [Combinators: `and_then`](error/option_unwrap/and_then.md)
158158
- [`Result`](error/result.md)

src/error/option_unwrap/question_mark.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
# Unpacking options with ?
1+
# Unpacking options with `?`
22

3-
You can unpack Options by using `match` statements, but it's often easier to use the `?` operator. If `x` is an `Option`, then evaluating `x?` will return the underlying value if `x` is Some, otherwise it will terminate whatever function is being executed and return `None`.
3+
You can unpack `Option`s by using `match` statements, but it's often easier to
4+
use the `?` operator. If `x` is an `Option`, then evaluating `x?` will return
5+
the underlying value if `x` is `Some`, otherwise it will terminate whatever
6+
function is being executed and return `None`.
47

58
```rust,editable
69
fn next_birthday(current_age: Option<u8>) -> Option<String> {
7-
// If `current_age` is None, this returns None.
8-
// If `current_age` is Some, the inner u8 gets assigned to `next_age`
10+
// If `current_age` is `None`, this returns `None`.
11+
// If `current_age` is `Some`, the inner `u8` gets assigned to `next_age`
912
let next_age: u8 = current_age?;
1013
Some(format!("Next year I will be {}", next_age))
1114
}
1215
```
1316

14-
You can chain many ?s together to make your code much more readable.
17+
You can chain many `?`s together to make your code much more readable.
1518

1619
```rust,editable
1720
struct Person {
@@ -30,10 +33,10 @@ struct PhoneNumber {
3033
}
3134
3235
impl Person {
33-
36+
3437
// Gets the area code of the phone number of the person's job, if it exists.
3538
fn work_phone_area_code(&self) -> Option<u8> {
36-
// This would need many nested `match` statements without the ? operator.
39+
// This would need many nested `match` statements without the `?` operator.
3740
// It would take a lot more code - try writing it yourself and see which
3841
// is easier.
3942
self.job?.phone_number?.area_code
@@ -52,4 +55,4 @@ fn main() {
5255
5356
assert_eq!(p.work_phone_area_code(), Some(61));
5457
}
55-
```
58+
```

src/testing/unit_testing.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,10 @@ failures:
7070
test result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out
7171
```
7272

73-
## Tests and ?
74-
None of the previous unit test examples had a return type. But in Rust 2018, your unit tests can return Result<()>, which lets you use `?` in them! This can make them much more concise.
73+
## Tests and `?`
74+
None of the previous unit test examples had a return type. But in Rust 2018,
75+
your unit tests can return `Result<()>`, which lets you use `?` in them! This
76+
can make them much more concise.
7577

7678
```rust,editable
7779
fn sqrt(number: f64) -> Result<f64, String> {
@@ -95,7 +97,7 @@ mod tests {
9597
}
9698
```
9799

98-
See [The Edition Guide][editionguide] for more details.
100+
See ["The Edition Guide"][editionguide] for more details.
99101

100102
## Testing panics
101103

src/trait/impl_trait.md

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
# impl Trait
1+
# `impl Trait`
22

3-
If your function returns a type that implements `MyTrait`, you can write its return type as `-> impl MyTrait`. This can help simplify your type signatures quite a lot!
3+
If your function returns a type that implements `MyTrait`, you can write its
4+
return type as `-> impl MyTrait`. This can help simplify your type signatures quite a lot!
45

56
```rust,editable
67
use std::iter;
78
use std::vec::IntoIter;
89
9-
// This function combines two Vec<i32> and returns an iterator over it.
10+
// This function combines two `Vec<i32>` and returns an iterator over it.
1011
// Look how complicated its return type is!
1112
fn combine_vecs_explicit_return_type<'a>(
1213
v: Vec<i32>,
@@ -25,7 +26,10 @@ fn combine_vecs<'a>(
2526
}
2627
```
2728

28-
More importantly, some Rust types can't be written out. For example, every closure has its own unnamed concrete type. Before `impl Trait` syntax, you had to allocate on the heap in order to return a closure. But now you can do it all statically, like this:
29+
More importantly, some Rust types can't be written out. For example, every
30+
closure has its own unnamed concrete type. Before `impl Trait` syntax, you had
31+
to allocate on the heap in order to return a closure. But now you can do it all
32+
statically, like this:
2933

3034
```rust,editable
3135
// Returns a function that adds `y` to its input
@@ -40,7 +44,10 @@ fn main() {
4044
}
4145
```
4246

43-
You can also use `impl Trait` to return an iterator that uses `map` or `filter` closures! This makes using `map` and `filter` easier. Because closure types don't have names, you can't write out an explicit return type if your function returns iterators with closures. But with `impl Trait` you can do this easily:
47+
You can also use `impl Trait` to return an iterator that uses `map` or `filter`
48+
closures! This makes using `map` and `filter` easier. Because closure types don't
49+
have names, you can't write out an explicit return type if your function returns
50+
iterators with closures. But with `impl Trait` you can do this easily:
4451

4552
```rust,editable
4653
fn double_positives<'a>(numbers: &'a Vec<i32>) -> impl Iterator<Item = i32> + 'a {
@@ -49,4 +56,4 @@ fn double_positives<'a>(numbers: &'a Vec<i32>) -> impl Iterator<Item = i32> + 'a
4956
.filter(|x| x > &&0)
5057
.map(|x| x * 2)
5158
}
52-
```
59+
```

0 commit comments

Comments
 (0)