On Leaving Swift for Rust
26 points by marcprux
26 points by marcprux
Some thoughts as a young programmer:
I love Rust, and have for most of my programming career, so I rebuke the idea that Rust is too complex for humans. Its been stable for just over 11 years. People have been writing great software in Rust, with their monkey brains and meat fingers, for over half of my life, and many continue to do so.
Actually, in my view, Rust has always been a particularly human-friendly language since it moves so many assumptions and invariants out of your brain and into the code where the compiler can prove them for you. Once you know the rules of Rust, there's a lot less mental load in understanding code because so much is guaranteed to you by the compiler. In fact, LLMs are only so good at writing Rust for these same reasons.
Yeah, agreed. I found Rust really easy to get into. Much easier than C and C++ for instance. Sure, it took me a good month or so to really get going, which in today's time is maybe an eternity. But at that point, I was already way more confident in my skills and doing cooler things than I'd been after 5+ years of C and C++.
Thanks for the thoughtful response, but I am a bit dismayed that the take-away from the post was that Rust is too hard for humans. That wasn't my meaning at all.
I think it is beyond dispute that Swift is an easier language than Rust, just like I think it is beyond dispute that Java is an easier language than C++. By "easy", I don't mean that Swift and Java are for dummies and Rust and C++ are for the rarefied elite. Rather, I think that Java and Swift make it easy to get stuff done without needing to struggle with difficult concepts and spend a lot of time learning. There is a reason why CS curriculums switched to Java for decades over demonstrably superior languages: it fast-tracks the route to tackling essential complexity without getting bogged down in the accidental complexity.
If the take-away was "use Rust because you can just get the chatbots to vipe-slop it all for you", then I clearly wasn't communicating very effectively. No one should be creating software without understanding the code. Seriously, don't be this guy. I'll be following up with a heartfelt apology to any and all who interpreted my words as advocacy of that brand of (sadly, all-to-common these days) solipsism.
I think it is beyond dispute that Swift is an easier language than Rust
I... am not sure that's true. Swift makes it much easier to me to leak memory, and much harder to write concurrent code. Swift is also just harder to use on the platforms where one might use Rust IME.
I likewise appreciate your thoughtful reply. To clarify a bit, "rust is too hard for humans" definitely wasn't the main takeaway I had from your post, but that sentiment just hits close to home with people like DHH sloping all over the place with a language I really enjoy and respect. I'll definitely be interested to see where you take day from here.
Lobste.rs has a big Rust constituency, and I think your post didn't really say much about the way forward (the "like and subscribe" part), so unfortunately I can see why it got derailed.
I have seen that happen in other threads too -- if you say something about a language, the whole thing gets derailed.
it moves so many assumptions and invariants out of your brain and into the code where the compiler can prove them for you
This! And it holds for many strongly-typed languages of course. When I happen to write code in Python I feel constantly “at risk” of forgetting something.
Swift does the same things you describe, and with a lot less annoying boilerplate like “return Ok(…)” and “.map(…)” and the horrors of dealing with futures. It is So. Much. Easier. To write code with the exact same semantics.
Rust people seem to disdain such ergonomics because they want every last detail to be expressed explicitly in code, like when you pass a reference or return an optional or clone a struct. Yes, the code doesn’t hide what’s going on (except for the “?” operator, or Deref conversions), but it is such a pain in the ass to keep writing that stuff, and to filter the meaning out of the noise when rereading code.
The problem is that Swift sometimes has extra overhead in the machine code it generates, usually in the form of extra copying. So it’s not going to be 100% as fast or small as Rust. Honestly I’d take easier over as-fast-as-possible, except maybe in some inner loops, but that’s not a choice I get except by switching languages (which I did, to Rust, unwillingly.)
The big problem with Swift is that it implicitly depends on a really big standard library — largely because of its overdesigned string API — and while that library can be swept under the rug on Apple platforms because it ships in the OS, other platforms don’t have that advantage, so when you build for those platforms you suddenly find your binary size jumps by 10 or 20 megabytes. (Not exaggerating.)
(Another contributing factor is that some things about reflection apparently make it impossible to effectively dead-strip unused code, as I found out when Bloaty showed that my release-mode binary contained everything from the huge AsyncCollections (sp?) package even though I only used one or two bits of it.)
annoying boilerplate like “return Ok(…)”
Funny thing about that. One of the Rust team created a macro crate (fehler) that got rid of return Ok(...) by taking inspiration from exception-based error handling... the ecosystem was uninterested, which killed any chance of it progressing down the "test as a crate and see if there's enough demand to consider making it more official" route and it's now unmaintained.
Granted, I was one of those people who didn't like it. I'm still of two minds about how async/await causes a mismatch between what the visible type signature says and what's actually getting passed around.
Overall, it's an artifact of Rust leaning into "code is read more often than it's written" as part of optimizing for codebases with long operational lifespans which will outlive all the original team members. It's much easier for me to scan a function I've never seen before if there's less syntactic sugar for me to need to keep in mind.
Yeah, I use “culpa” which is a fork of “fehler”.
It's much easier for me to scan a function I've never seen before if there's less syntactic sugar for me to need to keep in mind.
It’s much easier for me to read a function if the meaning is expressed concisely instead of with a bunch of boilerplate. Swift/Kotlin's “?” and “??” / “?:” operators are a great example, replacing a bunch of “map” / “and_then” / “unwrap_or” noise with very clear syntax.
No one disputes that APL is concise, but it's also very opaque for anyone who hasn't developed the "stenographer on a chorded keyboard"-esque skill of sight-reading it. Concision doesn't magically guarantee readability.
I'll agree that Rust's Iterator trait is an ugly god-object "even more of an embedded DSL than declarative macros" mess of a thing where I feel I spend most of my time interacting with it Ctrl+F-ing through my Zeal copy of the rustdoc page for type signatures. (Seriously. Declarative macros are a breeze to write from memory once you recognize that they're just a tweak on match with slightly different syntax for variable bindings and type annotations to avoid colliding with the syntax being templated, and a declarative equivalent to for paired up with their equivalent to .. from slice patterns.)
Beyond that, I've seen far too many of Rust's inspirations and progenitors and their too-"Perl is executable line noise"-esque syntax.
The question that needs to get hashed out in RFCs is where the "soul of the language" lies and whether you having trouble reading a function is a sign that the language needs to be amended as happened when try!(try!(foo).bar) got deprecated in favour of foo?.bar? or Rust introduced postfix await with no cited precursors, or whether you're reading Bournegol. (i.e. bending Rust into being written like a non-Rust language)
Your focus on map, and_then, and unwrap_or suggests to me that you're looking at code that was trying to be written like something like Haskell, when Rust is an aggressively mixed-paradigm language, to the point where integrating early return error-handling into iterator chains actively makes it hard to read and trying to get too functional in your coding style will displease the borrow checker.
Disclaimer: I am, at the moment, at best, a casual rust program reader.
nearly every week there is some new project converting some massively complex codebase from language X to Rust
In my experience, there is a missing word here. IMHO, the word complex should show up twice:
"nearly every week there is some new project converting some massively complex codebase from language X to complex Rust"
I accept that others will disagree. Some have already written so here. I humbly submit that there's a bit of incentive driven survivor bias there. It takes all kinds, 20 years ago, it was C++ vs those that, er, uh, didn't. It will be interesting to see over time, if simpler paradigm languages (probably not one of the current historical batch) emerge, allowing the new AI world to be more repeatable/concise. OR if complexity is now a managed thing of the past. Maybe the story will be "GC made the hard problem of memory a management for a large swath of programming problems a non issue, and AI made the hard problem of complex language models also a non issue."
Mad props to the author and his contributions in both spots regardless.
Yes I have a hard time gleaning much that is universal, or for that matter unbelievable, these kinds of posts are describing several collections of second system problems. Each one is though is worth talking about maybe not for it winning or losing or being easy or difficult or safer, but they are always all of those.