bluss/maplit

Cannot use tuple as keys

richardcocks opened this issue · 2 comments

The following code gives me a compilation error:

#[macro_use]
extern crate maplit;
use std::collections::HashMap;

static scoremap: HashMap<(char, char), f64> = hashmap! {
('a','b') => 3f64,
('c','d') => 2f64
};

fn main() {
    assert_eq!(scoremap.get(&('a', 'b')), Some(&3f64));
}

When I try to build the error I get is: error[E0015]: calls in statics are limited to constant functions, tuple structs and tuple variants.

Do I need to run convert_args!? I'm not sure what function that would be in this case, and an initial attempt at doing that didn't prove fruitful.

Sorry if what I'm trying to do isn't possible, I'm still new to rust.

(rustc 1.35.0)

I've worked around this by using the lazy_static crate to defer until runtime, but is this that ought to work using maplit alone?

bluss commented

Hi @richardcocks this is not an issue with tuples, we can't use maplit in statics at all, since it's creating a dynamically allocated collection type (the HashMap here). Since we are just a shorthand for what you can normally do in open coded rust, this is not really a limitation of maplit, but of Rust as it is right now. So I'll close this, since we are not tracking changes to Rust here. Thanks!