24次发布
0.1.23 | 2024年4月30日 |
---|---|
0.1.22 | 2024年4月30日 |
#146 in 过程宏
每月197次下载
16KB
154 行
请注意!这个crate仍然是不完整的!
Rust宏库,用于“不重复自己”
DRYlib是一个旨在减少重复代码数量的库。
Crates.io: https://crates.io/crates/drylib
请查看examples/clones.rs中的克隆宏示例
extern crate drylib;
use drylib::{clones, mutclones}; // Use the best library in the world
fn main() {
// You can define variables that you want to clone:
let digit = 2;
let vector = vec![1, 2, 3];
let string = "this is a string generated by the `clones`".to_owned();
// And you can clone them with the `clones` macro:
clones!(digit, vector, string); // Just type what variables you want to clone
// The `clones` macro creates new variables using the following formula:
// format!({CLONES_PREFIX}{identifier(name) of the variable}).
// By default CLONES_PREFIX is 'c', but you can specify it with following features:
// [clones-prefix-c, clones-prefix-cl, and so on until clones-prefix-clone]
// Select the one and prefixes will be appropriate.
//
// Therefore, the `clones` macro expands as follows:
//
// let cdigit = digit.clone();
// let cvector = vector.clone();
// let cstring = string.clone();
// We can print them:
println!("cdigit: {cdigit}, cvector: {cvector:?}, cstring: {cstring}");
// This will print: cdigit: 2, cvector: [1, 2, 3], cstring: this is a string generated by the `clones`
// By the way, you can use the `clones` macro specifying
// mutability of the variables that you want to clone as in here:
clones!(mut digit, vector, mut string);
// this one ^^^^^ and this one ^^^^^^ will be created as mutable variables,
// with the formula already described up above.
//
// This macro call expands as follows:
//
// let mut cdigit = digit.clone();
// let cvector = vector.clone();
// let mut cstring = string.clone();
// The variables are mutable, so you can easily reassign them:
cdigit = 4;
cstring = "this is a mutable cloned string generated by the `clones` macro".to_owned();
// And print:
println!("cdigit: {cdigit}, cvector: {cvector:?}, cstring: {cstring}");
// This will print: cdigit: 4, cvector: [1, 2, 3], cstring: this is a mutable cloned string generated by the `clones` macro
// Here is another one, the `mutclones` macro, it does the same thing as the clones macro,
// but created variables are all mutable.
mutclones!(digit, vector, string); // The `mutclones` macro expands in this code:
//
// let mut cdigit = digit.clone();
// let mut cvector = vector.clone();
// let mut cstring = string.clone();
// The variables are mutable, therefore you can easily reassign them:
cdigit = 4;
cvector = vec![4, 5, 6];
cstring = "this is a mutable cloned string generated by the `mutclones` macro".to_owned();
// And print them:
println!("cdigit: {cdigit}, cvector: {cvector:?}, cstring: {cstring}");
// This will print: cdigit: 4, cvector: [4, 5, 6], cstring: this is a mutable cloned string generated by the `mutclones` macro
}
然后查看examples/structs.rs中的pubstruct宏示例
extern crate drylib; // Import the drylib library
use drylib::*; // Bring all of the macros from the drylib into scope
fn main() {
// <++++++++++++ Let's start with creating tuple structs ++++++++++++>
// You can create a pub tuple struct using the pubstruct macro like that:
pubstruct!{
#[derive(Debug)]
Tuple(usize, i32, u32) // Define a tuple with types usize, i32, and u32
};
// This ^ will expand into this:
// pub struct Tuple(pub usize, pub i32, pub u32);
// So that's kinda the point of the macro, create pub struct with all of the fields
// in it are public as well.
let tuple = Tuple(0, 1, 2); // Let's create an instance of the Tuple
println!("{tuple:?}"); // Prints: Tuple(0, 1, 2)
// You can create a pub tuple struct with a generic type T as well:
pubstruct!{
#[derive(Debug)]
TupleT<T>(T, i32, u32)
};
let tuple_t = TupleT(0, 1, 2);
println!("{tuple_t:?}"); // Prints: TupleT(0, 1, 2)
// Also you create a struct with a lifetime:
pubstruct!{
#[derive(Debug)]
StructureLT<'a> {
greet: &'a str,
digit: i32,
}
}
let structure_lt = StructureLT { greet: "hello again", digit: 1 };
println!("{structure_lt:?}"); // Prints: StructureLT { greet: "hello again", digit: 1 }
// Create a struct with both of generics and lifetimes, that's within your power as well:
pubstruct!{
#[derive(Debug)]
StructureLTTU<'a, 'b, T, U> {
greet: &'a T,
array: &'b Vec<U>,
}
}
// And this ^ expands like this:
// pub struct StructureLTTU<'a, 'b, T, U> {
// pub greet: &'a T,
// pub array: &'b Vec<U>,
// }
let structure_lttu = StructureLTTU { greet: &"hello again", array: &vec![1, 2, 3] };
println!("{structure_lttu:?}"); // Prints: StructureLTTU { greet: "hello again", array: [1, 2, 3] }
// ... Continued in file examples/structs.rs
}