The Option type. See the module level documentation for more.

In Rust

See

Option

Hierarchy

  • OptionImpl

Methods

  • Returns true if the option is a Some value.

    Exapmles

    const x: Option<number> = Some(2);
    assert.equal(x.isSome(), true);

    const y: Option<number> = None;
    assert.equal(y.isSome(), false);

    In Rust

    Type Parameters

    • T

    Parameters

    Returns this is Some<T>

  • Returns true if the option is a None value.

    Examples

    const x: Option<number> = Some(2);
    assert.equal(x.isNone(), false);

    const y: Option<number> = None;
    assert.equal(y.isNone(), true);

    In Rust

    Type Parameters

    • T

    Parameters

    Returns this is None

  • Returns the contained Some value or a provided default.

    Arguments passed to unwrapOr are eagerly evaluated; if you are passing the result of a function call, it is recommended to use unwrapOrElse, which is lazily evaluated.

    Examples

    assert.equal(Some('car').unwrapOr('bike'), 'car');
    assert.equal(None.unwrapOr('bike'), 'bike');

    In Rust

    Type Parameters

    • T

    Parameters

    Returns T

  • Returns the contained Some value or computes it from a closure.

    Examples

    const k = 10;
    assert.equal(Some(4).unwrapOrElse(() => 2 * k), 4);
    assert.equal(None.unwrapOrElse(() => 2 * k), 20);

    In Rust

    Type Parameters

    • T

    Parameters

    • this: Option<T>
    • f: (() => T)
        • (): T
        • Returns T

    Returns T

  • Maps an Option<T> to Option<U> by applying a function to a contained value.

    Examples

    Converts an Option<string> into an Option<number>, consuming the original:

    const maybeSomeString = Some('Hello, World!');
    const maybeSomeLen = maybeSomeString.map((s) => s.length);

    assert.deepEqual(maybeSomeLen, Some(13));

    In Rust

    Type Parameters

    • T

    • U

    Parameters

    • this: Option<T>
    • f: ((arg: T) => U)
        • (arg: T): U
        • Parameters

          • arg: T

          Returns U

    Returns Option<U>

  • Returns the provided default result (if none), or applies a function to the contained value (if any).

    Arguments passed to map_or are eagerly evaluated; if you are passing the result of a function call, it is recommended to use mapOrElse, which is lazily evaluated.

    Examples

    const x = Some('foo');
    assert.equal(x.mapOr(42, (v) => v.length), 3);

    const y = None as Option<string>;
    assert.equal(y.mapOr(42, (v) => v.length), 42);

    In Rust

    Type Parameters

    • T

    • U

    Parameters

    • this: Option<T>
    • def: U
    • f: ((arg: T) => U)
        • (arg: T): U
        • Parameters

          • arg: T

          Returns U

    Returns U

  • Computes a default function result (if none), or applies a different function to the contained value (if any).

    Examples

    const k = 21;

    const x = Some('foo');
    assert.equal(x.mapOrElse(() => 2 * k, (v) => v.length), 3);

    const y = None as Option<string>;
    assert.equal(y.mapOrElse(() => 2 * k, (v) => v.length), 42);

    In Rust

    Type Parameters

    • T

    • U

    Parameters

    • this: Option<T>
    • def: (() => U)
        • (): U
        • Returns U

    • f: ((arg: T) => U)
        • (arg: T): U
        • Parameters

          • arg: T

          Returns U

    Returns U

  • Returns None if the option is None, otherwise returns optb.

    Examples

    const a = Some(2);
    const b: Option<string> = None;
    assert.deepEqual(a.and(b), None);

    const c: Option<number> = None;
    const d = Some('foo');
    assert.deepEqual(c.and(d), None);

    const e = Some(2);
    const f = Some('foo');
    assert.deepEqual(e.and(f), Some('foo'));

    const g: Option<number> = None;
    const h: Option<string> = None;
    assert.deepEqual(g.and(h), None);

    In Rust

    Type Parameters

    • T

    • U

    Parameters

    Returns Option<U>

  • Returns None if the option is None, otherwise calls f with the wrapped value and returns the result.

    Some languages call this operation flatmap.

    Examples

    const evenThenToString = (x: number): Option<string> => {
    return x % 2 === 0 ? Some(x.toString()) : None;
    };

    assert.deepEqual(Some(2).andThen(evenThenToString), Some('2'));
    assert.deepEqual(Some(5).andThen(evenThenToString), None); // odd!
    assert.deepEqual(None.andThen(evenThenToString), None);

    Often used to chain fallible operations that may return None.

    const arr2d = [['A0', 'A1'], ['B0', 'B1']];

    const item01 = Option.fromUndefinable(arr2d.at(0)).andThen((row) => {
    return Option.fromUndefinable(row.at(1));
    });
    assert.deepEqual(item01, Some('A1'));

    const item20 = Option.fromUndefinable(arr2d.at(2)).andThen((row) => {
    return Option.fromUndefinable(row.at(0));
    });
    assert.deepEqual(item20, None);

    In Rust

    Type Parameters

    • T

    • U

    Parameters

    Returns Option<U>

  • Returns None if the option is None, otherwise calls predicate with the wrapped value and returns:

    • Some(t) if predicate returns true (where t is the wrapped value), and
    • None if predicate returns false.

    This function works similar to Array.prototype.filter. You can imagine the Option<T> being an array over one or zero elements. filter() lets you decide which elements to keep.

    Examples

    const isEven = (n: number): boolean => n % 2 == 0;

    assert.deepEqual(None.filter(isEven), None);
    assert.deepEqual(Some(3).filter(isEven), None);
    assert.deepEqual(Some(4).filter(isEven), Some(4));

    In Rust

    Type Parameters

    • T

    Parameters

    • this: Option<T>
    • predicate: ((arg: T) => boolean)
        • (arg: T): boolean
        • Parameters

          • arg: T

          Returns boolean

    Returns Option<T>

  • Returns the option if it contains a value, otherwise returns optb.

    Arguments passed to or are eagerly evaluated; if you are passing the result of a function call, it is recommended to use orElse, which is lazily evaluated.

    Examples

    const a = Some(2);
    const b = None;
    assert.deepEqual(a.or(b), Some(2));

    const c = None;
    const d = Some(100);
    assert.deepEqual(c.or(d), Some(100));

    const e = Some(2);
    const f = Some(100);
    assert.deepEqual(e.or(f), Some(2));

    const g = None as Option<number>;
    const h = None;
    assert.deepEqual(g.or(h), None);

    In Rust

    Type Parameters

    • T

    Parameters

    Returns Option<T>

  • Returns the option if it contains a value, otherwise calls f and returns the result.

    Examples

    const nobody = (): Option<string> => None;
    const vikings = (): Option<string> => Some('vikings');

    assert.deepEqual(Some('barbarians').orElse(vikings), Some('barbarians'));
    assert.deepEqual(None.orElse(vikings), Some('vikings'));
    assert.deepEqual(None.orElse(nobody), None);

    In Rust

    Type Parameters

    • T

    Parameters

    Returns Option<T>

  • Returns Some if exactly one of this, optb is Some, otherwise returns None.

    Examples

    const a = Some(2);
    const b = None as Option<number>;
    assert.deepEqual(a.xor(b), Some(2));

    const c = None as Option<number>;
    const d = Some(2);
    assert.deepEqual(c.xor(d), Some(2));

    const e = Some(2);
    const f = Some(2);
    assert.deepEqual(e.xor(f), None);

    const g = None as Option<number>;
    const h = None as Option<number>;
    assert.deepEqual(g.xor(h), None);

    In Rust

    Type Parameters

    • T

    Parameters

    Returns Option<T>

  • Zips this with another Option.

    If this is Some(s) and other is Some(o), this method returns Some((s, o)). Otherwise, None is returned.

    Examples

    const x = Some(1);
    const y = Some('hi');
    const z = None as Option<number>;

    assert.deepEqual(x.zip(y), Some([1, 'hi']));
    assert.deepEqual(x.zip(z), None);

    In Rust

    Type Parameters

    • T

    • U

    Parameters

    Returns Option<[T, U]>

  • Converts from Option<Option<T>> to Option<T>.

    Examples

    Basic usage:

    const x: Option<Option<number>> = Some(Some(6));
    assert.deepEqual(x.flatten(), Some(6));

    const y: Option<Option<number>> = Some(None);
    assert.deepEqual(y.flatten(), None);

    const z = None as Option<Option<number>>;
    assert.deepEqual(z.flatten(), None);

    Flattening only removes one level of nesting at a time:

    const x: Option<Option<Option<number>>> = Some(Some(Some(6)));
    assert.deepEqual(x.flatten(), Some(Some(6)));
    assert.deepEqual(x.flatten().flatten(), Some(6));

    In Rust

    Type Parameters

    • U

    Parameters

    Returns Option<U>

Generated using TypeDoc