Are "break" and "continue" statements or expressions?

Rust By Example says

The break statement...

Rust std says

A break expression

Rust Reference says

A break expression

1 Like

The difference in Rust isn't as significant as in other languages, because it allows many statement-like constructs in expression position, but their return type is useless, either () (unit = nothing) or in case of break/return/continue it's ! (never).

7 Likes

Maybe break and continue are the Expression statements?

There is this thing you can do:

fn main() {
    let mut hunger = 0;
    let hungry = 'reading: loop {
        hunger += 1;
        
        if hunger < 2 {
            println!("read!");
            continue 'reading;
        }

        let fast = "eat!";
        break fast;
    };
    
    println!("{hungry}");
    // prints
    // "read!"
    // "eat!"
}
2 Likes

Rust By Example is a teaching document, so it says that break is a statement even though it's actually an expression, because almost any use of break as an expression instead of a statement is highly suspect code.

3 Likes

break is an expression; break; is a statement.

5 Likes

I'm sure the difference will be very useful in the international obfuscated Rust code competition:

'dance: while break 'dance {}
16 Likes

now I can't tell if you're expressing yourself, or making a statement :drum:

or both :grimacing:

/expression

1 Like

I'll throw in an obligatory mention of

break rust;

Rust Playground

Edit: Ah, see, it also works as an expression (i. e. without the semicolon)!

6 Likes

Maybe break and continue are the Expression statements?

You seem pretty intent on nailing down exactly what it is. Going by the reference, break and break 'label and break expr and break 'label expr are... BreakExpressions. Which is one of many ExpressionWithoutBlock, which can appear in an ExpressionStatement, before the ;. So it can be part of an expression statement. But it can also appear as a subexpression many other places, like inside of a BlockExpression. So break isn't always (part of) an expression statement, and there are expressions statements that don't contain break (or continue). Based on the reference, the exact thing that describes break is, again, a BreakExpression. Simple enough.

With that out of that way... what is the actual goal of nailing it down so exactly? The grammar presented in the reference is a best-effort, and isn't normative or complete, and I'm struggling to see why it's so important to you to find some definitive name to attach to the code snippet break.

2 Likes

I'm currently writing a Rust course and I want to give an exactly definition for "break" and "continue". Which definition would be closer to the truth? "break" and "continue" are expressions or statements or expression statements?

1 Like

I'm leaning more towards the expression statements version

Expressions, if there is no semicolon.

4 Likes

An expression statement is just "the thing that happens when you use any expression as a statement". break; (with semicolon) is an expression statement because break (without semicolon) is an expression. Therefore, the important part is that break is an expression.

9 Likes

Remember that you can do things like foo(break);. It's not useful, but it's allowed because break is undoubtedly an expression.

That said, this is a nuance that I would say is usually not appropriate to go into in detail in a course. It's an expression because that way it works in match arms without braces, but as a mental model one should generally think of it more like a statement because that's how you use it: for its side effect, not for its value.

9 Likes

Thank you all for the comprehensive answers. I think, now no one will have any questions

1 Like

It would be nice to summarize this into something nice and simple.

Would it be correct to say that everything is an expression? And a statement is an expression whose value is not used?

You are experiencing ontological confusion. Expressions and statements are not mutually exclusive! An expression can be a statement if it's syntactically in statement position; that doesn't make it any less of an expression, intrinsically.

You are doing your students a large disservice if you try to present them being exclusive or contradictory.

The concepts of expressions and statements are neither perfectly independent nor perfectly (anti-)correlated. The reason the line is blurry is that it doesn't matter all that much. "Statement" is largely a syntactic category; it basically exists so that blocks can contain multiple, sequential expressions and declarations.

Most of the useful things that you'll be able
to do with the language (ny which I mean among the features that produce actual, executable code, in contrast with the use of the type system) revolve around expressions.

3 Likes

I don't try to present them being exclusive or contradictory. I need explain to them. Are "break" and "continue" expressions or statements? Next, I say that expressions can be in the position of statements.If I said that the break statement is used to interrupt loops, I would be more wrong than right.