This D code splits a string in parts lazily, converts each part in a u32 and assigns the numbers to the first items of a stack allocated array:
void main() {
import std.stdio, std.conv, std.algorithm, std.string;
uint[10] data;
const txt = "1 2 3 4";
txt.splitter.map!(to!uint).copy(data[]);
data.writeln;
}
The output is:
[1, 2, 3, 4, 0, 0, 0, 0, 0, 0]
A similar Rust program:
fn main() {
let mut data = [0u32; 10];
let txt = "1 2 3 4";
for (d, part) in data.iter_mut().zip(txt.split_whitespace()) {
*d = part.parse().unwrap();
}
println!("{:?}", data);
}
Do I have something similar to copy() to copy a lazy iterable into something else, avoiding the explicit for loop?