53 lines
636 B
Plaintext
53 lines
636 B
Plaintext
---
|
|
title: Structs & Enums
|
|
description: Defining structs and enums with named, tuple, and struct variants.
|
|
icon: Box
|
|
---
|
|
|
|
### Structs
|
|
|
|
```mist
|
|
pub struct Point
|
|
{
|
|
i32 x;
|
|
i32 y;
|
|
}
|
|
|
|
pub struct Generic<T>
|
|
{
|
|
T value;
|
|
}
|
|
```
|
|
|
|
Fields can be public or private:
|
|
|
|
```mist
|
|
struct User
|
|
{
|
|
pub str& name;
|
|
i32 age; // private
|
|
}
|
|
```
|
|
|
|
### Enums
|
|
|
|
```mist
|
|
pub enum Option<T>
|
|
{
|
|
Some(T);
|
|
None;
|
|
}
|
|
|
|
pub enum Message
|
|
{
|
|
Quit;
|
|
Move(i32, i32);
|
|
Write { str& content; i32 length; };
|
|
}
|
|
```
|
|
|
|
Enum variants can be:
|
|
- **Named** — `Variant`
|
|
- **Tuple** — `Variant(T1, T2)`
|
|
- **Struct** — `Variant { T1 field1; T2 field2; }`
|