如果我要在Actix Web 3上使用此代码,它将工作,但我需要使用最新的稳定版本…所以4 ^ .
下面是有问题的代码片段(实际上这是我的全部代码):
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或更高版本中得到的错误。
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`
当我添加性状时,
impl Handler<T> for HttpResponse {
type Output = ();
type Future = ();
fn call(&self, args: T) -> Self::Future {
todo!()
}
}
我
impl Handler<T> for HttpResponse {
| - ^ not found in this scope
| |
| help: you might be missing a type parameter: `<T>`
这在V.3中可以工作,为什么现在不行?我能做什么?
您不打算自己实现Handler
,问题是处理程序需要是async
函数,详细信息请参阅文档:
// 👇
async fn hello_world() -> HttpResponse {
HttpResponse::Ok().body("Hello, Mr. World")
}
Actix Web v4删除了Future
对HttpResponse
的实现,导致此错误。编译器是正确的,但这不是最有用的错误。TL;博士:处理程序必须是异步的。
参见迁移指南