easier 0.1.0

making rust easier
Documentation

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 a few characters on things we do hundreds of times a day, adds up. Or making things easier to read makes debugging and understanding code much faster.

Secondary goals:

  • Minimize imports
  • Minimal build time

If you would like to suggest things that make rust a bit easier to use, you can submit a PR.

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() for iterators

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

let a= [1,2,3].iter().map(|a|a*2).to_vec();

instead of:

let a= [1,2,3].iter().map(|a|a*2).collect::<Vec<_>>();

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);