I would like to write something like the following in rust:
pub struct Point<T> {
pub x: T,
pub y: T,
}
impl<T> Point<T> {
pub fn from<U>(other: Point<U>) -> Point<T> {
Point {
x: other.x as T,
y: other as T,
}
}
}
This is not possible:
error[E0605]: non-primitive cast: `U` as `T`
--> src/lib.rs:9:16
|
9 | x: other.x as T,
| ^^^^^^^^^^^^
|
= note: an `as` expression can only be used to convert between primitive types. Consider using the `From` trait
Looking at this answer I learnt that the From
trait doesn't work for i32
to f32
conversion, which is what I wanted originally.
The simplest solution I can come up with is to simply write a function like:
pub fn float2_from_int2(v: Point<i32>) -> Point<f32> {
Point::<f32>::new(v.x as f32, v.y as f32)
}
Clearly rust has no problem casting from i32
to f32
. Is there a nicer way to write this?