【问题标题】:Returning a type in Rust在 Rust 中返回一个类型
【发布时间】:2020-02-27 10:05:26
【问题描述】:

我正在尝试创建一个 trait(StringToTable),它返回一个具有另一个 trait(Table) 实现的结构。

当我阅读了这个答案How to infer the return type of a function? 后,我尝试了“盒装类型”的方法,但没有成功。

我不知道调用我的 StringToTable 时的结构类型,所以我不能使用 'to_table' 或 'StringToTable' 之类的方法..

出现错误的游乐场:https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=cac035a6e6357c204156fab13449e865

我的代码:

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


    【解决方案1】:

    在第二种方法中,您可以通过声明类型参数 T 来修复编译错误。您可以在函数或 trait 上这样做。

    关于功能:

    fn to_table2<T>(table: String) -> Option<Box<dyn Table<T>>> where T: DeserializeOwned + Serialize + SearchIndex;
    

    关于特质:

    pub trait StringToTable<T> where T: DeserializeOwned + Serialize + SearchIndex {
    
        fn to_table2(table: String) -> Option<Box<dyn Table<T>>>;
    }
    

    另一种选择是使用关联类型:

    pub trait StringToTable {
        type T : DeserializeOwned + Serialize + SearchIndex;
    
        fn to_table2(table: String) -> Option<Box<dyn Table<Self::T>>>;
    }
    

    【讨论】:

    • 我不知道调用我的 StringToTable 时的结构类型,所以我不能使用 'to_table' 或 'StringToTable' 之类的方法...... to_table 函数可以返回任何实现特征 'DeserializeOwned + Serialize + SearchIndex'
    • 我认为有人需要知道使用的结构类型。类型参数应该添加到已知的地方。也许相关类型(见上面的编辑)对此有所帮助。您能否分享一个您计划如何使用 Table 和 StringToTable 特征的示例?
    • 关联类型在游乐场上工作。稍后我将使用我的真实来源进行测试。谢谢你现在
    • 我测试了关联类型,但现在我的问题在于这个特征的实现。对于 DeserializeOwned 和 Serialize 类型,我得到“错误 [E0225]:只有自动特征可以用作特征对象中的附加特征”。出现错误的新游乐场:play.rust-lang.org/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-31
    • 2022-07-31
    • 1970-01-01
    • 2020-08-15
    • 1970-01-01
    • 1970-01-01
    • 2014-12-16
    相关资源
    最近更新 更多