# 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:
```rust
for line in lines("/tmp/file.txt"){
println!("{line}");
}
```
instead of:
```rust
//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::>()`
easier:
```rust
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:
```rust
let a= [1,2,3].iter().map(|a|a*2).collect::>();
let b= [1,2,3].iter().map(|a|a*2).collect::>();
let c= [(1,2),(3,4)].into_iter().collect::>();
```
### into_vec() for items that impl IntoIterator
Use this on items that impl `IntoIterator` instead of `.into_iter().collect::>()`
easier:
```rust
let b = HashSet::::from_iter([1,2,3]).into_vec();
```
instead of:
```rust
let a = HashSet::::from_iter([1,2,3]).into_iter().collect::>();
```
### any()
Instead of `!vec.is_empty()` use `.any()` which is easier to read and type.
Can be used on slices, vecs
easier:
```rust
if vec.any(){
//do something
}
```
instead of:
```rust
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:
```rust
use easier::prelude::*;
let a: Vec = [1i32, 2,3].convert().unwrap();
let b: Vec = vec![0i8].convert().unwrap();
```
or convert any errors to the default value with:
```rust
let a = [1u32, 2, 3].convert_default::();
```
or convert any errors to the given value with:
```rust
let a:Vec = [1u32, 2, 3].convert_or(0.0);
```
### strings
This converts array of string slices to array of strings.
easier:
```rust
let a= ["a","b","c"].strings();
```
instead of
```rust
let a= ["a","b","c"].iter().map(|a|a.to_string()).collect::>();
//or
let a=["a".to_string(),"b".to_string(),"c".to_string()].into_iter().collect::>();
```
### group_by and group_by_map
Group by a key, and return a hashmap of items
easier:
```rust
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:
```rust
let sum = vec![1, 2, 3].act(|a| println!("{}", a)).sum::();
let sorted = vec![3, 2, 1].act_mut(|a| a.sort());
```
### sort and sort_by on iterators
Sort an iterable, and continue chain
easier:
```rust
let vec = vec![3, 2, 1];
let sorted_and_filtered = vec.iter().sort().filter(|a| a > &&1).to_vec();
```
instead of:
```rust
let vec = vec![3, 2, 1];
let mut sorted = vec.iter().collect::>();
sorted.sort();
let filtered = sorted.into_iter().filter(|a| a > &&1).collect::>();
```
sort_by
```rust
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:
```rust
let vec = vec![1, 3, 2, 2, 1];
let uniques = vec.into_iter().unique().to_vec();
assert_eq!(uniques, [1, 3, 2]);
```