When handwrite future ,when use await?

I am a newbie to rust, and my English is not very good. Why do some libraries in rust implement futures manually to realize their functions instead of using await combination? Is there any difference between them? Can you explain with examples? , For example, is there a difference between the following pseudo code fut3 and fut4? The newbie question may be a bit stupid, I feel a little embarrassed,Anyway, best wishes to all readers!

async fn sing_song(song: Song) { /* ... */ }

let fut3=async {
    let x=learn_song.await;
    sing_song(x).await;
}

let fut4=learn_song.then(|x|{
    sing_song(x)
})

The futures crate predates the async-await syntax. Those code may originally written before the async-await syntax added into the language. Or, as always, they may just prefer not to .await it.

1 Like

One of the common reasons is async methods. We don't (yet) have async method in traits in Rust (although there is the excellent async-trait crate for that, it is not always suitable), and so if you want to return a future from a trait method, you need it as an associated type. For that, it needs to be named, and async futures are anonymous:

trait AsyncTrait {
    type Ret: Future<Output = i32>;
    fn m(self) -> Self::Ret;
}

struct S;
impl AsyncTrait for S {
    type Ret = SFuture;
    fn m(self) -> Self::Ret { SFuture(self) }
}
struct SFuture(S);
impl Future for SFuture { ... }

In the future we'll have impl Trait in associated types, then this pattern will diminish. You can try now on nightly (playground):

impl AsyncTrait for S {
    type Ret = impl Future<Output = i32>;
    fn m(self) -> Self::Ret {
        async move { ... }
    }
}