Skip to content

How to ? different Error type in the same async block #66831

@CGQAQ

Description

@CGQAQ

according to this article
Using ? with different Error type in the same async block seems unachievable

async fn a() -> Result<(), reqwest::Error> {Ok(())}
async fn b() -> Result<(), std::io::Error> {Ok(())}

fn c() {
    async {
        a().await?;
        b().await?;
        Ok(())    // error msg 1
        // Ok::<(), impl std::error::Error>(())  // error msg 2
    };
}

async fn d() -> Result<(), impl std::error::Error> {
    a().await?;
    b().await?;
    Ok(())
}

1

 --> src/x.rs:6:9
  |
6 |         a().await?;
  |         ^^^^^^^^^^ cannot infer type

2

error[E0562]: `impl Trait` not allowed outside of function and inherent method return types
 --> src/x.rs:8:18
  |
8 |         Ok::<(), impl std::error::Error>(())
  | 

Maybe add something like this? Unify closure and block

async || -> Result<(), impl std::error::Error> {
        a().await?;
        b().await?;
        Ok(())
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions