Files
mist-website/content/docs/components/structs.mdx
T
2026-06-10 02:15:44 +02:00

70 lines
1.7 KiB
Plaintext

---
title: Structs
description: Data modeling with Mist's name-first field convention.
icon: Form
---
Structs in Mist follow the same structural logic as Rust, with fields using the language-wide `name Type` convention and comma-separated grouping.
## Basic Syntax
A struct is defined by its name followed by a block of fields. Each field places the identifier before the type.
```mist
pub struct Task {
pub name String,
pub state TaskState,
pub executions i32,
}
```
## Visibility
Use the `pub` modifier to make the struct or its individual fields accessible from other modules.
```mist
pub struct NetworkNode {
pub id u32,
address *str,
}
```
## Instantiation
Structs are instantiated using the standard brace syntax.
```mist
let task = Task {
name: "Initialize".to_string(),
state: TaskState::Pending,
executions: 0,
};
```
## 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 & Lifetimes
Generics and lifetimes are declared in angle brackets after the struct name. Reference types use the `*` prefix.
```mist
pub struct Buffer<'a, T> {
pub data *'a T,
pub len usize,
}
```
## Key Characteristics
- **Name-First Declaration**: Fields use `name Type` 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.