Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rust best practices when specifying a constant hash map [duplicate]

Tags:

hashmap

rust

I'm trying to set a constant, predefined hash map in Rust. I'm not sure what the best practice is in Rust for this.

use std::collections::HashMap;

pub const Countries: HashMap<&str, &str> = [
    ("UK", "United Kingdom"),
    ("US", "United States")
].iter().cloned().collect();

These will then be referenced later in the library.

If this is bad, I'm guessing a match in a function is the best way?

like image 677
joedborg Avatar asked Feb 18 '20 02:02

joedborg


People also ask

Are Hashmaps slow rust?

HashMap uses a slow (cryptographic) hasher by default; you can do better by swapping it out for a fast one.

Are Hashmaps ordered rust?

HashMap s are inherently unordered collections, and BTree s are inherently ordered by their keys.

How do you initialize a HashMap in Rust?

Starting with Rust 1.56, you can use from() to build a Hashmap from an array of key-value pairs. This makes it possible to initialize concisely without needing to specify types or write macros.

How do I return a value from a HashMap in Rust?

To access values from HashMap using keys: Import HashMap. Insert records in HashMap. Use get( & key) for getting the value.

What are the operations of HashMap in rust?

There are many operations for Rust HashMap execution like insertion, removal, iteration, addition, and many more. The key and value present within the hash table helps in one or the other way in taking decision regarding the accessibility of elements. HashMap uses hashing algorithm which is selected for providing resistance against HashDos attack.

How does HashMap protect against HashDoS attacks?

By default, HashMap uses a hashing algorithm selected to provide resistance against HashDoS attacks. The algorithm is randomly seeded, and a reasonable best-effort is made to generate this seed from a high quality, secure source of randomness provided by the host without blocking the program.

How does the HashMap method work?

The hash map is initially created with a capacity of 0, so it will not allocate until it is first inserted into. Creates an empty HashMap with at least the specified capacity. The hash map will be able to hold at least capacity elements without reallocating. This method is allowed to allocate for more elements than capacity.

What is the default hashing algorithm?

The default hashing algorithm is currently SipHash 1-3, though this is subject to change at any point in the future.


Video Answer


1 Answers

You can use https://crates.io/crates/lazy_static (lazily executed at runtime).

I personally use https://crates.io/crates/phf (compile-time static collections) for this if the data is truly static.

use phf::{phf_map};

static COUNTRIES: phf::Map<&'static str, &'static str> = phf_map! {
    "US" => "United States",
    "UK" => "United Kingdom",
};
like image 86
Martin Gallagher Avatar answered Oct 10 '22 17:10

Martin Gallagher