0

Consider the following:

struct Foo(f32); impl<T> std::convert::From<T> for Foo where f32: std::convert::From<T> { fn from(value : T) -> Self { Self(value.into()) } } fn main() { let a: Foo = 42.into(); } 

This of course doesn't work because

the trait bound f32: From<i32> is not satisfied

So I'm trying to add another trait implementation with less constraints:

impl std::convert::From<i32> for Foo { fn from(value : i32) -> Self { Self(value as f32) } } 

Which then gives me

conflicting implementations of trait From<i32> for type Foo

What's the issue here, shouldn't the generic instantiation be discard for i32 because the type requirement failed, thus allowing another explicit implementation for From<i32>?

2
  • Currently From<i32> for f32 is not implemented, but std may implement it at any time. Commented Aug 29, 2023 at 8:34
  • 3
    Also, always look at the whole error from cargo check: note: upstream crates may add a new impl of trait `std::convert::From<i32>` for type `f32` in future versions. Commented Aug 29, 2023 at 8:36

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.