相关疑难解决方法(0)

为什么在包含闭包的选项中“一种类型比另一种更通用”?

我写了一些编译好的代码,然后我变成TOption<T>,现在我收到了这个错误:

error[E0308]: mismatched type...one type is more general than the other
Run Code Online (Sandbox Code Playgroud)

在构建最小案例时,我注意到如果我更改data: &strdata: String代码再次编译正常。

游乐场链接

pub trait Subscriber {
    fn recv(&mut self, data: &str);
}

pub struct Task<T>(pub Option<T>)
where
    T: Fn(&str);

impl<T> Subscriber for Task<T>
where
    T: Fn(&str),
{
    fn recv(&mut self, data: &str) {
        self.0.take().unwrap()(data)
    }
}

fn main() {
    Task(Some(|_| ()));
}
Run Code Online (Sandbox Code Playgroud)

有人可以帮助我了解这里发生了什么吗?

rust

14
推荐指数
1
解决办法
1915
查看次数

标签 统计

rust ×1