Implementing From trait for T where T should implement Into trait

Viewed 51

Consider the following code

struct Complex {
    real: f64,
    img: f64
}

impl From<T: Into<f64>> for Complex {
    fn from(t: T) -> Complex {
        Complex { real: t.into(), img: 0.0_f64 }
    }
}

The complier gives error for the impl. Can someone tell me why, because I feel this should work on paper.

Help!

1 Answers

There is nothing wrong with the types constrains here. You need to declare the generics right after impl:

impl<T: Into<f64>> From<T> for Complex {
    fn from(t: T) -> Complex {
        Complex { real: t.into(), img: 0.0_f64 }
    }
}
Related