In the last few months I have been playing around with Rust, Mozilla’s new systems programming language. I worked on small personal projects and submitted a few pull requests (mostly refactoring) to Servo, a Mozilla research project to build a browser engine in Rust.
Another language that often gets mentioned alongside Rust is Go, also known as golang since Go is hard to search for (so is Rust to a lesser extent).
Both languages are compiled, static, strongly typed and have pointers. Both are at a lower level than popular languages like Ruby or Python. They both have been called systems programming languages though this description fits Rust more than it does Go.
I have often seen people asking which one is better or which language should be chosen over the other.
Because of this appearance of opposition and the fact that I already knew Rust, I decided to try Go and see if and how the languages were similar.
Initial impressions of Go
My initial impressions of Go are very good. The language is easy to learn and is nicely designed. If you know any of the following languages; C, C++, Java or C# you’ll be able to jump right in after reading a short tutorial.
The tooling is great, particularly gofmt which formats your code automatically according to the Go style guidelines. I’m using a Vim plugin for Go which calls gofmt each time I save. This process is very nice once you start using it.
One downside is how the package/module/directory structure needs to be set up. It’s not clear when your first start how everything must be structured. It’s important to read this part of the documentation before starting your first project. Otherwise you’ll end up like I did, needing to rearrange the file structure of my git repository because I didn’t follow the proper structure.
Even now that I know the correct file structure I still find it a bit odd and constraining. It’s definitely not one of the language’s strong point.
Comparing Go and Rust
While both languages may seem to serve a similar purpose at first, upon using both they are very different. I have seen many people asking which one is better but the answer depends on what you are building and what you are looking for in a language.
Go is easier to learn. There is deliberately less features in the language and you can hit the ground running after a short tutorial.
Programming is also easier. I spent more time solving problems with my algorithms than I did wrestling with the language.
Go is also garbage collected. That means less to worry about as you don’t have to manage memory. On the other hand, for some applications a garbage collector can be a big downside and it does mean less control.
Rust on the other hand is a more complex language. There is a lot of interesting things in the language and if you’re a language nerd you will love a lot of it’s features. This makes learning and mastering the language harder than Go, but it also makes for a powerful language with a lot of expressive concepts like meta programming through syntax extensions.
I have spent a lot of time battling with the Rust compiler. Compared to Go this means I spend more time solving problems related to the language itself rather than to the algorithms I am writing.
One thing I must say is that when a program finally does compile, there are much less run time errors than in any compiled language I have tried before.
The language isn’t garbage collected and also allows for more control like choosing if memory is allocated on the stack or the heap for example.
Go is a general purpose utilitarian language that’s suitable, amongst other things, for building tools and app back-ends.
Rust is a “PHD smarts” language that’s better suited for systems level stuff and programs where memory management and performance is important.
Go will probably become a go to language when I need a compiled portable low-level language. It’s more modern than C and it’s simpler than C#.
Rust on the other hand is more specialized and is a better choice when doing systems level programs, when garbage collection isn’t an option, or when performance is critical.
Both are wonderful languages and I intend to use them both going forward.