This crate provides a collection of useful macros to make tasks easier.
std::collections
:
VecDeque
from list of elements.HashSet
“ .BTreeSet
“ .BinaryHeap
“ .HashMap
from key-value pairs.BTreeMap
“ .LinkedList
from list of elements.LinkedList
, but reversed..collect()
comprehensions:
Iterator
collection comprehensions, others below are
based on this one.BinaryHeap
with collection comprehensions.BTreeMap
with “ .BTreeSet
with “ .VecDeque
with “ .HashMap
with “ .HashSet
with “ .Vec
with “ .Arc
.¹Box
.¹Cell
.¹Mutex
.¹RefCell
.¹Rc
.¹RwLock
.¹Cow
.Time/Duration:
Duration
object following a time pattern.²Returns a tuple if multiple parameters are given.
min
, sec
, nano
, micro
and milli
.std::collections
Usage of boxed
, same as arc
, cell
, cow
, mutex
and refcell
:
rust
assert_eq!(Box::new(10), boxed!(10));
Usage of hmap
, same as btmap
:
```rust
let mut map = HashMap::new();
map.insert("1", 1);
map.insert("2", 2);
map.insert("3", 3);
let map2 = hmap! {"1" => 1, "2" => 2, "3" => 3};
assert_eq!(map, map2); ```
Usage of hset
, same as btset
:
```rust
let set = hset! {1, 2, 3};
let mut set2 = HashSet::new(); set2.insert(1); set2.insert(2); set2.insert(3);
assert_eq!(set, set2); ```
Usage of deque
, same as bheap
, lkl
and rlkl
:
```rust
let deque = deque![1, 2, 3];
let mut deque2 = VecDeque::new(); deque2.pushback(1); deque2.pushback(2); deque2.push_back(3);
assert_eq!(deque2, deque); ```
Usage of c!
: It follows the syntax: c![<expr>; <<pattern> in <iterator>, >...[, if <condition>]]
.
Note that it generates a lazy Iterator that needs to be dealt with.
```rust
let vec = c![x; x in 0..10].collect::
// Or using type hints let vec: Vec<_> = c![x; x in 0..10].collect(); let set: HashSet<_> = c![i*2; &i in vec.iter()].collect(); let vec: Vec<_> = c![i+j; i in vec.into_iter(), j in set.iter(), if i%2 == 0 && j%2 != 0].collect(); ```
Usage of cvec!
, same as cdeque!
, clkl!
and cbheap!
:
```rust
// Normal comprehension
cvec![x; x in 0..10];
// You can filter as well cvec![x; x in 0..10, if x % 2 == 0]; ```
Usage of cset
, same as cbtset
:
```rust
// Normal comprehension
cset! {x; x in 0..10};
// You can filter as well cset! {x; x in 0..10, if x % 2 == 0}; ```
Usage of cmap
, same as cbtmap
:
```rust
// Normal comprehension
cmap! {x => x*2; x in 1..10};
// You can filter as well cmap! {x => x*2; x in 1..10, if x % 2 == 0}; ```
Usage of dur
and sleep
:
```rust
let d1 = dur!(10 sec);
let d2 = std::time::Duration::from_secs(10);
assert_eq!(d1, d2);
// Same syntax, but make the thread sleep for the given time sleep!(10 sec) ```
Usage of time
:
```rust
// Should print to stderr ≈ 2.0000 seconds
time!( sleep!(2 sec) );
// It also return the evaluated expression, like dbg! macro let x = time!( 100 + 20 ); ```
This software requires Rust version equal or above 1.39.0.
This software is licensed under the MIT Public License.