Skip to content

Latest commit

 

History

History
40 lines (32 loc) · 774 Bytes

File metadata and controls

40 lines (32 loc) · 774 Bytes
title taxonomy process
Try Operator
category
docs
twig
true

[TOC]

Try operator

The try operator (?) is a control flow operator which causes a function to return early in case the value being tried over has a certain value.

For Option, this causes the function to return if it has the Option::None variant.

fn checked_div_mod(a, b) {
    let div = a.checked_div(b)?;
    Some((div, a % b))
}

pub fn main() {
    if let Some((div, m)) = checked_div_mod(5, 2) {
        println!("Result: {}, {}", div, m);
    }

    if let Some((div, m)) = checked_div_mod(5, 0) {
        println!("Result: {}, {}", div, m);
    }
}
$> cargo run --bin rune -- run scripts/book/try_operator/basic_try.rn
Result: 2, 1
== () (7.4912ms)