Rust-lang - Tumblr Posts
a dinner date with rust
I’m a programmer and as a programmer I write code. There are a bunch of different programming “languages” that you can write code in, and one that I frequently find myself reaching for is called C. “Ok, cool, why do I care?” Well because C is monster that will eventually consume all of humanity. Created in the void from which all computing was born, it is The One Language that undergirds all the others. Rarely written but subtly permeating every crevice of our technology. It contains deep, dark secrets understood by only those steeped in the occult, the conduits to the souls of our computers, the systems programmers 🧙 (like me!)
oh and also pretty much every piece of software ever relies on it somehow ^.^ Don’t worry though everything is fine!
“Oh, that sounds Concerning!” you may say. Well, yes! Yes it is. And so Rust was born. The sales pitch for Rust goes something like this: “Rust! Because C is Bad and is Literally The Bane Of Your Existence.” Which... well, they make a compelling case! As a C programmer, I was skeptical of a language that claimed to have all the same features without any of the downsides, but eventually I decided to give it a go.
“Mmmmm tasty!” I say, biting into some bounds-checked arrays. I take a sip of the mutability rules and exclaim “Wow! This really is something incredible!”. For desert, I decide to order some of their world renowned ownership/borrowing system. It arrives, piping hot, and I begin to dig in. “Hmmm, this is nice... I think?” Worry begins to slip into my voice. I think to myself, “this does seem useful, but it feels like there’s some trade off being made here”. The waiter tells me how this was made with the worlds finest data race protection and my worry begins to grow.
A brief tangent on data races:
You know the little people who live inside the computer and make everything work? A data race is when 2+ little people are trying to work on the same project at once without talking to each other or making a plan. Things Will Go Wrong.
Data races are generally very bad and Rust does it’s very best to make sure that having one is literally impossible. The problem with that is that sometimes, you can have a data race, but it can actually be safe and fine and good! And by sharing the work, the program can finish it’s work much more quickly! However Rust is very quick to put it’s foot down and say “No! That’s too risky! I can’t allow you to put yourself in danger like that!” whereas C would say “Yeah, sure, go ahead, good luck ;-)”
So, this is where my dinner date with Rust started to go downhill. One of the things I write a lot of in C are “highly concurrent” which means that they have lots of little computer people working on them at once. It’s tricky, but it’s something I have a lot of expertise in. And when I tried to replicate those programs in Rust was fighting me at every turn.
It’s odd because while Rust is very picky about this stuff, and it even provides ways to tell it “Yes, I know what I’m doing, I promise you can trust me.” But these ways are just a little too clunky for my liking and proved to be enough of a pain to manage it really soured my whole meal. And I was so eager about it too!
I still would like to try Rust from time-to-time on smaller projects, but it’s really unfortunate that it’s just so unergonomic in many of the ways that matter most to me. And while it isn’t a great fit for my use-cases, I’d highly recommend it to anyone who wants to make their first foray into systems programming. For nearly everything aside from my specific use-case it seems to be leaps and bounds ahead of C, and C is really the only thing like it.
Rust is cool, tasty, and elegant to C’s old, rickety, and ugly, and it’s a huge boon for the systems programming ecosystem. But I get the sense that Rust isn’t exactly the C-killer it was made out to be and that C still has a place in the ecosystem (and our hearts 💖)
You ever start using a tool for a project and the vibes are just sliiiiightly off? That’s how I feel about actix-web. It seems nice, but I get the impression there may be something more sinister under the surface.
Tomorrow I am going to try out tide as a actix-web replacement and I will report back to you on how it goes. I’m worried because it seems a little disorganized/undirected but I think it’ll work out. I will need to pull in some new components to do connection pooling and the rest. Argh I really wish rocket had websocket support so I could use that :(
I tide-ed! The tide! It rise-ed! I tried tide! (ooo that’s a tongue twister!)
So, first impressions were very bad because the docs on cargo.io told me to install a version TWO WHOLE VERSIONS behind the actual correct one which just gives bad juju about the whole entire project. Y’know, if you can’t update something as simple as that, who’s to say any of the other docs are up to date?
And, very basic things like wiring up a database using shared state (across requests) seemed very poorly explained or totally undocumented when I dug around in the tide docs/repo (which in retrospect isn’t actually on the tide people but sssh I didn’t know that when I was getting this bad impression).
See, the tide docs did cover state, but because Diesel has connection pooling (which requires shared ownership), rust really didn’t like to put Diesel’s connection pool into tide’s state. This is the sort of compatibility problems that you hit when you use an unopinionated project like tide, but usually the devs try and smooth it over with good docs and maybe some special compatibility packages to fill the gaps.
It took me a while of digging through random github repos to figure out that actually this was Diesel’s fault, not tides! Diesel just didn’t really document their connection pooling features at all (the creates.io page for that module is nearly empty) and in my confusion I ended up using the wrong struct by accident.
That was the problem that I thought was being caused by tides state! Man, for a community as fast moving as rust and as assertive about how clean code and good docs are important, some major projects are really lacking :/
SO anyways, all that’s sorted now and on the whole tide looks pretty nice! It really reminds me of my early days with Sinatra in ruby, and I think it’ll do, at least until something more mature like Rocket gets websocket support.
Anyways, tl;dr the project is back on the rails 😎 (but not ruby on rails)