This article is more than 1 year old

In Rust We Trust: Stob gets behind the latest language craze

My, what beautiful curly braces you have

Stob Oh hey, Verity, how's it going? What have you been up to? Busy, busy, busy?

This is not a tech podcast. We can lose the opening toe-curl of friendly chat.

Look who's a Grumpy Gerty today. I see from your headline that you are extolling Rust?

Indeed yes, but please do stop poking your finger through the fourth wall, I've only just papered it.

As you wish. But I am puzzled; I thought you had caught Too Much Elm Disease?

Absolutely still a thing, but on the back burner until I can perfect my pronunciation of "Evan Czaplicki".

Uh-huh. So what are the optics on Rust, Vee?

("Optics"? Really? Good grief. I bet you are the sort of person who begins tweets "I was today years old".)

Rust is an up-and-coming, curly brackets systems language that is looking to dis-un-de-throne C++.

A curly brackets language that's going to outdo the Mighty DoublePlus? Well, nobody ever thought of trying that before. That's my sarcasm LED you see blinking, by the way.

No need to be like that. These Rust people have got some very innovative ideas.

Presumably these Rusties are unaware that the C++ committee is shovelling on new features apace, working harder than the Titanic's stoker trying to outrun the iceberg. For example, C++ is about to get <=> the spaceship operator.

Aargh, you got me there. Rust doesn't have that particular in-language emoticon, what with it being a language intended for grown-ups.

Show me the code

Love it when you patronise me. All right, if it's so clever, what would Rust do with an ordinary fragment of code like this?

int i = 0;
i++;

It would politely decline to compile it. For one thing, in Rust, types go behind the declaration...

Yuck. Reminds me of mouldy old Pascal. Real languages start with all types on the left, and then drift to the right in old age. Like people.

...and Rust also passes up on the whole int, long, short, long long long rigmarole, as was fashioned to accommodate machine word sizes and signed number representations of the 1960s. And also the Schrödinger char type that is simultaneously signed and unsigned. Furthermore, the language omits the ++ operator, both fore and aft versions...

No plus-plus? That's just plain disrespectful.

...which leads us to code that looks something like this:

let i: i32 = 0;
i = i + 1;

And that works, does it?

Nope. It still doesn't compile.

Eye-rolling emoji times five! Interrogative but very angry and vulgar three-letter acronym! Multiple shriek-stops!

So then we change the code again to something like this

let mut i = 0;
i = i + 1;

dumping the type, which we don't need (think auto), and that reduces the compilation errors list to a polite warning wondering why we initialise i to 0 instead of 1.

Wait a moment. What's that mut thing?

That allows us to modify i.

Why would I need the compiler's permission to modify i? It's my variable, I'll do with it as I please.

Rust's "variables" default to constants.

Now I understand. Seen this before. This is open persecution of proper, variable variables. It's The Chrysalids, isn't it? WATCH THOU FOR THE MUTANT...

Actually, the term is "mutable".

Mutable, schmootable. Updating a variable is going like smoking did in the 2000's, being shamed out of existence. Any time now, they'll bring on a rule that says you are only allowed to write code that does it if you sit with your laptop, freezing, in a hut 50 yards from the main building.

This is Elm all over again, right? This is yet more functional nonsense?

Rust does adhere to many of the tenets of functional programming.

Don't you "adhere" me. We know all about your fashionable functional languages. Try to call a routine to get a random number in a functional language, the compiler looks at you like you've boasted that you enjoy snacking on your own excrement.

Well, it's true that the compiler messages are much more chatty and helpful than C++'s, but I don't recall any scatological references. And the random number stuff is perfectly conventional.

The USP

Perhaps you'd like Rust better if I tell you about its big USP. What do you suppose this code does?

let s = String::from("Reg Reader");
let s1 = s;
println!("Hello, {}!", s);

Got your measure now. It'll be a question of what that code doesn't, the answer being "compile".

Quite right. I am hot and embarrassed, like a Raspberry Pi 4 with launch firmware, housed in the official case, attempting a stiffish compilation on a bright summer's morn.

But the reason that it doesn't compile is interesting.

I'll be the judge of that.

Rust has no garbage collector, so heap memory is tracked using RAII. The line

let s1 = s;

transfers the ownership of the string from s to s1. It's a move, not a copy. When s is used on the next line, the compiler knows it is already empty, hence the barf.

All sounds a bit fussy.

It is massively fussy. And I haven't told you the half of it.

The language implements a complete borrowing system. This strains to ensure that referenced memory is always valid, memory references can only be written to from one place, inter-thread data races are prevented and all sorts of similar restrictive shenanigans - at compile time.

Must be tough to code. I bet there's wailing and gnashing of teeth on Stack Overflow.

Well, yes and no. If you can work out how to get a compilable description of what you want, you are rewarded with a fast and secure executable.

Judging by your performance here, "compilable" might be a Big Ask. Are there any other features that Rust has that you would like to bore me with?

No, but I've got a list of features that Rust doesn't have:

  • no do/while loop (you are instead invited to break from an endless loop, like an animal)
  • no implementation inheritance (because OOP is sooooh 2003, darling)
  • no exceptions, by which I do mean cybernetically enhanced gotos, and not some meta-reference to the previous items in this list.

No exceptions? Like the sound of that. But what happens when the program gets a runtime error?

It falls flat on its arse, of course.

Or do you mean: how do you cope with errors? In which case the answer is the Result type, which is a bit like C++'s optional, except it stores the error in its other arm, as it were.

Thanks, that was as lucid as an essay on monads. Finally, which IDE should I be using with Rust: Vi or Emacs?

Visual Studio Code, of course, as - thanks to the iron jackboot of geekish fashion - it is now virtually mandatory for all programming languages on all platforms under all circumstances.

Although, to be fair, it is pretty good.

Great. And which rusty programmer font should I install into it?

This one, of course. A snip at 9 bucks a pop. "Program like nobody is watching."

Verity Stob is the pseudonym of a software developer based in London. Since 1988, she has written her "Verity Stob" column for .EXE magazine, Dr. Dobb's Journal and, since 2002, The Register. ®

More about

More about

More about

TIP US OFF

Send us news


Other stories you might like