Skip to content

Commit c18c0b8

Browse files
committed
Add examples in stdlib demonstrating the use syntax
1 parent 3bb20e3 commit c18c0b8

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

library/core/src/marker.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,26 @@ impl<T: ?Sized> Copy for &T {}
465465
///
466466
/// The `UseCloned` trait does not provide a method; instead, it indicates that
467467
/// `Clone::clone` is lightweight, and allows the use of the `.use` syntax.
468+
///
469+
/// ## .use postfix syntax
470+
///
471+
/// Values can be `.use`d by adding `.use` postfix to the value you want to use.
472+
///
473+
/// ```
474+
/// fn foo(f: Foo) {
475+
/// // if `Foo` implements `Copy` f would be copied into x.
476+
/// // if `Foo` implements `UseCloned` f would be cloned into x.
477+
/// // otherwise f would be moved into x.
478+
/// // FIXME: change this line to f.use when this lands.
479+
/// let x = f.r#use;
480+
/// // ...
481+
/// }
482+
/// ```
483+
///
484+
/// ## use closures
485+
///
486+
/// Use closures allow captured values to be automatically used.
487+
/// This is similar to have a closure that you would call `.use` over each captured value.
468488
#[unstable(feature = "ergonomic_clones", issue = "132290")]
469489
#[cfg_attr(not(bootstrap), lang = "use_cloned")]
470490
#[marker]

library/std/src/keyword_docs.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2121,8 +2121,8 @@ mod unsafe_keyword {}
21212121

21222122
#[doc(keyword = "use")]
21232123
//
2124-
/// Import or rename items from other crates or modules, or specify precise capturing
2125-
/// with `use<..>`.
2124+
/// Import or rename items from other crates or modules, use values under ergonomic clones
2125+
/// semantic, or specify precise capturing with `use<..>`.
21262126
///
21272127
/// ## Importing items
21282128
///
@@ -2177,6 +2177,11 @@ mod unsafe_keyword {}
21772177
/// The differences about paths and the `use` keyword between the 2015 and 2018 editions
21782178
/// can also be found in the [Reference][ref-use-decls].
21792179
///
2180+
/// ## Ergonomic clones
2181+
///
2182+
/// Use a values, copying its content if the value implements `Copy`, cloning the contents if the
2183+
/// value implements `UseCloned` or moving it otherwise.
2184+
///
21802185
/// ## Precise capturing
21812186
///
21822187
/// The `use<..>` syntax is used within certain `impl Trait` bounds to control which generic

0 commit comments

Comments
 (0)