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 +```