【问题标题】:Type signature for generic closure with trait constraint具有特征约束的通用闭包的类型签名
【发布时间】:2015-04-07 08:02:41
【问题描述】:

我有以下可以正常工作的结构:

pub struct Pattern {
    pub regex: &'static str,
    pub view: Fn (Request) -> Response,
}

但我想更改 view 以接受任何实现 Renderable 的类型(特征约束)。我期待它以这种方式工作:

pub struct Pattern {
    pub regex: &'static str,
    pub view: Fn <T: Renderable> (Request) -> T,
}

但没有运气。有什么想法吗?

【问题讨论】:

    标签: rust


    【解决方案1】:

    您想在结构上使用where 子句(以及该结构的任何实现):

    trait A { fn moo(&self); }
    struct S;
    
    struct Pattern<T>
        where T: A
    {
        view: Fn (T) -> S,
    }
    
    fn main() {}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-20
      • 2020-12-20
      相关资源
      最近更新 更多