You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
esdevver opened this issue
Apr 24, 2023
· 1 comment
· Fixed by #140197
Assignees
Labels
A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsE-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
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:{ifletSome(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.
The text was updated successfully, but these errors were encountered:
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
added
the
E-easy
Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
label
Apr 9, 2025
A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsE-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
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:
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.
The text was updated successfully, but these errors were encountered: