1- // Struct definitions and instantiation
1+ // Type definitions and instantiation
22
3- // Simple struct with basic fields
4- struct Point {
3+ // Simple type with basic fields
4+ type Point {
55 x: float;
66 y: float;
77}
88
9- // Struct with mixed data types
10- struct Person {
9+ // Type with mixed data types
10+ type Person {
1111 name: string;
1212 age: int;
1313 height: float;
1414 is_student: int; // Using int for boolean (1 = true, 0 = false)
1515}
1616
17- // Struct with integer fields
18- struct Rectangle {
17+ // Type with integer fields
18+ type Rectangle {
1919 width: int;
2020 height: int;
2121}
2222
23- // Empty struct (no fields)
24- struct Empty {
23+ // Empty type (no fields)
24+ type Empty {
2525}
2626
27- // Struct with optional fields
28- struct User {
27+ // Type with optional fields
28+ type User {
2929 id: int;
3030 username: string;
3131 email: string? ; // Optional email
3232 score: float? ; // Optional score
3333}
3434
35- // Creating struct instances using 'new' keyword
35+ // Creating type instances using 'new' keyword
3636let origin = new Point { x: 0.0 , y: 0.0 };
3737let my_point = new Point { x: 3.5 , y: 7.2 };
3838
39- // Struct instantiation with all field types
39+ // Type instantiation with all field types
4040let person1 = new Person {
4141 name: ' Alice' ,
4242 age: 25 ,
@@ -51,19 +51,19 @@ let person2 = new Person {
5151 is_student: 0
5252};
5353
54- // Struct instantiation with trailing comma (allowed)
54+ // Type instantiation with trailing comma (allowed)
5555let rect1 = new Rectangle {
5656 width: 10 ,
5757 height: 20 ,
5858};
5959
60- // Struct instantiation in single line
60+ // Type instantiation in single line
6161let rect2 = new Rectangle { width: 5 , height: 8 };
6262
63- // Empty struct instantiation
63+ // Empty type instantiation
6464let empty_instance = new Empty {};
6565
66- // Struct with optional fields
66+ // Type with optional fields
6767let user1 = new User {
6868 id: 1 ,
6969 username: ' alice123' ,
@@ -78,16 +78,16 @@ let user2 = new User {
7878 score: null // Optional field set to null
7979};
8080
81- // Using struct instances in expressions and assignments
81+ // Using type instances in expressions and assignments
8282let point_a = new Point { x: 1.0 , y: 2.0 };
8383let point_b = new Point { x: 4.0 , y: 6.0 };
8484
85- // Struct instantiation with computed values
85+ // Type instantiation with computed values
8686let computed_x = 5.0 + 3.0 ;
8787let computed_y = 2.0 * 4.0 ;
8888let computed_point = new Point { x: computed_x, y: computed_y };
8989
90- // Struct instantiation in function calls (when we get to functions with struct params)
90+ // Type instantiation in function calls (when we get to functions with type params)
9191fn create_default_person () - > Person {
9292 return new Person {
9393 name: ' Default' ,
@@ -99,7 +99,7 @@ fn create_default_person() -> Person {
9999
100100let default_person = create_default_person ();
101101
102- // Multiple instances of the same struct
102+ // Multiple instances of the same type
103103let top_left = new Point { x: 0.0 , y: 10.0 };
104104let top_right = new Point { x: 10.0 , y: 10.0 };
105105let bottom_left = new Point { x: 0.0 , y: 0.0 };
0 commit comments