From 4bfa22726ef25c08a4a340a396aa085ba042968f Mon Sep 17 00:00:00 2001 From: funkill2 Date: Thu, 12 Sep 2019 12:18:41 +0300 Subject: [PATCH 1/3] mark some elements as inline code --- src/SUMMARY.md | 2 +- src/error/option_unwrap/question_mark.md | 19 +++++++++++-------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 9ddb31e736..a271438b92 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -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) diff --git a/src/error/option_unwrap/question_mark.md b/src/error/option_unwrap/question_mark.md index 7cff868ad6..7437ff6c84 100644 --- a/src/error/option_unwrap/question_mark.md +++ b/src/error/option_unwrap/question_mark.md @@ -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) -> Option { - // 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 { @@ -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 { - // 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 @@ -52,4 +55,4 @@ fn main() { assert_eq!(p.work_phone_area_code(), Some(61)); } -``` \ No newline at end of file +``` From ba3bba823918c5c1183bea848c41ceefc0da0e9f Mon Sep 17 00:00:00 2001 From: funkill2 Date: Thu, 12 Sep 2019 12:40:33 +0300 Subject: [PATCH 2/3] added quotes in unit_testing --- src/testing/unit_testing.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/testing/unit_testing.md b/src/testing/unit_testing.md index a2965d998e..cd87706640 100644 --- a/src/testing/unit_testing.md +++ b/src/testing/unit_testing.md @@ -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 { @@ -95,7 +97,7 @@ mod tests { } ``` -See [The Edition Guide][editionguide] for more details. +See ["The Edition Guide"][editionguide] for more details. ## Testing panics From ce28351f7a93d0386dcb1208a128ceb1a9989221 Mon Sep 17 00:00:00 2001 From: funkill2 Date: Thu, 12 Sep 2019 12:42:35 +0300 Subject: [PATCH 3/3] added quotes in impl_trait --- src/SUMMARY.md | 2 +- src/trait/impl_trait.md | 19 +++++++++++++------ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/SUMMARY.md b/src/SUMMARY.md index a271438b92..f496410d4a 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -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) diff --git a/src/trait/impl_trait.md b/src/trait/impl_trait.md index 2ad3795475..601aaa3b64 100644 --- a/src/trait/impl_trait.md +++ b/src/trait/impl_trait.md @@ -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 and returns an iterator over it. +// This function combines two `Vec` and returns an iterator over it. // Look how complicated its return type is! fn combine_vecs_explicit_return_type<'a>( v: Vec, @@ -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 @@ -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) -> impl Iterator + 'a { @@ -49,4 +56,4 @@ fn double_positives<'a>(numbers: &'a Vec) -> impl Iterator + 'a .filter(|x| x > &&0) .map(|x| x * 2) } -``` \ No newline at end of file +```