我有一个用actix-web
编写的小后端应用程序.我想有一个类似于下面的函数.
async fn function(var: bool) -> impl Responder {
if var {
Redirect::to("link")
} else {
NamedFile::open_async("/file").await
}
}
错误:`if` and `else` have incompatible types expected `Redirect`, found future
.
我认为这可能可以通过将文件读入字符串并使用HttpResponse
来实现.但是,既然NamedFile
实现了Responder
,那么它是否可能在这种设置中工作?
如果go 掉if—else,这些都是有效的返回类型,但if—else语句的返回类型必须匹配.那么,在这种情况下,有没有可能不使用if—else?
编辑:链接的解决方案在这种情况下不起作用.将impl Responder更改为Box<dyn Responder>
会导致trait bounds失败.我收到以下错误消息:
the trait bound `std::boxed::Box<(dyn actix_web::Responder + 'static)>: actix_web::Responder` is not satisfied
也许它可以修复,但我没有足够的Rust泛型经验来做到这一点.