Skip to content

Commit

Permalink
Merge pull request #1254 from funkill/patch-1
Browse files Browse the repository at this point in the history
Inline code in some new/changed chapters
  • Loading branch information
marioidival authored Sep 12, 2019
2 parents 127e7dc + ce28351 commit dbcdccf
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@
- [Operator Overloading](trait/ops.md)
- [Drop](trait/drop.md)
- [Iterators](trait/iter.md)
- [impl Trait](trait/impl_trait.md)
- [`impl Trait`](trait/impl_trait.md)
- [Clone](trait/clone.md)

- [macro_rules!](macros.md)
Expand All @@ -152,7 +152,7 @@
- [Error handling](error.md)
- [`panic`](error/panic.md)
- [`Option` & `unwrap`](error/option_unwrap.md)
- [Unpacking options with ?](error/option_unwrap/question_mark.md)
- [Unpacking options with `?`](error/option_unwrap/question_mark.md)
- [Combinators: `map`](error/option_unwrap/map.md)
- [Combinators: `and_then`](error/option_unwrap/and_then.md)
- [`Result`](error/result.md)
Expand Down
19 changes: 11 additions & 8 deletions src/error/option_unwrap/question_mark.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
# Unpacking options with ?
# Unpacking options with `?`

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`.
You can unpack `Option`s 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`.

```rust,editable
fn next_birthday(current_age: Option<u8>) -> Option<String> {
// If `current_age` is None, this returns None.
// If `current_age` is Some, the inner u8 gets assigned to `next_age`
// If `current_age` is `None`, this returns `None`.
// If `current_age` is `Some`, the inner `u8` gets assigned to `next_age`
let next_age: u8 = current_age?;
Some(format!("Next year I will be {}", next_age))
}
```

You can chain many ?s together to make your code much more readable.
You can chain many `?`s together to make your code much more readable.

```rust,editable
struct Person {
Expand All @@ -30,10 +33,10 @@ struct PhoneNumber {
}
impl Person {
// Gets the area code of the phone number of the person's job, if it exists.
fn work_phone_area_code(&self) -> Option<u8> {
// This would need many nested `match` statements without the ? operator.
// This would need many nested `match` statements without the `?` operator.
// It would take a lot more code - try writing it yourself and see which
// is easier.
self.job?.phone_number?.area_code
Expand All @@ -52,4 +55,4 @@ fn main() {
assert_eq!(p.work_phone_area_code(), Some(61));
}
```
```
8 changes: 5 additions & 3 deletions src/testing/unit_testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ failures:
test result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out
```

## Tests and ?
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.
## Tests and `?`
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.

```rust,editable
fn sqrt(number: f64) -> Result<f64, String> {
Expand All @@ -95,7 +97,7 @@ mod tests {
}
```

See [The Edition Guide][editionguide] for more details.
See ["The Edition Guide"][editionguide] for more details.

## Testing panics

Expand Down
19 changes: 13 additions & 6 deletions src/trait/impl_trait.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# impl Trait
# `impl Trait`

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!
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!

```rust,editable
use std::iter;
use std::vec::IntoIter;
// This function combines two Vec<i32> and returns an iterator over it.
// This function combines two `Vec<i32>` and returns an iterator over it.
// Look how complicated its return type is!
fn combine_vecs_explicit_return_type<'a>(
v: Vec<i32>,
Expand All @@ -25,7 +26,10 @@ fn combine_vecs<'a>(
}
```

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:
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:

```rust,editable
// Returns a function that adds `y` to its input
Expand All @@ -40,7 +44,10 @@ fn main() {
}
```

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:
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:

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

0 comments on commit dbcdccf

Please sign in to comment.