Skip to content

Instantly share code, notes, and snippets.

@tautologico
Created April 4, 2013 02:50
Show Gist options
  • Select an option

  • Save tautologico/5307330 to your computer and use it in GitHub Desktop.

Select an option

Save tautologico/5307330 to your computer and use it in GitHub Desktop.
// multiple indexing for matrices
struct Matrix {
n: int,
m: int,
data: ~[int]
}
impl Index<[int, ..2], int> for Matrix {
fn index(&self, ix: &[int, ..2]) -> int {
self.data[ix[0] * self.m + ix[1]]
}
}
fn main() {
let a = Matrix { n: 2, m: 2, data: ~[1, 2, 3, 4] };
io::println(a[[0, 0]].to_str());
io::println(a[[1, 0]].to_str());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment