Why Rust Pulls Methods Out of the Struct
How do you add methods to a type? Sounds simple until you actually think about it.
Structs in Rust come in three shapes — named-field, tuple-like, unit-like. If methods lived inside the struct definition, you'd need a different syntax for each one. Messy.
Rust sidesteps this entirely. Methods don't live in the struct — they live in a separate impl block. One syntax, no matter which kind of struct you're working with.
struct Point(i32, i32); // tuple-like
impl Point {
fn distance_from_origin(&self) -> f64 {
((self.0.pow(2) + self.1.pow(2)) as f64).sqrt()
}
}That alone is neat. But here's the part that got me: this isn't a struct-only trick. The exact same impl syntax works for enums. It works for primitive types like i32. Any type can have methods.
That's when it clicked for me — this is probably why Rust barely uses the word "object." It prefers "value." An object usually implies behavior baked into the thing itself. A value is just... data. Methods are something you attach separately, through impl, regardless of what the value is.
It's a small design choice, but it says a lot about how Rust thinks: types are shapes for data first, and behavior is layered on top — uniformly, no exceptions for structs vs enums vs i32.
Once you see it, you can't unsee it.