On this page
  1. try!
  2. Availability
  3. Syntax & Overview
  4. Notes & Pitfalls
  5. Example

try!

Prev: macro_rules | Up: builtins | Next: std
Rel: Result & ? operator, builtins, Ownership, Borrowing & Lifetimes
Ref: Rust Docs — try!
Tags: #public #prog #rust #builtins #tier/legacy


Availability

Built-in / Prelude (Pre-2018 Macro) — available with zero use imports.

Syntax & Overview

try!(expr)

Legacy Rust 1.0 error-propagation macro superseded by the built-in ? operator.

Legacy / Avoid: Prefer Result & ? operator in all modern Rust.

Notes & Pitfalls

Example

fn sum_two(a: &str, b: &str) -> Result<i32, std::num::ParseIntError> {
    let x: i32 = a.parse()?;
    let y: i32 = b.parse()?;
    Ok(x + y)
}

fn main() {
    assert_eq!(sum_two("10", "32"), Ok(42));
    assert!(sum_two("10", "bad").is_err());
}

Linked from

Related notes