Code
trait Value {}
struct First {}
impl Value for First {}
struct Second {}
impl Value for Second {}
fn get_value(first: bool) -> impl Value {
if first {
return First {};
}
Second {}
}
Current output
Compiling playground v0.0.1 (/playground)
error[E0308]: mismatched types
--> src/lib.rs:13:5
|
9 | fn get_value(first: bool) -> impl Value {
| ---------- expected `First` because of return type
...
13 | Second {}
| ^^^^^^^^^ expected `First`, found `Second`
For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` (lib) due to 1 previous error
Desired output
Compiling playground v0.0.1 (/playground)
error[E0308]: mismatched types
--> src/lib.rs:13:5
|
9 | fn get_value(first: bool) -> impl Value {
| ---------- expected a single type implementing `Value` because of return type
...
|
11 | return First {};
| ^^^^^ return type resolved to be `First`
...
13 | Second {}
| ^^^^^^^^^ expected `First`, found `Second`
For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` (lib) due to 1 previous error
Rationale and extra context
Even though I had read the entire reference, I forgot about the rule that
Each possible return value from the function must resolve to the same concrete type.
And the existing message suggests that the return type in the signature is what breaks the Second {} return, but it isn't - instead, it is the fact that First {} is returned above
Other cases
Rust Version
Anything else?
No response
Code
Current output
Desired output
Rationale and extra context
Even though I had read the entire reference, I forgot about the rule that
And the existing message suggests that the return type in the signature is what breaks the
Second {}return, but it isn't - instead, it is the fact thatFirst {}is returned aboveOther cases
Rust Version
Anything else?
No response