const_lookup_map

Crates.ioconst_lookup_map
lib.rsconst_lookup_map
version0.1.0
sourcesrc
created_at2023-02-23 16:05:18.033155
updated_at2023-02-23 16:05:18.033155
descriptionRust map that can be defined in a const context.
homepage
repositoryhttps://github.com/thomas9911/const_lookup_map
max_upload_size
id792861
size8,868
Thomas Timmer (thomas9911)

documentation

README

const_lookup_map

Rust map that can be defined in a const context.

There are two ways to create it:

use const_lookup_map::{ConstLookup, lookup};

const LOOKUP_MACRO: ConstLookup<3, &str, &str> = lookup! {
    "best" => "better",
    "test" => "testing",
    "guessed" => "guessing",
};
use const_lookup_map::ConstLookup;

pub const LOOKUP: ConstLookup<4, &str, &str> = ConstLookup::new(
    ["bye", "hallo", "hey", "test"],
    [
        "bye.example.com",
        "hallo.example.com",
        "hey.example.com",
        "test.example.com",
    ],
);

One note; The keys should be in order/sorted because the get method will use this to effienctly get the value. See [ConstLookup::check_sorted]

Usage

use const_lookup_map::{ConstLookup, lookup};

const LOOKUP: ConstLookup<3, &str, &str> = lookup! {
    "best" => "better",
    "test" => "testing",
    "guessed" => "guessing",
};

fn my_function() {
  assert_eq!(LOOKUP.get(&"best"), Some(&"better"));
  assert_eq!(LOOKUP[&"best"], "better");
}
Commit count: 5

cargo fmt