The following code:
#![allow(dead_code)]
fn forgot_to_return() -> usize {
while 4 > 3 {
/* do something */
}
}
fn main() {
let _ = forgot_to_return();
}
Gives the error message:
<anon>:4:5: 6:6 error: mismatched types:
expected `usize`,
found `()`
(expected usize,
found ()) [E0308]
<anon>:4 while 4 > 3 {
<anon>:5 /* do something */
<anon>:6 }
error: aborting due to previous error
Playpen: http://is.gd/0sudKI
This is confusing, since it looks like the type of either 4 or 3 is () - it is not clear that the actual error is that the while loop is being treated as the final expression in the function, and its type does match the return type of the function.
The following code:
Gives the error message:
Playpen: http://is.gd/0sudKI
This is confusing, since it looks like the type of either
4or3is()- it is not clear that the actual error is that the while loop is being treated as the final expression in the function, and its type does match the return type of the function.