Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Rust

Idiom #196 Pass a sub-array

Given an integer array a of size n, pass the first, third, fifth and seventh, ... up to the m th element to a routine foo which sets all these elements to 42.

fn foo(el: &mut i32) {
    *el = 42;
}
a.iter_mut().take(m).step_by(2).for_each(foo);
void Foo(out int element)
{
    element = 42;
}

for (int i = 0; i < m; i += 2)
{
    Foo(out a[i]);
}

a.Length is the size of a at run-time.

Requires m <= a.Length; the code does not verify this beforehand.

References to individual elements are passed to Foo().

New implementation...
< >
tkoenig