Expand description
§letclone
A procedural macro for convenient variable cloning in Rust.
§Overview
letclone provides a clone! macro that simplifies the common pattern of
cloning variables into new bindings. Instead of writing verbose let statements
with .clone() calls, you can use the concise clone! macro.
The macro is especially useful when working with closures that need to capture cloned values, as it reduces boilerplate code significantly.
§Notes
clone! transparently unwraps syn::Expr::Group and continues processing the
inner expression. This matters mostly for tokens produced by macro expansion,
where Group is an implementation detail rather than a user-written Rust
expression.
This is different from a source-level parenthesized expression such as (a)
or (a + b), which parses as syn::Expr::Paren and is not treated as a
supported clone target.
§Examples
§Basic Usage
use letclone::clone;
let original = String::from("hello");
clone!(original);
// Equivalent to: let original = original.clone();§Field Access
use letclone::clone;
struct Person {
name: String,
}
let person = Person { name: String::from("Alice") };
clone!(person.name);
// Equivalent to: let name = person.name.clone();
assert_eq!(name, "Alice");§Mutable Bindings
use letclone::clone;
let original = String::from("hello");
clone!(mut original);
original.push_str(" world");
assert_eq!(original, "hello world");§Multiple Expressions
use letclone::clone;
let a = String::from("a");
let b = String::from("b");
clone!(a, b);
// Equivalent to:
// let a = a.clone();
// let b = b.clone();§Usage in Closures
use letclone::clone;
let name = String::from("Alice");
let scores = vec![85, 90, 95];
let closure = {
clone!(name, scores);
move || {
println!("Name: {}, Scores: {:?}", name, scores);
}
};Macros§
- clone
- Generates
let var = expr.clone();statements for one or more expressions