macro_rules! clone { ($(,)?) => { ... }; ($scope:ident . $name:ident $(, $($tail:tt)*)?) => { ... }; (mut $scope:ident . $name:ident $(, $($tail:tt)*)?) => { ... }; ($name:ident $(, $($tail:tt)*)?) => { ... }; (mut $name:ident $(, $($tail:tt)*)?) => { ... }; }
Expand description
Clone all the identifiers supplied as arguments.
clone!(x, y, z);
will generate:
let x = x.clone();
let y = y.clone();
let z = z.clone();
This is useful for capturing variables by copy in closures. For example:
let closure = {
clone!(x, y, z);
move || {}
};
If you need a mutable clone, clone!(mut x)
will generate:
let mut x = x.clone();
You can also clone struct members:
struct MyStruct {
x: i32
}
impl MyStruct {
fn clone_x(&self) {
// This will generate `let x = self.x.clone();`
clone!(self.x);
}
}