rustode
v0.1.2
Published
Rust-like Result and Option syntax.
Readme
rustode
Rust-like Result and Option syntax for TS/JS.
Installation
npm install rustodeUsage/Examples
Option<T>
import { Option, some } from "rustode";
let something: Option<string>;
something = some("Hello, World!");
if (something.isSome()) {
console.log(something.unwrap()); // Hello, World!
}import { Option, none } from "rustode";
let something: Option<string>;
something = none();
if (something.isNone()) {
console.log(something.unwrap()); // Error was thrown, attempting to unwrap a None.
}Result<T, E>
import { Result, ok } from "rustode";
let something: Result<string>;
something = ok("Hello, World!");
if (something.isOk()) {
console.log(something.unwrap()); // Hello, World!
}import { Result, err } from "rustode";
let something: Result<string, string>;
something = err("this isn't good...");
if (something.isErr()) {
console.log(something.unwrap()); // Error was thrown, attempting to unwrap an Err.
}Documentation
Option<T> methods
contains(value: Option<T>): boolean- Returns
trueif the option is aSomevalue containing the given value.
- Returns
expect(msg: string): T- Returns the contained
Somevalue. ThrowsErrorif the value is aNonewith a custom panic message provided bymsg.
- Returns the contained
isNone(): boolean- Returns
trueif the option is aNonevalue.
- Returns
isSome(): boolean- Returns
trueif the option is aSomevalue.
- Returns
unwrap(): T- Returns the contained
Somevalue. Throws Error if the value equalsNone.
- Returns the contained
unwrapOr(value: T): T- Returns the contained
Somevalue or a provided default.
- Returns the contained
unwrapOrElse(fn: () => T): T- Returns the contained
Somevalue or computes it from a closure.
- Returns the contained
Result<T, E> methods
expect(msg: string): T- Returns the contained
Okvalue. ThrowsErrorif the value is anErr, with an error message including the passedmsg, and the content of theErr.
- Returns the contained
expectErr(msg: string): E- Returns the contained Err value. Throws
Errorif the value is anOk, with an error message including the passedmsg, and the content of theOk.
- Returns the contained Err value. Throws
isErr(): boolean- Returns
trueif the result isErr.
- Returns
isOk(): boolean- Returns
trueif the result isOk.
- Returns
unwrap(): T- Returns the contained
Okvalue. Returns error if the value is anErr, with an error message
- Returns the contained
unwrapOr(value: T): T- Returns the contained
Okvalue or a provided default.
- Returns the contained
