# Return type annotation of async block

**URL:** <https://internals.rust-lang.org/t/return-type-annotation-of-async-block/12561>\
**Category:** language design\
**Created:** [June 15, 2020, 11:48am UTC](https://internals.rust-lang.org/t/return-type-annotation-of-async-block/12561 "2020-06-15T11:48:16Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![upsuper](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/upsuper/32/1074_2.png) [@upsuper](https://internals.rust-lang.org/u/upsuper)\
**Post date:** [June 15, 2020, 11:48am UTC](https://internals.rust-lang.org/t/return-type-annotation-of-async-block/12561/1 "2020-06-15T11:48:17Z")

</div>

Currently there is no way to specify return type of a `async` block other than having full annotated return value. In some cases it is not that easy either, e.g. if you have

```rust
async move {
    loop {
        let item = stream.next()?;
        process(item);
    }
}

```

I propose that we add syntax to allow specifying return type of a `async` block similar to how return type can be annotated on closures:

```rust
async move -> Result<(), Error> {
    // ...
}

```

I briefly searched in the original [RFC](https://rust-lang.github.io/rfcs/2394-async_await.html) for async-await and the internal forum, but didn't see anything proposed for this.

What do you think about this?

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [June 15, 2020, 11:59am UTC](https://internals.rust-lang.org/t/return-type-annotation-of-async-block/12561/2 "2020-06-15T11:59:26Z")

</div>

How about

```rust
async move {
    {
        // ...
    } as Result<(), Error>
}

```

or with `type_ascription`

```rust
async move {
    {
        // ...
    }: Result<(), Error>
}

```

Also.. what exactly do you mean by

> [@upsuper](#):
>
> full annotated return value

and why is it not desirable? Or is this just about moving a type annotation from one place to another?

Finally I don’t really like reusing a `->` notation for something that is not a function or a closure (even though a future is a bit _like_ a function).

---

<div class="post-metadata">

**Author:** ![Ixrec](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ixrec/32/6754_2.png) [@Ixrec](https://internals.rust-lang.org/u/Ixrec)\
**Post date:** [June 15, 2020, 12:01pm UTC](https://internals.rust-lang.org/t/return-type-annotation-of-async-block/12561/3 "2020-06-15T12:01:45Z")

</div>

I'm also wondering why an "immediately invoked closure" wouldn't be good enough. Unless I'm missing something, it'd be only a few extra characters over this proposal in all cases.

---

<div class="post-metadata">

**Author:** ![upsuper](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/upsuper/32/1074_2.png) [@upsuper](https://internals.rust-lang.org/u/upsuper)\
**Post date:** [June 15, 2020, 12:24pm UTC](https://internals.rust-lang.org/t/return-type-annotation-of-async-block/12561/4 "2020-06-15T12:24:20Z")

</div>

> [@steffahn](#):
>
> what exactly do you mean by "full annotated return value" and why is it not desirable?

It's the thing mentioned in the [async book](https://rust-lang.github.io/async-book/07_workarounds/03_err_in_async_blocks.html):

```rust
Ok::<(), MyError>(()) // <- note the explicit type annotation here

```

It's undesired because it's an unusual syntax and doesn't feel intuitive. And it's not quite doable in this case where there is an infinite loop.

> [@steffahn](#):
>
> or with `type_ascription`

This looks nice but I personally don't feel very comfortable with it.

Also I think it's really just imitating closures. There is no reason that any of the approaches you mentioned doesn't work with closures, but I still find it's clearer to use `-> ReturnType` for annotating return type of closure when it can't be induced otherwise.

It's probably just a personal feeling thing, though.

---

<div class="post-metadata">

**Author:** ![upsuper](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/upsuper/32/1074_2.png) [@upsuper](https://internals.rust-lang.org/u/upsuper)\
**Post date:** [June 15, 2020, 12:27pm UTC](https://internals.rust-lang.org/t/return-type-annotation-of-async-block/12561/6 "2020-06-15T12:27:28Z")

</div>

How do you do that? You mean something like

```rust
move || -> impl Future<Output = Result<(), Error>> {
    async move || {
        // ...
    }
}()

```

?

This doesn't look like "a few extra characters", and I failed to figure out anything simpler. Maybe I'm missing something. Could you elaborate?

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [June 15, 2020, 12:42pm UTC](https://internals.rust-lang.org/t/return-type-annotation-of-async-block/12561/7 "2020-06-15T12:42:04Z")

</div>

> [@upsuper](#):
>
> ```rust
> Ok::<(), MyError>(()) // <- note the explicit type annotation here
> 
> ```
> 
> It's undesired because it's an unusual syntax and doesn't feel intuitive.

Maybe `Ok(()) as Result<(), MyError>` feels more intuitive then.

The other approach using type parameters for `Ok` has the advantage that you can save repeating the return type:

`Ok::<_, MyError>(something_with_complicated_type())`

> [@upsuper](#):
>
> ```rust
> move || -> impl Future<Output = Result<(), Error>> {
> async move || {
> // ...
> }
> }()
> 
> ```
> 
> ?
> 
> This doesn't look like "a few extra characters", and I failed to figure out anything simpler. Maybe I'm missing something. Could you elaborate?

I suppose what @Ixrec might have meant was something like

```rust
async move { move || -> Result<(), Error> {
    // ...
}()}

```

I’m not sure if the `async_closure` feature includes the syntax

```rust
async move || -> Result<(), Error> {
    // ...
}()

```

Also note that I’m not necessarily against your idea (just against the use of an arrow). I would (personally) probably prefer it to look like this, especially also to more visibly differentiate it from the closure variant

```rust
async move Result<(), Error> {
    // ...
}

```

One should also consider that something like this could be useful with `try` blocks. In case we really want this, we should consider how this works syntactically similar for both.

---

<div class="post-metadata">

**Author:** ![Aloso](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/aloso/32/5039_2.png) [@Aloso](https://internals.rust-lang.org/u/Aloso)\
**Post date:** [June 16, 2020, 12:44pm UTC](https://internals.rust-lang.org/t/return-type-annotation-of-async-block/12561/8 "2020-06-16T12:44:54Z")

</div>

> [@steffahn](#):
>
> I’m not sure if the `async_closure` feature includes the syntax
> 
> ```rust
> async move || -> Result<(), Error> {
> // ...
> }()
> 
> ```

It does!

I initially thought that using an async closure would be more limiting than an async block, because you can `return`, `break`, `continue` and `?` across regular blocks but not across closures. However, control flow in async blocks has the same limitations as in closures. So if I'm not mistaken, it's always possible to convert an async block to an immediately-invoked async closure.

---

<div class="post-metadata">

**Author:** ![jjpe](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jjpe/32/9779_2.png) [@jjpe](https://internals.rust-lang.org/u/jjpe)\
**Post date:** [June 16, 2020, 10:39pm UTC](https://internals.rust-lang.org/t/return-type-annotation-of-async-block/12561/9 "2020-06-16T22:39:11Z")

</div>

I think it's bad idea to special-case this. Once `impl Trait` can be used to specify the types of locals, this problem will solve itself.

If, on the other hand, we special case this, we get clunky syntax for an even clunkier concept until the rest of time due to stability guarantees.

I believe the proper fix for this is just building out `impl Trait` so it can be used to its full potential.

---

<div class="post-metadata">

**Author:** ![upsuper](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/upsuper/32/1074_2.png) [@upsuper](https://internals.rust-lang.org/u/upsuper)\
**Post date:** [June 17, 2020, 7:49am UTC](https://internals.rust-lang.org/t/return-type-annotation-of-async-block/12561/10 "2020-06-17T07:49:21Z")

</div>

> [@jjpe](#):
>
> I think it's bad idea to special-case this. Once `impl Trait` can be used to specify the types of locals, this problem will solve itself.

That would only help when you assign the value to a local variable. `async`-block, however, just like closures, very often used as function arguments, and the function parameters are usually generic themselves as well. For example,

```rust
tokio::spawn(async move { ... });

```

---

<div class="post-metadata">

**Author:** ![upsuper](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/upsuper/32/1074_2.png) [@upsuper](https://internals.rust-lang.org/u/upsuper)\
**Post date:** [June 17, 2020, 7:52am UTC](https://internals.rust-lang.org/t/return-type-annotation-of-async-block/12561/11 "2020-06-17T07:52:12Z")

</div>

> [@steffahn](#):
>
> One should also consider that something like this could be useful with `try` blocks. In case we really want this, we should consider how this works syntactically similar for both.

It's actually more tricky when it comes to `try` blocks. If we follow the idea of `async` function, the annotation should be the `Future`'s `Output` type rather than the `Future` itself. And since `try` block is probably going to get `Ok`-wrapping, its type annotation would probably need to be the type of the `Ok` branch... which could be more controversial.

---

<div class="post-metadata">

**Author:** ![Nemo157](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nemo157/32/11585_2.png) [@Nemo157](https://internals.rust-lang.org/u/Nemo157)\
**Post date:** [June 17, 2020, 8:29am UTC](https://internals.rust-lang.org/t/return-type-annotation-of-async-block/12561/12 "2020-06-17T08:29:58Z")

</div>

One option would be to lean into the fact that an `async` block _is_ a closure to disconnect it from `try` blocks and tie the syntax more towards `Fn`-closures as proposed in the OP (there's a reason `async move` is a thing but `try move` isn't). Then, similar to `Fn`-closures being annotated with the type the closure returns, not the type of the closure, `Future`-closures should be annotated with the type the closure returns, not the type of the closure. (And `async` closures which are `Fn`-closures returning `Future`-closures are annotated with the final return type of both closures, not the type of either of the closures making it up).

```rust
let future
    : impl Future<Output = u32>
    = async move -> u32 { todo!() };

let function
    : impl Fn() -> u32
    = || -> u32 { todo!() };

let async_function
    : impl Fn() -> impl Future<Output = u32>
    = async move || -> u32 { todo!() };

```

---

<div class="post-metadata">

**Author:** ![jjpe](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jjpe/32/9779_2.png) [@jjpe](https://internals.rust-lang.org/u/jjpe)\
**Post date:** [June 17, 2020, 1:07pm UTC](https://internals.rust-lang.org/t/return-type-annotation-of-async-block/12561/13 "2020-06-17T13:07:43Z")

</div>

> [@upsuper](#):
>
> That would only help when you assign the value to a local variable. `async` -block, however, just like closures, very often used as function arguments, and the function parameters are usually generic themselves as well. For example,
> 
> ```rust
> tokio::spawn(async move { ... });
> 
> ```

If `impl Trait` can be used to its full potential, I don't see why those use cases would be a problem.

Mind you, it can still be some work to actually specify the types, but at least it will be possible.

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [June 17, 2020, 5:30pm UTC](https://internals.rust-lang.org/t/return-type-annotation-of-async-block/12561/14 "2020-06-17T17:30:28Z")

</div>

> [@upsuper](#):
>
> `tokio::spawn(async move { ... });`

I suppose one could also solve these kind of type-annotations with a method

```rust
use std::future::Future;
trait Outputting: Sized {
    fn outputting<O>(self) -> Self
    where Self: Future<Output=O>
    {
        self
    }
}
impl<T: Future> Outputting for T {}

fn foo() {
    let x = tokio::spawn(async move {1u8.into()}.outputting::<u32>());
}

```

---

<div class="post-metadata">

**Author:** ![upsuper](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/upsuper/32/1074_2.png) [@upsuper](https://internals.rust-lang.org/u/upsuper)\
**Post date:** [June 18, 2020, 7:23am UTC](https://internals.rust-lang.org/t/return-type-annotation-of-async-block/12561/15 "2020-06-18T07:23:21Z")

</div>

> [@jjpe](#):
>
> Mind you, it can still be some work to actually specify the types, but at least it will be possible.

There are lots of ways to make it possible for sure.

Rust doesn't go the Python path that everything should have only one right way, so there are lots of things which can be done otherwise still have some syntax sugar to make it either more straightforward or more ergonomic.

All your tricks can apply to closures as well, but as I mentioned before, I still believe the return type annotation of closure is nicer in general.

---

<div class="post-metadata">

**Author:** ![jjpe](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jjpe/32/9779_2.png) [@jjpe](https://internals.rust-lang.org/u/jjpe)\
**Post date:** [June 18, 2020, 11:26am UTC](https://internals.rust-lang.org/t/return-type-annotation-of-async-block/12561/16 "2020-06-18T11:26:32Z")

</div>

> [@upsuper](#):
>
> Rust doesn't go the Python path that everything should have only one right way,

No it doesn't. However that shouldn't mean that just every old redundant feature can or should be thrown in, because such features tend to cruft up any language that incorporates them on the long run.

Maybe you remember that inheritance in OO languages was once heralded as a really nifty feature. But that hype didn't mean the feature was pulling its weight, and by the time folks figured that out, it was way too late for the languages that incorporated them. Add a few more of such misfeatures (e.g. enums that aren't ADTs but semi-useless sugar for classes and thus can't be matched on, or a type system that isn't quite comprehensive enough, etc) and you get Java, essentially.

If you want syntax sugar for specifying types, you can always reach for a macro. It'll work without having to burden the language with features that are not only a bad idea (my opinion) but redundant (fact).

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [September 16, 2020, 11:26am UTC](https://internals.rust-lang.org/t/return-type-annotation-of-async-block/12561/17 "2020-09-16T11:26:48Z")

</div>

Closed on September 16, 2020, 11:26am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
