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.
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
Usage
Basic Usage
use clone;
let original = Stringfrom;
clone!;
// Equivalent to: let original = original.clone();
Field Access
use clone;
let person = Person ;
clone!;
// Equivalent to: let name = person.name.clone();
assert_eq!;
Method Call
use clone;
let container = Container ;
clone!;
// Equivalent to: let get_value = container.get_value().clone();
assert_eq!;
Mutable Bindings
Use the mut modifier to create mutable bindings:
use clone;
let original = Stringfrom;
clone!;
// Equivalent to: let mut original = original.clone();
original.push_str;
assert_eq!;
Multiple Expressions
Clone multiple variables in a single macro call:
use clone;
let data = Data ;
let var = Stringfrom;
clone!;
// Equivalent to:
// let field1 = data.field1.clone();
// let field2 = data.field2.clone();
// let var = var.clone();
assert_eq!;
assert_eq!;
assert_eq!;
Supported Expression Types
| Expression Type | Example | Expands To |
|---|---|---|
| Path/Variable | clone!(var) |
let var = var.clone(); |
| Field Access | clone!(obj.field) |
let field = obj.field.clone(); |
| Method Call | clone!(obj.method()) |
let method = obj.method().clone(); |
With mut |
clone!(mut var) |
let mut var = var.clone(); |
Limitations
- Tuple index access (e.g.,
tuple.0) is not supported - Only named fields and method calls are supported for expression derivation
License
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.