Crates.io | shadow-clone |
lib.rs | shadow-clone |
version | 1.2.1 |
source | src |
created_at | 2019-06-06 09:44:58.59268 |
updated_at | 2020-04-19 12:20:31.095236 |
description | A macro to clone variables into the current scope shadowing old ones. |
homepage | |
repository | https://gitlab.com/efunb/shadow-clone |
max_upload_size | |
id | 139342 |
size | 8,424 |
A macro to clone variables into the current scope shadowing old ones.
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.
Add
shadow-clone = "1"
to your cargo.toml
under [dependencies]
and add
use shadow_clone::shadow_clone;
to your main file.
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);
.
If you are viewing this from GitHub then this is a read only copy. Please contribute to the GitLab copy here.