#macro #simple #development-tools #no-std

no-std shadow-clone

A macro to clone variables into the current scope shadowing old ones

7 releases (4 stable)

1.2.1 Apr 19, 2020
1.2.0 Apr 18, 2020
0.1.1 Jun 6, 2019
0.0.1 Jun 6, 2019

#1569 in Rust patterns

Download history 874/week @ 2024-09-05 727/week @ 2024-09-12 450/week @ 2024-09-19 710/week @ 2024-09-26 364/week @ 2024-10-03 367/week @ 2024-10-10 839/week @ 2024-10-17 711/week @ 2024-10-24 685/week @ 2024-10-31 725/week @ 2024-11-07 582/week @ 2024-11-14 811/week @ 2024-11-21 531/week @ 2024-11-28 702/week @ 2024-12-05 803/week @ 2024-12-12 476/week @ 2024-12-19

2,588 downloads per month
Used in 2 crates

MIT/Apache

6KB

Shadow Clone

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

pipeline status Latest version Latest Docs License downloads-badge

Help

If you run into any issues or need help with using shadow-clone in your project please email incoming+efunb-shadow-clone-12722979-issue-@incoming.gitlab.com.

How to use

Add

shadow-clone = "1"

to your cargo.toml under [dependencies] and add

use shadow_clone::shadow_clone;

to your main file.

Examples

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);.

Docs

API Documentation

Warning

If you are viewing this from GitHub then this is a read only copy. Please contribute to the GitLab copy here.

No runtime deps