Module 3Result & LabLesson 8 of 920 min
The ? Operator
Propagate errors in multi-field parsers without pyramid code.
? means
On Result: if Err, return it from the function; if Ok, unwrap the value.
rust
let bid: f64 = part.parse().map_err(|e| format!("{e}"))?;Bid/ask parser with validation
Reject missing fields and crossed markets (ask < bid).
Exercise — already scaffolded; study and extend
Challenge
Also reject non-positive bid/ask.
Tips
- Function signature must return Result for ?
- ok_or converts Option to Result
- Validate invariants after parse
Try it live
The ? OperatorRust 2021
Loading editor…
Output
Press Run to compile and execute.