Skip to content

Commit e643cd0

Browse files
committed
Add examples in stdlib demonstrating the use syntax
1 parent c85e723 commit e643cd0

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

library/core/src/clone.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,25 @@ pub macro Clone($item:item) {
196196
///
197197
/// The `UseCloned` trait does not provide a method; instead, it indicates that
198198
/// `Clone::clone` is lightweight, and allows the use of the `.use` syntax.
199+
///
200+
/// ## .use postfix syntax
201+
///
202+
/// Values can be `.use`d by adding `.use` postfix to the value you want to use.
203+
///
204+
/// ```ignore (this won't work until we land use)
205+
/// fn foo(f: Foo) {
206+
/// // if `Foo` implements `Copy` f would be copied into x.
207+
/// // if `Foo` implements `UseCloned` f would be cloned into x.
208+
/// // otherwise f would be moved into x.
209+
/// let x = f.use;
210+
/// // ...
211+
/// }
212+
/// ```
213+
///
214+
/// ## use closures
215+
///
216+
/// Use closures allow captured values to be automatically used.
217+
/// This is similar to have a closure that you would call `.use` over each captured value.
199218
#[unstable(feature = "ergonomic_clones", issue = "132290")]
200219
#[cfg_attr(not(bootstrap), lang = "use_cloned")]
201220
pub trait UseCloned: Clone {

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)