Rust from a Gopher - Lesson 1

So I’ve decided to actually start learning Rust. I’ve considered it a couple of times before but I always found some reasons to avoid going deep. For example:

I’ve done some small hobby work with RISC-V in C, but this small hump seemed good enough to stop me from diving into Rust - oh for shame.

Fast forward to today I think I can live with the RISC-V split in Rust/embedded (or whatever it is now, I don’t actually know). I read there are some pretty cool floating point vector operations being finalized and possibly coming to market soon too - so Rust could be a pretty sweet option to experiment with writing some digital synth modules at some point too! (The Go ecosystem for both DSP and embedded is laughable, at least for good reasons for the latter)

Another major excuse I always came up with was:

  • I have a good job writing webapps in Go - and I love doing that! (and Go is great, all bow to the Gopher God)

Hey this hasn’t really changed in 12 months - but you know what has? My undying love of Go. I tried to write a gaming network server/backend. The idea was to combine rollback networking with the common ECS architecture for games. Well guess what? ECS is built to optimize memory accesses for game objects. And guess what else? Go sucks for trying to optimize beneath L2! I used a message bus between the ECS and networking layers, this is painful as crap without generics and to be honest - I’m really not feeling it for the Go generics.


Anyway enough of this preamble I hear you roar. What’s exciting? What’s the juice?!!

Not much I’m afraid. My simple plan is to start by documenting my thoughts as I clunk my way through the Rust Book. In this very blog post right here it begins with Chapter 1, the first in the league of 20! This exhilarating series will bestow upon you the journey of a complete ignorant, as he transitions his way to hopefully-slightly-less-ignorant by number 20!

OKAY! Drama aside, I really just want to summarize my thoughts on each chapter. Hopefully it may make for cute reading to either myself or my great-great-great grandchildren one day. Here’s hoping at least.

Oh spoiler alert - I’ll be comparing my Rust experience to my Go experience a whooole buttload throughout this series, sorry but that’s all I got (:

CHAPTER ONE

First impressions - verrrrry nice!!! Hey they give you this sweet e-book for free! I still remember to this day, a lazy Sunday which I spent in Sydney, loading up the Go tour and working my way through the 50 or so interactive exercises. Sated but still naive, I loaded a youtube video of Rob Pike explaining channels - I’m PRETTY SURE it was this exact video. (Seriously though, those glasses - who could forget?)

Having a free and well thought out introduction to a language is a godsend - honestly you’d probably question anything less since about 2015. This is cool, I dig it.

Installation

So I just download this script and pipe it to sh, okay not weird at all… Hangon what book am I reading again? It was at this point that I doubled back and checked that the book was actually like, the real rust book from the rust website with the real domain and stuff… Okay so yeah, seems official - To the Download!

In all seriousness I’ve seen people complain about this approach but to me it’s not a big deal. I’ve been fed up with Go as the downloads page sometimes makes too many assumptions - like if I want to pull an old linux build from my mac I feel like it sometimes ends up being a battle with their web UI. Maybe that’s fixed now but I’m still scarred I tell you.

So anyway, we download this rustup thing and use that to install rust. I don’t know the reasoning behind the whole .cargo/... sourcing. It definitely seems like there’s some tight coupling with cargo and rustup. I presume the 6-week release cycle has some impact on making the installs seemingly portable. In any case I’m kind of excited to find out really, but excited in the organic - this will probably bite me later but eventually I’ll understand type of way.

Hello World

The classic. Awesome. One of the first bits I like is fn over func as a keyword. I also checked out the binary sizes of a Rust hello world over Go, not that this really matters at all for a trivial piece of crap project but hey:

[ww hello_world]$ ls -l gomain rustmain 
-rwxrwxr-x 1 levi levi 2030698 Oct 20 23:21 gomain
-rwxrwxr-x 1 levi levi 3182392 Oct 20 23:19 rustmain

Go wins!!! Then I was like hmm, is there any funky linking?

[ww hello_world]$ ldd rustmain 
	linux-vdso.so.1 (0x00007ffcc0683000)
	libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fba9f151000)
	librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007fba9ef49000)
	libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fba9ed2a000)
	libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fba9eb12000)
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fba9e721000)
	/lib64/ld-linux-x86-64.so.2 (0x00007fba9f59a000) 

No, things seem fairly placid - how does it compare to the Go one?

[ww hello_world]$ ldd gomain 
	not a dynamic executable

O_____O" What?! I swear hello world didn’t used to be statically linked? Okay so I had to at least sanity check a simple web server would have some dynamic linking in Go:

[ww hello_world]$ ldd netgo 
	linux-vdso.so.1 (0x00007ffe06589000)
	libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007efca7baf000)
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007efca77be000)
	/lib64/ld-linux-x86-64.so.2 (0x00007efca7dce000)

Ahhh, there they go. My sweet sweet dlls … Of course none of this really means anything to me, but it’s curious to see more being linked in the hello world rust main than the web server in Go (what’s the deal with the librt linking btw rust?). That being said the web app bumped its size to ~6.5MB - I’m wondering if the Go compiler is being smart about cutting down on runtime for stupid programs and/or static compiling for the same programs.

Out of interest I also dumped a strace for each and you can view both the go_strace and the rust_strace if you’re interested. Spoiler: They’re quite different.

Cargo

This is a delight to me. Go still hasn’t recovered from its terrible lack of package management. I am still very meekly creeping around go modules, even though they are being used heavily in a lot of work projects. There are still plenty of projects using dep or even worse… It’s great to see versioning and packaging and building all being wrapped up in a single tool which is promoted by the language!

Final Thoughts

Bam - Lesson 1 is done! I liked it, it really wasn’t bad. A cool feature is having the book accessible through rustup too, so if you were stuck on an island but had rustup installed, you could have fun for a bit :)

One thing I didn’t particularly care for was having all the Windows shell samples and instructions given to me with the Linux ones. It’d be cool to select your OS, or have the backend just deliver me the trimmed down, OS-specific guide. It did do a good job in telling me that Rust is completely friendly to all major OS’es though, so that’s something.

I’m really keen to move forward with this tutorial and also with this series, it’s been a pleasure to write and reiterate these delicious morsels of anti-ignorance the book is giving me. So until the next chapter, so long Rustaceans and Gophers alike!


Update: Find the next post in the series here

comments powered by Disqus