setjmp marks a location and saves an execution context; longjmp restores that context, transferring control non-locally instead of returning through each intervening call. This escape route can reach error-handling code when an error or interrupt occurs deep in a call stack. section 12.9
A Rust implementation connects the control-flow mechanism to LLVM and operating-system facilities:
- Declare LLVM's setjmp and longjmp intrinsics with extern "C" signatures and link_name attributes. section 12.9.2
- Use the required link_llvm_intrinsics crate feature and call the declared functions inside unsafe code. section 12.9.2
- Store the jump buffer and shutdown flag in mutable global state, with the example restricted to non-Windows targets. section 12.9.5
Escaping recursive calls
A recursive dive() call normally prints increasing depth markers and then prints them again while returning. With longjmp, control can leave after the third call, skip the remaining stack frames, and continue in main(), producing an early-return message followed by finishing. section 12.9
Table 12.3 Comparing the intended output from listing 12.13 and listing 12.17 (view table figure)
Compare ordinary recursive unwinding with longjmp's direct transfer to main() through the contrasting depth-marker output. section 12.9


