LogoMist

Classes

Unified data and behavior with Java-style organization and Rust-powered execution.

Classes in Mist bridge the gap between Java's organizational structure and Rust's performance. They group fields, constructors, and methods within a single cohesive block, using fn for methods and *self for the instance parameter.

Basic Syntax

A class groups fields and methods together. Fields use semicolons and the name Type convention. Methods use fn and take *self as the first parameter for shared access.

pub class Logger {
    prefix String;

    pub fn info(*self, message *str) {
        self.log(LogLevel::Info, message);
    }

    fn log(*self, level LogLevel, message *str) {
        println!("{level} {} {}", self.prefix, message);
    }
}

The Constructor

Mist uses the explicit constructor keyword for initialization:

pub constructor() {
    self.prefix = "default".to_string();
}

Constructors can take parameters:

pub constructor(prefix String) {
    self.prefix = prefix;
}

Instance Methods & *self

Methods use *self (shared reference) or *mut self (mutable reference) as the first parameter. The return type is placed after the parameter list.

pub fn warning(*self, message *str) {
    self.log(LogLevel::Warning, message);
}

pub fn reset(*mut self) {
    self.prefix = String::new();
}

Inheritance

Classes support single inheritance with the : syntax. Use super -> Super::new() in the constructor to call the parent constructor. Override methods with override or override(Parent).

pub class Animal {
    pub name String;

    constructor() {
        self.name = "Rex".to_string();
    }

    pub fn speak(*self) {
        println!("Unknown");
    }

    pub fn legs(*self) {
        println!("Unknown");
    }
}

pub class Dog : Animal {
    constructor() {
        super -> Super::new();
    }

    pub override fn speak(*self) {
        println!("Woof!");
    }

    // Explicit override is useful for super inheritance
    pub override(Animal) fn legs(*self) {
        println!("4 legs");
    }
}

Generics

Classes support generic type parameters:

pub class Container<T> {
    pub value T;

    constructor(val T) {
        self.value = val;
    }

    pub fn get(*self) *T {
        &self.value
    }
}

Allocations

In classes, it's very important to initialize heap allocated fields in constructor using path -> expr, here is the most common example:

pub class Dog : Animal {
    pub heap_alloc Box<i32>;

    constructor() {
        super -> Super::new();
        self.heap_alloc -> Box::new(69);
    }
}

This outputs std::ptr::write which is a force write to memory without dropping what "exists", and it's commonly used in uninitialized memory, such as uninitialized dynamic fields.

Key Characteristics

  • Unified Scope: Data and behavior live in one class block.
  • fn Methods: Methods use the fn keyword, consistent with free functions.
  • *self Parameter: The self reference is explicit and uses prefix * syntax.
  • Inheritance: Single inheritance with override for polymorphic dispatch.
  • Encapsulation: Visibility modifiers (pub) control API exposure.
  • Zero-Cost Classes: Under the hood, Mist desugars these into idiomatic Rust structs and implementation blocks.

On this page