Handpicked Rusty Crates

crates to make your coding worthwhile

Handpicked Rusty Crates

crates to make your coding worthwhile

  • 1157 words → 6 min
|

I've been leaning Rust for a year now. Yes, I'm still learning, but I have in this time released1 and contributed2 to a number of crates.

But it is only because of some amazing crates that I was able to write the code that I wanted. And if they hadn't been there, I might have rage-quit Rust very early, because I would have had to write the myself.

If you are still learning Rust, like me, here are some crates that I enjoyed using or that have inspired me to try something new.

maplit or collect-mac

Rust lacks a few syntactic features that other languages have. One of which is built-in dictionary literals. While languages like swift have these, in Rust you can help yourself with either maplit or collect-mac.

Maplit offers a set of macros similar to Rust's built-in vec![] so you can write stuff like this:

let employees = hashmap!{
    "employees" => hashmap! {
        "employee  1" => hashmap!["attribute" => "value"],
        "employee  2" => hashmap!["attribute" => "value"],
        "employee  3" => hashmap!["attribute" => "value"],
        "employee  4" => hashmap!["attribute" => "value"],
        "employee  5" => hashmap!["attribute" => "value"],
        "employee  6" => hashmap!["attribute" => "value"],
        "employee  7" => hashmap!["attribute" => "value"],
        "employee  8" => hashmap!["attribute" => "value"],
        "employee  9" => hashmap!["attribute" => "value"],
        "employee 10" => hashmap!["attribute" => "value"],
        "employee 11" => hashmap!["attribute" => "value"],
        "employee 12" => hashmap!["attribute" => "value"],
        "employee 13" => hashmap!["attribute" => "value"],
        "employee 14" => hashmap!["attribute" => "value"],
        "employee 15" => hashmap!["attribute" => "value"],
        "employee 16" => hashmap!["attribute" => "value"],
        "employee 17" => hashmap!["attribute" => "value"],
        "employee 18" => hashmap!["attribute" => "value"],
        "employee 19" => hashmap!["attribute" => "value"],
        "employee 20" => hashmap!["attribute" => "value"],
    }
};

These little macros let you almost forget, they are not part of the stdlib.

multimap

Another crate that provides functionality, which should actually be built-in. Brian Anderson compiled a list of crates that feel like they should have been part of the std.

let mut map = MultiMap::new();

map.insert("key1", 42);
map.insert("key1", 1337);
map.insert("key2", 2332);

assert_eq!(map["key1"], 42);
assert_eq!(map.get("key1"), Some(&42));
assert_eq!(map.get_vec("key1"), Some(&vec![42, 1337]));

command line argument parser

Aka: clap. I started rewriting a command line application I had originally written in Ruby. There I had used Thor to configure the commands. Clap is very different but not less cool, for instance it lets you describe your interface in yaml, which is then compiled into your binary. Is also happens to be one of the best documented crates, with currently 19 elaborate examples. This is I wanna see libraries documented.

anything that starts with cargo-*

Cargo is already great itself, but it is also quite extensible and there are a number of tools that integrate with it.

  1. cargo-check quickly checks your code without compiling. This combines well with...
  2. cargo-watch, which repeats a command when you save a source file.
  3. cargo-add adds new dependencies to your project while
  4. cargo-graph produces a graph of all your dependencies.
  5. cargo-clone clones the source repo of a crate.
  6. cargo-fmt formats your code project and by now there are many more, like cargo-apk, cargo-brew, cargo-lipo, cargo-at, cargo-count, cargo-open, cargo-outdated, ... I'm gonna stop here.

clippy

This is named after the cute little cartoon paper clip from Word. It is much more sophisticated than that. Clippy is a growing set of lints that will tell you that you are writing bad Rust. Amazingly though it most of the time also tells you how to write better rust. Btw: there is a cargo command that will make it easier to use clippy called, you guessed correctly cargo-clippy .

itertools

What I already loved about Ruby and now love about Rust is iterators and iterator-adapters.The standard Interator-trait already has a lot of functionality, but itertools adds Chunks, Flatten, Group, Unique, Zip and so on.

boolinator

If you've gotten addicted to functional mapping of Option<T>s or Result<T,E>s, then you sometimes course at the fact that you have to handle booleans with icky if blocks. boolinator lets you convert booleans into Option<()> and other nicities.

chrono or datetime

Pick one, they are both pretty good. Chrono seems a bit more complete, while it sometimes lacks a complete rustacious feeling. Datetime has the faster string parser :D

prettytable

When you wanna print complex data onto the terminal, use this. It is quite fast and its API is really nice.

let table = table!(["ABC",     "DEFG", "HIJKLMN"],
                   ["foobar",  "bar",  "foo"],
                   ["foobar2", "bar2", "foo2"]
                  );
table.printstd();

simple_parallel or rayon

While I haven't played around with either of these yet, I so want to. Essentially either of them is capable of turning an ordinary iterator into a multithreaded parallel iterator.

Rayon:

use rayon::prelude::*;
fn sum_of_squares(input: &[i32]) -> i32 {
    input.par_iter()
         .map(|&i| i * i)
         .sum()
}

AMAZING

image

Images is a nice collection of native Rust libraries to encode and decode bmp, gif, ico, jpeg, png, ppm, tga, tiff and webp. Last time I checked that list was a lot shorter. These are mostly maintained by the people behind piston and also used in servo, so expect some high quality code here. I personally used this to do a little stenography experiment.

Honorable Mentions

This posts title image is called "crates" and is by Helen Cook.