The while/let pattern allows us to stay in a loop, as long as some
matching is true.
Let us define a tagged union to register measurement values and the absence values:
enum Measure {
Value(f64),
Nil,
}We define then a function that generates some Measure values
depending on an index:
fn get_measure(i: usize) -> Measure {
if i < 10 {
Measure::Value(1.12 * i as f64)
}
else {
Measure::Nil
}
}Let us initialize a global index:
let mut i: usize = 0;And use it to iterate over the Measure values returned by the
get_measure() function. We use the while/let construct to extract
the float values returned by the function. As soon as a Nil is
returned, we will quit the loop:
while let Measure::Value(x) = get_measure(i) {
print!("{x} ");
i += 1;
}
println!();0 1.12 2.24 3.3600000000000003 4.48 5.6000000000000005 6.720000000000001 7.840000000000001 8.96 10.080000000000002