Closed
Description
Given the following code (playground):
fn double(n: u32) -> u32 {
n * 2
}
fn triple(n: u32) -> u32 {
n * 3
}
fn f(n: u32) -> u32 {
let g = if n % 2 == 0 {
&double
} else {
&triple
};
g(n)
}
fn main() {
assert_eq!(f(7), 21);
assert_eq!(f(8), 16);
}
The current output is:
error[E0308]: `if` and `else` have incompatible types
--> src/main.rs:13:9
|
10 | let g = if n % 2 == 0 {
| _____________-
11 | | &double
| | ------- expected because of this
12 | | } else {
13 | | &triple
| | ^^^^^^^ expected fn item, found a different fn item
14 | | };
| |_____- `if` and `else` have incompatible types
|
= note: expected reference `&fn(u32) -> u32 {double}`
found reference `&fn(u32) -> u32 {triple}`
I would like this error to explain why these are incompatible despite having the same signature (or link to an explanation).
I would also like it to suggest an alternative, perhaps one of these:
- Change
&double
to&(double as fn(u32) -> u32)
- Change
&double
todouble