A collection of macro extensions for Rust's standard library data structures, simplifying the creation and manipulation of common collections such as HashMap, Vec, and more.
- Simplified Initialization: Use macros to easily create instances of common data structures.
- Supports Various Data Structures: Includes macros for
Vec,HashMap,Arc, and more. - Easy to Use: Intuitive syntax for quick data structure creation.
To install std-macro-extensions run cmd:
cargo add std-macro-extensionsHere are some examples of how to use the macros provided by this crate:
use std_macro_extensions::*;
fn main() {
let value = arc!(5);
}use std_macro_extensions::*;
fn main() {
let empty_map: BTreeMap<i32, i32> = b_tree_map!();
let b_tree_map_a: BTreeMap<&str, &str> = b_tree_map!(
"a" => "a",
"b" => "b"
);
}use std_macro_extensions::*;
fn main() {
let empty_set: BTreeSet<i32> = b_tree_set!();
let number_set: BTreeSet<i32> = b_tree_set!(1, 2, 3);
}use std_macro_extensions::*;
fn main() {
let empty_set: BTreeSet<i32> = b_tree_set!();
let number_set: BTreeSet<i32> = b_tree_set!(1, 2, 3);
}use std_macro_extensions::*;
fn main() {
let boxed_value = boxed!(10);
}use std_macro_extensions::*;
fn main() {
let cell_value: Cell<i32> = cell!(5);
}use std_macro_extensions::*;
fn main() {
let my_map: HashMap<&str, i32> = hash_map!();
let my_map: HashMap<&str, i32> = hash_map!("a" => 1, "b" => 2);
}use std_macro_extensions::*;
fn main() {
let my_set: HashSet<i32> = hash_set!();
let my_set: HashSet<i32> = hash_set!(1, 2, 3);
}use std_macro_extensions::*;
fn main() {
let my_list: LinkedList<i32> = linked_list!();
let my_list: LinkedList<i32> = linked_list!(1, 2, 3);
}use std_macro_extensions::*;
fn main() {
let my_mutex: Mutex<i32> = mutex!(5);
let lock: MutexGuard<'_, i32> = my_mutex.lock().unwrap();
}use std_macro_extensions::*;
fn main() {
let my_rc = rc!(5);
}use std_macro_extensions::*;
fn main() {
use std_macro_extensions::*;
let my_refcell = refcell!(5);
}use std_macro_extensions::*;
fn main() {
let my_rwlock = rw_lock!(5);
}use std_macro_extensions::*;
fn main() {
let empty_string = string!();
let hello_string = string!("Hello");
}use std_macro_extensions::*;
fn main() {
let empty_vector: Vec<i32> = vector!();
let numbers = vector!(1, 2, 3);
}use std_macro_extensions::*;
fn main() {
let empty_deque: VecDeque<i32> = vector_deque!();
let numbers = vector_deque!(1, 2, 3);
}let combined_path: String = join_paths!("/home/", "/user/", "/documents", "file.txt");
let another_path: String = join_paths!("C:/", "/Program Files", "App");let input: String = cin!();
println!("You typed: {}", input);let input: &str = "1 2 3";
let numbers: Vec<i32> = cin_parse!(input, Vec<i32>);
assert_eq!(numbers, vec![1, 2, 3]);
let single_input: &str = "12";
let number: i32 = cin_parse!(single_input, i32);
assert_eq!(number, 12);let name: &str = "Alice";
let age: i32 = 30;
cout!("Name: {}, Age: {}\n", name, age);endl!();let name: &str = "Alice";
let age: i32 = 30;
cout_endl!("Name: {}, Age: {}\n", name, age);fn sum(data: &[i32]) -> i32 {
data.iter().sum()
}
fn add_offset(data: &[i32], offset: i32) -> i32 {
data.iter().map(|x| x + offset).sum()
}
let nums: Vec<i32> = vec![1, 2, 3];
let total: i32 = execute!(sum, &nums);
assert_eq!(total, 6);
let total_with_offset: i32 = execute!(add_offset, &nums, 10);
assert_eq!(total_with_offset, 36);let data: Vec<i32> = vec![1, 2, 3];
async fn async_func(data: &[i32], offset: i32) -> i32 {
data.iter().map(|x| x + offset).sum()
}
let res: i32 = execute_async!(async_func, &data, 1).await;
assert_eq!(res, 9);arc!: Creates anArc<T>.vector!: Creates aVec<T>.map!: Creates aHashMap<K, V>.set!: Creates aHashSet<T>.b_tree_map!: Creates aBTreeMap<K, V>.b_tree_set!: Creates aBTreeSet<T>.list!: Creates aLinkedList<T>.heap!: Creates aBinaryHeap<T>.string!: Creates aString.boxed!: Creates aBox<T>.rc!: Creates anRc<T>.arc!: Creates anArc<T>.mutex!: Creates aMutex<T>.rw_lock!: Creates aRwLock<T>.cell!: Creates aCell<T>.ref_cell!: Creates aRefCell<T>.vector_deque!: Creates aVecDeque<T>.join_paths!: Combines multiple paths into a single valid path, handling overlapping slashes.cin!: Reads a line of input from the standard input.cin_parse!: Parses input into a specified type.cout!: Prints formatted output to the standard output.endl!: Prints a newline character to the standard output.cout_endl!: Prints formatted output followed by a newline character to the standard output.execute!: Executes a function with the provided arguments.execute_async!: Executes an asynchronous function with the provided arguments.
This project is licensed under the MIT License. See the LICENSE file for details.
Contributions are welcome! Please open an issue or submit a pull request.
For any inquiries, please reach out to the author at root@ltpp.vip.