rs_enums
is a Python module inspired by Rust’s Option
and Result
enums. It provides functional programming constructs for handling optional values (Option
) and results (Result
) with success or error outcomes. This module enables safer and more expressive code by allowing you to work with values that may or may not exist, or operations that may succeed or fail.
Option
: Represents a value that may or may not be present (Some
orNone
).Result
: Represents an operation that can either succeed (Ok
) or fail (Err
).- Common methods like
is_some()
,is_none()
,is_ok()
, andis_err()
are included for easy handling of these types.
Install via pip
:
pip install rs_enums
The Option
type represents an optional value that can either be present (Some
) or absent (None
). It provides methods to safely handle values that may not exist.
Some
: Represents a value that is present.is_some()
: Checks if the value is present.is_none()
: Checks if the value is absent.unwrap()
: Returns the value if present; raises an error if absent.expect(message)
: Returns the value if present; raises an error with a custom message if absent.
The Result
type represents a value that can either be successful (Ok
) or erroneous (Err
). It is useful for handling operations that may succeed or fail.
Ok
: Represents a successful value.Err
: Represents an error value.is_ok()
: Checks if the result is successful.is_err()
: Checks if the result is erroneous.unwrap()
: Returns the successful value if present; raises an error if erroneous.expect(message)
: Returns the successful value if present; raises an error with a custom message if erroneous.
from rs_enums.option import Option
from rs_enums.result import Result
# Example of Option
opt = Option(42)
if opt.is_some():
print(opt.unwrap()) # Output: 42
# Example of Result
result = Result.new(value="Success")
if result.is_ok():
print(result.unwrap()) # Output: Success
This project is licensed under the Apache-2.0 license.
We welcome contributions! Please see our CONTRIBUTING.md for guidelines on how to contribute to this project.