fn main(){
loop {
println!("rust is fun, but i wanna run");
}; // is this loop going to be statement?
}
I try some experiment. loop is an expression so the end of the loop brackets is not use semicolon ";". I put semicolon on it . Yay the code run smoothly, no error was found. But when i create this code:
fn main() {
let y: i32 = harga_martabak(1500);
println!("Harga martabak adalah: {y}");
}
fn harga_martabak( x: i32) -> i32 {
x + 9000; //put the semicolon here
}
My code is error because it is turn to statement and cant return the value. So why loop with/without ";" is not error?
First, you have written an infinite loop. This is important because infinite loops have a special result type: !. I tend to read this as "diverges". This is the type used for expressions which do not produce a result. For example:
let x: i32 = panic!("whoopsie!");
This works not because panic! returns an i32, but because panic!diverges. Once the panic happens, nothing after that matters. As a result, expressions of type ! can be coerced into any other type just fine.
That's why you can remove the semicolon after loop: it results in ! (because the loop never ends and thus never resolves to a value), which can be implicitly coerced to () which is the type that main expects the final expression to be.
With the semicolon is also valid because it's an infinite loop. It doesn't matter what happens after that.
The other reason code like this is valid is that every block without an explicit result expression automatically returns ().