Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update docs for impl keyword #136354

Merged
merged 1 commit into from
Feb 12, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 34 additions & 7 deletions library/std/src/keyword_docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -651,16 +651,24 @@ mod if_keyword {}

#[doc(keyword = "impl")]
//
/// Implement some functionality for a type.
/// Implementations of functionality for a type, or a type implementing some functionality.
///
/// There are two uses of the keyword `impl`:
/// * An `impl` block is an item that is used to implement some functionality for a type.
/// * An `impl Trait` in a type-position can be used to designate a type that implements a trait called `Trait`.
///
/// # Implementing Functionality for a Type
///
/// The `impl` keyword is primarily used to define implementations on types. Inherent
/// implementations are standalone, while trait implementations are used to implement traits for
/// types, or other traits.
///
/// Functions and consts can both be defined in an implementation. A function defined in an
/// `impl` block can be standalone, meaning it would be called like `Foo::bar()`. If the function
/// An implementation consists of definitions of functions and consts. A function defined in an
/// `impl` block can be standalone, meaning it would be called like `Vec::new()`. If the function
/// takes `self`, `&self`, or `&mut self` as its first argument, it can also be called using
/// method-call syntax, a familiar feature to any object oriented programmer, like `foo.bar()`.
/// method-call syntax, a familiar feature to any object-oriented programmer, like `vec.len()`.
///
/// ## Inherent Implementations
///
/// ```rust
/// struct Example {
Expand All @@ -680,6 +688,17 @@ mod if_keyword {}
/// self.number
/// }
/// }
/// ```
///
/// It matters little where an inherent implementation is defined;
/// its functionality is in scope wherever its implementing type is.
///
/// ## Trait Implementations
///
/// ```rust
/// struct Example {
/// number: i32,
/// }
///
/// trait Thingy {
/// fn do_thingy(&self);
Expand All @@ -692,11 +711,19 @@ mod if_keyword {}
/// }
/// ```
///
/// It matters little where a trait implementation is defined;
/// its functionality can be brought into scope by importing the trait it implements.
///
/// For more information on implementations, see the [Rust book][book1] or the [Reference].
///
/// The other use of the `impl` keyword is in `impl Trait` syntax, which can be seen as a shorthand
/// for "a concrete type that implements this trait". Its primary use is working with closures,
/// which have type definitions generated at compile time that can't be simply typed out.
/// # Designating a Type that Implements Some Functionality
///
/// The other use of the `impl` keyword is in `impl Trait` syntax, which can be understood to mean
/// "any (or some) concrete type that implements Trait".
/// It can be used as the type of a variable declaration,
/// in [argument position](https://rust-lang.github.io/rfcs/1951-expand-impl-trait.html)
/// or in [return position](https://rust-lang.github.io/rfcs/3425-return-position-impl-trait-in-traits.html).
/// One pertinent use case is in working with closures, which have unnameable types.
///
/// ```rust
/// fn thing_returning_closure() -> impl Fn(i32) -> bool {
Expand Down
Loading