Skip to content

Commit

Permalink
Rollup merge of rust-lang#73805 - poliorcetics:type-keyword, r=kennytm
Browse files Browse the repository at this point in the history
Document the type keyword

Partial fix of rust-lang#34601.

Two small examples, one clarifying that `type` only defines an alias, not a completely new type, the other explaining the use in traits.

@rustbot modify labels: T-doc,C-enhancement
  • Loading branch information
Manishearth committed Jun 30, 2020
2 parents fc12919 + e611a3f commit f180d3d
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions src/libstd/keyword_docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1536,9 +1536,44 @@ mod true_keyword {}
//
/// Define an alias for an existing type.
///
/// The documentation for this keyword is [not yet complete]. Pull requests welcome!
/// The syntax is `type Name = ExistingType;`.
///
/// [not yet complete]: https://github.com/rust-lang/rust/issues/34601
/// # Examples
///
/// `type` does **not** create a new type:
///
/// ```rust
/// type Meters = u32;
/// type Kilograms = u32;
///
/// let m: Meters = 3;
/// let k: Kilograms = 3;
///
/// assert_eq!(m, k);
/// ```
///
/// In traits, `type` is used to declare an [associated type]:
///
/// ```rust
/// trait Iterator {
/// // associated type declaration
/// type Item;
/// fn next(&mut self) -> Option<Self::Item>;
/// }
///
/// struct Once<T>(Option<T>);
///
/// impl<T> Iterator for Once<T> {
/// // associated type definition
/// type Item = T;
/// fn next(&mut self) -> Option<Self::Item> {
/// self.0.take()
/// }
/// }
/// ```
///
/// [`trait`]: keyword.trait.html
/// [associated type]: ../reference/items/associated-items.html#associated-types
mod type_keyword {}

#[doc(keyword = "unsafe")]
Expand Down

0 comments on commit f180d3d

Please sign in to comment.