【发布时间】:2021-12-13 06:19:07
【问题描述】:
认为这很简单。
只想创建一个哈希映射,其中键是一个元组 (u8, &str),表示一组有序问题的数量,而字符串文字表示问题本身。然后值将是另一个元组 (&str, [&str;4]) 代表正确的字母和可能的答案
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.")
]
};
错误信息是:"What is a programmi...' unexpected和<struct literal field> expected, got '('
【问题讨论】:
-
HashMap::from { key: value, ... }语法无效。也许你想要HashMap::from([(key, value), ...])就像这个答案:stackoverflow.com/a/69640610/2189130 -
谢谢你是def。一个错误但虽然现在错误是
mismatched types [E0308] expected[((u8, &str), (&str, [&str; 4])); ], found(i32, &str)` ` -
另外,如果问答是有序和编号的,为什么还要使用哈希图?
Vec不是更合适吗? -
@kmdreko 是的,元组的 vec 听起来更合适
标签: rust