re: Embrace the static typing system: Strong types VIEW POST


This is a really good article, nicely explained!

One tip you might not be aware of: you could potentially take your From/Into implementation one step further by making the Circle constructor generic over any type that can be converted into a Radius:

impl Circle {
    fn new<T: Into<Radius>>(radius: T) -> Circle {
        let radius = radius.into();
        Circle {

fn main() {
    let a = Circle::new(Radius::new(2.0));
    let b = Circle::new(Diameter::new(4.0));

This gives you the same nice interface you'd get from function overloading, but without, as you mentioned, having to go back and write a million new functions when you create a new type :) It does come at the expense of making it a little less obvious a conversion is happening, though.

code of conduct - report abuse