Structs
Structs group named fields into a single type.
Declaring a Struct
kode
struct Person {
name: string,
age: int,
email: string
}
struct Point {
x: int,
y: int
}Creating Instances
kode
let person = Person {
name: "Alice",
age: 30,
email: "alice@example.com"
}
let point = Point { x: 10, y: 20 }Accessing Fields
kode
print(person.name) // "Alice"
print(person.age) // 30
print(point.x) // 10
print(point.y) // 20Struct with Methods
kode
struct Circle {
radius: float
}
impl Circle {
fn area() -> float {
return 3.14159 * this.radius * this.radius
}
fn circumference() -> float {
}
}
let circle = Circle { radius: 5.0 }
print(circle.area()) // 78.54
print(circle.circumference()) // 31.42Struct Introspection
kode
let person = Person { name: "Bob", age: 25, email: "bob@test.com" }
// Get field names
let fieldNames = keys(person)
print(fieldNames) // ["name", "age", "email"]
// Get field values
let fieldValues = values(person)
print(fieldValues) // ["Bob", 25, "bob@test.com"]