rustode
Rust-like Result and Option syntax for TS/JS.
Installation
npm install rustode
Usage/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
true
if the option is aSome
value containing the given value.
- Returns
-
expect(msg: string): T
- Returns the contained
Some
value. ThrowsError
if the value is aNone
with a custom panic message provided bymsg
.
- Returns the contained
-
isNone(): boolean
- Returns
true
if the option is aNone
value.
- Returns
-
isSome(): boolean
- Returns
true
if the option is aSome
value.
- Returns
-
unwrap(): T
- Returns the contained
Some
value. Throws Error if the value equalsNone
.
- Returns the contained
-
unwrapOr(value: T): T
- Returns the contained
Some
value or a provided default.
- Returns the contained
-
unwrapOrElse(fn: () => T): T
- Returns the contained
Some
value or computes it from a closure.
- Returns the contained
Result<T, E>
methods
-
expect(msg: string): T
- Returns the contained
Ok
value. ThrowsError
if 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
Error
if 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
true
if the result isErr
.
- Returns
-
isOk(): boolean
- Returns
true
if the result isOk
.
- Returns
-
unwrap(): T
- Returns the contained
Ok
value. Returns error if the value is anErr
, with an error message
- Returns the contained
-
unwrapOr(value: T): T
- Returns the contained
Ok
value or a provided default.
- Returns the contained