Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Function 如何声明一个函数,该函数接受任何可以转换为HashMap的内容<;字符串,字符串>;_Function_Hashmap_Rust - Fatal编程技术网

Function 如何声明一个函数,该函数接受任何可以转换为HashMap的内容<;字符串,字符串>;

Function 如何声明一个函数,该函数接受任何可以转换为HashMap的内容<;字符串,字符串>;,function,hashmap,rust,Function,Hashmap,Rust,我有一个结构,看起来像这样: struct Fields { map: HashMap<String, String> } 这是一个错误 error[E0277]:特性绑定'std::convert::Into+'static:std::marker::Sized'不满足 -->src/main.rs:1:1 | 1 |/fn集合图(字段:I){ 2 | | // ... 3 | | } || ^`std::convert::Into+'static`在编译时没有已

我有一个结构,看起来像这样:

struct Fields {
    map: HashMap<String, String>
}
这是一个错误

error[E0277]:特性绑定'std::convert::Into+'static:std::marker::Sized'不满足
-->src/main.rs:1:1
|
1 |/fn集合图(字段:I){
2 | |     // ...
3 | | }
|| ^`std::convert::Into+'static`在编译时没有已知的常量大小
|
=帮助:特性'std::marker::Sized'未为'std::convert::Into+'静态实现`
=注意:只有元组的最后一个元素可以具有动态大小的类型
错误[E0038]:无法将特征'std::convert::Into'设置为对象
-->src/main.rs:1:1
|
1 | fn集合图(字段:I){
|^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
=注意:该特征不能要求“Self:Sized”`

您忘记将
项的元组元素作为类型(与traits相反)传递。以下操作应该有效:

fn set_map<S: Into<String>, T: Into<String>, I: IntoIterator<Item=(S, T)>>(fields: I) {
    ...
}
fn集合图(字段:I){
...
}

两个不同的参数
S
T
而不是一个参数,允许您将不同的
转换为元组中的
类型。

我将为元组元素使用两个单独的参数。这两个元素是否具有相同的类型无关紧要,只要它们都可以转换为字符串。神奇!谢谢!
fn set_map<S: Into<String>, T: Into<String>, I: IntoIterator<Item=(S, T)>>(fields: I) {
    ...
}