Skip to content

Instantly share code, notes, and snippets.

@matklad
Created December 10, 2017 20:31
Show Gist options
  • Save matklad/607babb23cc7b6382e4c57e416bc3854 to your computer and use it in GitHub Desktop.
Save matklad/607babb23cc7b6382e4c57e416bc3854 to your computer and use it in GitHub Desktop.
extern crate alias;
use std::cell::Cell;
fn pairwise_diffs(xs: &mut [i32]) {
let xs: &[Cell<i32>] = alias::slice(xs);
// Iterating & mutating *without* indices!
for (x, y) in xs.iter().zip(xs[1..].iter()) {
x.set(y.get() - x.get())
}
}
fn main() {
let mut xs = [1, 1, 2, 3, 5, 8, 13, 21, 34];
pairwise_diffs(&mut xs);
eprintln!("xs = {:?}", xs);
// xs = [0, 1, 1, 2, 3, 5, 8, 13, 34]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment