From 09ff36dff8e25a32833be0d038a3ac5cb560bc92 Mon Sep 17 00:00:00 2001 From: Stephan Rehfeld Date: Wed, 13 May 2026 17:49:59 +0200 Subject: [PATCH] Put some stuff into lib.rs --- src/lib.rs | 124 ++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 147 ++++++++-------------------------------------------- 2 files changed, 146 insertions(+), 125 deletions(-) create mode 100644 src/lib.rs diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..cc74975 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,124 @@ +use std::ops::{Add, Div, Mul, Sub}; + +#[derive(Copy, Clone)] +pub struct Vector3 { + x: f64, + y: f64, + z: f64, +} + +impl Vector3 { + pub fn new(x: f64, y: f64, z: f64) -> Vector3 { + Vector3 { x, y, z } + } +} + +impl Add for Vector3 { + type Output = Vector3; + + fn add(self, rhs: Vector3) -> Self::Output { + Vector3::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z) + } +} + +impl Sub for Vector3 { + type Output = Vector3; + + fn sub(self, rhs: Vector3) -> Self::Output { + Vector3::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z) + } +} + +impl Mul for Vector3 { + type Output = Vector3; + + fn mul(self, rhs: f64) -> Self::Output { + Vector3::new(self.x * rhs, self.y * rhs, self.z * rhs) + } +} + +impl Mul for f64 { + type Output = Vector3; + + fn mul(self, rhs: Vector3) -> Self::Output { + Vector3::new(self * rhs.x, self * rhs.y, self * rhs.z) + } +} + +impl Mul for Vector3 { + type Output = f64; + + fn mul(self, rhs: Vector3) -> Self::Output { + self.x * rhs.x + self.y * rhs.y + self.z * rhs.z + } +} + +impl Div for Vector3 { + type Output = Vector3; + + fn div(self, rhs: f64) -> Self::Output { + Vector3::new(self.x / rhs, self.y / rhs, self.z / rhs) + } +} + +#[derive(Copy, Clone)] +pub struct Point3 { + x: f64, + y: f64, + z: f64, +} + +impl Point3 { + pub fn new(x: f64, y: f64, z: f64) -> Point3 { + Point3 { x, y, z } + } +} + +impl Add for Point3 { + type Output = Point3; + + fn add(self, rhs: Vector3) -> Self::Output { + Point3::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z) + } +} + +impl Sub for Point3 { + type Output = Vector3; + + fn sub(self, rhs: Point3) -> Self::Output { + Vector3::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z) + } +} + +pub struct Plane { + point: Point3, + normal: Vector3, +} + +impl Plane { + pub fn new(point: Point3, normal: Vector3) -> Plane { + Plane { point, normal } + } +} + +pub struct Line { + point: Point3, + direction: Vector3, +} + +pub struct Sphere { + point: Point3, + radius: f64, +} + +impl Sphere { + pub fn new(point: Point3, radius: f64) -> Sphere { + Sphere { point, radius } + } +} + +pub enum Geometry { + Sphere(Sphere), + Plane(Plane), + Line(Line), +} diff --git a/src/main.rs b/src/main.rs index 82a840d..ad45a64 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,128 +1,4 @@ -use std::ops::{Add, Div, Mul, Sub}; -use std::collections:: - -#[derive(Copy, Clone)] -struct Vector3 { - x: f64, - y: f64, - z: f64, -} - -impl Vector3 { - fn new(x: f64, y: f64, z: f64) -> Vector3 { - Vector3 { x, y, z } - } -} - -impl Add for Vector3 { - type Output = Vector3; - - fn add(self, rhs: Vector3) -> Self::Output { - Vector3::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z) - } -} - -impl Sub for Vector3 { - type Output = Vector3; - - fn sub(self, rhs: Vector3) -> Self::Output { - Vector3::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z) - } -} - -impl Mul for Vector3 { - type Output = Vector3; - - fn mul(self, rhs: f64) -> Self::Output { - Vector3::new(self.x * rhs, self.y * rhs, self.z * rhs) - } -} - -impl Mul for f64 { - type Output = Vector3; - - fn mul(self, rhs: Vector3) -> Self::Output { - Vector3::new(self * rhs.x, self * rhs.y, self * rhs.z) - } -} - -impl Mul for Vector3 { - type Output = f64; - - fn mul(self, rhs: Vector3) -> Self::Output { - self.x * rhs.x + self.y * rhs.y + self.z * rhs.z - } -} - -impl Div for Vector3 { - type Output = Vector3; - - fn div(self, rhs: f64) -> Self::Output { - Vector3::new(self.x / rhs, self.y / rhs, self.z / rhs) - } -} - -#[derive(Copy, Clone)] -struct Point3 { - x: f64, - y: f64, - z: f64, -} - -impl Point3 { - fn new(x: f64, y: f64, z: f64) -> Point3 { - Point3 { x, y, z } - } -} - -impl Add for Point3 { - type Output = Point3; - - fn add(self, rhs: Vector3) -> Self::Output { - Point3::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z) - } -} - -impl Sub for Point3 { - type Output = Vector3; - - fn sub(self, rhs: Point3) -> Self::Output { - Vector3::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z) - } -} - -struct Plane { - point: Point3, - normal: Vector3, -} - -impl Plane { - fn new(point: Point3, normal: Vector3) -> Plane { - Plane { point, normal } - } -} - -struct Line { - point: Point3, - direction: Vector3, -} - -struct Sphere { - point: Point3, - radius: f64, -} - -impl Sphere { - fn new(point: Point3, radius: f64) -> Sphere { - Sphere { point, radius } - } -} - -enum Geometry { - Sphere(Sphere), - Plane(Plane), - Line(Line), -} +use hello::*; fn main() { let sphere1 = Sphere::new(Point3::new(0.0, 0.0, 0.0), 25.0); @@ -145,4 +21,25 @@ fn main() { Geometry::Line(l) => println!("A Line"), } } + + let mut lottery = [4, 8, 15, 16, 23, 42]; + + let mut sum = 0; + + for value in lottery { + sum += value; + } + + println!("The sum is {}", sum); + + let mut lottery_string = Vec::new(); + for value in lottery { + lottery_string.push(value.to_string()); + } + + let lottery_string_2: Vec = lottery + .iter() + .filter(|x| **x > 8) + .map(|x| x.to_string()) + .collect(); }