gpt4 book ai didi

rust - Rust actix_web::main “expected ` std::result::Result <(),std::io::Error >` because of return type”但建议的类型不起作用

转载 作者:行者123 更新时间:2023-12-03 11:48:02 28 4
gpt4 key购买 nike

我不熟悉 rust ,并开始尝试使用actix_web和sqlx。目标是创建一个简单的开源Blog引擎,但是在实现CLI参数解析器和基本SQL连接池之后,该代码将不再编译。我收到以下错误:

error[E0308]: mismatched types
--> src/main.rs:17:1
|
17 | #[actix_web::main]
| ^^^^^^^^^^^^^^^^^^
| |
| expected enum `std::result::Result`, found `()`
| help: try using a variant of the expected enum: `Ok(#[actix_web::main])`
18 | async fn main() -> std::io::Result<()> {
| ------------------- expected `std::result::Result<(), std::io::Error>` because of return type
|
= note: expected enum `std::result::Result<(), std::io::Error>`
found unit type `()`
= note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to previous error

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

To learn more, run the command again with --verbose.
该错误建议使用 std::result::Result<(), std::io::Error>作为返回类型,但是用它替换当前返回类型时,我得到相同的错误:
error[E0308]: mismatched types
--> src/main.rs:17:1
|
17 | #[actix_web::main]
| ^^^^^^^^^^^^^^^^^^
| |
| expected enum `std::result::Result`, found `()`
| help: try using a variant of the expected enum: `Ok(#[actix_web::main])`
18 | async fn main() -> std::result::Result<(), std::io::Error> {
| --------------------------------------- expected `std::result::Result<(), std::io::Error>` because of return type
|
= note: expected enum `std::result::Result<(), std::io::Error>`
found unit type `()`
= note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to previous error

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

To learn more, run the command again with --verbose.
到目前为止,我找不到任何人遇到同样的问题,也找不到任何原因。
我的代码:
/Cargo.toml
[package]
name = "rusty_read"
version = "0.1.0"
authors = ["LeMoonStar <webmaster@unitcore.de>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
actix-web = "3.0.0-beta.1"
rust-ini = "0.15"
sqlx = { version = "0.4.0-beta.1", features = [ "all-databases", "any", "tls" ] }
clap = "2"
/src/main.rs
use actix_web::{get, web, App, HttpServer, Responder};
use ini::Ini;
use sqlx::pool::Pool;
use sqlx::any;


#[get("/")]
async fn index() -> impl Responder {
format!("index")
}

#[get("/article/{id}")]
async fn article(info: web::Path<u32>) -> impl Responder {
format!("article nr.{}", info)
}

#[actix_web::main]
async fn main() -> std::result::Result<(), std::io::Error> {
let conf = Ini::load_from_file("conf.ini").unwrap_or(Ini::load_from_str("[Server]
bindAddress = \"127.0.0.1:8080\"

[Database]
url = \"mysql://user:password@localhost/blog\"
").unwrap());

let matches = clap::App::new("Rusty Read")
.version("0.1 INDEV")
.author("LeMoonStar <webmaster@unitcore.de>")
.about("a blog engine written in rust")
.subcommand(clap::SubCommand::with_name("config")
.about("sets various configurations for the blog")
.arg(clap::Arg::with_name("address")
.short("a")
.long("adress")
.help("sets the address the http server binds on (eg. 127.0.0.1:8080)")
.takes_value(true))
.arg(clap::Arg::with_name("database")
.short("d")
.long("database")
.help("sets the url to the database (eg. mysql://user:password@localhost/blog)")
.takes_value(true)))
.subcommand(clap::SubCommand::with_name("init_database")
.about("Initializes the database which is set in the conf.ini file (or with the config command)"))

.get_matches();

if let Some(matches) = matches.subcommand_matches("config") {
if matches.is_present("address") {
conf.section(Some("Server")).unwrap()
.insert("bindAddress", "127.0.0.1:8080");
}
if matches.is_present("database") {
conf.section(Some("Database")).unwrap()
.insert("url", "mysql://user:password@localhost/blog");
}
} else if let Some(matches) = matches.subcommand_matches("init_database") {

} else {
let mut section = conf.section(Some("Server")).expect("conf.ini requires a [Server] section.");
let bind_address = section.get("bindAddress").expect("conf.ini's [Server] section requires a bindAdress.");

section = conf.section(Some("Database")).expect("conf.ini requires a [Database] section.");
let db_url = section.get("url").expect("conf.ini's [Database] section requires a url.");

let pool = Pool::<any::Any>::connect(db_url).await.expect("database connection pool could not be created.");

HttpServer::new(move || {
App::new()
.service(article)
.service(index)
.data(pool.clone())
})
.bind(bind_address).expect("could not bind http server")
.run()
.await;
}
}
我希望有人解决此问题,因为在存在此问题的情况下,我无法继续从事该项目。

最佳答案

Ok(())确实在工作。.我之前曾尝试将所有代码复制粘贴,并且做cargo check我遇到了与您在评论中写的相同的问题。然后我尝试清除main函数中的代码,然后再逐段复制粘贴代码。在添加Ok(())作为main的返回之后。甚至在我完成复制粘贴之前,错误就更改了。看
the screenshot of different error on my local.
然后我尝试通过添加clone()来修复它,如下所示:

..
conf.section(Some("Server")).unwrap().clone()
..
conf.section(Some("Database")).unwrap().clone()
..

然后我 cargo run它。服务器工作正常! see the screenshot here

关于rust - Rust actix_web::main “expected ` std::result::Result <(),std::io::Error >` because of return type”但建议的类型不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63287281/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com