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) {
...
}