Small Bytes: Rust (Premium)

Even non-programmers have likely heard of Rust, a programming language that’s taken the developer world by storm. If not, consider the most recent Stack Overflow Developer Survey, which notes that Rust is the most loved programming language among its respondents for the 7th year in a row: “87 percent of developers say they want to continue using it,” the survey explains. “Rust also ties with Python as the most wanted technology, with TypeScript running a close second.”

Or consider that Linus Torvalds and the other maintainers of the Linux kernel have decided to make Rust the first programming language not named C to be used in their work. The Linux 6.1 kernel, released just last month, is the first to incorporate Rust code. Granted, it’s early days, and the Rust compiler used on Linux apparently needs some work, but the conservative maintainers of the kernel apparently think so much of Rust that they are moving forward anyway. The goal is a world in which all new Linux kernel code is written in Rust.

So what’s the attraction?

It all comes down to code safety. While C and Rust are syntactically similar—Rust is one of far too many C-like languages, as I think of them, or “curly braces languages,” as others might say—they couldn’t be further apart when it comes to writing safe code. Safe code is code that doesn’t—cannot—improperly handle memory. A situation that leads to approximately two-thirds of all security vulnerabilities.

Put another way, C is a powerful language that puts the onus of writing safe code on the developer, while Rust is a powerful language whose compiler and coding tools ensure that code written by the developer is safe. And it does so without the overhead that usually accompanies this type of advance. For example, C# code is described as “managed code” because it runs within the .NET runtime, which runs on top of whatever OS the user has. But the Rust compiler, like that of C, generates native code that runs directly on the OS. So there’s no performance tradeoff for the safety features it provides.

For most applications, the performance tradeoff of garbage collection and the other advantages of .NET and other runtimes is not noticeable, especially in this age of powerful computers and devices. But those turning to Rust have other, more low-level concerns where this tradeoff is unacceptable. They’re writing drivers, perhaps. Or OS kernels, as with Linux. And last week, we learned that Microsoft is adding Rust support to its Azure Sphere Internet of Things (IoT) platform.

Rust is cross-platform and works across Windows, Mac, and Linux. It’s extensible via third-party libraries, which are called crates, many of which are exactly what you’d expect: native API wrappers for common libraries and frameworks. Claiming that Rust is having a moment is understating the appeal: the C code that runs the planet will never disappear, but after many false starts at replacing C—the most obvious misstep being C++—we may have finally found its successor. And from what I can tell on the outside, this one has legs.

Getting started with Rust is easy enough and even a basic “Hello, world!” program can demonstrate how this language can protect developers from mistakes. Using the instructions on the Rust website, I installed Rust and ensured it was all working properly with the cargo command-line executable as recommended

cargo –version

And with that out of the way, it was time to create the simplest Rust app imaginable. Again using the instructions, I created a new Rust project from the command line.

cargo new Rust1

And then I opened the folder with Visual Studio Code and examined the Rust file it created, called main.rs. Amusingly, this file is literally “Hello, world!”.

Even this basic code block shows how similar Rust code looks to C code: there is a main function, there are curly braces, and there are semicolons. So nothing unusual, at least so far.

Then I used the terminal in Visual Studio Code to run the Rust compiler and generate an exe (which is found in target\debug\ within the project folder). It works fine, though it throws a warning tied to my use of a capital letter in the project name.

Other than the warning, no surprises. But let’s scratch the surface of what makes Rust special. We’ll do that by creating a variable. You can do this explicitly, by specifying a data type. Or you can do so implicitly, where the data type is inferred.

let greeting = “Hello, world!”;
let greeting2:&str = “Hello, world!”;

Rust variables are also immutable by default, meaning that they cannot be changed. And so the following code would generate an error at compile time:

let greeting = “Hello, world!”;
let greeting = “Some other text”;

Rust describes this design decision as “one of many nudges Rust gives you to write your code in a way that takes advantage of the safety and easy concurrency that Rust offers.” But you can, of course, create a mutable variable (what many might call an actual variable). You do so using the mut keyword.

let mut greeting = “Hello, world!”;
greeting = “Some other text”;

And … this compiles and runs.

And while this should be obvious, JavaScript, the most popular and widely-used language in the world, is dynamically typed and would allow code like this:

var greeting = “Hello, world!”;
greeting = 5;

Suffice it to say, the Rust equivalent will not fly: you can make a variable mutable, but you cannot change its type. Rust is no JavaScript.

Gain unlimited access to Premium articles.

With technology shaping our everyday lives, how could we not dig deeper?

Thurrott Premium delivers an honest and thorough perspective about the technologies we use and rely on everyday. Discover deeper content as a Premium member.

Tagged with

Share post

Thurrott