3 个版本
0.1.2 | 2024 年 6 月 27 日 |
---|---|
0.1.1 | 2024 年 6 月 23 日 |
0.1.0 | 2024 年 6 月 16 日 |
#794 in Rust 模式
每月 52 次下载
用于 get-blessed
35KB
495 行
实现 Rust 原始类型的方法/工具
请注意,我不是 Rust 大师,这些方法可能不是特别快,也可能不遵循最佳实践
安装
将以下内容添加到您的 Cargo.toml 中
[dependencies]
# if you want methods for a specific type then add them as a feature
type_utilities = { version = "0.1.0", features = ["strings", "bool"] }
# if you want all methods available for all types then add
type_utilities = { version = "0.1.1", features = ["all_types"] }
# features currently available:
# strings, bool, vec, "result", "option"
并像这样引入特性
use type_utilities::strings::methods::*;
类型
字符串
迄今为止,我已为字符串 String
和 &str
实现
字符串.第一()
示例
use crate::strings::methods::SelectNth;
let case1 = String::from("obtain the first character");
assert_eq!("o", case1.first());
let case2 = String::from(" Obtain the first character");
assert_eq!(" ", case2.first());
字符串.remove_whitespaces()
示例
use crate::strings::methods::RemoveWhitespaces;
let case1 = String::from("first case ");
assert_eq!("firstcase", case1.remove_whitespaces());
let case2 = String::from(" this is another test with normal ");
assert_eq!("thisisanothertestwithnormal", case2.remove_whitespaces());
字符串.to_snake_case()
示例
use crate::strings::methods::ToCases;
let case1 = String::from("this is the first case");
assert_eq!("this_is_the_first_case", case1.to_snake_case());
let case2 = String::from(" this is the second case2 ");
assert_eq!("this_is_the_second_case2", case2.to_snake_case());
字符串.to_camel_case()
示例
use crate::strings::methods::ToCases;
let case1 = String::from("this is the first case");
assert_eq!("thisIsTheFirstCase", case1.to_camel_case());
let case2 = String::from(" this is the second case2 ");
assert_eq!("thisIsTheSecondCase2", case2.to_camel_case());
let case3 = String::from(" This is a third case 3");
assert_eq!("thisIsAThirdCase3", case3.to_camel_case());
字符串.to_pascal_case()
示例
use crate::strings::methods::ToCases;
let case1 = String::from("this is the first case");
assert_eq!("ThisIsTheFirstCase", case1.to_pascal_case());
let case2 = String::from(" this is the second case2 ");
assert_eq!("ThisIsTheSecondCase2", case2.to_pascal_case());
let case3 = String::from(" This is a third case 3");
assert_eq!("ThisIsAThirdCase3", case3.to_pascal_case());
布尔型
迄今为止,我为 bool
实现了
布尔型.toggle()
use crate::bool::methods::*;
#[test]
fn toggle_method_works() {
let mut case1 = true;
case1.toggle();
assert!(!case1);
case1.toggle();
assert!(case1);
}
结果
迄今为止,我为 Result<T, E>
实现了
result.filter()
示例
use crate::result::methods::Filter;
let case1 = "2".parse::<i32>();
let case1 = case1.filter(|nu| *nu > 1);
assert_eq!(case1, Ok(&2));
let case2 = "err".parse::<i32>();
let case2 = case2.filter(|nu| *nu < 10);
match case2 {
Ok(_) => {},
Err(e) => {
assert!(e.is_some());
},
};
let case3 = "10".parse::<i32>();
let case3 = case3.filter(|nu| *nu > 20);
match case3 {
Ok(_) => {},
Err(e) => assert!(e.is_none())
};
向量
迄今为止,我为 Vec<T> where T: PartialEq + Clone
实现了
选项
迄今为止,我为 Option<T>
实现了
option.is_none_and()
示例
use crate::option::methods::IsNone;
let case1 = Some(1);
assert!(!case1.is_none_and(|| true));
let case2 : Option<i32> = None;
assert!(case2.is_none_and(|| case1.is_some()));