case_insensitive_hashmap

Crates.iocase_insensitive_hashmap
lib.rscase_insensitive_hashmap
version1.0.1
sourcesrc
created_at2020-06-05 15:41:26.097445
updated_at2024-07-05 19:33:52.672429
descriptionA HashMap that uses case-insensitive strings as keys.
homepage
repositoryhttps://github.com/PhilipDaniels/case-insensitive-hashmap
max_upload_size
id250373
size25,179
Philip Daniels (PhilipDaniels)

documentation

https://docs.rs/case_insensitive_hashmap/

README

CaseInsensitiveHashMap

A wrapper around the std::collections::HashMap that uses case-insensitive Strings for keys.

Since this is a simple wrapper around the standard HashMap, please see its documentation for more information.

The key type of the CaseInsensitiveHashMap is always UniCase<String>. Most methods that have a key parameter have a constraint <K: Into<Key>>. This means that you can call them with a String, a &str or a UniCase<String> if you already have one. This make the API more ergonomic than the alternative of using UniCase<String> directly as a key type in your own std::collections::HashMap.

Examples

use unicase::UniCase;
use case_insensitive_hashmap::CaseInsensitiveHashMap;

let mut map = CaseInsensitiveHashMap::new();
map.insert("a", 20);
map.insert("B".to_string(), 40);

// All these are valid key forms.
assert!(map.contains_key("A"));
assert!(map.contains_key("A".to_string()));
let uc = UniCase::new("A".to_string());
assert!(map.contains_key(uc));

// Lookup of values is case-insensitive.
assert_eq!(map.get("a"), Some(&20));
assert_eq!(map.get("A"), Some(&20));

assert_eq!(map["a"], 20);
assert_eq!(map["A"], 20);

Implementation

This uses the UniCase crate to handle the case-insensitivity. Strings that are used as keys are wrapped in UniCase objects so that they hash and compare for equality in a case-insensitive manner.

Release Notes

  • 1.0.1 - bumped version of UniCase to 2.7.0.
Commit count: 19

cargo fmt