Getting started with Rust
This weekend, while toiling away solving technical problems on a separate project, it occurred to me that I needed to print numbers that…
This weekend, while toiling away solving technical problems on a separate project, it occurred to me that I needed to print numbers that are human readable. Initially I decided to roll my own, but I quickly remembered Rust has the beautiful depth of a module system sitting right at my fingertips.
Problem is, human-format didn’t exist.
Good news is, it does now.
Before jumping in, let’s take a step back though, and talk about Rust overall.
What’s Good?
Here are some of my favorite aspects of the Rust ecosystem.
- The syntax feels good. There are some odd and seemingly inconsistent choices made given the intentions of the language, but overall it feels natural.
- There are tools for all necessary tasks. Initializing, Formatting, Testing, Documentation, Benchmarking, Building, Running, and Publishing all built into Cargo from the get-go.
- Borrowing provides a new paradigm of thought. Gone are the days of referencing lost resources or forgetting to free a handle, as the compiler will strictly not allow you to.
- Cross Platform Automation is already functional. Leverage Travis-CI and Appveyor to build and validate your code across Windows, Linux and MacOS.
- Central documentation provides great cross package referencing. Docs.rs provides an indexed source for documentation of all modules published on Crates.io.
In all, Rust does a lot of things right. Each of these deserves its own call out.
A New Project
To initialize a new project you can call cargo init
to create a library, or cargo init --bin
to create a binary. It will automagically create the relevant files, and initialize a git repository, leaving the project in a state to run a sanity check and see code working:
Cargo provides some great features here, wrapping up the internals of what is necessary for a compiled artifact to be created from your code. Though the details are never too far away…
In the ./src
directory, you will see a file called main.rs
and inside it is a conspicuously familiar application to any seasoned programmer.
The main
function declaration is the entry point for the application, without it your application will fail to compile. This is what happens if you rename main
to main2
.
That being said, main
is already there, so we can proceed.
println!
is an example of a macro in Rust. They are used to build off of the typical capabilities of the Rust language to provide improved syntactical capability through encapsulating code for convenience. println!
, print!
and format!
are three such macros that find their way into your code when debugging.
Finally, you should see a file called cargo.toml
. This is the manifest file for your project. It houses package, feature flags, build configuration details, dependencies, and many other values and is human readable.
Next, I will be discussing the most interesting aspect of the Rust ecosystem: the partnership between Documentation and Testing. This is a very promising feature and one that I hope starts getting more traction in other communities, like that of the JavaScript community which doesn’t seem to take Documentation or Testing so seriously.
Drop your feedback and comments in below.
Good luck and good compiler wrangling to you.