Skip to content

Instantly share code, notes, and snippets.

@ciwolsey
Last active March 1, 2021 22:00
Show Gist options
  • Save ciwolsey/24440dd2c09b585f799bf803f54b395d to your computer and use it in GitHub Desktop.
Save ciwolsey/24440dd2c09b585f799bf803f54b395d to your computer and use it in GitHub Desktop.
struct Entity {
shape: Shape
}
enum Shape {
Square (u32),
Rectangle { width: u32, height: u32 },
}
trait Area {
fn get_area(&self) -> u32;
}
impl Area for Entity {
fn get_area(&self) -> u32 {
match self.shape {
Shape::Rectangle { width, height } => width * height,
Shape::Square (width) => width * width,
}
}
}
fn main() {
let a = Entity {
shape: Shape::Rectangle { width: 3, height: 2 }
};
let b = Entity {
shape: Shape::Square(3)
};
println!("A: {}", a.get_area());
println!("B: {}", b.get_area());
}
struct is never constructed: `Entity`
enum is never used: `Shape`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment