Why Rust are the most used (Popular) Programming Language in Blockchain ?
What Programming Language for Blockchain
So what does a programming language for blockchain actually mean ?
The number one, would be writing web front ends for like Web3 apps or DeFi apps or something like that, that’s NOT where Rust is popular these days. So you see a lot of JavaScript there has eaten the world and it’s just ubiquitous because it’s ubiquitous it’s so popular for every anything running in a web browser.The second thing, where a programming language comes into play with blockchain is writing the actual code of the blockchain validators. So that means the code that’s running on a smart contract / blockchain is executed by so many validators and the software for that validator that needs to be written somehow and Rust is popular in that field so that was number two.The number three, is writing the actual smart contracts, so writing the Dapps (decentralized apps) that run on top of a smart contract blockchain there you need a programming language as well and Rust is gaining a lot of traction in that field as well.
Explaination why blockchain need validators.
So what’s a blockchain, it means it’s something that’s not 100% centralized, so you have lots of validators that validate transactions, so blocks are being produced by someone who gets lucky and gets to produce a block at a certain point in time, but then there’s others that witness that and validate that block, and there is so many validators it could be, just tens of validators in like highly centralized blockchains such as BSC, but it can be thousands of validators in highly decentralized blockchains such as Solana, which has one of the highest Nakamoto coefficients in the industry, the Nakamoto coefficient is measuring the decentralization of a blockchain, but also other blockchains have High degrees of decentralization.
Why Rust than other Programming Language
So let’s talk about why Rust, and what are actually the features, what’s different with Rust than with other programming languages.
The Validator Concerns
If you think about that validator software and the smart contract software and you remember that blockchains are about potentially thousands of validators, running the same code over and over again just to validate that though the one validator that produced the block has done the right thing, so all the others need to replay that and execute the same code just in order to validate it.
So if we think about with a lot of validator,The performance is really important in blockchain.
The Scaling Concerns
And then if you also think about blockchain’s vision, the public blockchains are there to make a meaningful change to society and get the world away from that centralized Winner Takes all kind of mode to a more community-owned Community controlled owner our own data mode which the blockchain stands for, if we want to make that meaningful change to society, we have to get a lot of people and eventually billions of people get on board with the blockchain. That means the blockchain needs to scale, there’s no point in something that can do a hundred transactions per second and then you have to Silo it into layer twos with ZK proofs and stuff like that, which are great things, but they are not kind of that worldwide scaling in just one layer and Global price Discovery and stuff like that we want to do.
So if we want to scale that whole thingThat means both the code and the validator and also the code in the smart contracts, they need to be fast.
So that means having like interpreted programming languages and even if they have just-in-time compilers and stuff, it’s probably not the right choice.
You want to have a programming language that’s close enough to the hardware to be as fast as possible.So that’s where Rust comes into play.
The Programming Safety Concerns
If you think of Hardware programming close to the hardware other languages such as C or C++ come to mind as well, Rust is kind of a modern version of these C or C++ things, and it has an additional feature or that’s called Safety, so all the problems that there have been classically a lot of problems with C and C++ programs called memory leaks, so that means you allocate some memory then the programmer forgets to de-allocate it, and frankly it is tough to get the code correct that in all circumstances and in all branches that the code executes you do the right thing, and properly deallocate what you’ve allocated before and stuff like that, and these memory leaks are just crazy stuff most of the time, they are to blame when you need to reboot your PC or something because some and in modern operating systems of course it has been greatly reduced but I think 10–20 years back with Windows and everything, it crashed all the time just because the apps had memory leaks and we cannot afford that in a blockchain.
It has to the blockchain has to be a 100% reliable, we don’t want it down or anything like that.So it has to be a safe programming language, and it has to be safe in concurrency mode as well.
The Concurrency and Pararellism
So if we do things in parallel and if we want to scale, we need to do things in parallel, we cannot just do one thing after the other we have to do as many things in parallel as we can in order to make that whole thing scalable but when things run concurrently,
And by the way there’s a subtle difference between concurrency and parallelism,
So parallel means we have different Hardware that actually runs in real parallel.And concurrency, just means it seems like it’s in parallel but under the hood it’s kind of time sliced, so we only have one piece of one several threads.
But whether it’s concurrency or whether it’s parallelism, it is hard to write code that is safe, when it’s comes to concurrency, Rust helps there, and it is tough to write code in Rust that is not time Save, so that’s a great advantage of Rust, which many blockchain developers feel attracted to, you need that safe code that also runs safely in concurrent environments you needed performant.
And there is one feature that is quite unique, if you get all these three performance, and then safety, and concurrent environments, and then number three is not having a garbage collection.
The Garbage Collection
So all these there are so many other programming languages that want to avoid those memory leaks that I talked about in the beginning of this video, and in order to avoid the memory leaks like the programming language manages the memory itself, so you the programmer doesn’t have to allocate the memory himself and then or herself obviously and deallocate it.
The programming language does it for them, but then there might be variables or data in memory that becomes unusable, just has aged and it’s not accessible anymore in the code anyway, so it can be deleted, so then these programming languages let the memory run full and when the memory threatens to come under pressure, they stop the world right they stop all your program that the developer has developed, and then they run a garbage collection trying to find pieces in memory that are not needed anymore, and can be disposed of and they stopped the world, it’s kind of unacceptable in anything that’s close to real-time environments, not saying that blockchains are necessarily always real-time applications, but garbage collection doesn’t work in real time, that garbage collection also is not really advantageous in blockchain environments, because you want some predictability.
Think about the block times or slot times in Solana which is 400 milliseconds, it’s just 0.4 seconds, it’s like boom boom boom, every garbage collection and we never know in advance how long the garbage collection takes right it can take 40 milliseconds, but in some cases it can take 2 seconds, and that’s not what we want.
So if we want a programming language that’s fast, because it’s close to the hardware, it needs to be compiled, it needs to be type safe, it needs to be safe, with regards to concurrency, and have no garbage collection, then the air gets really-really thin.
Try to figure it out, you can do C or C++ Assembly Language, which all of them are low level older languages, that all have their advantages but also they don’t feel modern, and having modern C++ is it has so many issues and just writing everything in C is really low level, you don’t have high level constructs or stuff like that.
So I think Rust is kind of the only choice and there is another programming language emerging right now that’s built on top of Rust, it’s called Move.
Rust Family of Languages
It’s the Move language and that’s just built as kind of a subset of Rust and has some special features, that make these Move semantics that originated in Rust, Rust has the Move semantics, so you Move ownership of of a block of memory of a variable from one place to the others, just to make sure that there is no shared ownership, and then you have a concurrency conflict, that’s going to be avoided, so that’s why Rust came up with these this unique Move semantics.
And that’s taken to the extreme in that programming language called Move it’s being adopted by some modern blockchains, such as Aptos or Sui, so that’s also a great language, it’s also supported by Solana recently, so you can write smart contracts in Move, but I would consider that kind, like kind of the rust family of languages.
Summary
Rust is programming language that’s fast, because it’s close to the hardware, it’s compiled, it’s type safe, it’s safe with regards to concurrency, and have no garbage collection, then the air gets really-really thin. So that was my little overview on why Rust is so popular in the blockchain community.
References:
Youtube: The Rust language is popular in crypto. But why tho? — Blockchain Bernie
Rust for Blockchain was originally published in Coinmonks on Medium, where people are continuing the conversation by highlighting and responding to this story.