VC: update docs
This commit is contained in:
@@ -1,14 +1,14 @@
|
||||
---
|
||||
title: Enums
|
||||
description: Defining algebraic data types with Mist's data-first convention.
|
||||
description: Defining algebraic data types with Mist's type-first convention.
|
||||
icon: Layers
|
||||
---
|
||||
|
||||
Enums in Mist serve as powerful algebraic data types (ADTs), maintaining the exact behavior and safety of Rust enums while using square brackets for tuple variant types and `name Type` for struct-like fields.
|
||||
Enums in Mist serve as powerful algebraic data types (ADTs), maintaining the exact behavior and safety of Rust enums while using parentheses for tuple variants and `Type name` for struct-like fields.
|
||||
|
||||
## Basic Syntax
|
||||
|
||||
An enum can contain unit variants, tuple variants (with types in square brackets), or struct-like variants.
|
||||
An enum can contain unit variants, tuple variants (with types in parentheses), or struct-like variants.
|
||||
|
||||
```mist
|
||||
pub enum TaskState {
|
||||
@@ -16,8 +16,8 @@ pub enum TaskState {
|
||||
InProgress,
|
||||
Completed,
|
||||
Failed {
|
||||
reason String,
|
||||
code i32,
|
||||
String reason,
|
||||
i32 code,
|
||||
},
|
||||
}
|
||||
```
|
||||
@@ -29,57 +29,59 @@ Mist supports all standard variant shapes:
|
||||
```mist
|
||||
enum OptionInt {
|
||||
None, // Unit
|
||||
Some[i32], // Tuple (square brackets)
|
||||
Some(i32), // Tuple (parentheses)
|
||||
}
|
||||
|
||||
enum Shape {
|
||||
Circle { radius i32 }, // Struct-like
|
||||
Rect { w i32, h i32 },
|
||||
Circle { i32 radius }, // Struct-like
|
||||
Rect { i32 w, i32 h },
|
||||
}
|
||||
```
|
||||
|
||||
### Instantiation & Matching
|
||||
|
||||
Tuple variants are created with parentheses and matched with brackets:
|
||||
Tuple variants are created and matched with parentheses:
|
||||
|
||||
```mist
|
||||
let x = OptionInt::Some(42);
|
||||
|
||||
match (x) {
|
||||
match x {
|
||||
OptionInt::None => { println!("none"); }
|
||||
OptionInt::Some[v] => { println!("{}", v); }
|
||||
OptionInt::Some(v) => { println!("{}", v); }
|
||||
}
|
||||
```
|
||||
|
||||
Struct variants use brace notation:
|
||||
|
||||
```mist
|
||||
let c = Shape::Circle { radius: 5 };
|
||||
let c = Shape::Circle {
|
||||
radius: 5,
|
||||
};
|
||||
|
||||
match (c) {
|
||||
match c {
|
||||
Shape::Circle { radius } => { println!("{}", radius); }
|
||||
Shape::Rect { .. } => { /* ignore */ }
|
||||
}
|
||||
```
|
||||
|
||||
## Generics & Lifetimes
|
||||
## Generics
|
||||
|
||||
Enums declare generics and lifetimes in angle brackets after the name. Reference types use the `*` prefix.
|
||||
Enums declare generics and lifetimes in angle brackets after the name.
|
||||
|
||||
```mist
|
||||
pub enum Validation<'a, T> {
|
||||
Valid(T),
|
||||
Invalid {
|
||||
message *'a str,
|
||||
error_id u32,
|
||||
&'a str message,
|
||||
u32 error_id,
|
||||
},
|
||||
}
|
||||
```
|
||||
|
||||
## Key Characteristics
|
||||
|
||||
- **Consistent Declaration**: Struct-like variants use the `name Type` order, consistent with Mist structs.
|
||||
- **Square Bracket Tuples**: Tuple variant types use `[]` brackets, distinct from function calls.
|
||||
- **Consistent Declaration**: Struct-like variants use `Type name` order, consistent with Mist structs.
|
||||
- **Parentheses Tuples**: Tuple variants use `()` syntax, consistent with Rust.
|
||||
- **Rust-Native ADTs**: Enums compile directly to Rust enums, allowing exhaustive pattern matching and zero-cost abstraction.
|
||||
- **Shared Visibility**: The `pub` modifier at the enum level exports all variants.
|
||||
- **Comma-Separated Members**: Fields within struct-like variants are separated by commas.
|
||||
|
||||
Reference in New Issue
Block a user