首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >错误:没有为‘`fn() -> HttpResponse实现特征HttpResponse

错误:没有为‘`fn() -> HttpResponse实现特征HttpResponse
EN

Stack Overflow用户
提问于 2022-06-24 19:47:45
回答 2查看 462关注 0票数 0

如果我要在Actix 3上使用这段代码,它可以工作,但我需要使用最新的稳定版本.那么4^。

下面是有问题的片段(实际上,这是我的全部代码):

代码语言:javascript
复制
use actix_web::{web, App, HttpResponse, HttpServer, ResponseError, Handler};


 fn hello_world() -> HttpResponse {
    HttpResponse::Ok().body("Hello, Mr. World") }
 
 #[actix_web::main] async fn main() -> std::io::Result<()> {
     HttpServer::new(move || App::new().route("hello", web::get().to(hello_world)))
         .bind("0.0.0.0:8000")?
         .run()
         .await
 }

以下是我在版本4或更高版本中遇到的错误。

代码语言:javascript
复制
web::get().to(hello_world)))
    |                                                                  -- ^^^^^^^^^^^ the trait `Handler<_>` is not implemented for `fn() -> HttpResponse {hello_world}`
    |                                                                  |
    |                                                                  required by a bound introduced by this call
    |
note: required by a bound in `Route::to`
   --> /Users/xavierfontvillasenor/.cargo/registry/src/github.com-1ecc6299db9ec823/actix-web-4.1.0/src/route.rs:211:12
    |
211 |         F: Handler<Args>,
    |            ^^^^^^^^^^^^^ required by this bound in `Route::to`

当我加上这个特征时,

代码语言:javascript
复制
impl Handler<T> for HttpResponse {
    type Output = ();
    type Future = ();

    fn call(&self, args: T) -> Self::Future {
        todo!()
    }
}

我得到了

代码语言:javascript
复制
impl Handler<T> for HttpResponse {
  |     -        ^ not found in this scope
  |     |
  |     help: you might be missing a type parameter: `<T>`

这在V.3中是可行的,为什么现在不行?我能做什么?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-06-24 22:31:26

您不打算自己实现Handler,问题是处理程序必须是async函数,有关详细信息,请参阅文档。

代码语言:javascript
复制
// 
async fn hello_world() -> HttpResponse {
    HttpResponse::Ok().body("Hello, Mr. World")
}
票数 2
EN

Stack Overflow用户

发布于 2022-06-26 04:21:36

Actix v4删除了导致此错误的FutureHttpResponse实现。编译器是正确的,但它不是最有用的错误。TL;DR:所有处理程序必须是异步的。

请参阅迁移指南

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72748775

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档