I'm trying to solve an old puzzle (from Advent of Code) to learn some Rust. The goal is to calculate the brightness of some grid of lamps. Full description at Advent of Code - Day 6 (2015).
First i define a trait Lamp
pub trait Lamp {
fn toggle(&mut self);
fn turn_on(&mut self);
fn turn_off(&mut self);
fn brightness(&self) -> u32;
}
I implement the trait for two kinds of lamps
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct SwitchLamp {
on: bool
}
impl Lamp for SwitchLamp {
...
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct DimmerLamp {
intensity: u32
}
impl Lamp for DimmerLamp {
...
}
I want to put the Lamp
s in a Grid
#[derive(Copy, Clone)]
pub struct Grid<Lamp> {
grid: [[Lamp; 1000]; 1000]
}
but unfortunately the compiler complains that the methods are not implemented in scope.
impl<Lamp> Grid<Lamp> {
fn toggle(&mut self, x: usize, y: usize) {
self.grid[x][y].toggle();
}
...
}
The complete error message reads:
error[E0599]: no method named `toggle` found for type `Lamp` in the current scope
--> src/day06.rs:98:25
|
98 | self.grid[x][y].toggle();
| ^^^^^^
|
= help: items from traits can only be used if the trait is implemented and in scope
= note: the following trait defines an item `toggle`, perhaps you need to implement it:
candidate #1: `Lamp`
Full code can be found at day06.rs
Can anybody help me understand what i am doing wrong, and help me fix the problem?