rustbites rustbites bite-sized rust tips
Archives About
#018 Jul 13, 2022
option iterator flatten

18. flatten options

Use flatten to iterate over only Some values if you have a collection of Options.

1
2
3
4
5
6
let nums = vec![None, Some(2), None, Some(3), None];

let mut iter = nums.into_iter().flatten();

assert_eq!(iter.next(), Some(2));
assert_eq!(iter.next(), Some(3));

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

Share:
← Previous 16. Option/Result match?! Next → 19. breaking from labeled blocks

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