Steve Klabnik

“The most violent element in society is ignorance.” - Emma Goldman

Read this first

You can’t “turn off the borrow checker” in Rust

Every once in a while, someone will talk about unsafe in Rust, and how it “turns off the borrow checker.” I think this framing leads to misconceptions about unsafe and how it interacts with safe code.

Here’s some code that causes a borrow checker error:

fn main() {
    let mut x = 5;

    let y = &x;
    let z = &mut x;

    println!("{}", y);

And the error:

error[E0502]: cannot borrow `x` as mutable because it is also borrowed as immutable
 --> src/
4 |     let y = &x;
  |              - immutable borrow occurs here
5 |     let z = &mut x;
  |                  ^ mutable borrow occurs here
6 | }
  | - immutable borrow ends here

Rust won’t let us have a &T and a &mut T to the same T at the same time.

If unsafe “turned off the borrow checker, we’d expect this code to work:

fn main() {
    let mut x = 5;

    unsafe {
        let y = &x;
        let z = &mut x;

Continue reading →

Is WebAssembly the return of Java Applets & Flash?

In my last post on WebAssembly, I made the following claim:

Some have compared WebAssembly to Java applets; in some ways, they’re very right, but in some ways, they’re very wrong. Eventually I’ll write a post about the wrong, but for now, the right: in some sense, WebAssembly is a different way of accomplishing what the JVM set out to do: it’s a common virtual machine that can be used to build very cross-platform software.

A lot of people expressed interest in me elaborating, so let’s get to it! For this post, I’m going to make three comparisons: to Flash, to Java Applets, and occasionally to PNaCL. Secondly, this post is going to focus on the web use-case for WebAssembly, even though the previous post was about non-web uses. We’ll make that comparison in the next post. Finally, this post is kind of like eating tapas, there’s a bunch of little sections. I feel like it’s a bit short...

Continue reading →

WebAssembly is more than the web

While WebAssembly was given its name for good reason, I also think it’s quite the misnomer. When you hear about WebAssembly, don’t just think of the web: think bigger.

Many programmers know that WebAssembly is a technology that lets you run non-JavaScript code in the browser. That is true, but there’s a lot more to it. You see, the spec is written in a very interesting way: there’s the core, what WebAssembly actually is. Here’s the opening section:

To support the embedding of WebAssembly into different environments, its specification is split into layers that are specified in separate documents.

  • Core specification - Defines the semantics of WebAssembly modules and its instruction set, independent from a concrete embedding.

  • API specifications - Define application programming interfaces enabling the use of WebAssembly modules in concrete embedding environments.

It goes on to say:


Continue reading →

“The Expressive C++17 Coding Challenge (in Rust)” Revisited

In October of last year, I wrote a post, “The Expressive C++17 Coding Challenge (in Rust)”. For various reasons, it got brought up again in the D world, and seb has written a new post. It’s good, you should check it out!

However, it links to my gist, not my blog post. As I said back then:

I held myself to the same constraints as the original contest; no external packages is a bit painful in Rust, but it’s not too bad. Mostly it would let me eliminate boilerplate while also improving correctness, and making the code a bit shorter.

So, that got me thinking: What would this look like if I could use external packages? I took about an hour, and knocked it out. I have two versions to show you today, one where I pay no attention to allocations, and one where it’s zero-allocation.

First, the “whatever just allocate” version:

extern crate csv;
extern crate serde_derive;

Continue reading →


As someone who has significantly different beliefs than Peter Thiel, today is likely my second-to-last power of two years alive.

Two years ago, I wrote a post about turning 30, and I pretty much feel the same way. Never enough time, tick tock, tick tock. I have today off (thanks Mozilla!), but am still considering working anyway, because I really care about what I’m working on, and am excited about it. We’ll see.

I feel like I have more to say here, but also don’t. So maybe let’s leave it at that.

View →

Looking back at Rust in 2018

A careful reader will note the date of this post; it’s fundamentally in response to this call for blog posts, with a rhetorical device of looking backwards as if my personal plans for Rust came true. If you’re reading this in 2019 or beyond, this may be a work of fiction.

It’s been an exciting year for Rust. Well, it depends on what you mean by “exciting”, exactly. In a word, this year has been about polish. I find this kind of thing exciting! It’s a very different kind of exciting than the “new and shiny” exciting. It’s “take what we have and make it shiny.”

Let’s recap the major achievements of the year.

 Rust 2018

First of all, we shipped our first epoch! If you type cargo new today, you’ll see a new shiny entry in your Cargo.toml:

name = "new-program"
version = "0.1.0"
authors = ["steveklabnik <>"]

epoch = "2018"


What does this...

Continue reading →

Five years with Rust

Today is five years since I first heard of Rust. I wrote a post last year about it. This year, I have something slightly different in mind.

Five years is a long time in tech. It’s also a long time for me to be focused on one specific thing; other than the pizza job I held in high school and college, I’ve never stayed in a single job for five years. (This is true today as well; it’s not like I got a job with Rust the first day I learned of its existence.) I started with Ruby and Rails in 2008/2009, and my final commits to Rails were in August/September of 2013. So at this point, my career has basically been split in half, rather than dominated by Ruby.

Five years ago tomorrow, I registered

Yesterday, I had lunch with someone I’m very honored to call a friend. We talked about a lot of things, but also about Rust’s past, present, and future. One thing...

Continue reading →

User-Agent: moz://a

Every six months, Mozilla gets all of its employees from around the world together in one place, for a week. This morning was the plenary session, with Mozilla leadership talking about our successes this year, as well as plans for the future. I noticed a theme that, while not super overt, I find very interesting, and bodes well for Mozilla’s future.

The words “user agent” kept coming up in various aspects of the plenary. The User-Agent header has a… shall we say storied history. When most people hear the term “user agent”, they mostly think of the “user agent string”. This is passed through the User-Agent header to identify which browser you’re using.

Well, that’s how most people think of it, but that’s not the whole story. If it is supposed to identify which browser you’re using, why is it not the Browser header? What is this “user agent” term anyway?

The concept is a bit more broad...

Continue reading →

The Expressive C++17 Coding Challenge (in Rust)

A few weeks ago, two C++ bloggers initiated a challenge (here and here): use the shiny new features of C++17 to write a small program that manipulates CSV files. The link is to the results; you can see the examples people came up with.

I’ve written a follow-up post too!

I decided to implement it in Rust for fun. You should give it a try, in whatever language you use, it’s a nice sample problem. I held myself to the same constraints as the original contest; no external packages is a bit painful in Rust, but it’s not too bad. Mostly it would let me eliminate boilerplate while also improving correctness, and making the code a bit shorter.

Anyway, here it is:

I implemented From for everything so I could use ?, but then main just has a ton of unwraps. I normally would remove those too, but the challenge does not...

Continue reading →

Want to join the Rust docs team?

One of Rust’s major goals this year is to provide mentoring at all levels of the project. This is a goal I’m very on-board with. There’s lots of ways to get involved with Rust, but I’d like to talk about the bit I’m most involved in: documentation.

There’s many ways to contribute to Rust’s docs. Some people contribute occasional PRs, some people end up contributing a lot. No matter what your interest, I’d like to help. But before I get into those details, why should someone get involved with documentation? I have my own reasons, but I also have a pitch for you:

Working on documentation is a fantastic way to learn something in a deeper way.

In order to document something, you have to understand it, even if only a little bit! It’s a good excuse to dig into the details. And I’ve found that many people are eager to help explain something to you in return for you writing that down and...

Continue reading →