Syntax: fn type name

This commit is contained in:
2026-06-16 03:29:01 +02:00
parent eea72f5bb3
commit aadccb7f38
15 changed files with 9 additions and 842 deletions
-23
View File
@@ -1,23 +0,0 @@
pub module attribute_test;
#[derive(Debug)]
pub struct Debuggable {
value i32,
}
pub fn test_derive_attribute() {
let d = Debuggable { value: 42 };
assert_eq!(d.value, 42);
}
#[allow(unused_variables)]
pub fn test_allow_attribute() {
let x = 42;
let y = "unused";
assert_eq!(x, 42);
}
pub fn run_tests() {
test_derive_attribute();
test_allow_attribute();
}
-56
View File
@@ -1,56 +0,0 @@
pub module class_test;
pub class Animal<T> {
pub name String;
pub val T;
constructor() {
self.name = "Rex".to_string();
}
pub fn speak(*self) {
println!("Unknown");
}
pub fn test_speak(*self) {
self.speak();
}
pub fn static_method() {}
}
pub class Mammal<T> : Animal<T> {
constructor() {
super = Super::new();
}
pub fn args_test(*self, x i32, y *str) {}
pub fn returning<'a>(*self) *'a str {
"Mammal"
}
}
pub class Dog : Mammal<i32> {
constructor() {
super = Super::new();
}
pub override(Animal<i32>) fn speak(*self) {
println!("Woof!");
}
pub override fn returning(*self) *str {
"Dog"
}
}
pub fn run_tests() {
let dawg = Dog::new();
dawg.args_test(10, "hello");
dawg.test_speak();
println!("{}", dawg.returning());
}
-62
View File
@@ -1,62 +0,0 @@
pub module enum_test;
enum OptionInt {
None,
Some[i32],
}
pub fn test_enum_named() {
let x = OptionInt::None;
match x {
OptionInt::None => { assert_eq!(1, 1); }
OptionInt::Some[_] => { assert_eq!(0, 1); }
}
}
pub fn test_enum_tuple() {
let x = OptionInt::Some(42);
match x {
OptionInt::None => { assert_eq!(0, 1); }
OptionInt::Some[v] => { assert_eq!(v, 42); }
}
}
enum Shape {
Circle { radius i32 },
Rect { w i32, h i32 },
}
pub fn test_enum_struct() {
let c = Shape::Circle { radius: 5 };
match c {
Shape::Circle { radius } => { assert_eq!(radius, 5); }
Shape::Rect { .. } => { assert_eq!(0, 1); }
}
let r = Shape::Rect { w: 3, h: 4 };
match r {
Shape::Circle { .. } => { assert_eq!(0, 1); }
Shape::Rect { w, h } => { assert_eq!(w * h, 12); }
}
}
enum Color {
Red,
Green,
Blue,
}
pub fn test_enum_eq() {
let c = Color::Red;
match c {
Color::Red => { assert_eq!(1, 1); }
_ => { assert_eq!(0, 1); }
}
}
pub fn run_tests() {
test_enum_named();
test_enum_tuple();
test_enum_struct();
test_enum_eq();
}
-66
View File
@@ -1,66 +0,0 @@
pub module function_test;
fn greet() {
let mut x = 1;
x++;
}
pub fn test_basic_function() {
greet();
}
fn add(a i32, b i32) i32 {
a + b
}
pub fn test_fn_params() {
assert_eq!(add(2, 3), 5);
}
pub fn test_fn_return() i32 {
42
}
pub fn test_fn_early_return() i32 {
return 100;
0
}
pub fn test_fn_mut_param(mut x i32) i32 {
x = x + 10;
x
}
pub fn test_method_call() {
let s = "hello";
assert_eq!(s.len(), 5);
assert_eq!(s.to_uppercase(), "HELLO");
}
fn triple(x i32) i32 {
add(x, add(x, x))
}
pub fn test_nested_fn_calls() {
assert_eq!(triple(5), 15);
}
fn factorial(n i32) i32 {
if n <= 1 { 1 }
else { n * factorial(n - 1) }
}
pub fn test_recursive_fn() {
assert_eq!(factorial(5), 120);
}
pub fn run_tests() {
test_basic_function();
test_fn_params();
assert_eq!(test_fn_return(), 42);
assert_eq!(test_fn_early_return(), 100);
assert_eq!(test_fn_mut_param(5), 15);
test_method_call();
test_nested_fn_calls();
test_recursive_fn();
}
-56
View File
@@ -1,56 +0,0 @@
pub module impl_test;
struct Vector2 {
x i32,
y i32,
}
impl Vector2 {
pub fn magnitude(*self) i32 {
self.x * self.x + self.y * self.y
}
}
pub fn test_inherent_impl() {
let v = Vector2 { x: 3, y: 4 };
assert_eq!(v.magnitude(), 25);
}
trait Display {
fn display(*self) String;
}
impl Display for Vector2 {
fn display(*self) String {
format!("({}, {})", self.x, self.y)
}
}
pub fn test_trait_impl_block() {
let v = Vector2 { x: 1, y: 2 };
let s = v.display();
assert_eq!(s.len() > 0, true);
}
impl Vector2 {
pub fn add(*self, other Vector2) Vector2 {
Vector2 {
x: self.x + other.x,
y: self.y + other.y,
}
}
}
pub fn 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 fn run_tests() {
test_inherent_impl();
test_trait_impl_block();
test_multiple_impls();
}
-46
View File
@@ -1,46 +0,0 @@
pub module struct_test;
struct Point {
x i32,
y i32,
}
pub fn test_struct_create_access() {
let p = Point { x: 10, y: 20 };
assert_eq!(p.x, 10);
assert_eq!(p.y, 20);
}
pub fn test_struct_mut_field() {
let mut p = Point { x: 1, y: 2 };
p.x = 100;
assert_eq!(p.x, 100);
}
pub fn test_struct_destructure() {
let p = Point { x: 3, y: 4 };
let Point { x, y } = p;
assert_eq!(x, 3);
assert_eq!(y, 4);
}
struct Line {
start Point,
end Point,
}
pub fn test_struct_nested() {
let l = Line {
start: Point { x: 0, y: 0 },
end: Point { x: 5, y: 10 },
};
assert_eq!(l.start.x, 0);
assert_eq!(l.end.y, 10);
}
pub fn run_tests() {
test_struct_create_access();
test_struct_mut_field();
test_struct_destructure();
test_struct_nested();
}
-41
View File
@@ -1,41 +0,0 @@
pub module trait_test;
trait Speak {
fn speak(*self) String;
}
trait Greet : Speak {
fn greet(*self) String;
}
struct Dog {
name String,
}
impl Speak for Dog {
fn speak(*self) String {
"woof".to_string()
}
}
impl Greet for Dog {
fn greet(*self) String {
"hello".to_string()
}
}
pub fn test_trait_impl() {
let d = Dog { name: "Rex".to_string() };
assert_eq!(d.speak(), "woof");
}
pub fn test_trait_with_requirement() {
let d = Dog { name: "Buddy".to_string() };
assert_eq!(d.speak(), "woof");
assert_eq!(d.greet(), "hello");
}
pub fn run_tests() {
test_trait_impl();
test_trait_with_requirement();
}