Files
mist-website/content/docs/components/structs.mdx
T
2026-06-28 22:45:01 +02:00

85 lines
1.7 KiB
Plaintext

---
title: Structs
description: Data modeling with Mist's type-first field convention.
icon: Form
---
Structs in Mist follow the same structural logic as Rust, with fields using the language-wide `Type name` convention and comma-separated grouping.
## Basic Syntax
A struct is defined by its name followed by a block of fields. Each field places the type before the identifier.
```mist
pub struct Task {
pub String name,
pub TaskState state,
pub i32 executions,
}
```
## Visibility
Use the `pub` modifier to make the struct or its individual fields accessible from other modules.
```mist
pub struct NetworkNode {
pub u32 id,
str& address,
}
```
## Instantiation
Structs are instantiated using standard brace syntax.
```mist
let task = Task {
name: "Initialize".to_string(),
state: TaskState::Pending,
executions: 0,
};
```
## Mutation
Use `let mut` to allow field reassignment.
```mist
let mut p = Point {
x: 1,
y: 2,
};
p.x = 100;
```
## Destructuring
Struct patterns use `let` with the struct name and field bindings:
```mist
let p = Point {
x: 3,
y: 4,
};
let Point { x, y } = p;
```
## Generics
Generics and lifetimes are declared in angle brackets after the struct name.
```mist
pub struct Buffer<'a, T> {
&'a T data,
usize len,
}
```
## Key Characteristics
- **Type-First Declaration**: Fields use `Type name` order, consistent with function parameters and variable declarations.
- **Comma-Separated Members**: Fields are separated by commas, maintaining a clean delimiter style.
- **Rust Compatibility**: Maps 1:1 to Rust structs, ensuring zero-cost abstraction and full ecosystem interoperability.
- **Direct Visibility**: The `pub` modifier controls access at the struct and field level.