Closed
Description
I tried this code (playground):
let a = &1;
let b = &2;
let val = if true {
a + 1
} else {
b
};
I expected to see this happen: The compiler suggests turning b
into *b
, since a + 1
is an i32
but b is a &i32
.
Instead, this happened:
error[E0308]: `if` and `else` have incompatible types
--> src/main.rs:9:9
|
6 | let val = if true {
| _______________-
7 | | a + 1
| | ----- expected because of this
8 | | } else {
9 | | b
| | ^ expected integer, found `&{integer}`
10 | | };
| |_____- `if` and `else` have incompatible types
|
help: consider dereferencing the borrow
|
8 | } else *{
9 | b
10 | };
|
If you apply the suggestion, you get another compile error:
error: expected `{`, found `*`
--> src/main.rs:6:12
|
6 | } else *{
| ^ expected `{`
|
help: try placing this code inside a block
|
6 | } else { *{
7 | b
8 | } };
|
Applying that suggestion makes it compile, but is much harder to read than just *b
.
Meta
rustc --version
: 1.50.0-nightly (2020-12-03 5be3f9f)