An open API service indexing awesome lists of open source software.

https://github.com/slimenull/rustsharp

Common components for better program design of Rust language
https://github.com/slimenull/rustsharp

Last synced: about 1 year ago
JSON representation

Common components for better program design of Rust language

Awesome Lists containing this project

README

          

# RustSharp

Using Rust coding style in C#.

## Usage

Use the `Result` to handle method return values and error messages:

```csharp
// Wrap a method with "Result" return value

Result ParseInt(string str)
{
try
{
return Result.Ok(int.Parse(str));
}
catch (Exception ex)
{
return Result.Err(ex.Message);
}
}

// Call that method

string input = Console.ReadLine()!;
var result = ParseInt(input);

// Check if value is "Ok" or "Err"

var isOk = result.IsOk;
var isErr = result.IsErr;

// Match the result values

result.Match(
(ok) => {
Console.WriteLine($"The integer you typed is {ok}");
},
(err) => {
Console.WriteLine($"The string you typed is not a integer, {err}");
});

// You can also use switch statement

switch (result)
{
case OkResult ok:
Console.WriteLine($"The integer you typed is {ok.Value}");
break;
case ErrResult err:
Console.WriteLine($"The string you typed is not a integer, {err.Value}");
break;
}
```

Use `Option` in C#

```csharp
// Write a simple method with Option return value

Option Divide(float a, float b)
{
if (b != 0.0) {
return Option.Some(a / b);
} else {
return Option.None();
}
}

// call that method

var option = Divide(114, 514);

// Check if the Option has value

var isSome = option.IsSome;
var isNone = option.IsNone;

// Match the result values

option.Match(
(value) => {
Console.WriteLine($"Result: {value}");
},
() => {
Console.WriteLine("No value");
});

// Or use switch statement

switch (option)
{
case SomeOption some:
Console.WriteLine($"Result: {some.Value}");
break;
default:
Console.WriteLine("No value");
break;
}
```

Common methods of Result and Option

```csharp
// Convert Result to Option

var result = Result.Ok("Success value");
var option = result.Ok(); // returns Option contains the success value

// Unwrap Result and Option

var result = Result.Ok("Success value");
string value = result.Unwrap();
```