我正在玩Rust,并且可能咬得比我可以咀嚼的多得多,我正在尝试编写一个模块来封装我应用程序其余部分的数据库流量.我正在努力解决的代码如下:
pub fn create_statement(cypher: &str, params: &HashMap<&str, &str>) -> rusted_cypher::Statement {
let mut statement = rusted_cypher::Statement::new(cypher);
for (field, value) in params.iter() {
statement.with_param(field.to_owned(), value.to_owned());
}
return statement;
}
Run Code Online (Sandbox Code Playgroud)
这会出现以下错误:error[E0382]: use of moved value: statement.我认为,我的搜索引导我达到这意味着什么(Statement结构不可复制,因此被移动,然后......实际上不再可访问,我猜?),但我不确定如何绕过它.有人能指出我的解决方案吗?
我需要将 a 传递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)
}
Run Code Online (Sandbox Code Playgroud)
这不是建议问题的重复 - 构造 aHashMap不需要类型声明,而这涉及HashMap作为函数的参数,它需要。