rustbites rustbites bite-sized rust tips
Archives About
#019 Aug 18, 2022
option iterator

19. breaking from labeled blocks

As of 1.65, it is possible to label plain block expression and terminate that block early.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
let result = 'block: {
    let result = 20i32;
    if result < 10 {
        break 'block 1;
    }
    if result > 10 {
        break 'block 2;
    }
    3
};
assert_eq!(result, 2);

This post is licensed under CC BY 4.0 by the author.

Share:
← Previous 18. flatten options Next → 20. let-else statements

Trending Tags

Rust Std Iterators Collections Iterator Performance Option Strings Concurrency Integers Slices Vec Parsing Result Error-Handling Rust-1.95 Math Path Slice Sync Arrays Fs Overflow Ownership Sorting Allocation Atomics Functional Interior-Mutability Mem Stdlib Channels Hashmap Pattern-Matching Smart-Pointers Arc Bitwise Cow Ffi Lazylock Rust-1.93 Vecdeque Async Attributes Borrow-Checker Btreemap Cell Closures Debugging Entry Ergonomics Filesystem Generics Idioms Io Iter Pitfall Rust-1.94 Search Algorithms Api-Design Arithmetic Bit-Manipulation Char Compile-Time Default Enum Float Future Hints Impl-Trait Lazy Lazycell Macro Macros Match Maybeuninit Mutex Nonzero Numerics Oncelock Patterns Peekable Pin Ranges Rc Rust-1.96 Rust-2024 Rwlock Smart-Pointer State-Machine Static Testing Threads Traits Utf-8 Alignment Array-Windows Atomic Atomicity Benchmarking Binary-Heap Binaryheap Bool Box Break Bytes Casting Cfg Clippy Closure Cmp Collect Combinators Comparison Compiler Conditional-Compilation Condvar Const Const-Generics Constants Control-Flow Controlflow Conversions Copy Cross-Platform Data-Processing Deref Diagnostics Dispatch Display Drain Duration Enumerate Enums Error-Messages File File-Locking Filter Flatten Floats Fmt Fn Fnmut Fnonce Fold Formatting From_fn Gotcha Guards Hashset If-Let Initialization Inline Intersperse Into-Iter Lazy-Init Lazy-Initialization Let-Chains Lifetime Lifetimes Lints Loop Map Map_while Match-Guards Matching Mem-Replace Memory Niche Num Numeric Oncecell Optimization Ord Ordering Panic Panics Pathbuf Pettern Phantom-Data Pin-Project Pipes Position Precision Processes Raw String Refcell Rust-1.79 Rust-1.88 Rust-1.91 Scan Scoped-Threads Security Sort Statics Str String Structs Syntax Time Tokio Trait-Objects Try-Fold Type-Inference Type-Safety Types Unicode Unsafe Unsafecell Unzip Upcasting Validation Weak Zero-Sized-Types

Recently updated

#281 281. mpsc::channel — Send Results Back Instead of Fighting Over a Mutex #282 282. mpsc::sync_channel — A Channel That Pushes Back When the Consumer Falls Behind #283 283. recv_timeout — Wait for a Message, But Not Forever #284 284. try_recv — Poll the Channel Without Stalling the Loop #285 285. try_iter — The Drain Loop You Wrote This Morning, as One Line

About

A collection of bite-sized Rust tips and tricks. Small, focused lessons to help you write better Rust.

info@rustbites.com
rustbites rustbites · 2022—2026
X RSS

This site uses cookies for analytics and ads. Learn more