A `yield` statement suspends a coroutine and optionally sends a value to its caller. Resuming continues execution after the yield; returning completes the coroutine, which cannot be resumed again. Omitting the yield value is equivalent to yielding `()`. section 8.2
Listing 8.6 A basic coroutine in Rust
#![feature(coroutines, #1
coroutine_trait, #1
stmt_expr_attributes)] #1
use core::f64::consts::PI;
use std::ops::{Coroutine, CoroutineState};
use std::pin::Pin;
fn main() {
let mut yield_pi = #[coroutine] #2
|| { #3
yield PI; #4
"Coroutine complete!" #5
};
loop {
match Pin::new(&mut yield_pi).resume(()) { #6
CoroutineState::Yielded(val) => { #7
dbg!(&val);
}
CoroutineState::Complete(val) => { #8
dbg!(&val);
break;
}
}
}
}
See a coroutine closure yield π on its first resume, then return a completion string on its second. section 8.2









