From bb459067532499e9717ae25abf1d848a62e73282 Mon Sep 17 00:00:00 2001 From: Stephan Rehfeld Date: Wed, 29 Apr 2026 18:28:57 +0200 Subject: [PATCH] Important Rust Pattern: Inheritance does not exist, use enum to store multiple different types in same Collection --- src/main.rs | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 872489a..478bb44 100644 --- a/src/main.rs +++ b/src/main.rs @@ -95,6 +95,12 @@ struct Plane { normal: Vector3, } +impl Plane { + fn new(point: Point3, normal: Vector3) -> Plane { + Plane { point, normal } + } +} + struct Line { point: Point3, direction: Vector3, @@ -111,14 +117,22 @@ impl Sphere { } } +enum Geometry { + Sphere(Sphere), + Plane(Plane), +} + fn main() { let sphere1 = Sphere::new(Point3::new(0.0, 0.0, 0.0), 25.0); let sphere2 = Sphere::new(Point3::new(1.0, -45.4, 3.55), 2.0); let sphere3 = Sphere::new(Point3::new(3.0, 2.0, 1.0), 0.5); + let plane1 = Plane::new(Point3::new(1.0, 2.0, 3.0), Vector3::new(0.0, 1.0, 0.0)); + let mut v = Vec::new(); - v.push(sphere1); - v.push(sphere2); - v.push(sphere3); + v.push(Geometry::Sphere(sphere1)); + v.push(Geometry::Plane(plane1)); + v.push(Geometry::Sphere(sphere2)); + v.push(Geometry::Sphere(sphere3)); }