Challenge 3: Rust's ? Operator as Monad-Shaped, With No Mention of "Monad" — Possible Solution ==================================================================== // Concrete Rust example (conceptual, not compiled): // // fn find_user(id: i32) -> Result { // if id == 5 { Ok("alice@example.com".to_string()) } // else { Err("user not found".to_string()) } // } // // fn find_order_by_email(email: &str) -> Result { // if email == "alice@example.com" { Ok("Order #42".to_string()) } // else { Err("no order found".to_string()) } // } // // fn find_recent_order(id: i32) -> Result { // let email = find_user(id)?; // <- the ? operator // let order = find_order_by_email(&email)?; // <- again // Ok(order) // } // // The ? operator does exactly two things at each call site, and both // are exactly what Haskell's >>= does for Either: (1) if the Result // is Err, IMMEDIATELY return that Err from the whole function, // short-circuiting everything after it -- exactly Either's own // short-circuit-on-Left behavior; (2) if the Result is Ok, unwrap the // REAL value inside and bind it to a plain variable (`email`), making // it available to the NEXT line -- exactly what >>= does by handing // the unwrapped value to the next function in the chain. // // find_user(id)?.and_then(|email| find_order_by_email(&email)) would // be the more explicit method-chaining version of the same thing -- // and .and_then() is, structurally, >>= with the arguments in a // different order and Rust's own naming instead of Haskell's. // // Nowhere in Rust's own official documentation, its standard library // naming, or its everyday community vocabulary does the word "monad" // appear -- Rust's designers built and named this mechanism around // practical error-propagation ergonomics, not around formal category // theory terminology. But the STRUCTURE is identical: a type wrapping // a value that might be "there" or "not there" (or "ok" or "error"), // with a chaining operation that unwraps the real value for // successful cases and short-circuits for failure cases. That // structure is, formally, exactly what a monad is -- the behavior // exists in Rust in full, just under different names and without // the formal framing Haskell makes explicit. WHY THIS WORKS AS AN ANSWER ------------------------------ This provides a concrete, realistic Rust example using ? and traces its exact two behaviors (short-circuit on Err, unwrap-and-bind on Ok) directly onto Either's own >>= behavior, correctly noting Rust's genuine avoidance of the word "monad" while confirming the underlying structure is identical -- matching the chapter's own closing claim.