如果我要在Actix 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中是可行的,为什么现在不行?我能做什么?
发布于 2022-06-24 22:31:26
您不打算自己实现Handler,问题是处理程序必须是async函数,有关详细信息,请参阅文档。
//
async fn hello_world() -> HttpResponse {
HttpResponse::Ok().body("Hello, Mr. World")
}发布于 2022-06-26 04:21:36
Actix v4删除了导致此错误的Future的HttpResponse实现。编译器是正确的,但它不是最有用的错误。TL;DR:所有处理程序必须是异步的。
请参阅迁移指南
https://stackoverflow.com/questions/72748775
复制相似问题