--- title: Classes description: Unified data and behavior with Java-style organization and Rust-powered execution. icon: Shapes --- 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 C-style method signatures with `&self` for the instance parameter. ## Basic Syntax A class groups fields and methods together. Fields use semicolons and the `Type name` convention. Methods use C-style syntax with `&self` as the first parameter for shared access. ```mist pub class Logger { String prefix; pub void info(&self, message str&) { self.log(LogLevel::Info, message); } void log(&self, LogLevel level, message str&) { println!("{level} {} {}", self.prefix, message); } } ``` ## The Constructor Mist uses the explicit `constructor` keyword for initialization: ```mist pub constructor() { self.prefix = "default".to_string(); } ``` Constructors can take parameters: ```mist pub constructor(String prefix) { self.prefix = prefix; } ``` ## Instance Methods & `&self` Methods use `&self` (shared reference) or `&mut self` (mutable reference) as the first parameter. ```mist pub void warning(&self, message str&) { self.log(LogLevel::Warning, message); } pub void 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 by placing `override(Parent)` after the parameter list. ```mist pub class Animal { pub String name; constructor() { self.name = "Rex".to_string(); } pub String speak(&self) { "Unknown".to_string() } } pub class Dog : Animal { constructor() { super = Super::new(); } pub String speak(&self) override(Animal) { "Woof!".to_string() } } ``` ## Trait Implementations Traits can be implemented directly inside a class body: ```mist pub class Dog : Animal { constructor() { self.name = name; } pub String speak(&self) override(Animal) { "Woof!".to_string() } impl std::fmt::Display { Result<(), std::fmt::Error> fmt(&self, std::fmt::Formatter<'_> mut& f) { write!(f, "🐾 {}", self.name) } } } ``` ## Generics Classes support generic type parameters: ```mist pub class Container { T value; constructor(T val) { self.value = val; } pub &T get(&self) { &self.value } } ``` ## Key Characteristics - **Unified Scope**: Data and behavior live in one class block. - **C-Style Methods**: Return type before name — no `fn` keyword. - **`&self` Parameter**: The self reference is explicit and uses `&` syntax. - **Inheritance**: Single inheritance with `override(Parent)` for polymorphic dispatch. - **Encapsulation**: Visibility modifiers (`pub`) control API exposure. - **Inline Impl**: Traits can be implemented directly within the class body. - **Zero-Cost Classes**: Under the hood, Mist desugars these into idiomatic Rust structs and implementation blocks.