Skip to content

Commit fd5f4ec

Browse files
authored
Rollup merge of rust-lang#73805 - poliorcetics:type-keyword, r=kennytm
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
2 parents 6b2f6c4 + e611a3f commit fd5f4ec

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

src/libstd/keyword_docs.rs

+37-2
Original file line numberDiff line numberDiff line change
@@ -1536,9 +1536,44 @@ mod true_keyword {}
15361536
//
15371537
/// Define an alias for an existing type.
15381538
///
1539-
/// The documentation for this keyword is [not yet complete]. Pull requests welcome!
1539+
/// The syntax is `type Name = ExistingType;`.
15401540
///
1541-
/// [not yet complete]: https://github.com/rust-lang/rust/issues/34601
1541+
/// # Examples
1542+
///
1543+
/// `type` does **not** create a new type:
1544+
///
1545+
/// ```rust
1546+
/// type Meters = u32;
1547+
/// type Kilograms = u32;
1548+
///
1549+
/// let m: Meters = 3;
1550+
/// let k: Kilograms = 3;
1551+
///
1552+
/// assert_eq!(m, k);
1553+
/// ```
1554+
///
1555+
/// In traits, `type` is used to declare an [associated type]:
1556+
///
1557+
/// ```rust
1558+
/// trait Iterator {
1559+
/// // associated type declaration
1560+
/// type Item;
1561+
/// fn next(&mut self) -> Option<Self::Item>;
1562+
/// }
1563+
///
1564+
/// struct Once<T>(Option<T>);
1565+
///
1566+
/// impl<T> Iterator for Once<T> {
1567+
/// // associated type definition
1568+
/// type Item = T;
1569+
/// fn next(&mut self) -> Option<Self::Item> {
1570+
/// self.0.take()
1571+
/// }
1572+
/// }
1573+
/// ```
1574+
///
1575+
/// [`trait`]: keyword.trait.html
1576+
/// [associated type]: ../reference/items/associated-items.html#associated-types
15421577
mod type_keyword {}
15431578

15441579
#[doc(keyword = "unsafe")]

0 commit comments

Comments
 (0)