Refined Types in Rust: Parse, Don't Validate
dev.to·2h·
Discuss: DEV
📦Serde
Preview
Report Post

Originally published on Entropic Drift


The String Problem

How many times have you seen function signatures like this?

fn send_email(to: String, subject: String, body: String) -> Result<(), Error>;

fn create_user(email: String, username: String, password: String) -> Result<User, Error>;

fn transfer_money(from_iban: String, to_iban: String, amount: f64) -> Result<(), Error>;

Every String in these signatures is misleading. They don’t accept any string—they accept specific formats. Pass "not-an-email" to send_email and it will fail. Pass "hello" as an IBAN and your money transfer crashes.

The signature promises one thing; the implementation demands another.

Validation Everywhere…

Similar Posts

Loading similar posts...