Skip to content

Commit 738e58d

Browse files
committed
Document impl keyword
This commit also splits out linky-line-thingies into two lines, which judging from the source code for tidy, should be enough to make it shut up and accept me for who I am, dammit.
1 parent 5393b27 commit 738e58d

File tree

1 file changed

+71
-5
lines changed

1 file changed

+71
-5
lines changed

src/libstd/keyword_docs.rs

Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
///
3636
/// For more information on what `as` is capable of, see the [Reference]
3737
///
38-
/// [Reference]: https://doc.rust-lang.org/reference/expressions/operator-expr.html#type-cast-expressions
38+
/// [Reference]:
39+
/// https://doc.rust-lang.org/reference/expressions/operator-expr.html#type-cast-expressions
3940
/// [`crate`]: keyword.crate.html
4041
/// [`use`]: keyword.use.html
4142
mod as_keyword { }
@@ -90,7 +91,8 @@ mod as_keyword { }
9091
///
9192
/// [`static`]: keyword.static.html
9293
/// [pointer]: primitive.pointer.html
93-
/// [Rust Book]: https://doc.rust-lang.org/stable/book/2018-edition/ch03-01-variables-and-mutability.html#differences-between-variables-and-constants
94+
/// [Rust Book]:
95+
/// https://doc.rust-lang.org/stable/book/2018-edition/ch03-01-variables-and-mutability.html#differences-between-variables-and-constants
9496
/// [Reference]: https://doc.rust-lang.org/reference/items/constant-items.html
9597
mod const_keyword { }
9698

@@ -221,7 +223,8 @@ mod enum_keyword { }
221223
///
222224
/// For more information on FFI, check the [Rust book] or the [Reference].
223225
///
224-
/// [Rust book]: https://doc.rust-lang.org/book/second-edition/ch19-01-unsafe-rust.html#using-extern-functions-to-call-external-code
226+
/// [Rust book]:
227+
/// https://doc.rust-lang.org/book/second-edition/ch19-01-unsafe-rust.html#using-extern-functions-to-call-external-code
225228
/// [Reference]: https://doc.rust-lang.org/reference/items/external-blocks.html
226229
mod extern_keyword { }
227230

@@ -364,7 +367,8 @@ mod fn_keyword { }
364367
/// [`impl`]: keyword.impl.html
365368
/// [`break`]: keyword.break.html
366369
/// [`IntoIterator`]: iter/trait.IntoIterator.html
367-
/// [Rust book]: https://doc.rust-lang.org/book/2018-edition/ch03-05-control-flow.html#looping-through-a-collection-with-for
370+
/// [Rust book]:
371+
/// https://doc.rust-lang.org/book/2018-edition/ch03-05-control-flow.html#looping-through-a-collection-with-for
368372
/// [Reference]: https://doc.rust-lang.org/reference/expressions/loop-expr.html#iterator-loops
369373
mod for_keyword { }
370374

@@ -442,10 +446,72 @@ mod for_keyword { }
442446
///
443447
/// [`match`]: keyword.match.html
444448
/// [`let`]: keyword.let.html
445-
/// [Rust book]: https://doc.rust-lang.org/stable/book/2018-edition/ch03-05-control-flow.html#if-expressions
449+
/// [Rust book]:
450+
/// https://doc.rust-lang.org/stable/book/2018-edition/ch03-05-control-flow.html#if-expressions
446451
/// [Reference]: https://doc.rust-lang.org/reference/expressions/if-expr.html
447452
mod if_keyword { }
448453

454+
#[doc(keyword = "impl")]
455+
//
456+
/// The implementation-defining keyword.
457+
///
458+
/// The `impl` keyword is primarily used for defining implementations on types. There are two kinds
459+
/// of implementations: Inherent implementations and trait implementations. Inherent
460+
/// implementations define functions that operate on a type, known in object-oriented languages as
461+
/// methods. Trait implementations are used to give a type a trait, and implement any of the
462+
/// required associated items or methods that it requires.
463+
///
464+
/// ```rust
465+
/// struct Example {
466+
/// number: i32,
467+
/// }
468+
///
469+
/// impl Example {
470+
/// fn boo() {
471+
/// println!("boo! Example::boo() was called!");
472+
/// }
473+
///
474+
/// fn answer(&mut self) {
475+
/// self.number += 42;
476+
/// }
477+
///
478+
/// fn get_number(&self) -> i32 {
479+
/// self.number
480+
/// }
481+
/// }
482+
///
483+
/// trait Thingy {
484+
/// fn do_thingy(&self);
485+
/// }
486+
///
487+
/// impl Thingy for Example {
488+
/// fn do_thingy(&self) {
489+
/// println!("doing a thing! also, number is {}!", self.number);
490+
/// }
491+
/// }
492+
/// ```
493+
///
494+
/// For more information on implementations, see the [Rust book][book1] or the [Reference].
495+
///
496+
/// The other use of the `impl` keyword is in `impl Trait` syntax, which can be seen as a shorthand
497+
/// for "a concrete type that implements this trait". Its primary use is working with closures,
498+
/// which have type definitions generated at compile time that can't be simply typed out.
499+
///
500+
/// ```rust
501+
/// fn thing_returning_closure() -> impl Fn(i32) -> bool {
502+
/// println!("here's a closure for you!");
503+
/// |x: i32| x % 3 == 0
504+
/// }
505+
/// ```
506+
///
507+
/// For more information on `impl Trait` syntax, see the [Rust book][book2].
508+
///
509+
/// [book1]: https://doc.rust-lang.org/stable/book/2018-edition/ch05-03-method-syntax.html
510+
/// [Reference]: https://doc.rust-lang.org/reference/items/implementations.html
511+
/// [book2]:
512+
/// https://doc.rust-lang.org/stable/book/2018-edition/ch10-02-traits.html#returning-traits
513+
mod impl_keyword { }
514+
449515
#[doc(keyword = "let")]
450516
//
451517
/// The `let` keyword.

0 commit comments

Comments
 (0)