Created
December 10, 2017 20:31
-
-
Save matklad/607babb23cc7b6382e4c57e416bc3854 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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