Simple if condifition

This commit is contained in:
Stephan Rehfeld 2026-04-08 17:38:35 +02:00
parent f899a55a08
commit 8779759e7a

View File

@ -1,114 +1,12 @@
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 number = 24;
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();
// Simlar as we know it from other programing languages
// No parantheses around condigition.
// Condition has to be a boolean
if number > 23 {
println!("foo");
} else {
println!("bar");
}
}