<分区>
我需要传递一个 HashMap
到一个函数,但是这个 HashMap
理论上可以将整数和字符串都作为值。我如何为此形成相关函数的类型签名?我是 Rust 的新手,在尝试之后 fn do_thing(params: &HashMap<String, _>)
遇到失败,我没有想法。
fn do_thing(params: &HashMap<String, _>) {
// Irrelevant - regardless, the compiler fails because I cannot use the type placeholder in a function's type sig.
}
fn main() {
let params = HashMap::new();
params.insert(field, value); // field will always be a string, value could be a string or an integer
do_thing(¶ms)
}
这不是建议问题的副本 - 构造 HashMap
不需要类型声明,而这涉及 HashMap
作为函数的参数,它确实如此。