commit f899a55a08764877434ddc57ea898fe3308ccdfc Author: Stephan Rehfeld Date: Wed Apr 8 17:28:43 2026 +0200 Our code so far. diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..bdfea16 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "hello" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..f6f3649 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "hello" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..4479984 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,114 @@ +fn foo() { + println!("Hello from foo()"); +} + +fn answer_to_all_questions() -> u32 { + 42 +} + +fn add_two_numbers(a: u32, b: u32) -> u32 { + a + b +} + +trait Vector { + fn dot(&self, other: &Self) -> f32; + fn length(&self) -> f32; +} + +struct Vector2 { + x: f32, + y: f32, +} + +impl Vector2 { + pub fn new(x: f32, y: f32) -> Vector2 { + Vector2 { x, y } + } +} + +impl Vector for Vector2 { + fn dot(&self, other: &Vector2) -> f32 { + self.x * other.x + self.y * other.y + } + + fn length(&self) -> f32 { + self.dot(self).sqrt() + } +} + +struct Vector3 { + x: f32, + y: f32, + z: f32, +} + +impl Vector3 { + pub fn new(x: f32, y: f32, z: f32) -> Vector3 { + Vector3 { x, y, z } + } +} + +impl Vector for Vector3 { + fn dot(&self, other: &Vector3) -> f32 { + self.x * other.x + self.y * other.y + self.z * other.z + } + + fn length(&self) -> f32 { + self.dot(self).sqrt() + } +} + +impl std::ops::Add for Vector3 { + type Output = Vector3; + fn add(self, other: Self) -> Self { + Vector3::new(self.x + other.x, self.y + other.y, self.z + other.z) + } +} + +struct Meter { + value: f32, +} + +impl Meter { + pub fn new(value: f32) -> Meter { + Meter { value } + } +} + +struct SquareMeter { + value: f32, +} + +impl SquareMeter { + pub fn new(value: f32) -> SquareMeter { + SquareMeter { value } + } +} + +impl std::ops::Mul for Meter { + type Output = SquareMeter; + + fn mul(self, rhs: Self) -> Self::Output { + SquareMeter::new(self.value * rhs.value) + } +} + +fn main() { + let vec1 = Vector3::new(2.0, 0.0, 0.0); + let vec2 = Vector3::new(1.0, 1.0, 0.0); + + let vec3 = vec1 + vec2; + + let m1 = Meter::new(10.0); + let m2 = Meter::new(90.0); + + let area = m1 * m2; + + println!("Hello, world!"); + println!("Answer to all questions: {}", answer_to_all_questions()); + println!("Add two number: {}", add_two_numbers(23, 42)); + + println!("x: {}, y: {}, z: {}", vec3.x, vec3.y, vec3.z); + + foo(); +}