Learning Rust pt. 1 — Package Manager
The first language I learned well was Perl, then Python, now attempting Rust. I feel most people that read this will be coming from a similar background as this blog is more DFIR focused (a community that enjoys using Python). That being the case, I want to show some of the Rust concepts coming from my point of view and make some comparisons.
One of the first things we quickly and naturally learn how to do is to utilize other peoples libraries. I have always enjoyed how easy this is in Perl and Python.
With Python we install a library using pip.
pip install xlsxwriter
So easy! We can now use the xlsxwriter library. All we have to do is read the docs, and we can now automate spreadsheet creation. Now, all that is needed is to import the library:
import xlsxwriter
This concept is powerful. It makes coding efficient and community driven. It is just as easy in Perl to do the same. We use the PPM (Perl Package Manager) to install a library then a simple:
use package;
That package is now imported and ready to roll.
The reason I show these examples is because I want you remember how easy it is to install a library and import it in a language we are familiar with. The reason being is that it is just as easy in Rust. To me this was surprising. I have messed around with C and one of the really annoying things is trying to link up another library to use. The fact that I can use a lower language and still install and import other libraries just as simply as Python and Perl has a bit of the wow factor for me. It gives me a bit more momentum in wanting to learn this language.
So, what is the package manager for Rust? Its Cargo. Cargo installs crates (AKA packages) and helps you build them. You have actually already seen cargo in action and might not have noticed it!
Lets look at how easy it is. Remember yesterday and the day before when we compiled and ran RustyUsn? It has external libraries it uses, but we didn’t even install any external libraries. That’s how EASY cargo is! You didn’t even have to use it.
Lets look at our first piece of code for RustyUsn. The cargo.toml file:
[package]
name = "usntest"
version = "0.1.0"
authors = ["Matthew Seyer <matthew.seyer@gmail.com>"][dependencies]
byteorder = "*"
chrono = {version = "0.2", features = ["serde", "rustc-serialize"]}
clap = "*"
That is it. Just by defining a dependency, cargo will insure that its specific version is installed and linked just for you. If you want to see, go back to where we ran ‘cargo build’ for the project and look at everything it downloaded and compiled. Please check out the Cargo Guide to learn more on how Cargo works and why you need it to create a Rust project. It explains it better than I can and it is a Rust fundamental.
Now you know a little bit about the Rust package manager and how it works behind the scenes. One of my favorite Rust spots is https://github.com/kud1ing/awesome-rust. This is a listing of other libraries people have made in Rust. Like I said the other day, it’s a relatively new language, so there are still not a lot of libraries. But, the list is constantly growing.
Before we start diving into more concepts and how to code them in Rust I want to show you what I have found to be the most helpful tools for coding in Rust. So… that’s next up.