Rust 移动到actix web 3.0时出错
迟做总比不做好,所以我开始重新学习Rust,并决定专注于actix和actix web 我在actix web 1.0中运行了这些代码,但在actix web 3.0中似乎没有运行: main.rsRust 移动到actix web 3.0时出错,rust,actix-web,Rust,Actix Web,迟做总比不做好,所以我开始重新学习Rust,并决定专注于actix和actix web 我在actix web 1.0中运行了这些代码,但在actix web 3.0中似乎没有运行: main.rs use messages_actix::MessageApp; fn main() -> std::io::Result<()> { std::env::set_var("RUST_LOG", "actix_web=info"
use messages_actix::MessageApp;
fn main() -> std::io::Result<()> {
std::env::set_var("RUST_LOG", "actix_web=info");
env_logger::init();
let app = MessageApp::new(8081);
app.run() // error here
}
#[macro_use]
extern crate actix_web;
use actix_web::{middleware, web, App, HttpRequest, HttpServer, Result};
use serde::Serialize;
pub struct MessageApp {
pub port: u16,
}
#[derive(Serialize)]
pub struct IndexResponse{
pub message: String,
}
#[get("/")]
pub fn index(req: HttpRequest) -> Result<web::Json<IndexResponse>> { // error here
let hello = req
.headers()
.get("hello")
.and_then(|v| v.to_str().ok())
.unwrap_or_else(|| "world");
Ok(web::Json(IndexResponse {
message: hello.to_owned(),
}))
}
索引错误:未为fn(HttpRequest)->std::result::result{::register::index}实现trait工厂
impl MessageApp {
pub fn new(port: u16) -> Self {
MessageApp{ port }
}
pub fn run(&self) -> std::io::Result<()> {
println!("Starting HTTP server at 127.0.0.1:{}", self.port);
HttpServer::new(move || {
App::new()
.wrap(middleware::Logger::default())
.service(index)
})
.bind(("127.0.0.1", self.port))?
.workers(8)
.run() //error here
}
}
impl MessageApp{
pub fn new(端口:u16)->Self{
MessageApp{port}
}
发布fn运行(&self)->std::io::Result{
println!(“在127.0.0.1:{},self.port启动HTTP服务器);
HttpServer::新建(移动| |{
App::new()
.wrap(中间件::记录器::默认值())
.服务(索引)
})
.bind(((“127.0.0.1”,自端口))?
.工人(8)
.run()//此处出错
}
}
错误:预期的枚举std::result::result
,找到的结构Server
已检查迁移,但找不到与列出的错误相关的内容
非常感谢您的帮助…感谢…较新版本的actix web
现在使用了异步等待
语法,该语法在Rust 1.39中变得稳定。您必须使处理程序异步:
#[get(“/”)
发布异步fn索引(请求:HttpRequest)->结果{
// ...
}
创建HttpServer
现在是一个async
操作:
impl MessageApp{
发布fn运行(&self)->std::io::Result
HttpServer::新建(…)
.run()
.等待
}
}
您可以使用main
宏在主函数中使用async/await:
#[actix_web::main]
异步fn main()->std::io::Result{
让app=MessageApp::new(8081);
app.run().wait
}
非常有魅力……非常感谢您的帮助。。。