5 releases (3 breaking)
Uses new Rust 2024
0.4.1 | Mar 15, 2025 |
---|---|
0.4.0 | Mar 15, 2025 |
0.3.0 | Mar 15, 2025 |
0.2.0 | Mar 15, 2025 |
0.1.0 | Mar 14, 2025 |
#1 in #result
376 downloads per month
49KB
1.5K
SLoC
better_option_result 🦀
A library that provides enhanced versions of Rust's Option
and Result
types with consistent naming conventions.
The Problem
Rust's standard library Option
and Result
types have fantastic methods, but their naming conventions are either inconsistent with the rest of the library, or too vague. Some examples include:
Result::is_ok_and
andResult::is_err_and
vsOption::is_some_and
andOption::is_none_or
.- What does
Option::or
do? Can you tell without looking at the signature, just by looking at the name?
These drawbacks make it harder to predict and remember method names, especially for newcomers.
For demonstration, try to guess the name of the Result<T, E>
method that takes a Result<U, E>
and returns another Result,U, E>
. With this library, you just need to think what is changed, so in this case, Result::into_ok_of_arg
.
In the standard library, the same method is called and
, which is more concise, and in its specific context it makes sense, so we have stable aliases for them too.
The Solution
better_option_result
provides drop-in replacements for Option
and Result
with:
-
Consistent naming conventions following Rust's own guidelines:
into_*
for methods that consumeself
and transform ownershipas_*
for reference-based operationsis_*
for boolean checks*_lazy
for lazily evaluated operations*_of_arg
for operations that return a new type based on the argument
-
Full backward compatibility with standard library method names:
- Standard (core) library aliases available on demand with the
aliases
default feature flag.
- Standard (core) library aliases available on demand with the
Features
- 🔄 Comprehensive method aliases that follow consistent naming patterns
- 🧠 Intuitively predictable naming - once you know the pattern, you can guess method names
- 🔗 Complete compatibility with the standard library
- 🔍 Extended functionality with additional Boolean logic operations
- 🛠️ Zero dependencies, we're even
#![no_std]
Installation
Add the library using cargo
:
cargo add better_option_result
...or add it manually to your Cargo.toml
:
[dependencies]
better_option_result = "*"
Examples
todo
Why Use BetterOkRes?
- Learning: The consistent naming makes it easier to understand patterns
- Better code completion: Logical grouping of related methods with prefix-based naming
- Greater expressivity: The methods are aimed to be more descriptive
- Zero-cost abstraction: Compiles down to the same machine code as the usual
Option
andResult
types
Contributing
Contributions are welcome! Please feel free to submit a Pull Request or open an issue on GitHub.
License
This project is licensed under the MIT License - see the LICENSE file for details.