Skip to content

Commit 5443fa7

Browse files
committed
Update docs for impl keyword
1 parent 7f36543 commit 5443fa7

File tree

1 file changed

+31
-7
lines changed

1 file changed

+31
-7
lines changed

library/std/src/keyword_docs.rs

+31-7
Original file line numberDiff line numberDiff line change
@@ -651,16 +651,21 @@ mod if_keyword {}
651651

652652
#[doc(keyword = "impl")]
653653
//
654-
/// Implement some functionality for a type.
654+
/// * An `impl` block is an item that is used to implement some functionality for a type.
655+
/// * An `impl Trait` in a type-position can be used to designate a type that implements a trait called `Trait`.
656+
///
657+
/// # Implementing Functionality for a Type
655658
///
656659
/// The `impl` keyword is primarily used to define implementations on types. Inherent
657660
/// implementations are standalone, while trait implementations are used to implement traits for
658661
/// types, or other traits.
659662
///
660-
/// Functions and consts can both be defined in an implementation. A function defined in an
661-
/// `impl` block can be standalone, meaning it would be called like `Foo::bar()`. If the function
663+
/// An implementation consists of definitions of functions and consts. A function defined in an
664+
/// `impl` block can be standalone, meaning it would be called like `Vec::new()`. If the function
662665
/// takes `self`, `&self`, or `&mut self` as its first argument, it can also be called using
663-
/// method-call syntax, a familiar feature to any object oriented programmer, like `foo.bar()`.
666+
/// method-call syntax, a familiar feature to any object-oriented programmer, like `foo.bar()`.
667+
///
668+
/// ## Inherent Implementations
664669
///
665670
/// ```rust
666671
/// struct Example {
@@ -680,6 +685,17 @@ mod if_keyword {}
680685
/// self.number
681686
/// }
682687
/// }
688+
/// ```
689+
///
690+
/// It matters little where an inherent implementation is defined;
691+
/// its functionality is in scope wherever its implementing type is.
692+
///
693+
/// ## Trait Implementations
694+
///
695+
/// ```rust
696+
/// struct Example {
697+
/// number: i32,
698+
/// }
683699
///
684700
/// trait Thingy {
685701
/// fn do_thingy(&self);
@@ -692,11 +708,19 @@ mod if_keyword {}
692708
/// }
693709
/// ```
694710
///
711+
/// It matters little where a trait implementation is defined;
712+
/// its functionality can be brought into scope by importing the trait it implements.
713+
///
695714
/// For more information on implementations, see the [Rust book][book1] or the [Reference].
696715
///
697-
/// The other use of the `impl` keyword is in `impl Trait` syntax, which can be seen as a shorthand
698-
/// for "a concrete type that implements this trait". Its primary use is working with closures,
699-
/// which have type definitions generated at compile time that can't be simply typed out.
716+
/// # Designating a Type that Implements Some Functionality
717+
///
718+
/// The other use of the `impl` keyword is in `impl Trait` syntax, which can be understood to mean
719+
/// "any (or some) concrete type that implements Trait".
720+
/// It can be used as the type of a variable declaration,
721+
/// in [argument position](https://rust-lang.github.io/rfcs/1951-expand-impl-trait.html)
722+
/// or in [return position](https://rust-lang.github.io/rfcs/3425-return-position-impl-trait-in-traits.html).
723+
/// One pertinent use case is in working with closures, which have unnameable types.
700724
///
701725
/// ```rust
702726
/// fn thing_returning_closure() -> impl Fn(i32) -> bool {

0 commit comments

Comments
 (0)