# Generics and conversions

**URL:** <https://users.rust-lang.org/t/generics-and-conversions/140297>\
**Category:** help\
**Created:** [May 27, 2026, 3:16pm UTC](https://users.rust-lang.org/t/generics-and-conversions/140297 "2026-05-27T15:16:59Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![piotrpsz](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/piotrpsz/32/52696_2.png) [@piotrpsz](https://users.rust-lang.org/u/piotrpsz)\
**Post date:** [May 27, 2026, 3:16pm UTC](https://users.rust-lang.org/t/generics-and-conversions/140297/1 "2026-05-27T15:16:59Z")

</div>

Below I will describe the problem. Question is, exists there a way to solve it (or how to do it).

I would like to have the Point type for i32 or f32 coordinates:

```rust
struct Point<T> {
   x: T,
   y: T,
};

```

where T is i32 or f32.

And now i would like to have something like this:

```rust
From <Point<T>> for Point<U> { // Point<i32> -> Point<f32>
   ...
}

```

and

```rust
From <Point<U>> for Point<T> {. // Point<f32> -> Point<i32>
   ...
}

```

Is there a way to solve it (or how to do it)?

piotrpsz

---

<div class="post-metadata">

**Author:** ![Morgane55440](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/morgane55440/32/50376_2.png) [@Morgane55440](https://users.rust-lang.org/u/Morgane55440)\
**Post date:** [May 27, 2026, 3:30pm UTC](https://users.rust-lang.org/t/generics-and-conversions/140297/2 "2026-05-27T15:30:49Z")

</div>

if you know you want only `i32` and `f32`, you should just write them both.  
note that  
`impl<T,U> From <Point<T>> for Point<U>` and `impl<T,U> From <Point<U>> for Point<T>` are the exact same thing.

```rs
impl From <Point<i32>> for Point<f32> {
   // TODO 
}

impl From <Point<f32>> for Point<i32> {
   // TODO 
}

```

---

<div class="post-metadata">

**Author:** ![marlez](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/marlez/32/52182_2.png) [@marlez](https://users.rust-lang.org/u/marlez)\
**Post date:** [May 27, 2026, 3:33pm UTC](https://users.rust-lang.org/t/generics-and-conversions/140297/3 "2026-05-27T15:33:40Z")

</div>

I would also consider restricting the types, if necessary:

```rust
trait Coordinate {}
impl Coordinate for i32 {}
impl Coordinate for f32 {}

struct Point<T: Coordinate> {
    x: T,
    y: T,
}

impl From<Point<f32>> for Point<i32> {
    fn from(value: Point<f32>) -> Self {
        todo!()
    }
}

impl From<Point<i32>> for Point<f32> {
    fn from(value: Point<i32>) -> Self {
        todo!()
    }
}

```

---

<div class="post-metadata">

**Author:** ![piotrpsz](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/piotrpsz/32/52696_2.png) [@piotrpsz](https://users.rust-lang.org/u/piotrpsz)\
**Post date:** [May 27, 2026, 3:39pm UTC](https://users.rust-lang.org/t/generics-and-conversions/140297/4 "2026-05-27T15:39:57Z")

</div>

Bingo. This is exactly what I was looking for.  
Thank you so much. You're 'marlez' greate 🙂

piotrpsz

---

<div class="post-metadata">

**Author:** ![giocri](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/giocri/32/49821_2.png) [@giocri](https://users.rust-lang.org/u/giocri)\
**Post date:** [May 27, 2026, 3:43pm UTC](https://users.rust-lang.org/t/generics-and-conversions/140297/5 "2026-05-27T15:43:14Z")

</div>

either

```rust
trait NumericConversion<T>{
 fn from(surce:T)->Self;
}
impl NumericConversion<i32> for f32{
.....
}
impl NumericConversion<f32> for i32{
.....
}
.... //other conversion you want to support

```

or

```rust
trait CoordType{
 fn from_f32(surce:f32)->Self;
 fn to_f32(self)->f32;
}
impl CoordType for f32{
.....
}
impl CoordType for i32{
.....
}
....//other types you want to support

```

then you use the traits for the conversion of the whole object

---

<div class="post-metadata">

**Author:** ![Morgane55440](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/morgane55440/32/50376_2.png) [@Morgane55440](https://users.rust-lang.org/u/Morgane55440)\
**Post date:** [May 27, 2026, 3:43pm UTC](https://users.rust-lang.org/t/generics-and-conversions/140297/6 "2026-05-27T15:43:23Z")

</div>

it is strongly advised not to put trait bounds on struct definition, as they will infect everything else and lead to a lot of noise.  
bounds belong on `impl`s

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [August 25, 2026, 3:43pm UTC](https://users.rust-lang.org/t/generics-and-conversions/140297/7 "2026-08-25T15:43:50Z")

</div>

Closed on August 25, 2026, 3:43pm UTC

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.
