happy-rusty
    Preparing search index...

    Function Ok

    • Creates a Result<T, E> representing a successful outcome containing a value. This function is used to construct a Result that signifies the operation was successful by containing the value T.

      Type Parameters

      • T

        The type of the value to be contained in the Ok result.

      • E = never

        The type of the error that the result could potentially contain (not used in this case).

      Parameters

      • value: T

        The value to wrap as an Ok result.

      Returns Result<T, E>

      A Result<T, E> that contains the provided value, representing the Ok case.

      1.0.0

      const goodResult = Ok<number, Error>(1); // Result<number, Error> with a value
      if (goodResult.isOk()) {
      console.log(goodResult.unwrap()); // Outputs: 1
      }
    • Creates a Result<void, E> representing a successful outcome with no value. This overload is used when the operation succeeds but doesn't produce a meaningful value.

      In Rust, this would be Ok(()) using the unit type (). Since JavaScript doesn't have a unit type, we use void instead.

      Type Parameters

      • E = never

        The type of the error that the result could potentially contain.

      Returns Result<void, E>

      A Result<void, E> representing a successful operation with no value.

      1.4.0

      function saveToFile(path: string): Result<void, Error> {
      try {
      fs.writeFileSync(path, data);
      return Ok(); // Success with no return value
      } catch (e) {
      return Err(e as Error);
      }
      }

      const result = saveToFile('/tmp/data.txt');
      if (result.isOk()) {
      console.log('File saved successfully');
      }