DEV Community

Jon Stødle
Jon Stødle

Posted on • Originally published at blog.jonstodle.com on

You're Allowed To Write Slow Rust Code

There's a thing that comes up from time to time in the Rust community: people wanting to optimize their code. Make the code run faster. Make it allocate less memory. These are worthy goals, but maybe not necessary for all projects. Just because your program is written in Rust, it doesn't have to be optimized to the moon and back to do it's job. In a lot of cases, you'll be fine using .clone().

There's a fantastic quote from a discussion on the Rust user forums which I always think of when these discussions emerge:

Just because Rust allows you to write super cool non-allocating zero-copy algorithms safely, doesn't mean every algorithm you write should be super cool, zero-copy and non-allocating.

-- trentj on The Rust Programming Language Forum

Of course there's a time and place for super cool no-allocating zero-copy algorithms, but very often it's not the time, nor the place. You can write fantastically effective code with Rust, but you don't have to! A lot of the time it's entirely fine to just write code that works with minimal effort. You don't have to spend 4 days trying to figure out how lifetimes work just to avoid calling .clone() a few times.

Don't spend time trying to make micro optimizations now. Take the easy route. You'll probably come back to that piece of code in a few months time, smile, and change that .clone() to something more efficient.

Happy coding!

NB: Watch this video by Jon Gjengset if you want a good, pragmatic walk through of lifetimes in Rust.

Top comments (2)

Collapse
 
faraazahmad profile image
Syed Faraaz Ahmad

That's great advice! I've been building a tiny compiler in Rust recently and I've sprinkled 'a lifetime params everywhere without truly understanding how lifetimes work in rust just so I can avoid using String

Collapse
 
mardab profile image
Marcin Dąbrowski

@faraazahmad Good to know. Your comment made me look it up, looks promising, I hope that you keep going.