I encountered this syntax error today:
fn compiler_error() -> u32 {
if true {
4i32
} else {
5i32
} as u32
}
error: expected expression, found `as`
--> src/lib.rs:7:7
|
7 | } as u32
| ^^ expected expression
|
help: parentheses are required to parse this as an expression
|
3 ~ (if true {
4 | 4i32
5 | } else {
6 | 5i32
7 ~ }) as u32
|
This seems unintuitive (granted, I've only been working in Rust for six months).
- In this case, is requiring parentheses a bug?
- If not, what is the reasoning for the requirement? Variable assignments do not require them:
fn alternative_fix() -> u32 { let foo = if true { 4i32 } else { 5i32 } as u32; foo }