Skip to content

Incorrect / incomplete information about where you can break with a value #110758

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
esdevver opened this issue Apr 24, 2023 · 1 comment · Fixed by #140197
Closed

Incorrect / incomplete information about where you can break with a value #110758

esdevver opened this issue Apr 24, 2023 · 1 comment · Fixed by #140197
Assignees
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.

Comments

@esdevver
Copy link

esdevver commented Apr 24, 2023

Location

https://doc.rust-lang.org/std/keyword.break.html

Summary

The documentation states that "When associated with loop, a break expression may be used to return a value from that loop. This is only valid with loop and not with any other type of loop. If no value is specified, break; returns (). Every break within a loop must return the same type."

"This is only valid with loop" is not true, as one can break with a value from within a non-loop block expression.

Example:

let final_value = 'outer: {
    let value: Option<Value> = get_value();
    'inner: {
        if let Some(value) = value { break 'outer value; }
    }
    // do stuff here maybe
};

Whilst I admit the example I provide seems a little contrived, I use this practice myself.

For context, the technical reason why you can't break from say a while or for loop is because those loops are expected to yield the () type.

I believe that this part of the documentation should be amended/elaborated on as I find this pattern useful and practical for writing readable code.

@esdevver esdevver added the A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools label Apr 24, 2023
@lolbinarycat lolbinarycat added the E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. label Apr 9, 2025
@ktnlvr
Copy link
Contributor

ktnlvr commented Apr 21, 2025

@rustbot claim

VlaDexa added a commit to VlaDexa/rust that referenced this issue May 2, 2025
Document breaking out of a named code block

Closes rust-lang#110758.
@bors bors closed this as completed in 30e556e May 2, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue May 2, 2025
Rollup merge of rust-lang#140197 - ktnlvr:master, r=workingjubilee

Document breaking out of a named code block

Closes rust-lang#110758.
github-actions bot pushed a commit to model-checking/verify-rust-std that referenced this issue May 9, 2025
Document breaking out of a named code block

Closes rust-lang#110758.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants