structs
This commit is contained in:
@@ -18,7 +18,6 @@ i32 add(i32 a, i32 b) {
|
|||||||
void log_status(str* message) {
|
void log_status(str* message) {
|
||||||
println!("{}", message);
|
println!("{}", message);
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Visibility & Exports
|
## Visibility & Exports
|
||||||
@@ -29,7 +28,6 @@ Functions are private to their module by default. The `public` modifier exports
|
|||||||
public i32 get_version() {
|
public i32 get_version() {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Mutable Parameters
|
## Mutable Parameters
|
||||||
@@ -40,7 +38,6 @@ Parameters follow Rust’s ownership rules but use Mist’s local variable synta
|
|||||||
void update_score(i32 mut current_score, i32 bonus) {
|
void update_score(i32 mut current_score, i32 bonus) {
|
||||||
current_score = current_score + bonus;
|
current_score = current_score + bonus;
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Generics & Lifetimes
|
## Generics & Lifetimes
|
||||||
@@ -52,7 +49,6 @@ public str'a* choose_longer<'a, T: Display>(str'a* s1, str'a* s2, T meta) {
|
|||||||
println!("Metadata: {}", meta);
|
println!("Metadata: {}", meta);
|
||||||
return if (s1.len() > s2.len()) { s1 } else { s2 };
|
return if (s1.len() > s2.len()) { s1 } else { s2 };
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Attributes & Metadata
|
## Attributes & Metadata
|
||||||
@@ -64,7 +60,6 @@ Metadata is applied via the `#[attr]` syntax directly above the declaration for
|
|||||||
public bool is_active(u32 id) {
|
public bool is_active(u32 id) {
|
||||||
return id > 0;
|
return id > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Key Characteristics
|
## Key Characteristics
|
||||||
|
|||||||
@@ -0,0 +1,64 @@
|
|||||||
|
---
|
||||||
|
title: Structs
|
||||||
|
description: Data modeling using Mist's type-first convention.
|
||||||
|
icon: Form
|
||||||
|
---
|
||||||
|
|
||||||
|
Structs in Mist follow the same structural logic as Rust, but apply the language-wide `type name` declaration style and allow for comma-separated field grouping.
|
||||||
|
|
||||||
|
## Basic Syntax
|
||||||
|
|
||||||
|
A struct is defined by its name followed by a block of fields. Each field follows the Mist convention of placing the type before the identifier, separated by commas.
|
||||||
|
|
||||||
|
```rust
|
||||||
|
public struct Task {
|
||||||
|
public String name,
|
||||||
|
public TaskState state,
|
||||||
|
public i32 executions,
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
## Visibility
|
||||||
|
|
||||||
|
Use the `public` modifier to make the struct or its individual fields accessible from other modules.
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
public struct NetworkNode {
|
||||||
|
public u32 id,
|
||||||
|
str* address,
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
## Instantiation
|
||||||
|
|
||||||
|
Structs are instantiated using the standard brace syntax.
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
var task = Task {
|
||||||
|
name: "Initialize".to_string(),
|
||||||
|
state: TaskState::Pending,
|
||||||
|
executions: 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
## Generics & Lifetimes
|
||||||
|
|
||||||
|
Generics and lifetimes are declared in angle brackets after the struct name. Lifetimes are associated with the reference/pointer type within the field declarations.
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
public struct Buffer<'a, T> {
|
||||||
|
public T'a* data,
|
||||||
|
public usize len,
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
## Key Characteristics
|
||||||
|
|
||||||
|
- **Type-First Declaration**: Fields use the `type name` order to match function parameters and variable declarations.
|
||||||
|
- **Comma-Separated Members**: Fields are separated by commas, maintaining a clean and consistent delimiter style.
|
||||||
|
- **Rust Compatibility**: Maps 1:1 to Rust structs, ensuring zero-cost abstraction and full ecosystem interoperability.
|
||||||
|
- **Direct Visibility**: The `public` keyword replaces `pub` for a more consistent modifier language across the codebase.
|
||||||
Reference in New Issue
Block a user