Our code so far.

This commit is contained in:
Stephan Rehfeld 2026-04-08 17:28:43 +02:00
commit f899a55a08
4 changed files with 128 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

7
Cargo.lock generated Normal file
View File

@ -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"

6
Cargo.toml Normal file
View File

@ -0,0 +1,6 @@
[package]
name = "hello"
version = "0.1.0"
edition = "2024"
[dependencies]

114
src/main.rs Normal file
View File

@ -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();
}