Mux Code Examples
A curated collection of Mux code examples organized by category. These examples demonstrate Mux's core features with runnable, commented code.
All examples on this page demonstrate real Mux language features. Feel free to copy them to the playground and modify them!
Getting Started
Hello World
Every Mux program starts with a main function. This simple example demonstrates how to print text to the console.
Variables and Types
Mux has explicit typing with type inference. Variables can be declared with explicit types or using auto for inferred types. Constants are declared with const.
Functions
Functions in Mux can have required and optional parameters. Default values are specified directly in the parameter list. Functions return values using the return keyword.
Control Flow
Conditionals
Mux supports if/else statements with optional else if chains. Boolean expressions work with comparison operators.
Loops
Mux supports for loops with range() and while loops. The for loop can iterate over a range with optional step.
Pattern Matching
The match expression is Mux's powerful pattern matching tool. It works with enums, optionals, and custom types.
Collections
Lists
Lists are ordered, growable collections. They support indexing, push/pop operations, and various utility methods.
Maps
Maps store key-value pairs. Keys and values can be any type. Access values using bracket notation or the get() method for safe access.
Sets
Sets are unordered collections of unique elements. They provide fast membership testing with contains().
Types
Enums
Enums define tagged unions with data variants. Each variant can hold different types of data.
Classes
Classes encapsulate data and behavior. They can implement interfaces and contain methods with access to instance data via self.
Generic Classes
Generics allow you to write flexible, reusable code that works with any type while maintaining type safety.
Error Handling
Result Types
Result types represent either a successful value (ok) or an error (err). Pattern matching is used to handle both cases.
Optional Types
Optional types represent a value that might not exist. some(value) contains a value, none indicates absence.
Operators
Arithmetic
Mux supports standard arithmetic operators plus exponentiation.
Logical Operators
Boolean operators include ! (not), && (and), and || (or). They support short-circuit evaluation.
Comparison Operators
Compare values using standard comparison operators. All comparisons return boolean values.
References and Pointers
Reference Parameters
Functions can accept references to modify the original value or avoid copying large data.
Lambda Functions
Mux supports anonymous lambda functions that can be assigned to variables and passed around.
Try It Yourself
Want to experiment with these examples? Visit the Mux Playground to write and run your own code!