UseCloned in std::clone - Rust (original) (raw)

pub trait UseCloned: Clone { }

🔬This is a nightly-only experimental API. (ergonomic_clones #132290)

Expand description

Trait for objects whose Clone impl is lightweight (e.g. reference-counted)

Cloning an object implementing this trait should in general:

The UseCloned trait does not provide a method; instead, it indicates thatClone::clone is lightweight, and allows the use of the .use syntax.

§.use postfix syntax

Values can be .used by adding .use postfix to the value you want to use.

ⓘ

fn foo(f: Foo) {
    // if `Foo` implements `Copy` f would be copied into x.
    // if `Foo` implements `UseCloned` f would be cloned into x.
    // otherwise f would be moved into x.
    let x = f.use;
    // ...
}

§use closures

Use closures allow captured values to be automatically used. This is similar to have a closure that you would call .use over each captured value.

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.