Rust: A Systems Programming Language for Tomorrow's Networked World. (Just slowly started on it maybe a week ago and am very happy that I made that step.)

Rust: A Systems Programming Language for Tomorrow's Networked World. (Just slowly started on it maybe a week ago and am very happy that I made that step.)

By rhyzom | rhyzom | 23 Mar 2020


Aside from functional programming (Haskell, Scala, OCaml..) picking up momentum and getting adopted across a range of industries and business (and appearing to be particularly well suited for financial applications, hence why Cardano went for Haskell and Tezos chose OCaml), I began also noticing Rust suddenly raising in popularity and from a relatively young language with barely much adoption, starting to actually even increasingly become a sought after skill. And upon closer investigation, for very good reasons. So, in case you were wondering what you should pick up to learn so that you may have a chance to land an IT job earning you disproportionately more money (and, as is sometimes the case, for proportionately disproportionate little effort and without the burden of much responsibility cos you're a programmer, not some disposable sysadmin or something) than other things that aren't as cool any more.

So, anyway, I been getting into Rust these days - cos of Holochain initially (as they build their framework and initial SDK in Rust), but then quickly realized that it's about a lot more than that. So, here's a few things I learned upon looking up Rust initially which aroused my curiosity - Rust is meant as a C++ replacement designed by Mozilla for Firefox and it can compile to WebAssembly. It combines speed and safety/security in a manner that makes it incredibly fast and resource efficient. The trade off to this being the steeper learning curve (compared to say Python which one picks up fairly fast, but don't worry, that doesn't mean Rust's logic and syntax isn't incomprehensibly byzantine, on the contrary, it makes a lot of sense). It can go as low or high-level as you want it to and Rust-based domain-specific languages (like what most smart contract languages are) can easily be implemented using macros (basically a way of meta-programming, a kind of interface in the middle which we'll discuss in more detail further along).

That said, Rust appears to be a perfect fit for Holochain for a whole lot of reasons (WASM compatibility being pretty priority one), but also Holochain and Mozilla seem to be unofficial partners in that they appear to share similar goals and philosophy and Mozilla is said to have particular interest in Holochain as it is widely believed the Internet of tomorrow will be one of peer-to-peer and composed of distributed applications, exactly like Holochain - and because of that Mozilla wants to be in position to create the first browser which provides easy access to this world of distributed p2p apps like holochain's. And WebAssembly (WASM) is expected to substitute things like Javascript for web applications with a much faster and resource efficient lower assembly level language for the web.

Rust is implemented only on an LLVM compiler, so therefore not completely self-supporting. Variables in Rust are immutable (read-only) by default and can be made mutable (read-write, changeable during execution) by explicitly declaring them so with the mut keyword. Memory management is perhaps the most important part of any programming language and how Rust goes about that is what makes it fairly unique. Unlike as with some popular languages like Go, Java and .NET, Rust does not use a  garbage collector (a way of automatically managing memory where a collector automatically reclaims no longer needed objects from memory at periodic intervals) and memory is instead allocated to either a stack or a heap - things such as value type variables are stored on the stack because of it being more compact, ordered and accessing the contents of its data much faster (first in, first out - or FIFO for short, is the principle of how pushdown stack models operate) and heaps are used to store more complex data types of unknown sizes at compile time (things like reference type variables, for example).

9c2VH.png

A (pushdown) stack is an ordered data structure which can store small pieces of data and access them fast. A heap on the other hand has no particular order and is meant for storing bigger chunks of data.

For comparison, the EVM (Ethereum Virtual Machine, Ethereum contracts' compiler target) is a simple stack-based virtual machine (VM) as Ethereum contracts require a fairly small set of op-codes and operations take place fast and do not need to occupy memory any longer once having changed the state of the contract/ledger. Or when compared to Python or Javascript for example, there's a lot less memory control involved in those so they also tend to be slower. Rust is also statically and strongly typed programming language - which means that all types are known at compile time and are so designed as to make it significantly harder to write incorrect programs. Due to its memory management and control it's also sometimes referred to as a systems language, implying that it generates the best possible machine code with full control of memory use.

The system of ownership which defines Rust's memory management is a rather new concept for many, if not most programmers, but also what fundamentally makes it appealing and so preferable in so many instances. For example, Discord just recently switched from Go to Rust re-implementing the client and published a blog post explaining the reasons for deciding to make that transition - and they are mostly to do exactly with more efficient memory management and quite significant performance improvements. Ownership in Rust means that each value has a variable which is referred to as its owner - that owner is defined by its scope (with the value getting dropped should the owner go out of scope) and there can only be a single owner of each value at a time.

Rust

In any case, I really recommend Steve Donovan's "Gentle Introduction to Rust" for starters, I found it to be a breeze and really well articulated and put together. It also makes sense for Rust to be implemented as a smart contracts and distributed apps language given its properties and what contracts by definition require to be trusted and inspire confidence (implementing a Rust-derivative domain-specific contract scripting language, that is, with the necessary constraints and contract-oriented syntax, is what I mean).

And this concludes my super brief and basic Wiki-style introductory overview of Rust - the plan is to continue adding and publishing pieces on Rust, both technical and demonstrative ones as well as conceptual and explanatory more relating to programming languages theory and the various design rationales and considerations involved in building a programming language.

Next I'll go into types, functions, macros and meta-programming with specific/concrete examples, explain package management and dependencies (cargo), the compiler and error handling, traits and others. Also perhaps a quick cheatsheet-like sketch of Rust's syntactical idiosyncracies. 

It's really a great language to get to learn (I am in the process of learning it, haven't really gotten the hang of it to write anything too complex) and undoubtedly an enormous plus/advantage to know/have some idea of in the future ahead.

Basically, it seems to me (as mentioned earlier), right now if you want to get a well paid and most likely interesting job/work programming, you should either get into functional (Haskell and the like) or Rust. Only, one should keep in mind that functional programming is so much like actual mathematics that it is precisely that feature of it which turns off many non-mathematically inclined programmers. While Rust is likely to become the new Javascript in the long run.

How do you rate this article?

4


rhyzom
rhyzom

Verum ipsum factum. Chaotic neutral.


rhyzom
rhyzom

Ad hoc heuristics for approaching complex systems and the "unknown unknowns". Techne & episteme. Verum ipsum factum. In the words of Archimedes: "Give me a lever and a place to rest it... or I shall kill a hostage every hour." Rants, share-worthy pieces and occasional insights and revelations.

Publish0x

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.