5 Surprising Powers of Rust

Rust can build domain-specific languages via macros, run without an operating system (bare-metal), enforce API correctness through its type system, execute inline assembly, and validate SQL queries at compile time.

Build Your Own Programming Language

Domain-Specific Languages (DSLs) in Rust

A DSL is a small language designed to solve a specific problem, like HTML for webpages or SQL for queries. Rust's macro system lets you embed DSLs directly into your code—for example, creating a custom map macro that uses object syntax and converts it to valid Rust at compile time.

Run Without an Operating System

Bare-Metal Programming

Rust can compile binaries that run directly on hardware without an operating system layer. This capability makes Rust ideal for firmware, embedded devices, and even building custom operating systems like Redox OS, since the program can make direct hardware calls instead of relying on system calls.

Enforce Correct API Usage

Type State Pattern for Impossible-to-Misuse APIs

Rust's type system can encode rules so APIs cannot be misused. The type state pattern creates objects with different states, each with specific available methods. Transitions between states consume the old state and return the new one, preventing accidental reuse of outdated states.

Why Rust Excels at Type State Pattern

Rust has no default constructors, so you explicitly choose the starting state. Its ownership system ensures that when a method transitions states by consuming self, the old state is permanently invalidated, preventing misuse that other languages allow.

Write Assembly Inline

Direct CPU Access via Assembly Macro

Rust is one of the few modern languages allowing direct CPU communication through the assembly macro. You can write assembly instructions directly in Rust code for operations that can't be executed through Rust or C, making it essential for bootloaders, kernels, and bare-metal embedded firmware.

Validate SQL at Compile Time

SQLx: Compile-Time SQL Query Verification

The SQLx library uses Rust's macro system to optionally check SQL queries at compile time before the program runs. This catches mistakes like misspelled table names early, and can be disabled in CI/CD or local development when a live database is unavailable.

Notable quotes

Rust can encode rules into the type system to enforce that APIs are used correctly. — Let's Get Rusty
Rust is one of the few modern languages that allows you to talk to the CPU directly. — Let's Get Rusty
SQLx will optionally check your SQL queries at compile time to validate that they're correct. — Let's Get Rusty
Let's Get Rusty
5 min video
3 min read
5 Surprising Powers of Rust
You just saved 2 min.
The big takeaway
Rust can build domain-specific languages via macros, run without an operating system (bare-metal), enforce API correctness through its type system, execute inline assembly, and validate SQL queries at compile time.
Build Your Own Programming Language
Domain-Specific Languages (DSLs) in Rust
A DSL is a small language designed to solve a specific problem, like HTML for webpages or SQL for queries. Rust's macro system lets you embed DSLs directly into your code—for example, creating a custom map macro that uses object syntax and converts it to valid Rust at compile time.
Run Without an Operating System
Bare-Metal Programming
Rust can compile binaries that run directly on hardware without an operating system layer. This capability makes Rust ideal for firmware, embedded devices, and even building custom operating systems like Redox OS, since the program can make direct hardware calls instead of relying on system calls.
1
Traditional: Program → Operating System → Hardware
2
Bare-Metal Rust: Program → Hardware (direct)
Rust eliminates the OS layer for direct hardware access
Enforce Correct API Usage
Type State Pattern for Impossible-to-Misuse APIs
Rust's type system can encode rules so APIs cannot be misused. The type state pattern creates objects with different states, each with specific available methods. Transitions between states consume the old state and return the new one, preventing accidental reuse of outdated states.
1
Object in State A (specific methods available)
2
Call transition method (consumes State A)
3
Object now in State B (different methods available)
4
State A cannot be used again
Type state pattern enforces valid state transitions
Why Rust Excels at Type State Pattern
Rust has no default constructors, so you explicitly choose the starting state. Its ownership system ensures that when a method transitions states by consuming self, the old state is permanently invalidated, preventing misuse that other languages allow.
Write Assembly Inline
Direct CPU Access via Assembly Macro
Rust is one of the few modern languages allowing direct CPU communication through the assembly macro. You can write assembly instructions directly in Rust code for operations that can't be executed through Rust or C, making it essential for bootloaders, kernels, and bare-metal embedded firmware.
Validate SQL at Compile Time
SQLx: Compile-Time SQL Query Verification
The SQLx library uses Rust's macro system to optionally check SQL queries at compile time before the program runs. This catches mistakes like misspelled table names early, and can be disabled in CI/CD or local development when a live database is unavailable.
Without SQLx
SQL errors discovered at runtime
With SQLx
SQL errors caught at compile time
SQLx shifts SQL validation from runtime to compile time
Worth quoting
"Rust can encode rules into the type system to enforce that APIs are used correctly."
— Let's Get Rusty, at [1:33]
"Rust is one of the few modern languages that allows you to talk to the CPU directly."
— Let's Get Rusty, at [2:58]
"SQLx will optionally check your SQL queries at compile time to validate that they're correct."
— Let's Get Rusty, at [3:27]
Made with Glimpse by Wozart
glimpse.wozart.com/v/xjhrxe6d
Share this infographic

More like this