From 00a52e3d631df0a7610f4f17a9bd6d60205239fd Mon Sep 17 00:00:00 2001 From: Jasper Ras Date: Mon, 4 Aug 2025 08:46:49 +0200 Subject: vault backup: 2025-08-04 08:46:49 --- Loop labels.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Loop labels.md (limited to 'Loop labels.md') diff --git a/Loop labels.md b/Loop labels.md new file mode 100644 index 0000000..2110c71 --- /dev/null +++ b/Loop labels.md @@ -0,0 +1,27 @@ +[[Rust]] +[[Returning values from loops]] + +In nested loops we can use labels to determine which one to break from: +```rust +fn main() { + let mut count = 0; + 'counting_up: loop { + println!("count = {count}"); + let mut remaining = 10; + + loop { + println!("remaining = {remaining}"); + if remaining == 9 { + break; + } + if count == 2 { + break 'counting_up; + } + remaining -= 1; + } + + count += 1; + } + println!("End count = {count}"); +} +``` \ No newline at end of file -- cgit v1.2.3