What it feels like to program in Rust sometimes

3 minute read Published: 2025-07-18

A wholesome datæ.


The young man nervously rang the door-bell. A gruff man opened the door; after a few seconds of sizing him up, he nodded towards the inside, inviting him in.

They sat in the living-room. After a few tense seconds, the silence broke.

— Son… what are your intentions with my [u8]?
— Uh… I'm just borrowing her, sir.
— Her curfew is 'a, just so you know.
— I'll have her back by then. I promise.
— You better. Now, son… tell me something. Are you two, you know… exclusive?
— Yes, sir.
— Then you better put a &mut on it. That way the others know to stay away.
— Will do, sir.

While they had been talking, a woman walked into the room. He recognised her as his datæ's mother. Her stare, somehow, seemed even more penetrating than her husband's.

— Well, sonny, let me just make sure you're presentable, alright?
— Presentable, ma'am?
— Yes, presentable. You'll never be her type if you can't manage that much. So: do you implement Debug?
The young man looked at his clothes. “I… don't see why not?”
A scoff came as a reply.
— Humph! What a derivative response. Alright then, off you go.

The young man gulped. The first hurdle had been cleared; now, to ensure the datæ itself went well.

Explanation

OK, the above text was meant to be humorous. It was also, however, meant to be an accurate representation of a programmer's negotiation with the borrow and type checkers, and portray them as benevolent, if a bit overprotective.

After all, suppose we go on a teen-age date with our significant other—a girl, for the purposes of this article. Let's also suppose we're borrowing her from her parents, and therefore she has a curfew we have to respect. Thus, our date cannot under any circumstances go beyond our date's curfew; if it does, she'll leave us alone while we still believe the date to be ongoing, embarrassing us greatly.

This is the biggest realisation that underpins the concept of the borrow checker: When you borrow something, the dead-line for returning it underpins absolutely everything you might want to do with it. Thus, our date has the curfew as a parametre: If it's early then we're pressed for time, else we don't really need to worry. This is, more or less, a real-life analogue to Rust's borrow checking.

Let's go over the above dialogue line-by-line and see how it corresponds to Rust syntax:

First of all, we need to declare what data we need:

struct Datæ     {
    data:         [u8],
}

On with the dialogue:

— Son… what are your intentions with my [u8]?
— Uh… I'm just borrowing her, sir.

struct Datæ     {
    data: &       [u8],
}

The & sign signifies that we don't own this piece of data.

— Her curfew is 'a, just so you know.

struct Datæ     {
    data: &'a     [u8],
}

The data will only live until 'a

— I'll have her back by then. I promise.

struct Datæ<'a> {
    data: &'a     [u8],
}

…so our data-type must by necessity also only live until 'a.

'a underpins our entire struct. Thus, 'a is a parametre of our data-type, so any possible Datæ we want to declare will be different depending on that the 'a is for its specific case.

— You better. Now, son… tell me something. Are you two, you know… exclusive?
— Yes, sir.
— Then you better put a &mut on it. That way the others know to stay away.
— Will do, sir.

struct Datæ<'a> {
    borrowed: &'a mut [u8],
}

— Do you implement Debug?
— I… don't see why not?
— Humph! What a derivative response. Alright then, off you go.

#[derive(Debug)]
struct Datæ<'a> {
    borrowed: &'a mut [u8],
}