Core Concepts / Error Handling

Error Handling: Option / Result

Aero has no exceptions; it expresses optionality and errors with two enums.

Option

enum Option<T> {
    Some(T),
    None,
}

Common methods: is_some / is_none / unwrap_or(o, def)

let v = vec_new();            // 空 Vec
let x = v.get(0);             // 越界安全返回(见 Vec 语义)

Result

enum Result<T, E> {
    Ok(T),
    Err(E),
}

Common methods: is_ok / is_err / unwrap_or / unwrap_err_or

Assertions

assert(cond);        // 假则打印诊断并非零退出
assert_eq(a, b);     // 不等则失败终止

Handling Errors with match

match r {
    Result::Ok(v) => print("got %d\n", v),
    Result::Err(e) => print("failed\n"),
}
Aero 文档 · 基于 Aero 1.2.0 文档整理 · MIT
SereinTeam