DEV Community

loading...

How to Stick with Rust

pieohpah profile image Joe Chasinga ・3 min read

Put Your Trust in Rust

For a while, I did not get what the hype about Rust was all about. To me, the language appeared opaque and overly verbose. I had tried a few times to learn Rust, every time giving up for some other priorities that came up.

When I joined Recurse Center, during orientation when the new "recursers" lined up introduce themselves and what they wanted to do there (Recurse Center is a curriculum-less bootcamp), I said I wanted to learn Rust. It was my first attempt out of several ones down the road.

In my opinion, a great language must gives incremental jolts of success to the programmer, however inexperience. It must allows a programmer to write quick crappy programs and run. It must grows with the programmer.

I believe most programmers who aren't successful learning Rust because they don't anticipate a peak learning curve right from the start. Rust is a special language because it actually introduces genuinely new features in decades. Therefore it requires a considerable overhaul of the mental frame which appears to be a barrier to learning for some.

Believe it or not, once you get through the peak learning curve, which should last roughly around a month or two (at least for me), it goes downhill from there. The language turns into a productivity engine that is flexible enough to grow with the programmer. It allows you to write quick crappy program that doesn't suck at all (And I'd argue that when your worst code is already fine, there's only room for better).

I've been writing Rust for my startup for the last few months, and here is a few tips I think will help newcomers ease into making Rust stick:

Avoid using &str

Against popular opinions, I'd advocate trying to avoid using &str (and effectively lifetime annotations) at first. Of course, it won't be possible all the time, but it keeps the first few dips pleasant. You might be allocating much heap memory with String and clones, but chances are your program still run much faster than a Python counterpart.

Learn modules

Rust Modules are a source of frustration to many. But it's not much to grok and worth the time to get it out of the way at first.

Use crates

Rust is super modular and very easy to put crates together to build meaningful applications, due to the superb cargo cli that is comparable to npm. Using external crates is a great way to get more done without going into the nitty gritty detail at first. Crates with friendly APIs and docs can go a long, long way.

Focus on building, not learning

If your goal is to learn Rust, you will likely fail because the return on investment is too low. That was how I failed a couple of times before. Think of a reasonably-sized project you'd have built with another language, and fixate on using Rust instead. Here are some ideas:

  • Command line tool
  • API Server
  • Game

Take the compiler seriously

What's really unique about Rust is its helpful compiler messages. This alone had made 60% of my success. Many great languages with really obscure errors. Rust compiler almost teaches you. It reveals how much care the language designers put into making the user experience. We all make more mistakes than success, and we should learn from each of them.

Use cargo check

Rust is notoriously slow at compiling, and that can kill your fire. Try to run cargo check on your code before running it to save some time.

Don't give up

Don't give up and hop onto learning another shiny language. While there are a plethora of "cool" new ones out there to learn, nothing gets close to the balance Rust has to offer.

Discussion (4)

pic
Editor guide
Collapse
brokenthorn profile image
Paul-Sebastian Manole • Edited

Oh, and "Learn the lingo" so that the compiler CAN teach you. :)

Awesome write-up. It pretty much sums up my experience as well.

And btw, you do use &str (string slices), but not where you would normally use it, thinking in ways similar to other programming languages. In Rust you have two types of Strings because of the data oriented approach (which is something you should learn if you want to get better at Rust: DOP vs OOP) + lifetimes.

One's a heap allocated string like in most languages (ie. Rust's String struct), the other is a local, stack-based string which you almost always use when dealing with strings locally, like when working with them (ie. you don't need to allocated and deallocated strings all the time as often as one might think you do with Rust).

The beginner problems with &str stem from the lifetimes of Rust types.

&str being a reference, it has an implicit lifetime in most cases. When you start working with functions returning these, you get into a bit of trouble because sometimes the compiler can't make out the correct lifetime for these references, and requires the (Rust noobie) programmer to explicitly define these lifetimes.

But then the programmer needs to understand Rust lifetime contexts... And most of us have or have had a hard time trying to understand this new concept. I believe once you learn lifetimes in Rust, the rest is easy (because you can experiment more easily without getting frustrated by the compiler's errors).

Collapse
pieohpah profile image
Joe Chasinga Author

Great, thanks for the elaboration!

Collapse
brokenthorn profile image
Paul-Sebastian Manole

You're welcome. Even though I ended up elaborating kind of a lot, it's just my 2 cents, because I'm still just a Rust newbie.

Collapse
divee789 profile image
Divine Olokor

Nice article, I also ran into the same problems with rust. Can you recommend any open source project that a beginner can look through to better understand real-world implementations of rust?