Crates.io | mod |
lib.rs | mod |
version | 0.1.0-1 |
source | src |
created_at | 2024-06-10 15:56:46.508834 |
updated_at | 2024-06-10 16:29:04.660953 |
description | mod is a Rust project that provides two simple and useful utility functions for converting between Option and Result types. |
homepage | https://github.com/limitcool/mod |
repository | https://github.com/limitcool/mod |
max_upload_size | |
id | 1267342 |
size | 9,157 |
English| 简体中文
mod
is a Rust project that provides two simple and useful utility functions for converting between Option
and Result
types. The project includes two main functions:
option_to_result<T, E>
: Converts an Option<T>
to a Result<T, E>
.result_to_option<T, E>
: Converts a Result<T, E>
to an Option<T>
.You can add this project to your Rust project by running the following command:
cargo add mod
First, ensure that you include the mod
crate in your project:
use r#mod::{option_to_result, result_to_option};
option_to_result
Converts an Option<T>
to a Result<T, E>
. If the Option
is Some
, it returns the value wrapped in Ok
; if it is None
, it returns the specified error.
pub fn option_to_result<T, E>(option: Option<T>, err: E) -> Result<T, E>
where
E: Clone,
{
match option {
Some(value) => Ok(value),
None => Err(err),
}
}
Example:
let some_value = Some(42);
let none_value: Option<i32> = None;
let error = "Error";
assert_eq!(option_to_result(some_value, error.to_string()), Ok(42));
assert_eq!(option_to_result(none_value, error.to_string()), Err("Error".to_string()));
result_to_option
Converts a Result<T, E>
to an Option<T>
. If the Result
is Ok
, it returns the value wrapped in Some
; if it is Err
, it returns None
.
pub fn result_to_option<T, E>(result: Result<T, E>) -> Option<T>
where
E: Clone,
{
match result {
Ok(value) => Some(value),
Err(_) => None,
}
}
Example:
let ok_result = Ok::<i32, &str>(42);
let err_result = Err::<i32, &str>("Error");
assert_eq!(result_to_option(ok_result), Some(42));
assert_eq!(result_to_option(err_result), None);
This project includes some unit tests to verify the correctness of the functionality. You can run the tests by executing the following command:
cargo test
The test cases are located in the tests
module and cover the option_to_result
and result_to_option
functions:
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_option_to_result() {
let some_value = Some(42);
let none_value: Option<i32> = None;
let error = "Error";
assert_eq!(option_to_result(some_value, error.to_string()), Ok(42));
assert_eq!(option_to_result(none_value, error.to_string()), Err("Error".to_string()));
}
#[test]
fn test_result_to_option() {
let ok_result = Ok::<i32, &str>(42);
let err_result = Err::<i32, &str>("Error");
assert_eq!(result_to_option(ok_result), Some(42));
assert_eq!(result_to_option(err_result), None);
}
}
This project is licensed under the GPL license. See the LICENSE file for details.
Contributions are welcome! Please submit issues or pull requests.
Thank you for using mod
! If you have any questions or suggestions, feel free to contact us.