3 releases (breaking)

0.3.0 Sep 27, 2024
0.2.0 Nov 26, 2023
0.1.0 Jun 27, 2023

#363 in Development tools

MIT license

35KB
764 lines

easier

making rust easier!

Set of extensions and methods that make rust a little easier to use for day to day tasks, as well as big projects.
There are also convenience methods, which are sometimes a bit easier to remember, or at the very least require less typing.

Primary goal is to be useful, saving time or making things easier to understand.

See some examples below for some of the functionality. Key features:

  • group_by for iterators which is missing in std
  • unique for iterators which is missing in std
  • sort and sort_by for iterators which is missing in std
  • to_vec,to_hashset,to_hashmap for iterators which makes things a bit easier to read
  • read lines from file in a one line with simple syntax
  • act and act_mut for performing an action on an item, and continue iter chain

Examples

Include: use easier::prelude::*;

lines(path)

Read lines one by one from file. Note, this makes the assumption that you can read from the file, and will panic if it cannot read from file, or file does no exist. This makes just quickly reading lines way simpler, but is less rusty, as we are not passing errors to caller.

easier:

for line in lines("/tmp/file.txt"){
    println!("{line}");
}

instead of:

//Or can also use ? 
 for line in BufReader::new(File::open(&path).unwrap()).lines() {
            let line = line.unwrap();
            println!("{line}");
        }

//Or more fully...
  let file = File::open(&path);
        match file {
            Ok(file) => {
                for line in BufReader::new(file).lines() {
                    match line {
                        Ok(line) => println!("{line}"),
                        Err(_) => panic!("Error"),
                    }
                }
            }
            Err(_) => panic!("Error"),
        }

to_vec() to_hashset() to_hashmap() for iterators

Use this on iterators instead of .collect::<Vec<_>>()
easier:

let a= [1,2,3].iter().map(|a|a*2).to_vec();
let b= [1,2,3].iter().map(|a|a*2).to_hashset();
let c= [(1,2),(3,4)].into_iter().to_hashmap();

instead of:

let a= [1,2,3].iter().map(|a|a*2).collect::<Vec<_>>();
let b= [1,2,3].iter().map(|a|a*2).collect::<HashSet<_>>();
let c= [(1,2),(3,4)].into_iter().collect::<HashMap<_,_>>();

into_vec() for items that impl IntoIterator

Use this on items that impl IntoIterator instead of .into_iter().collect::<Vec<_>>() easier:

let b = HashSet::<u8>::from_iter([1,2,3]).into_vec();

instead of:

let a = HashSet::<u8>::from_iter([1,2,3]).into_iter().collect::<Vec<_>>();

any()

Instead of !vec.is_empty() use .any() which is easier to read and type. Can be used on slices, vecs

easier:

if vec.any(){
   //do something
}

instead of:

if !vec.is_empty(){
   //do something
}

convert

This converts between types as long as there is a try_from This should cover many std types
easier:

use easier::prelude::*;
let a: Vec<f64> = [1i32, 2,3].convert().unwrap();
let b: Vec<i32> = vec![0i8].convert().unwrap();

or convert any errors to the default value with:

  let a = [1u32, 2, 3].convert_default::<f64>();

or convert any errors to the given value with:

  let a:Vec<f64> = [1u32, 2, 3].convert_or(0.0);

strings

This converts array of string slices to array of strings.

easier:

let a= ["a","b","c"].strings();

instead of

  let a= ["a","b","c"].iter().map(|a|a.to_string()).collect::<Vec<_>>();
  //or
  let a=["a".to_string(),"b".to_string(),"c".to_string()].into_iter().collect::<Vec<_>>();

group_by and group_by_map

Group by a key, and return a hashmap of items

easier:

 let vec = vec![1, 1, 2, 3, 3, 1];
 let map = vec.group_by(|a| *a);
 assert_eq!(map.len(), 3);
 assert_eq!(map.get(&1).unwrap(), &vec![1, 1, 1]);
 assert_eq!(map.get(&2).unwrap(), &vec![2]);
 assert_eq!(map.get(&3).unwrap(), &vec![3, 3]);

//with structs
let persons = vec![
            Person {
                name: "Alice".to_string(),
                age: 20,
            },
            Person {
                name: "Bob".to_string(),
                age: 20,
            },
            Person {
                name: "Charlie".to_string(),
                age: 30,
            },
        ];


let map = persons.iter().group_by(|a| a.age);
assert_eq!(map.len(), 2);


let map = persons.iter().group_by_map(|a| a.age, |a| a.name.as_str());
assert_eq!(map.get(&30).unwrap().first().unwrap(), &"Charlie");

act and act_mut on iterators

Act on an item, and continue the iterator chain. It returns self

easier:

let sum = vec![1, 2, 3].act(|a| println!("{}", a)).sum::<i32>();
let sorted = vec![3, 2, 1].act_mut(|a| a.sort());

sort and sort_by on iterators

Sort an iterable, and continue chain easier:

let vec = vec![3, 2, 1];
let sorted_and_filtered = vec.iter().sort().filter(|a| a > &&1).to_vec();

instead of:

let vec = vec![3, 2, 1];
let mut sorted = vec.iter().collect::<Vec<_>>();
sorted.sort();
let filtered = sorted.into_iter().filter(|a| a > &&1).collect::<Vec<_>>();

sort_by

 let sorted=vec![4., 1., 3., 5., 2.].into_iter().sort_by(|a, b| a.partial_cmp(b).unwrap()).to_vec();

unique items on iterators

Get the unique items from an iterable easier:

let vec = vec![1, 3, 2, 2, 1];
let uniques = vec.into_iter().unique().to_vec();
assert_eq!(uniques, [1, 3, 2]);

No runtime deps