Skip to content

Commit fbdf895

Browse files
hkBstgitbot
authored and
gitbot
committed
Update docs for impl keyword
1 parent 1523883 commit fbdf895

File tree

1 file changed

+34
-7
lines changed

1 file changed

+34
-7
lines changed

std/src/keyword_docs.rs

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

652652
#[doc(keyword = "impl")]
653653
//
654-
/// Implement some functionality for a type.
654+
/// Implementations of functionality for a type, or a type implementing some functionality.
655+
///
656+
/// There are two uses of the keyword `impl`:
657+
/// * An `impl` block is an item that is used to implement some functionality for a type.
658+
/// * An `impl Trait` in a type-position can be used to designate a type that implements a trait called `Trait`.
659+
///
660+
/// # Implementing Functionality for a Type
655661
///
656662
/// The `impl` keyword is primarily used to define implementations on types. Inherent
657663
/// implementations are standalone, while trait implementations are used to implement traits for
658664
/// types, or other traits.
659665
///
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
666+
/// An implementation consists of definitions of functions and consts. A function defined in an
667+
/// `impl` block can be standalone, meaning it would be called like `Vec::new()`. If the function
662668
/// 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()`.
669+
/// method-call syntax, a familiar feature to any object-oriented programmer, like `vec.len()`.
670+
///
671+
/// ## Inherent Implementations
664672
///
665673
/// ```rust
666674
/// struct Example {
@@ -680,6 +688,17 @@ mod if_keyword {}
680688
/// self.number
681689
/// }
682690
/// }
691+
/// ```
692+
///
693+
/// It matters little where an inherent implementation is defined;
694+
/// its functionality is in scope wherever its implementing type is.
695+
///
696+
/// ## Trait Implementations
697+
///
698+
/// ```rust
699+
/// struct Example {
700+
/// number: i32,
701+
/// }
683702
///
684703
/// trait Thingy {
685704
/// fn do_thingy(&self);
@@ -692,11 +711,19 @@ mod if_keyword {}
692711
/// }
693712
/// ```
694713
///
714+
/// It matters little where a trait implementation is defined;
715+
/// its functionality can be brought into scope by importing the trait it implements.
716+
///
695717
/// For more information on implementations, see the [Rust book][book1] or the [Reference].
696718
///
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.
719+
/// # Designating a Type that Implements Some Functionality
720+
///
721+
/// The other use of the `impl` keyword is in `impl Trait` syntax, which can be understood to mean
722+
/// "any (or some) concrete type that implements Trait".
723+
/// It can be used as the type of a variable declaration,
724+
/// in [argument position](https://rust-lang.github.io/rfcs/1951-expand-impl-trait.html)
725+
/// or in [return position](https://rust-lang.github.io/rfcs/3425-return-position-impl-trait-in-traits.html).
726+
/// One pertinent use case is in working with closures, which have unnameable types.
700727
///
701728
/// ```rust
702729
/// fn thing_returning_closure() -> impl Fn(i32) -> bool {

0 commit comments

Comments
 (0)