### Coroutine Creation and Interaction Example Source: https://github.com/amanieu/corosensei/blob/master/README.md Demonstrates how to create a coroutine, resume it with input, and handle yielded values or return results. This is useful for implementing cooperative multitasking or complex state machines. ```rust use corosensei::{Coroutine, CoroutineResult}; fn main() { println!("[main] creating coroutine"); let mut coroutine = Coroutine::new(|yielder, input| { println!("[coroutine] coroutine started with input {}", input); for i in 0..5 { println!("[coroutine] yielding {}", i); let input = yielder.suspend(i); println!("[coroutine] got {} from parent", input) } println!("[coroutine] exiting coroutine"); }); let mut counter = 100; loop { println!("[main] resuming coroutine with argument {}", counter); match coroutine.resume(counter) { CoroutineResult::Yield(i) => println!("[main] got {:?} from coroutine", i), CoroutineResult::Return(()) => break, } counter += 1; } println!("[main] exiting"); } ``` -------------------------------- ### Coroutine Panic Propagation Example Source: https://github.com/amanieu/corosensei/blob/master/README.md Demonstrates how a panic within a coroutine unwinds through the coroutine stack and out to the caller. The coroutine becomes unresumable after a panic. ```rust use std::panic::{catch_unwind, AssertUnwindSafe}; use corosensei::Coroutine; fn main() { println!("[main] creating coroutine"); let mut coroutine = Coroutine::new(|yielder, ()| { println!("[coroutine] yielding 42"); yielder.suspend(42); println!("[coroutine] panicking"); panic!("foobar"); }); println!("[main] resuming coroutine"); let result = catch_unwind(AssertUnwindSafe(|| coroutine.resume(()))); println!( "[main] got value {} from coroutine", result.unwrap().as_yield().unwrap() ); println!("[main] resuming coroutine"); let result = catch_unwind(AssertUnwindSafe(|| coroutine.resume(()))); println!( "[main] caught panic \"{}\" from coroutine", result.unwrap_err().downcast_ref::<&'static str>().unwrap() ); println!("[main] exiting"); } ``` -------------------------------- ### Linked Backtrace Example Source: https://github.com/amanieu/corosensei/blob/master/README.md Illustrates how backtraces from within a coroutine include the parent stack, showing where the coroutine was last resumed. This aids in debugging. ```rust use backtrace::Backtrace; use corosensei::Coroutine; #[inline(never)] fn sub_function(coroutine: &mut Coroutine<(), (), ()>) { coroutine.resume(()); } fn main() { let mut coroutine = Coroutine::new(move |_, ()| { let trace = Backtrace::new(); println!("{:?}", trace); }); sub_function(&mut coroutine); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.