Numeric default fallback VS exclusive trait bound

⚓ Rust    📅 2026-06-26    👤 surdeus    👁️ 2      

surdeus

In the following example I would expect that f32 can be inferred?

trait Scalar { type Out; }
impl Scalar for f32 { type Out = f32; }
// commenting this impl compiles
impl Scalar for f64 { type Out = f64; }

fn op<T: Scalar<Out = f32>>(_: T) {}

fn main() {
    // only `f32` satisfies op's `Out = f32`, `T` should be uniquely inferrable
    op(0.2);
}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
error[E0271]: type mismatch resolving `<f64 as Scalar>::Out == f32`
  --> src/main.rs:10:8
   |
10 |     op(0.2);
   |     -- ^^^ type mismatch resolving `<f64 as Scalar>::Out == f32`
   |     |
   |     required by a bound introduced by this call
   |
note: expected this to be `f32`
  --> src/main.rs:4:34
   |
 4 | impl Scalar for f64 { type Out = f64; }
   |                                  ^^^
note: required by a bound in `op`
  --> src/main.rs:6:17
   |
 6 | fn op<T: Scalar<Out = f32>>(_: T) {}
   |                 ^^^^^^^^^ required by this bound in `op`

For more information about this error, try `rustc --explain E0271`.
error: could not compile `playground` (bin "playground") due to 1 previous error

Can someone help me understand why the code gets rejected or is this a bug?

3 posts - 3 participants

Read full topic

🏷️ Rust_feed