expression
Generic async functions
The compiler specializes a generic async function for its concrete type arguments. Await the future to obtain its output; each instantiation may have a different anonymous future type. The fixture-only ready_now helper polls once and rejects Pending; it is not an executor. Expected stdout (not executed): 3 6 Common mistake: Expecting different generic instantiations to return the same concrete future type.
Formal Syntax & Structure
rust syntaxrust
async fn count<T: AsRef<str>>(value: T) -> usizeWorking Example
Example Snippetrust
fn main() { use std::{future::Future, task::{Context, Poll, Waker}}; fn ready_now<F: Future>(f: F) -> F::Output { let mut f = std::pin::pin!(f); let mut cx = Context::from_waker(Waker::noop()); match f.as_mut().poll(&mut cx) { Poll::Ready(value) => value, Poll::Pending => panic!("fixture expected immediate readiness"), } } async fn count<T: AsRef<str>>(value: T) -> usize { value.as_ref().len() } println!("{} {}", ready_now(count("tea")), ready_now(count(String::from("coffee"))));}