[][src]Macro shadow_clone::shadow_clone

macro_rules! shadow_clone {
    { $to_clone:ident, $($tt:tt)* } => { ... };
    { mut $to_clone:ident, $($tt:tt)* } => { ... };
    { (mut) $to_clone:ident, $($tt:tt)* } => { ... };
    { $to_clone:ident } => { ... };
    { mut $to_clone:ident } => { ... };
    { (mut) $to_clone:ident } => { ... };
    () => { ... };
}

Use this macro to clone variables into the current scope shadowing old ones.

Examples

This example deliberately fails to compile
let s = "foo".to_string();
let c = move |x: i32| format!("{}{}", s, x);
let bar = s;

This will not compile as s has been moved into the closure.

This issue can be solved with this macro.

use shadow_clone::shadow_clone;
let s = "foo".to_string();
{
    shadow_clone!(s);
    let c = move |x: i32| format!("{}{}", s, x);
}
let bar = s;

That expands to,

use shadow_clone::shadow_clone;
let s = "foo".to_string();
{
    let s = s.clone();
    let c = move |x: i32| format!("{}{}", s, x);
}
let bar = s;

You can also clone multiple variables separated by commas. shadow_clone!(foo, bar);

You can also bind a clone as mutable by prefixing with mut. shadow_clone!(mut foo);