核心机制 / 错误处理
错误处理 Option / Result
Aero 没有异常,用两个枚举表达可选与错误。
Option
enum Option<T> {
Some(T),
None,
}常用方法: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),
}常用方法:is_ok / is_err / unwrap_or / unwrap_err_or
断言
assert(cond); // 假则打印诊断并非零退出
assert_eq(a, b); // 不等则失败终止match 处理错误
match r {
Result::Ok(v) => print("got %d\n", v),
Result::Err(e) => print("failed\n"),
}