Why do the following two pieces of code compile:
A)
impl Rectangle {
fn can_hold(&self, other : &Rectangle) -> bool{
if self.width > other.width && self.height > other.height {
return true;
}
return false;
}
}
B)
impl Rectangle {
fn can_hold(&self, other : &Rectangle) -> bool{
if self.width > other.width && self.height > other.height {
true
}else{
false
}
}
}
But not this:
C)
impl Rectangle {
fn can_hold(&self, other : &Rectangle) -> bool{
if self.width > other.width && self.height > other.height {
true
}
return false;
}
}
which fails to compile with the error message
expected `()`, found `bool`
Why does the compiler think there is a () return type?
Actually also I wonder why you cannot do something like this:
D)
impl Rectangle {
fn can_hold(&self, other : &Rectangle) -> bool{
if self.width > other.width && self.height > other.height {
true
}
false
}
}
Because I think this could be perfectly parsed by a compiler and the intention is clear (or not?). Why wouldn't it be clear I would want to return the result of this expression.