【发布时间】:2020-02-27 10:05:26
【问题描述】:
我正在尝试创建一个 trait(StringToTable),它返回一个具有另一个 trait(Table) 实现的结构。
当我阅读了这个答案How to infer the return type of a function? 后,我尝试了“盒装类型”的方法,但没有成功。
我不知道调用我的 StringToTable 时的结构类型,所以我不能使用 'to_table' 或 'StringToTable' 之类的方法..
我的代码:
1°接近:
fn to_table(table: String) -> Option<Box<dyn Table<DeserializeOwned + Serialize + SearchIndex>>>;
我明白了:
error[E0225]: only auto traits can be used as additional traits in a trait object
--> src/main.rs:18:75
|
18 | fn to_table(table: String) -> Option<Box<dyn Table<DeserializeOwned + Serialize + SearchIndex>>>;
| ---------------- ^^^^^^^^^
| | |
| | additional non-auto trait
| | trait alias used in trait object type (additional use)
| first non-auto trait
| trait alias used in trait object type (first use)
2°接近:
fn to_table2(table: String) -> Option<Box<dyn Table<T>>> where T: DeserializeOwned + Serialize + SearchIndex;
我明白了:
error[E0412]: cannot find type `T` in this scope
--> src/main.rs:20:68
|
20 | fn to_table2(table: String) -> Option<Box<dyn Table<T>>> where T: DeserializeOwned + Serialize + SearchIndex;
| ^ not found in this scope
** 我的代码已经简化
我正在尝试做的事情:在 tcp 服务器上接收一个随机名称并获得它的等效结构。
【问题讨论】:
标签: rust