https://github.com/rnag/map-to-const
Easily convert `HashMap<K, V>` to constant `[(K, V); N]` values.
https://github.com/rnag/map-to-const
Last synced: 5 months ago
JSON representation
Easily convert `HashMap<K, V>` to constant `[(K, V); N]` values.
- Host: GitHub
- URL: https://github.com/rnag/map-to-const
- Owner: rnag
- License: mit
- Created: 2022-03-18T23:44:26.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2022-03-19T21:51:28.000Z (almost 4 years ago)
- Last Synced: 2024-09-14T15:23:12.919Z (over 1 year ago)
- Language: Rust
- Size: 19.5 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# map-to-const
[
](https://github.com/rnag/map-to-const)
[
](https://crates.io/crates/map-to-const)
[
](https://docs.rs/map-to-const)
[
](https://github.com/rnag/map-to-const/actions?query=branch%3Amain)
Easily convert `HashMap` to constant `[(K, V); N]` values.
---
This crate works with Cargo with a `Cargo.toml` like:
```toml
[dependencies]
map-to-const = "0.2"
```
## Getting started
Add some usage to your application.
Here's an example of using `map-to-const` in code:
```rust
use map_to_const::*;
use std::collections::HashMap;
fn main() {
// Create a HashMap in some manner. Ideally, this will be formatted
// and returned in an API response or similar.
let my_map = HashMap::from([("testing", "123"), ("hello", "world")]);
let const_value = map_to_const(&my_map, "my const name");
println!("{const_value}");
// prints:
// const MY_CONST_NAME: [(&str, &str); 2] = [
// ("hello", "world"),
// ("testing", "123"),
// ];
// later in code, construct the hashmap from the `const` slice:
// let my_map = HashMap::from(MY_CONST_NAME);
}
```
## Examples
You can check out sample usage of this crate in the [examples/](https://github.com/rnag/map-to-const/tree/main/examples)
folder in the project repo on GitHub.
## Contributing
Contributions are welcome! Open a pull request to fix a bug, or [open an issue][]
to discuss a new feature or change.
Check out the [Contributing][] section in the docs for more info.
[Contributing]: CONTRIBUTING.md
[open an issue]: https://github.com/rnag/map-to-const/issues
## License
This project is proudly licensed under the MIT license ([LICENSE](LICENSE)
or http://opensource.org/licenses/MIT).
`map-to-const` can be distributed according to the MIT license. Contributions
will be accepted under the same license.
## Authors
* [Ritvik Nag](https://github.com/rnag)