Nir*_*nde 4 mongodb rust rust-crates actix-web
尝试使用 rust 中的 actix-web 和 mongodb 制作服务器。出现错误
该特征
std::convert::From<mongodb::error::Error>未实现std::io::Error
这是我的代码
use actix_web::{web, App, HttpRequest, HttpServer, Responder};
use mongodb::{options::ClientOptions, Client};
async fn greet(req: HttpRequest) -> impl Responder {
let name = req.match_info().get("name").unwrap_or("World");
format!("Hello {}!", &name)
}
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
// Parse a connection string into an options struct.
let mut client_options = ClientOptions::parse("mongodb://localhost:27017")?;
// Manually set an option.
client_options.app_name = Some("My App".to_string());
// Get a handle to the deployment.
let client = Client::with_options(client_options)?;
// List the names of the databases in that deployment.
for db_name in client.list_database_names(None)? {
println!("{}", db_name);
}
HttpServer::new(|| {
App::new()
.route("/", web::get().to(greet))
.route("/{name}", web::get().to(greet))
})
.bind("127.0.0.1:8000")?
.run()
.await
}
Run Code Online (Sandbox Code Playgroud)
我错过了什么吗?
推理
?这意味着您在末尾调用的函数之一可以返回 a mongodb::error::Error。但 的签名main是 a std::io::Result<()>,这是一个隐含的Result<(), std::io::Error>。它可以接受的唯一错误类型是 io::Error,而不是 mongodb::Error。
看起来您要转义的所有函数都可能返回 this mongodb::error::Error,因此您可以尝试将主签名更改为这样的结果:Result<(). mongodb::error::Error>。但我建议您对这些潜在错误进行适当的错误处理,因为这是您的 main().
解决方案
至少将这些更改?为。程序仍然会崩溃,但它会以对您有意义的.expect("Some error message");方式崩溃。
| 归档时间: |
|
| 查看次数: |
5085 次 |
| 最近记录: |