Take While Inclusive Keeps Boundary
Use take_while_inclusive when the item that fails the predicate is itself meaningful — the iterator stops after yielding the boundary element.
Code
fn main() {
let readings = [21.5, 22.0, 88.1, 19.9, 20.2];
// take_while:首个超限样本被吃掉,边界值不可见
let plain: Vec<f64> = readings
.into_iter()
.take_while(|r| *r < 50.0)
.collect();
println!("take_while: {plain:?}");
// take_while_inclusive:超限的那条也产出,告警要带上肇事样本
let with_boundary: Vec<f64> = readings
.into_iter()
.take_while_inclusive(|r| *r < 50.0)
.collect();
println!("inclusive: {with_boundary:?}");
// 首个元素就超限:普通版得到空集,inclusive 仍返回那条
let hot: Vec<f64> = [90.0, 20.0]
.into_iter()
.take_while_inclusive(|r| *r < 50.0)
.collect();
println!("hot first: {hot:?}");
}How the snippet works
Count the function main as the entire surface area of Take While Inclusive Keeps Boundary.
Using it
The demo output is labeled (take_while:, inclusive:, hot first:), so every printed line maps back to its originating operation.
Key statements
let readings = [21.5, 22.0, 88.1, 19.9, 20.2];
let with_boundary: Vec<f64> = readings
.into_iter()
.take_while_inclusive(|r| *r < 50.0)
.collect();
let hot: Vec<f64> = [90.0, 20.0]
.into_iter()
.take_while_inclusive(|r| *r < 50.0)
.collect();
Interactive Tools for This Topic
FAQ
- What does Take While Inclusive Keeps Boundary print when run?
- Running the file directly is enough — expect labeled output lines such as `take_while:`, `inclusive:`, `hot first:`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.