# collect-all
[](https://github.com/dzfrias/collect-all/actions)
[](https://crates.io/crates/collect-all)
[](https://docs.rs/collect-all)
A macro to create collection literals for any type! Think of
[`vec!`](https://doc.rust-lang.org/std/macro.vec.html), but for any collection
type!
Internally, this just uses the
[`Iterator::collect()`](https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.collect)
method. Since `collect` is powered by
[`FromIterator`](https://doc.rust-lang.org/std/iter/trait.FromIterator.html),
implement it to use this macro for your own types!
## Examples
Basic use, inferring the type of the collection
```rust
use collect_all::collect;
use std::collections::HashSet;
let hashset: HashSet<i32> = collect![1, 2, 3];
assert_eq!(HashSet::from_iter([1, 2, 3]), hashset);
```
Specifying the type in the macro invocation
```rust
use collect_all::collect;
use std::collections::HashMap;
let hashmap = collect![HashMap<&str, u8>: ("one", 1), ("two", 2)];
assert_eq!(HashMap::from_iter([("one", 1), ("two", 2)]), hashmap);
```
Creating with a given capacity
```rust
use collect_all::collect;
use std::path::PathBuf;
let pathbuf = collect![PathBuf: "path", "to", "file"; 10];
assert_eq!(PathBuf::from("path/to/file"), pathbuf);
assert!(pathbuf.capacity() >= 10);
```
Note that this relies on a few things that are **not** strictly guaranteed for a given
collection:
1. `with_capacity` is a method of the target type (for example, see [`Vec::with_capacity`](https://doc.rust-lang.org/alloc/vec/struct.Vec.html#method.with_capacity))
2. The target type implements [`Extend`](https://doc.rust-lang.org/std/iter/trait.Extend.html)
## License
This crate is licensed under the
[MIT](https://github.com/dzfrias/collect-all/LICENSE) license.