【发布时间】:2018-05-24 18:28:57
【问题描述】:
我有一个看起来像这样的结构:
struct Fields {
map: HashMap<String, String>
}
对于人体工程学,我想要一个同时接受&str 和String 的函数。我读到HashMap 有一个特征FromIterator<(K, V)>,所以在抽象层面上,我可以从 anything 工作,它产生一个对变成字符串的迭代器。
类似地,如果我想要一个接受任何可以转换为String 的函数,我可以使用绑定的T: Into<String>。
对于可以转换为字符串的对的迭代器是否可以这样做?从概念上讲:
fn set_map<I: IntoIterator<Item=(Into<String>, Into<String>)>>(fields: I) {
// ...
}
这个错误与
error[E0277]: the trait bound `std::convert::Into<std::string::String> + 'static: std::marker::Sized` is not satisfied
--> src/main.rs:1:1
|
1 | / fn set_map<I: IntoIterator<Item = (Into<String>, Into<String>)>>(fields: I) {
2 | | // ...
3 | | }
| |_^ `std::convert::Into<std::string::String> + 'static` does not have a constant size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `std::convert::Into<std::string::String> + 'static`
= note: only the last element of a tuple may have a dynamically sized type
error[E0038]: the trait `std::convert::Into` cannot be made into an object
--> src/main.rs:1:1
|
1 | fn set_map<I: IntoIterator<Item = (Into<String>, Into<String>)>>(fields: I) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::convert::Into` cannot be made into an object
|
= note: the trait cannot require that `Self : Sized`
【问题讨论】: