Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/hadoop/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rust 什么';在actix web的主要方法中,处理错误的最佳方法是什么?_Rust_Actix Web - Fatal编程技术网

Rust 什么';在actix web的主要方法中,处理错误的最佳方法是什么?

Rust 什么';在actix web的主要方法中,处理错误的最佳方法是什么?,rust,actix-web,Rust,Actix Web,在ActixWeb中,有没有一种方法可以处理main方法中的错误 我有以下代码,正如您所看到的,该函数中有三个地方可能会死机:对数据库URL环境变量的引用、连接池的创建和模板引擎(tera)的初始化 #[actix_web::main] 异步fn main()->std::io::Result{ dotenv().ok(); 让database_url=std::env::var(“database_url”).expect(“database_url必须设置”); let manager=Co

在ActixWeb中,有没有一种方法可以处理main方法中的错误

我有以下代码,正如您所看到的,该函数中有三个地方可能会死机:对数据库URL环境变量的引用、连接池的创建和模板引擎(tera)的初始化

#[actix_web::main]
异步fn main()->std::io::Result{
dotenv().ok();
让database_url=std::env::var(“database_url”).expect(“database_url必须设置”);
let manager=ConnectionManager:::新建(数据库url);
让pool=pool::builder().build(manager).expect(“创建连接池失败”);
HttpServer::新建(移动| |{
让tera=tera::new(“templates/***”).unwrap();
App::new()
.data(pool.clone())
.数据(tera)
.route(“/”,web::get().to(索引))
})
.bind(“127.0.0.1:8000”)?
.run()
.等待
}
因此,我尝试更改该函数的返回类型,并使用以前定义的枚举来处理这些错误:

async fn main() -> Result<HttpServer, ServerError> {
    dotenv().ok();
    let database_url = std::env::var("DATABASE_URL")?;
    let manager = ConnectionManager::<PgConnection>::new(database_url);
    let pool = Pool::builder().build(manager)?;

    HttpServer::new(move || {
        let tera = Tera::new("templates/**/*")?;
        App::new()
            .data(pool.clone())
            .data(tera)
            .route("/", web::get().to(index))
    })
    .bind("127.0.0.1:8000")?
    .run()
    .await
}
async fn main()->结果{
dotenv().ok();
让数据库url=std::env::var(“数据库url”)?;
let manager=ConnectionManager:::新建(数据库url);
让pool=pool::builder().build(manager)?;
HttpServer::新建(移动| |{
让tera=tera::new(“templates/***”)?;
App::new()
.data(pool.clone())
.数据(tera)
.route(“/”,web::get().to(索引))
})
.bind(“127.0.0.1:8000”)?
.run()
.等待
}
但编译器会显示以下消息:

[Running 'cargo run']

error[E0107]: missing generics for struct `HttpServer`
    |
    | async fn main() -> Result<HttpServer, ServerError> {
    |                           ^^^^^^^^^^ expected 4 type arguments
    |

note: struct defined here, with 4 type parameters: `F`, `I`, `S`, `B`

    |
    | pub struct HttpServer<F, I, S, B>
    |            ^^^^^^^^^^ -  -  -  -
help: use angle brackets to add missing type arguments
    |
    | async fn main() -> Result<HttpServer<F, I, S, B>, ServerError> {
    |                                     ^^^^^^^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0107`.
error: could not compile
[运行“货物运行”]
错误[E0107]:缺少结构'HttpServer'的泛型`
|
|异步fn main()->结果{
|^^^^^^^^^^^^^应为4个类型参数
|
注意:这里定义了struct,有4个类型参数:F`、`I`、`S`、`B`
|
|发布结构HttpServer
|            ^^^^^^^^^^ -  -  -  -
帮助:使用尖括号添加缺少的类型参数
|
|异步fn main()->结果{
|                                     ^^^^^^^^^^^^
错误:由于上一个错误而中止
有关此错误的详细信息,请尝试“rustc--explain E0107”。
错误:无法编译

我在这里可以遵循的最佳操作过程是什么?我是否应该在main方法中处理此类错误?

main方法是否需要返回
Ok(HttpServer)
?如果没有,我想你可以尝试使用main方法返回
Result
。你为什么不把第一个原始方法改为return
std::Result::Result
?请注意,
std::io::Result
实际上是
std::Result::Result
的别名/快捷方式,所以所有这一切都需要改变
st>的错误类型d::io::Error
服务器Error
,我想这正是您试图做的。std::io::Result的名称一直困扰着我,我想应该是类似
std::io::IoResult
的东西才能清楚……(与
IoError
的想法相同);它与普通的
std::result::result
略有不同,因为您不能指定自定义错误类型。
[Running 'cargo run']

error[E0107]: missing generics for struct `HttpServer`
    |
    | async fn main() -> Result<HttpServer, ServerError> {
    |                           ^^^^^^^^^^ expected 4 type arguments
    |

note: struct defined here, with 4 type parameters: `F`, `I`, `S`, `B`

    |
    | pub struct HttpServer<F, I, S, B>
    |            ^^^^^^^^^^ -  -  -  -
help: use angle brackets to add missing type arguments
    |
    | async fn main() -> Result<HttpServer<F, I, S, B>, ServerError> {
    |                                     ^^^^^^^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0107`.
error: could not compile