This commit is contained in:
2026-06-05 12:32:12 +02:00
parent 0955bca5bc
commit d5501ef25e
14 changed files with 821 additions and 11 deletions
+54
View File
@@ -0,0 +1,54 @@
struct Vector2 {
Int x,
Int y,
}
impl Vector2 {
pub Int magnitude(self*) -> Int {
self.x * self.x + self.y * self.y
}
}
pub void test_inherent_impl() {
let v = Vector2 { x: 3, y: 4 };
assert_eq!(v.magnitude(), 25);
}
trait Display {
String display(self*);
}
impl Display for Vector2 {
String display(self*) {
format!("({}, {})", self.x, self.y)
}
}
pub void test_trait_impl_block() {
let v = Vector2 { x: 1, y: 2 };
let s = v.display();
assert_eq!(s.len() > 0, true);
}
impl Vector2 {
pub Vector2 add(self*, Vector2 other) {
Vector2 {
x: self.x + other.x,
y: self.y + other.y,
}
}
}
pub void test_multiple_impls() {
let a = Vector2 { x: 1, y: 2 };
let b = Vector2 { x: 3, y: 4 };
let c = a.add(b);
assert_eq!(c.x, 4);
assert_eq!(c.y, 6);
}
pub void run_tests() {
test_inherent_impl();
test_trait_impl_block();
test_multiple_impls();
}