Core Intermediate

08  Error handling

Rust models absence and failure in the type system: return an Option for a maybe-missing char, then use ? to propagate parse errors.

Step 2 / 2

Parse both strings as i32 and return their sum in add_str, using ? to propagate errors.

Hints
  • s.parse::<i32>()? unwraps on success or returns the error early; finish with Ok(...).
08_error_handling.rs
Reset to starterCompiled in a sandbox; hidden checks must pass.
Reveal solution
fn add_str(a: &str, b: &str) -> Result<i32, std::num::ParseIntError> {
    let x = a.parse::<i32>()?;
    let y = b.parse::<i32>()?;
    Ok(x + y)
}