期望的不透明类型,找到enum"结果"



当我想在rust中像这样匹配函数的结果时:

#[get("/v1/user")]
pub fn user_info(){
match get_user_info(16){
Ok(sk) => {
info!("get user info success:" + sk)
},
Err(e) => {
info!("error:" + e)
},
}
}

显示如下错误:

error[E0308]: mismatched types
--> src/biz/music/test_controller.rs:53:9
|
53 |         Ok(sk) => {
|         ^^^^^^ expected opaque type, found enum `Result`
|
note: while checking the return type of the `async fn`
--> src/common/net/rest_client.rs:4:50
|
4  | pub async fn get_user_info(input_user_id:i64) -> Result<(), Box<dyn std::error::Error>> {
|                                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ checked the `Output` of this `async fn`, expected opaque type
= note: expected opaque type `impl std::future::Future`
found enum `Result<_, _>`
error[E0308]: mismatched types
--> src/biz/music/test_controller.rs:57:9
|
57 |         Err(e) => {
|         ^^^^^^ expected opaque type, found enum `Result`
|
note: while checking the return type of the `async fn`
--> src/common/net/rest_client.rs:4:50

这是get_user_info函数:

pub async fn get_user_info(input_user_id:i64) -> Result<(), Box<dyn std::error::Error>> {
let url = "http://dolphin-post-service.reddwarf-pro.svc.cluster.local:11014/post/user/";
let uid = string_to_static_str(input_user_id.to_string());
let resp = reqwest::get(format!("{}{}", url, uid))
.await?
.json::<HashMap<String, String>>()
.await?;
println!("{:#?}", resp);
Ok(())
}

如何解决这个问题,我从网上搜索,告诉我函数是返回一个Future,但我没有在rust中找到await关键字,如何处理这种情况?我该怎么做才能成功呢?

async fn返回值为FutureFuture是可以产生一个值的异步计算(尽管该值可能为空,例如())。同样在async fn中,您可以使用.await来等待另一个实现Futuretrait的类型完成。

get_user_info是一个async函数(返回一个Future)。因此,要得到实际结果,必须使用await,它将等待get_user_info

的完成
pubasyncfn user_info(){匹配get_user_info(16)。等待{Ok(()) => {dbg !("get user info success:");},Err(e) => {dbg !("错误:");},}}

最新更新