DEV Community

Cover image for The Borrow Checker and Memory Management

The Borrow Checker and Memory Management

Steven Trotter on July 28, 2019

Introduction I've been playing around with Rust for about 12 months now on and off and one of the things I've learned is I love Rust. Wh...
Collapse
 
oh_god_it_works profile image
mawOfMalfunctions

The one thing I never seem to get into my head are lifetime parameters. The syntax is just so strange and it seems hard to find examples for specific cases anywhere. It feels like fighting the borrow checker but without even knowing what it wants.

Collapse
 
strottos profile image
Steven Trotter

Agreed, lifetimes are more difficult and I figured perhaps warrants an article in itself. I certainly haven't tried to cover them here, would have been too much. In principal they're not so bad but in practice can be a different experience, I still struggle at times and have been known to use other techniques in order to avoid having to (which is clearly not the right way of doing things).

I'll try and put together a lifetimes article at some point as well. I'd be curious to have an example of the kind of thing you do struggle with if you'd be so kind/have one to hand.

Collapse
 
krusenas profile image
Karolis

I would identify myself as mainly a Go developer but in some cases I wish I wrote few services in Rust :) Especially where something is really simple and just need to take care of used memory.
The approach that I took in learning Rust was first reading a book, then do a small service, then more github.com/rust-lang/rustlings to better understand various types and their use cases.

All in all, Rust is not an easy language and I am still far from being comfortable when writing code in it. I hope it will change. Agree regarding unit tests, compared to Go's unit testing they are not straightforward, well... nothing is in this language :D But it's fun!

Collapse
 
strottos profile image
Steven Trotter

Honestly, I'd say if it's simple there's not much point in learning Rust for this purpose unless performance is really critical. I'd stick to Go in this case. If you feel like unhappy with Rust then maintainability alone should be a good enough reason.

That said though I really think Rust is workable and not as hard as people think. I've come across articles before complaining that "Rust can't do this thing" and I've tried it and it works fine so I think it's come and is still coming a long way. Really my aim here is hopefully to make people realise some of the "hard" things aren't as bad as they seem, and maybe to get a few people to give it another go as I'm pretty sure it's better than it used to be a few years back.

But yeah, it's not easy, but I think it's worth it and I think it'll be a super valuable skill in time. I think it's time is yet to come I guess.

Collapse
 
strottos profile image
Steven Trotter

Thanks for reading anyway. Hope you enjoyed it or gained something from it. There is more to come hopefully when I get time :)

Collapse
 
strottos profile image
Steven Trotter

For anyone that is still confused by any of the above it might be worth a read of this article too, this does a great job of explaining: medium.com/@thomascountz/ownership...

Collapse
 
batorshikh profile image
Bat-Orshikh

Thanks for the great article.
Recent few days, I really like to learn rust programming language cuz it's little bit similar to c, c++.

Collapse
 
lepidora profile image
Mia

Thanks for the article! I've been meaning to learn Rust for a while and this is a nice and concise introduction to it.

Collapse
 
strottos profile image
Steven Trotter

Really glad you enjoyed it, thanks.

Collapse
 
deepu105 profile image
Deepu K Sasidharan • Edited

Great article. The first line of the article says The Borrow checker but the following content doesn't fit, is it a typo?

Collapse
 
strottos profile image
Steven Trotter

Yeah that wasn't clear, have edited it so that hopefully it makes more sense now. Basically there was one section called the borrow checker which is the whole article, but yeah, that's just dumb and unclear.

Glad you enjoyed the article and thanks very much for the feedback.