A modern Rust utility library delivering modularity, performance & extras ported from JavaScript Lodash
Add lo_
to your Cargo.toml
:
[dependencies]
lo_ = { version = "0.3.0", features = ["async_retry"] }
Or run the following command:
cargo add lo_ --features "async_retry"
For quick use, you can import and call built-in helpers like camel_case, snake_case, etc., without needing the trait:
use lo_::camel_case;
let input = "Foo Bar";
let result = camel_case(input);
println!("{:?}", result); // "fooBar"
Use the Transform trait for chainable and expressive string utilities:
use lo_::CaseTransform;
let s = "HelloWorld";
println!("to_snake_case: {:?}", s.to_snake_case()); // "hello_world"
println!("to_camel_case: {:?}", s.to_camel_case()); // "helloWorld"
println!("to_title_case: {:?}", s.to_title_case()); // "Hello World"
println!("to_lower_first: {:?}", s.to_lower_first()); // "helloWorld"
println!("to_upper_first: {:?}", s.to_upper_first()); // "HelloWorld"
let k = "My Résumé";
println!("to_kebab_case: {:?}", k.to_kebab_case()); // "my-resume"
use lo_::WordTransform;
let input = "fred, barney, & pebbles";
println!("to_words: {:?}", input.to_words()); // ["fred", "barney", "pebbles"]
println!("to_slug: {:?}", "Rust is awesome 🚀".to_slug()); // "rust-is-awesome"
use lo_::UtilityTransform;
let num: Option<i32> = "123".to_safe_parse();
println!("to_safe_parse: {:?}", num); // Some(123)
use lo_::WordTransform;
let text = "Rust is blazing fast and memory-efficient.";
let wrapped = text.wordwrap(10, "\n", false);
println!("{}", wrapped);
/*
Rust is
blazing
fast and
memory-efficient.
*/
use lo_::{UtilityTransform, Alignment};
let s = "42";
assert_eq!(s.pad(5, "0", Alignment::Left), "00042");
assert_eq!(s.pad(5, " ", Alignment::Right), "42 ");
assert_eq!(s.pad(6, "-", Alignment::Center), "--42--");
use lo_::WordTransform;
let s = "Hello world, this is Rust!";
let words = s.to_words();
assert_eq!(words, vec!["Hello", "world", "this", "is", "Rust"]);
use std::collections::HashMap;
use lo_::UtilityTransform;
let mut data = HashMap::new();
data.insert("name", "Ragnar");
data.insert("lang", "Rust");
let template = "Hi {name}, welcome to {lang}!";
let rendered = template.to_template(&data);
assert_eq!(rendered, "Hi Ragnar, welcome to Rust!");
use lo_::{chunk, find, uniq};
let array = vec![1, 2, 3, 4, 5, 6, 7];
let size = 3;
let chunks = chunk(array, size);
println!("array chunks: {:?}", chunks); // array chunks: [[1, 2, 3], [4, 5, 6], [7]]
let numbers = vec![1, 2, 3, 4, 5];
let is_even = |x: &i32| *x % 2 == 0;
println!("{:?}", find(&numbers, is_even)); // Some(2)
let input = vec![2, 1, 2];
let output = uniq(input);
println!("{:?}", output) // [2, 1]
use lo_::retry;
use std::time::Duration;
let mut count = 0;
let result = retry(4, Duration::from_millis(10), || {
count += 1;
if count < 3 {
Err("fail")
} else {
Ok("success")
}
});
println!("{:?} after {:?} retry", result, count); // Ok("success") after 3 retry