简体   繁体   English

预期特征 object `dyn Responsability`,找到类型参数 `T`

[英]expected trait object `dyn Responsability`, found type parameter `T`

I am trying to implement a responsability chain in Rust:我正在尝试在 Rust 中实现责任链:

link to playground 链接到游乐场

use std::error::Error;

struct Query {
    query: String,
}

struct Response {
    response: u64,
}

trait Responsability {
    fn take(&self, iterator: std::slice::Iter<Box<dyn Responsability>>, query: Query) -> Result<Response, Box<dyn Error>>;
}

struct ResponsabilityChain<T: Responsability> {
    responsabilities: Vec<Box<T>>,
}

impl<T: Responsability> ResponsabilityChain<T>
where
    T: Responsability,
{
    pub fn new(responsabilities: Vec<T>) -> Self {
        let responsabilities = responsabilities.into_iter()
            .map(|elt| Box::new(elt))
            .collect();
        
        Self { responsabilities }
    }
    
    pub fn launch(&self, query: Query) -> Result<Response, Box<dyn Error>> {
        let iterator = self.responsabilities.iter();
        let responsability = iterator.next().unwrap();
        
        responsability.take(iterator, query)
    }
}

fn main() {
    println!("Hello, world!");
}

The infamous message is:臭名昭著的消息是:

Compiling playground v0.0.1 (/playground) error[E0308]: mismatched types --> src/main.rs:35:29 |编译 Playground v0.0.1 (/playground) 错误[E0308]: mismatched types --> src/main.rs:35:29 | 19 | 19 | impl<T: Responsability> ResponsabilityChain | impl<T: Responsability> 责任链 | - this type parameter... 35 | - 此类型参数... 35 |
responsability.take(iterator, query) | responsability.take(迭代器,查询) |
^^^^^^^^ expected trait object dyn Responsability , found type parameter T | ^^^^^^^^ 预期特征 object dyn Responsability ,找到类型参数T | = note: expected struct std::slice::Iter<'_, Box<(dyn Responsability + 'static)>> found struct std::slice::Iter<'_, Box<T>> = help: type parameters must be constrained to match other types = note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters = 注意:预期的 struct std::slice::Iter<'_, Box<(dyn Responsability + 'static)>>找到 struct std::slice::Iter<'_, Box<T>> = 帮助:类型参数必须限制匹配其他类型 = 注意:有关更多信息,请访问https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters

For more information about this error, try rustc --explain E0308 .有关此错误的更多信息,请尝试rustc --explain E0308 error: could not compile playground due to previous error错误:由于先前的错误,无法编译playground

I do not understand why the compiler complains expecting Box<dyn Responsability> while having Box<T> since I specify T: Responsability .我不明白为什么编译器在拥有Box<T>时抱怨期望Box<dyn Responsability> ,因为我指定了T: Responsability What do I do wrong?我做错了什么?

dyn I and <T> where T: I are different types in Rust, so the compiler complains since there's no implicit conversion. dyn I<T> where T: I是 Rust 中的不同类型,因此编译器会抱怨,因为没有隐式转换。

T is a concrete type determined at compile time. T是在编译时确定的具体类型。 dyn I it is a "trait object", it is dynamic, and concrete type is unknown, but sort of carried within. dyn I它是一个“特征对象”,它是动态的,具体类型未知,但有点内在。

A good video on the topic.关于这个话题的好视频

Conversion from <T> where T: I to dyn I is not free, it has a runtime cost, so has to be explicit with the Rust's philosophy.<T> where T: Idyn I的转换不是免费的,它有运行时成本,所以必须明确 Rust 的哲学。

The code could be fixed by using Vec<Box<dyn Responsability>> in all places.可以通过在所有地方使用Vec<Box<dyn Responsability>>来修复代码。 It will also allow you passing arbitrary types to new() , which is probably what you want, because Vec<T> has to contain objects of the same type (remember that this type is determined at compile time).它还允许您将任意类型传递给new() ,这可能是您想要的,因为Vec<T>必须包含相同类型的对象(请记住,此类型是在编译时确定的)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不匹配的类型预期特征 object `dyn Trait` 找到结构 `Struct` - mismatched types expected trait object `dyn Trait` found struct `Struct` Rust 显示预期特征 object `dyn Future`,在传递 function 作为参数时发现不透明类型 - Rust showing expected trait object `dyn Future`, found opaque type when passing function as a param 使用泛型参数实现特征; 预期类型参数“T”,找到类型参数“SQ” - Implementing a trait with generic parameters; expected type parameter `T`, found type parameter `SQ` 元组中的 Rust trait 对象 --- 预期的 trait 对象,找到的类型 - Rust trait object in tuple --- expected trait object, found type 将闭包传递给trait方法:期望的类型参数,找到闭包 - Passing closure to trait method: expected type parameter, found closure 期望的trait core :: ops :: FnMut,找到类型参数 - expected trait core::ops::FnMut, found type parameter 实现特征时“预期的类型参数,找到的结构” - “expected type parameter, found struct” when implementing trait 结构不会强制在 function 返回中实现 dyn Trait 类型 - Struct won't coerce to implemented dyn Trait type in function return 预期类型参数“T”,在 Rust 中找到“&amp;T” - expected type parameter `T`, found `&T in Rust 为什么我不能用 let _: Arc 创建一个特征 object<dyn trait> = value.into()?</dyn> - Why can't I create a trait object with let _: Arc<dyn Trait> = value.into()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM