// `type Item = ..;` inside an `impl` binds a name the same block's signatures // use as `Self::Item`. A `const` in an `impl` becomes a module-level constant // named for both, since Nim has no per-type constant namespace. struct Grid { cells: Vec, width: usize, } trait Cells { type Out; fn get_at(&self, i: usize) -> Self::Out; fn total(&self) -> Self::Out; } impl Cells for Grid { type Out = i32; fn get_at(&self, i: usize) -> Self::Out { self.cells[i] } fn total(&self) -> Self::Out { let mut t: i32 = 0; for c in self.cells.iter() { t += c; } t } } impl Grid { const BORDER: i32 = 7; fn at(&self, r: usize, c: usize) -> i32 { self.cells[r * self.width + c] } } fn main() { let g = Grid { cells: vec![1, 2, 3, 4, 5, 6], width: 3, }; println!("{} {}", g.get_at(0), g.get_at(5)); println!("{}", g.total()); println!("{} {}", g.at(0, 2), g.at(1, 0)); println!("{}", Grid::BORDER); }