Skip to content

Commit 89d8979

Browse files
authored
Rollup merge of #72761 - poliorcetics:use-keyword-doc, r=Dylan-DPC
Added the documentation for the 'use' keyword This is a partial fix of #34601. I heavily inspired myself from the Reference on the `use` keyword. I checked the links when compiling the documentation, they should be ok. I also added an example for the wildcard `*` in the case of types, because it's behaviour is not *import everything* like one might think at first.
2 parents e8bb4c7 + 4bae9e5 commit 89d8979

File tree

1 file changed

+54
-2
lines changed

1 file changed

+54
-2
lines changed

src/libstd/keyword_docs.rs

+54-2
Original file line numberDiff line numberDiff line change
@@ -1213,9 +1213,61 @@ mod unsafe_keyword {}
12131213
//
12141214
/// Import or rename items from other crates or modules.
12151215
///
1216-
/// The documentation for this keyword is [not yet complete]. Pull requests welcome!
1216+
/// Usually a `use` keyword is used to shorten the path required to refer to a module item.
1217+
/// The keyword may appear in modules, blocks and even functions, usually at the top.
1218+
///
1219+
/// The most basic usage of the keyword is `use path::to::item;`,
1220+
/// though a number of convenient shortcuts are supported:
1221+
///
1222+
/// * Simultaneously binding a list of paths with a common prefix,
1223+
/// using the glob-like brace syntax `use a::b::{c, d, e::f, g::h::i};`
1224+
/// * Simultaneously binding a list of paths with a common prefix and their common parent module,
1225+
/// using the [`self`] keyword, such as `use a::b::{self, c, d::e};`
1226+
/// * Rebinding the target name as a new local name, using the syntax `use p::q::r as x;`.
1227+
/// This can also be used with the last two features: `use a::b::{self as ab, c as abc}`.
1228+
/// * Binding all paths matching a given prefix,
1229+
/// using the asterisk wildcard syntax `use a::b::*;`.
1230+
/// * Nesting groups of the previous features multiple times,
1231+
/// such as `use a::b::{self as ab, c, d::{*, e::f}};`
1232+
/// * Reexporting with visibility modifiers such as `pub use a::b;`
1233+
/// * Importing with `_` to only import the methods of a trait without binding it to a name
1234+
/// (to avoid conflict for example): `use ::std::io::Read as _;`.
1235+
///
1236+
/// Using path qualifiers like [`crate`], [`super`] or [`self`] is supported: `use crate::a::b;`.
1237+
///
1238+
/// Note that when the wildcard `*` is used on a type, it does not import its methods (though
1239+
/// for `enum`s it imports the variants, as shown in the example below).
1240+
///
1241+
/// ```compile_fail,edition2018
1242+
/// enum ExampleEnum {
1243+
/// VariantA,
1244+
/// VariantB,
1245+
/// }
12171246
///
1218-
/// [not yet complete]: https://github.com/rust-lang/rust/issues/34601
1247+
/// impl ExampleEnum {
1248+
/// fn new() -> Self {
1249+
/// Self::VariantA
1250+
/// }
1251+
/// }
1252+
///
1253+
/// use ExampleEnum::*;
1254+
///
1255+
/// // Compiles.
1256+
/// let _ = VariantA;
1257+
///
1258+
/// // Does not compile !
1259+
/// let n = new();
1260+
/// ```
1261+
///
1262+
/// For more information on `use` and paths in general, see the [Reference].
1263+
///
1264+
/// The differences about paths and the `use` keyword between the 2015 and 2018 editions
1265+
/// can also be found in the [Reference].
1266+
///
1267+
/// [`crate`]: keyword.crate.html
1268+
/// [`self`]: keyword.self.html
1269+
/// [`super`]: keyword.super.html
1270+
/// [Reference]: ../reference/items/use-declarations.html
12191271
mod use_keyword {}
12201272

12211273
#[doc(keyword = "where")]

0 commit comments

Comments
 (0)