小编龙方淞*_*龙方淞的帖子

使用 Result::map 和 Box 时无法推断类型

为什么这不会编译?

trait T {}

fn f<U: 'static + T, V, E>(f2: V) -> impl Fn() -> Result<Box<dyn T>, E>
where
    V: Fn() -> Result<U, E>,
{
    move || -> Result<Box<dyn T>, E> { f2().map(Box::new) }
}
Run Code Online (Sandbox Code Playgroud)

错误信息是:

error[E0308]: mismatched types
 --> src/lib.rs:7:40
  |
7 |     move || -> Result<Box<dyn T>, E> { f2().map(Box::new) }
  |                                        ^^^^^^^^^^^^^^^^^^ expected trait T, found type parameter
  |
  = note: expected type `std::result::Result<std::boxed::Box<(dyn T + 'static)>, _>`
             found type `std::result::Result<std::boxed::Box<U>, _>`
  = help: type parameters …
Run Code Online (Sandbox Code Playgroud)

rust

2
推荐指数
1
解决办法
409
查看次数

标签 统计

rust ×1