0

Thought this would be simple.

Just want to create a hash map where the keys are a tuple (u8, &str) representing the number of a ordered set of problems and the string literal representing the question itself. Then the values would be another tuple (&str, [&str;4]) representing the correct letter and the possible answers

const ANSWER_KEY: HashMap<(u8, &str), (&str, [&str;4])> = HashMap::from {
    (1, "What is a programming language?") : ("B", [
        "A. A language that improves programming.",
        "B. A human readable language for humans that translates to a binary language to communicate with computers.",
        "C. A language read by computers that translates to a unary language to communicate with humans.",
        "D. A language created by Alan Turing.")
    ]
};

The error message is: "What is a programmi...' unexpected and <struct literal field> expected, got '('

Sergio Bost
  • 2,591
  • 2
  • 11
  • 29
  • `HashMap::from { key: value, ... }` is not valid syntax. Perhaps you wanted `HashMap::from([(key, value), ...])` like in this answer: https://stackoverflow.com/a/69640610/2189130 – kmdreko Oct 28 '21 at 02:14
  • Thanks that was def. an error but although now the error is `mismatched types [E0308] expected `[((u8, &str), (&str, [&str; 4])); ]`, found `(i32, &str)` ` – Sergio Bost Oct 28 '21 at 02:23
  • 2
    Also, why are you using a hashmap if the Q&As are ordered and numbered? Wouldn't a `Vec` be more suitable? – kmdreko Oct 28 '21 at 02:47
  • @kmdreko Yeah a vec of tuples sounds more appropriate – Sergio Bost Oct 28 '21 at 02:50

1 Answers1

1

You can declare like this:

let answer: HashMap<(u8, &str), (&str, [&str;4])> = HashMap::from(
[
    ((1, "What is a programming language?"), ("B", [
    "A. A language that improves programming.",
    "B. A human readable language for humans that translates to a binary language to communicate with computers.",
    "C. A language read by computers that translates to a unary language to communicate with humans.",
    "D. A language created by Alan Turing."]))
]); 

You cannot declare const due to error [E0015]: calls in constants are limited to constant functions, tuple structs and tuple variants

Zeppi
  • 1,175
  • 6
  • 11