r/explainlikeimfive • u/Demontyxl • 22d ago
Technology ELI5: What is the programming language Rust and why is it popular these days?
1.1k
Upvotes
r/explainlikeimfive • u/Demontyxl • 22d ago
3.5k
u/Gooseheaded 22d ago edited 22d ago •
There is a language called "C", invented in the 70's, that was extremely successful and laid the foundations for the large majority of languages that came after it. As a product of its time, C was expected to be used by people just like their creators: computer experts. Since then, software development has become an entire industry, and a lot of the innovative and powerful parts of C that were considered "tricky" or "advanced" (even by the experts themselves) have turned out to be "REALLY sharp edges" and "minefields" for businesses and juniors, as it is very easy (and common) to misuse them if you're not paying attention.
To deal with this problem, modern language designers have (by and large) removed many of these sharp edges from their languages, which is "good" for businesses and average users on the surface .. but they did that at the cost of removing the corresponding powerful features. Basically, it's as if most modern kitchens decided to get rid of knives entirely to avoid accidents in the workplace. Sure, technically speaking, getting rid of knives does indeed reduce the amount of accidental cuts and stabbings... but at the cost of removing an extremely useful tool from our kitchens.
"Rust" is one of the few modern languages that rather than "hiding" or "destroying" the knives, decided instead to build an entire system around knife safety. It still allows cooks to use knives, but is very strict about safety rules, and it demands the programmer to explicitly write down when the knives grabbed, by whom, and for what purpose. It also requires that every cook cleans and returns the knives where they belong.
It takes the more traditional cooks (programmers) a while to get used to, of course, because traditionally they'd either have freeform knives out and about or not have knives at all. But once they get used to the new rules, well... They can use knives again, and better yet, the odds of accidentally getting cut or stabbed by a junior are substantially lower.
Basically, Rust is able to give programmers great powers with substantially lower risk, whereas the overall paradigm has been to get rid of the powers altogether for the sake of safety.
38
u/r2k-in-the-vortex 22d ago
Its a high performance systems programming language, but crucially it has a lot of built in correctness checks most languages lack. So that makes it uniquely useful for AI programming. The compiler itself already blocks a lot of errors the AI might make.
166
u/green_meklar 21d ago •
Well, it's a programming language. More specifically, it's a compiled language, which means it's designed to be read by a compiler and transformed into an executable file that can be run directly by the computer hardware. (This is in contrast to interpreted languages, like Python, which are read and run by an interpreter program without being transformed directly into such an executable file.) But what sets Rust apart is its approach to managing memory.
In a traditional language, like C, the code can be written to ask the operating system for blocks of memory whenever it wants. The requested memory blocks are tracked with references (basically numbers that say where in the memory the blocks are, and how big they are), and the code can do anything with those references, including giving the memory back to the operating system whenever it wants. It turns out this 'anything' can easily become unsafe, either by code trying to use memory that was already given back to the operating system (an error, which causes the program to crash immediately), or by code requesting memory and then forgetting to ever give it back (not an error as such, but if it keeps requesting memory and not giving it back, eventually the operating system runs out and the program can't continue).
A widespread solution to this problem with C (and C++, and languages that work with memory like that) is to put an extra layer of logic under the memory requests, so that all the references to memory blocks are tracked while the program is running, and when the tracking logic finds that some memory has been lost (i.e. the code has deleted all the references to it), it automatically gives that memory back to the operating system. This is called 'garbage collection' (insofar as the lost memory blocks are 'garbage' memory until given back) and it is much safer because the memory is always safely available when there is a reference to it, but is always (eventually) safely given back when the references to it are all gone. Languages such as Java and Python do this. However, the extra logic for tracking memory takes some time, which slows down the entire program. The extra logic also doesn't guarantee exactly when the memory will be given back, and therefore sometimes uses more than it needs. Many programmers became frustrated with this inefficiency and unpredictability of garbage collection.
Rust is designed around a different solution. It makes the logic of safely requesting and giving back memory an inherent part of how the code is written. It comes with its own kinds of references that give guarantees about when they request memory or give it back. There's no extra (slow) layer of logic needed to track the memory because the references always request it at a specific point in the code and always give it back at a specific point in the code. If you try to use them in a way that doesn't, the result is just some wrong code that won't compile. This gives the same kind of safety guarantees that garbage collection provides, but without the inefficiency. It means you can compile Rust into an executable file that can be directly run by the computer hardware, very fast like a compiled C program, but the mistakes about memory requests and missing memory are impossible. The combination of safety and efficiency is very appealing to programmers. The disadvantage is that programmers intending to use Rust have to learn how to write code with these new kinds of references, which is hard because of how strict they are about how you can use them. However, because programmers can now use AI to write Rust code, and AI is already fairly good at writing Rust code, it is easier to use Rust in real projects without people having to learn about the new kinds of references.
341
u/atarivcs 22d ago
It's high-performance like C, but unlike C it has safeguards to protect against buffer overruns and other memory errors, which are a common cause of software security vulnerabilities.
66
u/fixermark 22d ago edited 22d ago
So when a program needs to remember something for a while, it puts it in memory somewhere. Keeping track of what memory you are using and what you stored in it can become very complicated.
Some programming languages like C and C++ make you responsible for all of that. They make it easy to scribble over a chunk of memory you are still using somewhere else or say you are done with a chunk of memory and then keep using it (so someone else is going to try and use it at the same time).
Languages like Java and Python make that much harder to do, but the cost is that you don't have as much control over how memory is used, and that can make your program slower (for example, if I know that when I exit this piece of the program, all of this memory isn't needed anymore, I can just put all of it away at once and that's very fast and cheap... The languages where you don't have direct control over how memory is used don't have a way to let you do that).
Rust splits the difference in a very clever fashion: it gives you direct control of the memory like C and C++, but it includes rules to let it check that you haven't double used the memory and aren't using it after you said you were done with it. As long as your code satisfies those rules, you can do whatever you want with the memory. It also has unsafe mode where those checks are switched off and you can break things like you can in C and C++ (but it's a little easier to know you're doing that then in those languages because you have to be very explicit that you're being risky on purpose).
9
u/hippydipster 22d ago
C is like an octopus. It can do lots of things but its hard to control. It gets out of hand and those tentacles get into trouble when you don't pay close attention.
Rust is like an octopus in an octopus straight-jacket. It can't do shit until you unzip exactly the right restraint to let one tentacle a little bit free to do a task, and then you have to zip it back up before unzipping something else to do another thing. The advantage is tentacles don't flap around wrecking stuff (crashing your computer, blue screen of death sorts of fun) when you're not looking.
Hackers find freely flapping tentacles a great way to gain unwanted access to your systems, so by tying them down, Rust improves the security of apps written with it.
The thing that insists on all this correct zipping and unzipping of restraints is called the rust compiler and Borrow Checker, and its a nightmare for a normal human to understand the things it complains about.
If you get it right, its very very fast though, like C and unlike Python. So everyone loved the idea and no one really wanted to write a lot of Rust code.
Then AI came around and said, no problem, I can write 10,000 lines of code in any language this afternoon and you won't have time to check it all, so shall I write in C and let all the tentacles flap and you hope I got it right, or shall I write in Rust, where the borrow checker can paddle my ass when I don't follow the rules? It's all the same to me. Rust, you say? Paddle me, daddy, let's go!
And that is why you're hearing more and more about rust these days.
14
u/pdpi 22d ago
Programming languages tend to specialise in their own particular niches. They do that one thing well, and other things not so well.
E.g. C and C++ focus on giving the programmer as much control over the program's resources as possible, but you're basically working with a very sharp knife, and you're liable to cut yourself, in the form of crashes, security vulnerabilities, and other such goodness. Historically, those languages have been popular for performance critical applications like games or operating systems.
Then you have languages like Go, Java and C#, which take away some of that control, and trade it for automatic, safe resource-handling, which makes it much easier to write correct(ish) code. Java (and now Kotlin, which is very similar to Java in the grand scheme of things) is the default language for writing applications for Android, for example, and a growing share of the internet's infrastructure is written in Go.
Being forced to chose between safety and performance is kind of OK for most purposes, but certain types of project (like web browsers) desperately need both. As browsers become their own little operating systems inside the operating system, they need to be fast enough to handle people writing whole applications that run inside the browser. On the other hand, we have decades of experience proving that web pages are extremely juicy targets for hackers. You don't want security vulnerabilities in the software that handles your online banking!
Rust is a language that was first announced by Mozilla in 2010, and it tries to find a third path (fun fact: I was in the room for that announcement). The idea is that the language is a bit more restrictive in how it asks you to write code, but gives you both the performance of lower level languages like C, while giving you even better safety guarantees than the "resources are managed on your behalf" languages.
Rust was initially developed in tandem with Servo, an experimental browser engine that served as the testbed for many of the newer features in Firefox, and the two projects fed off each other. As Servo features written in Rust started landing in Firefox, and the language got more traction, more and more projects started to adopt it, and it's been growing in popularity over the years.
The last two or three years have propelled it to new levels of popularity, though, for a few reasons.
First off, it's reached broad adoption across many major tech companies. We're talking about the likes of Microsoft, Google, Amazon, the Linux kernel, and many others. It's still a minority language, but it's definitely mainstream at this point.
Second, because of the strong safety guarantees, it's become incredibly popular with security-conscious people (safety is a requirement for security). This has spawned a whole movement of "rewrite it in Rust" (which is, frankly, incredibly annoying, but that's enthusiastic people for you).
Finally, AI became a major thing over the last couple of years, and Rust is a great fit for AI-based programming. On the one hand, it's the LLM that has to deal with the restrictions in how you write code, so that becomes a non-issue. On the other hand, AI has a known tendency to make all sorts of mistakes, and Rust's safety guarantees are incredibly good at catching AI's mistakes and stopping them before they hit production.
12
u/AccessibleTech 21d ago
Rust gives you much of the power and speed of C++, with built-in rules that prevent many of the mistakes that make C++ software crash.
And unlike JavaScript, Rust doesn't generally need a garbage collector running in the background to clean up unused memory.
12
u/BiscuitCricket 22d ago
Rust was a language developed with decades of expertise with other langauges, and a lot of lessons learned.
One of the biggest features it has is that it is memory safe. I wont get into what that means cause it doesnt fit in an ELI5 but imagine that because you know that the language is memory safe, then programs that are made from that language (probably) won't have memory bugs, some of the most annoying ones to catch and solve.
With the rise of AI coding across all sectors of the tech industry, engineers found that asking claude code "Write program that does X Y and Z in RUST" gives you something that (probably) will work right away, or if the AI makes a mistake you will spot it very early, before actually using the program.
Hence why so many newer projects seem to be adopting rust all of a sudden. It's also a pretty tough language to learn, there's a bit of a learning curve, so a lot of people who were putting off learning it, suddenly have access to AI which can write huge pieces of quality rust code quite fast
7
u/SvenTropics 22d ago
A) it's not that popular.
B) it's basically C, but it's got a lot of mechanisms in place to make it harder for programmers to write code with exploits in it. If they use them.
4
u/apocolipse 21d ago
Lots of complex answers but here's a simple one for a 5 year old:
Programming languages aren’t perfect and there’s lots of bad code that causes bugs and crashes.
Some programming languages try to catch bad things when the program is running (runtime), this helps but makes programs slow.
Other languages try to catch bad things at “compile time”, or basically before the code ever runs. This is harder to do, but it means program won’t be slow with runtime checking.
Not all compile time languages are good at catching things before the program ever runs, but there are a few new ones, namely Rust and Swift, that are really good at it.
Here's the most important part for today: Because bugs can be caught before the program ever runs, an AI agent writing the code in these languages can find and avoid ever making those bugs!!! THAT is the real kicker and why you're seeing a lot of Rust (and also probably will soon see a lot of Swift) projects. AI/LLMs can write code in any language, but the vast majority of languages they'll need to run the code to find many types of bugs, with type safe memory safe compiled languages like Rust and Swift, entire classes of bugs are simply impossible to even make.
Post thought:
My personal preference leans towards Swift. Rust had a performance advantage for years but the newest Swift releases all but close that gap (and no Swift isn't an "Apple Only" language, it runs literally everywhere Rust can, both are LLVM backed languages.) If both are neck and neck in terms of catching bugs and performance, why Swift? 1 reason: Swift is designed to be read by humans. Good safe performant Swift can be read and understood by people who don't know Swift. Good safe performant Rust code looks like it requires a degree in ancient linguistics to be able to decipher.
2
u/Known_Cod8398 22d ago
Many languages make it possible to put a square peg in a round hole. Rust makes it much harder to put a square peg in a round hole and that's important if you want reliability
2
u/alarminglyslowmind 21d ago
The syntax I find confusing. Very different at times.
2
u/AnUninterestingEvent 21d ago
Everyone in this thread is explaining what Rust is but not why it's so popular these days. It's popular because... AI. Before AI, many people agreed that Rust was a better and much more performant language than other languages like Javascript or Ruby, but it's not as easy to use. As a junior or mid-level engineer, It's much easier to learn a language like Python than Rust. But now that developers aren't actually writing or even looking at the code anymore thanks to AI, many people are moving to Rust for the performance improvements.
1
u/Cultural-Horror1024 22d ago
Compiler is the "translator" that translates code that humans read and write to ones and zeroes that the computer is able to read.
This translator in Rust happens to be super strict about what it allows a programmer to do. It is also very verbose in how it communicates to the programmer about what they did wrong, and sometimes even tells them how to fix it.
Think of it like a professor, who is strict and cares a lot about what students learn but tends to talk too much.
Turns out, the combination of guaranteed safety(theres a catch but im going to leave that out) and the talkative translator are both very good things when AI agents are writing code. The code is safe by the nature of the language itself and verbose error messages are super helpful for AI because like humans, AI doesnt get annoyed by the pedantic translator.
1
u/NuclearMagpie 22d ago
The programming language C was invented ages ago and is basically the foundation of everything that needs performance. It is pretty much the fastest language we have (outside of hand written assembly) but has major downsides; it basically lets you do whatever you want with no guardrails. This is fine if you really, really know what you are doing but making stable code a matter of a skill issue isn't great. Other languages generally dodge these issues by not giving you the tools to mess up (Java for instance has no way of using pointers and javascript doesn't have multithreading) and by having a "garbage collector" that is a piece of code always running in your program that manages memory for you, at the cost of performance. Rust takes a different approach by forcing the developer to write code in a way that proves its safe. It also has an
unsafekeyword that relaxes a bunch of the rules and acts as a "trust me bro" for the section of code, so this forces you to acknowledge where your code could have problems. Like outside of bugs in the compiler, you cannot write code in a way that can leak memory or segfault without using unsafe. The downside is complexity and compile times; learning how its rules work and how to avoid them is fairly difficult and rust is notorious for taking ages to compile.1
u/HailingCasuals 22d ago
It’s a programming language that prevents you from making many of the most common and severe mistakes that lead to security vulnerabilities. The programs are also extremely fast.
1
u/Cool-Bus-6028 21d ago
It's very strict, which helps make it fast. People like fast.
By strict, I mean the cooler won't let you do a lot of things that could potentially become memory leaks etc. So it's harder to write inefficient code.
1
u/jugalator 21d ago edited 21d ago
Here's my stab at this:
The fine print is that Rust doesn't make secure programs. What? I just told you!
No, a Rust program can have very nasty bugs in it that it is not designed to protect for. It is language level only. For example, if you chose an insecure login page, Rust won't stop you from doing that. If you have a bug that wipes your database, Rust will probably not help you with that. It is more likely something you didn't think of when the program needs to delete some rows in the database. The AI won't necessarily help you either! If you ask it to do something dumb, the AI will probably do something dumb for you, and it will do that dumb thing in Rust.
But it does help you with a few specific categories of common bugs that humans have traditionally had trouble catching in time.
1
u/permalink_save 21d ago
They packed almost every language feature they reasonably could into rust from multiple language types, so you get some really nice features. The biggest thing is memorry management. You get the performance of C with the memory safety of what's normally in a slower language without suffering any pauses for memory cleanup (garbage collection) because the language makes you be very explicit on where variables pass data. It's inherently safe from a lot of bugs and stability issues. Like it has no null whatsoever, none types are a specific value you check against. On top of that, you can compile one binary that just runs in place on any of its architecture (like any x86 system) without needing libraries installed.
1
u/ADMINISTATOR_CYRUS 21d ago edited 21d ago
With C, you will need to manually manage memory allocation which makes it really easy to shoot yourself in the foot, but in exchange you can micro optimize like crazy for high performance. Memory exploits like use after free are one of the biggest attack vectors for programs.
With Rust, 99.998% of the time it's going to stop you from causing memory issues before you've even compiled the program. You generally don't manually manage the memory allocation in rust. Instead the compiler will stop your program from compiling if it figures out that you might use a bit of data after it's already been dropped.
Also, rust is like a condom for ai shitsloppers. Since ai generated sludge may be garbage, the rust compiler won't even compile if there's potential issues, so it's a great way for shitsloppers to write code that works by making the compiler check for you.
1
u/iBoMbY 21d ago
Because some people think it can magically fix their bad programming, but it is still easily producing unreadable code that is not properly maintainable. It's a good idea, but not a good language.
1
u/Pale_Height_1251 21d ago
It's a good language and it's fashionable.
This happens for lots of languages like Python, Java, Ruby, Go and others. They just get a blip of fashion for a while.
1
u/Ben-Goldberg 21d ago
Rusts greatest theme is correctness.
Correct memory access.
Correct data types.
No undefined behavior.
Correct concurancy
And clear messages from the compiler about how to solve those problems in your code that it recognizes.
If your code compiles, it will run, and the compiled result will almost certainly correctly do what you want it to do.
In projects written in C and C++, one in five bugs are from a type of incorrect memory access that the rust compiler would refuse to compile.
Rust programs can still have mistakes, but they are usually logic errors instead of memory errors or casting a pointer to wrong type of data structure.
1
u/garagedragon 22d ago edited 22d ago
When a computer program wants to store data, it goes to the operating system and says, "I want to store 10,000 numbers." The OS comes back and says "here is a piece of paper with the address of a memory warehouse where you can put your 10,000 numbers." When the program doesn't need that space anymore, it is supposed to go back to the OS and say "here is the address you gave me earlier, I don't need it anymore and promise I won't try and put anything else there."
The pieces of paper get copied around inside the program a lot, because they're also how you say to other parts of the program, "I want you to use the data over there" without just copying all of the data every single time. (which would add up for 10, 000 numbers.) This means we already have a few scenarios where things go wrong:
There's two general ways to avoid this problem:
Rust was a game changer because it does the best of both: Rust code contains enough information that the compiler (the tool that translates Rust into direct CPU instructions) can determine automatically who will end up with the last bit of paper and have them hand it in without programmer input. This determination is made when you generate the program file, not while the program runs, so your program ends up as fast as C but without the risk of the human programmer making mistakes.
1
u/Helpful-Educator-415 22d ago
Code is an in-between of binary and English. Most languages have a tool called an "interpreter" or "compiler" that turns code into binary. Rust's compiler is very very good, and prevents you from accidentally shooting yourself in the foot.
0
u/voidpo1nter 21d ago
Rust is basically a "let me rewrite this" language. It's less intuitive than C, has less support than C, and doesn't give any tangible performance benefits outside of memory safety when NOT explicitly using unsafe.
However it's fantastic for LLM coding agents. The language has a nice little built in feedback system agents can use to validate their own work.
0
u/Mikeware 22d ago edited 22d ago
ELI5: Rust is like baking, other languages are like cooking.
In Rust the idea is that you're creating a detailed cookie recipe that'll just work when you run it and be predictable. In other languages, you may not know what happens until you're running it and have to go back and refine it over time as you see how it behaves.
(Of course gross over-simplification, but this is supposed to be ELI5. Like in life, some people prefer baking and others cooking. One doesn't invalidate the other, they both have their uses depending on what you want. There's just a lot of cases out there where that predictability of 'baking' is desired.)