DEV Community

Tamir Bahar
Tamir Bahar

Posted on • Edited on • Originally published at tamir.dev

Adventures in Rust

In the few years since Rust came out, I've frequently found myself explaining what an amazing language Rust is. Laying out the ways in which it is going to change the systems-programming world, and what a pleasure Rust is to code in.

A few months back, I finally got around to trying it out. It was horrid. The darned borrow-checker just wouldn't let me do anything. I couldn't get anything meaningful to compile. Despite years of programming in various languages I felt completely incompetent to handle it. I resolved to drop Rust and focus on other things.

Despite my initial disappointment and discouragement, I still could not get Rust out of my head. Its premise is too promising to put down after one go. Besides, I cannot be beaten by a programming language! And yet, it can wait. It is too much work for now. Some day, maybe in a few years, I'll write Rust.

Using Rust, however, is a completely different experience. Rust tools are absolutely amazing. As a Windows user through-and-through I got used to open-source tools (especially command line tools) not being supported on Windows. (No, cygwin is not a valid solution.) I don't blame the devs - they work on Linux. Even if they have the time to spend on the Windows port, they don't necessarily have a Windows machine to test it on. And yet - I am used to being disappointed. That is why, when I first heard of rg(an amazing grep replacement) and fd(an amazing find replacement) I knew that they would not work on Windows. But, being my optimistic self - I checked. And a good thing I did that.

To install Rust tools, the easiest way is to install the Rust toolset and compile them. A daunting task in every other language, yet a breeze in Rust.

  1. Head over to rustup.rs and install Rust (A single command-line on Linux, a single executable on Windows)
  2. cargo install ripgrep fd-find
  3. That's it. Really. Now use the tools.

This was when I realized how amazing Rust really is. Even if you ignore the language completely - it's tooling and package management is unparalleled. Having published Python packages in the past, I was amazed at the simple publishing and installation mechanisms. Having used C and C++ I was simply amazed at a systems-programming with package management. So while still somewhat scared of the borrow-checker, I decided that my next CLI tool will be written in Rust. The easy-as-pie deployment bought be over completely.

Some months after that, I finally found myself in need of a new CLI tool. I was faced with a continuous log I wanted to filter for duplicates. sort -u sorts, so it cannot work on streams. Of course, there is probably some sed or awk magic I can use, but I want something simple. Besides, a tool that filters out duplicates seems like the perfect beginner project for getting hands-on with Rust. So I went ahead and creates uq. After finishing it, I published it on crates.io. cargo install uq on a second machine, and it worked. Both Windows and Linux. A friend tried it, and it simply worked! I never had such a pleasant deployment experience. It is practically easier to install from source then send a compiled binary! And it works cross-platform out of the box.

A short while later I wanted to group some log entries by a regex. I looked around and could not find a simple way to do it. So, once again, I turned to Rust. Some borrow-checker-wrestling later and the first version of groupby was complete. Yay!

A short time later I had one of the best open-source experiences I've ever had. Someone started using groupby, looked at my terrible code, and posted this issue:

Little code suggestions #1

Hello

I find this little program will be useful for many things I do (I usually do something like that with combination of sed, sort, …). I also looked into the code. Do I guess right that you're still learning Rust? Could I provide few little tips?

  • I glimpsed at least one unwrap that can be triggered by the user input (giving a too large group ID), which will result in ugly error message instead of nice useful one.
  • Do you choose BTreeMap/BTreeSet for some specific reason? Is it the order of elements? If not, HashMap and HashSet are likely to be faster.
  • Both variants (unique vs all) look very similar and differ only in the inner data type and the method used to push/insert. I think this could be done with just one piece of code that is generic over the type, and adding your own trait that implements the adding for either. Would you like me to show you such code?

Having someone more experienced in Rust come in and help me improve my very naïve code was great. And it was my first time getting a "this is great, may I help you?" comment and not a "this is great, I want this as well" one.

For the time being, I keep spending more time wrestling the borrow-checker than writing actual code in Rust. But I am (almost) sure it is due to lack of experience. On the plus-side, I'm becoming better at detecting lifetime issues in other languages as well.

So, for anyone who hasn't done it yet, I highly recommend using Rust-based tools. Just for the amazing experience of things working (and compiling!) out of the box. Later, if you choose to actually code in it, be sure to brace yourself for a somewhat bumpy ride. Friends tell me that after a time Rust becomes easier, speeding up their development. I'm not there yet, but I'm working on it.

Top comments (14)

Collapse
 
buntine profile image
Andrew Buntine • Edited

Rust is one of those languages that wil force you to become a more rounder programmer because, depending on your background, it will help to introduce you to a more rigid, defensible style of programming. When learning, I loved the fact that error-handling is furnished to the programmer at such a high-level. You may also find that the community is friendly, yet typically far more advanced than the insert-popualr-language-here groups. By necessity, you will be forced to learn a lot about your program after you've written it (how it's laid out in memory, the effect of CPU context switches, etc). These are typically concepts that are lost on programmers working in more abstract environments.

Trust me, your struggles with the borrow checker will become much less frequent when you begin to really understand how it works and why it's there. I found that even when I knew, it still took a few months before I really knew. I am glad that Rust generally does a really good job with it's error messaging, also. So even though you may be having difficulty compiling your code, the error messages will go into great detail and even provide possible fixes.

Collapse
 
omawhite profile image
Omar White

What would you suggest for someone interested in learning rust. What resources should I go through to learn with? What would be something a rust newbie should try to build?

Collapse
 
buntine profile image
Andrew Buntine

The official "book" is the best place to learn rust. It's a relatively fast-paced book but does not assume you have any familiarity with Rust (or any other systems language): doc.rust-lang.org/book/second-edit...

In terms of what to do, a nice way to get proficient in a language is via Exercism: exercism.io/

When learning a new language I like to implement the Game of Life and a Brainf*ck interpreter.

Thread Thread
 
omawhite profile image
Omar White

Ive never heard of exercism before looks like a great resource. Thanks!

Thread Thread
 
8uurg profile image
8uurg

I can really recommend the Brainf*ck interpreter. Also try implementing some optimizations such as combining operations.

Collapse
 
cradee profile image
Paul Bochar

/r/dailyprogrammer really cathcy.

Thread Thread
 
dmerejkowsky profile image
Dimitri Merejkowsky

Thanks for letting us know about exercism.io. Looks really nice :)

Collapse
 
ben profile image
Ben Halpern

Super awesome post. It's definitely the kind of thing that makes me want to give Rust some more time. I too gave it a bit of learning time but fell off the wagon quickly.

@vaidehijoshi we talked a bit about your short adventures in Rust, any quick tips for a total Rust newbie?

Collapse
 
semrov profile image
semrov • Edited

Don't forget to mention that Rust beginners shouldn't begin with developing Tree/Graph like data structures (and anything that requires "multiple" ownership), until they really fully understand how borrow checker and ownership work, and why ownership rules are really hard to satisfy in these cases. It can be very depressing fighting with these "cannot move out of borrowed content" messages, causing developers to give up learing Rust-lang too early.

Collapse
 
samipietikainen profile image
Sami Pietikäinen

I would also recommend using clippy when working with Rust. It's a linter that can help to catch common mistakes, and in general help to write more idiomatic Rust code.

I've also written about my experiences coming from C/C++ if you're interested.

Collapse
 
upsuper profile image
Xidorn Quan

Looking at the code, I believe there are several things can be optimized further in uq, e.g. holding a StdinLock rather than using stdin() for each line, having a persisted String as buffer to avoid allocating for getting string every time.

Collapse
 
tmr232 profile image
Tamir Bahar

Thanks! Feel free to submit a PR :)

Collapse
 
ishanigupta27 profile image
Ishani Gupta

Great Post !!!

I have been recently interested in various languages and comparing them with each other. I agree that coding in Rust is not an easy task ( added that it has 3500 + open issues), yet the thought of omitting the entire garbage collection overhead and giving the responsibility to the programmer with the feature of "Ownership" is intriguing.

I love these new ideas in Rust!

Collapse
 
arcticspacefox profile image
ArcticSpaceFox

More rust tutorial posts?