August 2, 2025

Rust’s .map is cool

I know Rust is still pretty niche, so consider this piece of idiomatic Typescript:

const client = this.clients.get(user_id);
if (client) {
    client.status = ClientStatus.Disconnected;
}

A Rust version:

self.clients.get_mut(&user_id).map(|client| {
    client.status = ClientStatus::Disconnected;
});

Excuse the fact that this might not be idiomatic Rust, but I think it’s profound. In other languages I find my higher level blocks are mucked up” by variables which are only part of a lower level expression. The Rust version scopes the entire behavior set client status to disconnected” to a self-contained expression. Client is encapsulated to it’s need, not leaked to outer scope. I find this type of Rust code excellent for ensuring a clean, high quality DX. I’ve always argued that Rust is good despite memory safety - its ergonomics are some of the best in the game.



Copyright Nathanael Bennett 2025 - All rights reserved