Type mismatch when returning from inside if statement [duplicate]
Type mismatch when returning from inside if statement [duplicate] This question already has an answer here: The following code fails to compile: fn testing() -> i32 { let x = 5; if x == 5 { 5 } 6 } with this error: error E0308: mismatched types. (expected (), found integral variable) If I put an explicit return in front of the 5 , or if I put the 6 inside an else block, everything works fine. What exactly is Rust complaining about? return 5 6 else This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question. 2 Answers 2 In Rust, nearly everything is an expression, including if - else . if generates a value, but there is no value for the expression if the expression fails (i.e. x != 5 ). When putting the 6 into the else block, the if - else statement returns an integral (which will be an ...