DEV Community

Discussion on: Golang or Rust, that is the question.

Collapse
 
stevepryde profile image
Steve Pryde

I don't think these two languages are really competitors. I've used both and I think the reasons you would choose Go are different than the reasons you would choose Rust.

It might also depend on what you want to build, and the kinds of things you value in a programming language.

I would choose Go for things at work that need contribution and buy-in from other developers (with varying skill levels). As others have mentioned Go is easier to learn, and easier to write, at least to begin with. It also has a much larger standard library and many of the available libraries and frameworks are more mature.

However I find Rust more rewarding (subjective) and more expressive. I've used several different programming languages over the years including C, C++, some Java, Perl, Python, Javascript, Typescript, Rust and recently I've been learning Go as well. Go feels more like all of the others than Rust. Rust is different in its strong bias towards correctness, safety-by-default, and trying to eliminate certain classes of bugs entirely (and actually succeeding in some cases). What I mean is that in most other languages (including Go) you can avoid bugs by being more careful, following better conventions and best practices and even then you'll still make the same mistakes sometimes and trip up. Because programming is hard sometimes. Go still has null pointers (although it's still safer than C or C++). Rust does not.

With Rust, of course you will still make mistakes, but there are some mistakes that are just a lot harder to make. Let's take a simple example using Go and Rust. Mutexes in Go, just like other languages, require you to make sure you remember to lock and unlock at the right times, and make sure you never access shared data outside of the mutex. Otherwise you will get data races and/or crashes. The language won't tell you if you forget. Go will not stop you from accessing data behind a mutex without locking it.

But with Rust you cannot get access to the data without first locking the mutex. That is because the way to get access is by calling the lock() method, and that returns the value. And Rust's borrowing rules mean that the returned value is only accessible within the scope of the mutex lock - so you cannot get it wrong (there are ways around it but you have to try really hard to break it). This is just one example but there are many cases like this where Rust really goes a step further to prevent you from making mistakes. It is a different approach to safety and correctness than with most other languages. This also makes it more difficult to learn, and in Rust you find that you often spend longer writing the code and a lot less time debugging afterwards.

You could (and should) build similar mechanisms in Go and other languages to make it more difficult to make mistakes, but that job is left to you. My recommendation is to learn Rust first, and then learn Go afterwards. Learning Rust will teach you much more about memory models, data structures (and how they allocate memory or use cpu), and safe programming practices, and then you'll be able to more easily spot common mistakes in other languages and code more defensively to avoid them.

Go looks like a really useful language to learn (hence why I'm learning it too) but I think Rust will teach you more. Just my opinion. Learn both ;)

Collapse
 
atomzwieback profile image
Atomzwieback

Thanks for that really detailed answer

Collapse
 
leob profile image
leob • Edited

Great explanation, spot on about Rust's built in "correctness" - with Rust the compiler can check/validate a LOT more than other compilers that I know of, so with Rust "if it compiles correctly" (i.e. if it compiles at all) then you can already be a lot more certain that it will also run correctly - that's a pretty unique capability AFAIK ...

P.S. regarding "I don't think these two languages are really competitors" - I've read that often, and in general it seems true, but in my opinion it would be interesting to try and use Rust for the same sort of things which Golang is typically used for - let them compete head-to-head instead of within their own "niche"