std:core
Core types and utilities available in every Pleno program.
Import
These types are available globally without explicit import.
Result Type
The Result<T, E> type represents either success (Ok) or failure (Err).
Ok<T>(value: T): Result<T, E>
Creates a successful result containing value.
pleno
fn divide(a: Float, b: Float): Result<Float, String> {
if (b == 0.0) {
return Err("Division by zero");
}
return Ok(a / b);
}
print(divide(10.0, 4.0).unwrap()); // 2.5RunErr<E>(error: E): Result<T, E>
Creates a failure result containing error.
Result Methods
isOk()returnstrueforOk.isErr()returnstrueforErr.unwrap()returns the value, or throwscalled unwrap on Err.unwrapOr(default)returns the value, ordefaultforErr.expect(message)returns the value, or throwsmessage.
pleno
let good = Ok(42);
let bad = Err("timeout");
print(good.isOk()); // true
print(bad.isErr()); // true
print(good.unwrap()); // 42
print(bad.unwrapOr(0)); // 0
print(good.expect("no value")); // 42RunOption Type
The Option<T> type represents an optional value.
Some<T>(value: T): Option<T>
Creates an option containing a value.
pleno
let users = ["Ada", "Grace"];
fn findUser(id: Int): Option<String> {
if (id >= 0 && id < users.length) {
return Some(users[id]);
}
return None;
}
print(findUser(0).unwrapOr("nobody")); // Ada
print(findUser(9).unwrapOr("nobody")); // nobodyRunNone: Option<T>
Represents the absence of a value.
Option Methods
isSome()returnstrueif there is a value.isNone()returnstrueif there is no value.unwrap()returns the value, or throwscalled unwrap on None.unwrapOr(default)returns the value, ordefaultforNone.expect(message)returns the value, or throwsmessage.
pleno
let name = Some("Ada");
let missing: Option<String> = None;
print(name.isSome()); // true
print(missing.isNone()); // true
print(name.unwrap()); // Ada
print(missing.unwrapOr("unknown")); // unknown
print(name.expect("no name")); // AdaRun