diff options
| author | Manish Goregaokar <manishsmail@gmail.com> | 2020-07-01 07:42:54 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-07-01 07:42:54 -0700 |
| commit | 178b0c2e9954ede56d552f8a625ef59524ab3f60 (patch) | |
| tree | d18177c710cff2bf230a59591842769591e7bd7a /src/libstd | |
| parent | ec41d01d4f7c668a75eff1ba724b8a67c7ec90b4 (diff) | |
| parent | e611a3fb8423f178e856813fc1a1f2397980bd8a (diff) | |
| download | rust-178b0c2e9954ede56d552f8a625ef59524ab3f60.tar.gz rust-178b0c2e9954ede56d552f8a625ef59524ab3f60.zip | |
Rollup merge of #73805 - poliorcetics:type-keyword, r=kennytm
Document the type keyword Partial fix of #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
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/keyword_docs.rs | 39 |
1 files changed, 37 insertions, 2 deletions
diff --git a/src/libstd/keyword_docs.rs b/src/libstd/keyword_docs.rs index d0716c5205d..0b3386c05d5 100644 --- a/src/libstd/keyword_docs.rs +++ b/src/libstd/keyword_docs.rs @@ -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")] |
