Skip to content

Sebastians-codes/RustLikeReturnValues

Repository files navigation

RustLikeReturnValues

RustLikeReturnValues brings Rust-inspired Option<T> and Result<T, E> types to modern C#. The libraries focus on ergonomic APIs, predictable performance, and explicit success/failure handling.

Packages

  • RustLikeOption – expressive option type with helpers like Map, AndThen, Filter, JSON converters, and IEnumerable interop.
  • RustLikeResult – rich result type with pattern matching, async helpers, JSON converters, and composition primitives.
  • RustLikeExtension – utility extensions built on top of Option/Result, including collection helpers, Try wrappers, and the lightweight RLogger<T>.

Why Rust-Like Values in C#?

  • Keeps error handling explicit without exceptions dominating control flow.
  • Encourages railway-oriented pipelines with fluent APIs.
  • Minimises allocations for common paths; optimised for net9.0 with nullable reference types.
  • Plays nicely with LINQ, async/await, and JSON serialisation out of the box.

Getting Started

  1. Add the projects you need to your solution (project references today, NuGet packages when published).
  2. Restore and build:
    dotnet restore
    dotnet build
  3. Import namespaces:
    using RustLikeValues.RustLikeOption;
    using RustLikeValues.RustLikeResult;
    using RustLikeValues.RustLikeExtension;

Option Quick Look

var maybePort = Option.TryCreate(() => int.Parse(Environment.GetEnvironmentVariable("PORT")));

var port = maybePort
    .Filter(p => p is >= 1024 and <= 65535)
    .UnwrapOr(8080);

Console.WriteLine($"Starting server on port {port}");

Key points:

  • Construct with Option.Some(value) or Option.None, or rely on implicit conversions via ToOption().
  • Use Map, MapAsync, AndThen, Filter, Unwrap, UnwrapOr, and Match for fluent workflows.

Result Quick Look

Result<int, string> CalculateTotal(IEnumerable<string> rawValues)
{
    return rawValues
        .Select(value => Result.Try(() => int.Parse(value)))
        .Sequence()
        .Map(numbers => numbers.Sum())
        .MapErr(ex => ex.Message);
}

var total = CalculateTotal(["10", "20", "30"]).Match(
    ok => $"Total: {ok}",
    err => $"Failed: {err}"
);

Console.WriteLine(total);

Highlights:

  • Construct with Result.Ok(value) / Result.Err(error) or implicit helpers (OkValue, ErrValue).
  • Compose pipelines with Map, MapErr, AndThen, async variants, Transpose, and Sequence.
  • Extract values via Match, Unwrap, UnwrapOr, and UnwrapErr when appropriate.

Extensions & Utilities

  • Collection helpers: Sequence, FilterMap, FindFirst, FindLast, and dictionary Get extensions.
  • Result.Try wrapper captures exceptions as Result<T, Exception>.
  • RLogger<T> composes console/file/custom providers and returns Result<Unit, Exception> so logging failures stay explicit.

JSON Integration

Option<T> and Result<T, E> ship with System.Text.Json converters so you can serialise and deserialise them without additional setup.

License

MIT – see LICENSE if present, or add your preferred license file before distributing.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages