1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
//! A bundle of useful macros and functions to make your Quality Of Life when programming rust
//! better.
//!
//! If you have any reccomendations on what to add, please open an issue on the repo/make a merge
//! request.
/// Create a string from the given type. Shorthand for `"foo".to_string()` or `String::new()`.
/**
```
use qol::S;
fn main() {
assert_eq!(S!(), String::new());
string(S!("foo"));
string(S!(123));
/* i_need_string("bar"); */ // expected String, found `&str`
}
fn string(_: String) {}
```
**/
#[macro_export]
macro_rules! S {
() => {
String::new()
};
("") => {
String::new()
};
($s:expr) => {
$s.to_string()
};
}
/// Create a hashmap with the given key/value pairs.
/**
```
use {
qol::{map, S},
std::collections::HashMap,
};
let _nums: HashMap<i32, String> = map! {
1 => S!("one"),
2 => S!("two"),
3 => S!("three"),
4 => S!("four"),
5 => S!("five"),
6 => S!("six")
};
```
**/
#[macro_export]
macro_rules! map {
{ $($key:expr => $value:expr),+, } => {
{
use std::collections::HashMap;
let mut m = HashMap::new();
$(
m.insert($key, $value);
)+
m
}
};
{ $($key:expr => $value:expr),+ } => {
{
use std::collections::HashMap;
let mut m = HashMap::new();
$(
m.insert($key, $value);
)+
m
}
};
}