This example:
struct Struct<T> {
data: T,
}
impl Struct<u8> {
fn new(data: u8) -> Struct<u8> {
Self { data }
}
}
impl Struct<f64> {
fn new(data: f64) -> Struct<f64> {
Self { data }
}
}
fn main() {
let s: Struct<u8> = Struct::new(0_u8);
let s: Struct<f64> = Struct::new(0_f64);
}
Throws this compile error:
error[E0034]: multiple applicable items in scope
--> src/main.rs:18:33
|
18 | let s: Struct<u8> = Struct::new(0_u8);
| ^^^ multiple `new` found
|
note: candidate #1 is defined in an impl for the type `Struct<u8>`
--> src/main.rs:6:5
|
6 | fn new(data: u8) -> Struct<u8> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: candidate #2 is defined in an impl for the type `Struct<f64>`
--> src/main.rs:12:5
|
12 | fn new(data: f64) -> Struct<f64> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error[E0034]: multiple applicable items in scope
--> src/main.rs:19:34
|
19 | let s: Struct<f64> = Struct::new(0_f64);
| ^^^ multiple `new` found
|
note: candidate #1 is defined in an impl for the type `Struct<u8>`
--> src/main.rs:6:5
|
6 | fn new(data: u8) -> Struct<u8> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: candidate #2 is defined in an impl for the type `Struct<f64>`
--> src/main.rs:12:5
|
12 | fn new(data: f64) -> Struct<f64> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Which doesn't make sense to me because the argument type and the return type are explicitly annotated and in both cases it's clear only one of the two candidate functions satisfy the type requirements so why can't the Rust compiler figure it out?