Skip to main content

Mux Code Examples

A curated collection of Mux code examples organized by category. These examples demonstrate Mux's core features with runnable, commented code.

tip

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.

hello.mux
Loading...

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.

variables.mux
Loading...

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.

functions.mux
Loading...

Control Flow

Conditionals

Mux supports if/else statements with optional else if chains. Boolean expressions work with comparison operators.

conditionals.mux
Loading...

Loops

Mux supports for loops with range() and while loops. The for loop can iterate over a range with optional step.

loops.mux
Loading...

Pattern Matching

The match expression is Mux's powerful pattern matching tool. It works with enums, optionals, and custom types.

pattern_matching.mux
Loading...

Collections

Lists

Lists are ordered, growable collections. They support indexing, push/pop operations, and various utility methods.

lists.mux
Loading...

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.

maps.mux
Loading...

Sets

Sets are unordered collections of unique elements. They provide fast membership testing with contains().

sets.mux
Loading...

Types

Enums

Enums define tagged unions with data variants. Each variant can hold different types of data.

enums.mux
Loading...

Classes

Classes encapsulate data and behavior. They can implement interfaces and contain methods with access to instance data via self.

classes.mux
Loading...

Generic Classes

Generics allow you to write flexible, reusable code that works with any type while maintaining type safety.

generic_stack.mux
Loading...

Error Handling

Result Types

Result types represent either a successful value (ok) or an error (err). Pattern matching is used to handle both cases.

results.mux
Loading...

Optional Types

Optional types represent a value that might not exist. some(value) contains a value, none indicates absence.

optionals.mux
Loading...

Operators

Arithmetic

Mux supports standard arithmetic operators plus exponentiation.

arithmetic.mux
Loading...

Logical Operators

Boolean operators include ! (not), && (and), and || (or). They support short-circuit evaluation.

logical.mux
Loading...

Comparison Operators

Compare values using standard comparison operators. All comparisons return boolean values.

comparisons.mux
Loading...

References and Pointers

Reference Parameters

Functions can accept references to modify the original value or avoid copying large data.

references.mux
Loading...

Lambda Functions

Mux supports anonymous lambda functions that can be assigned to variables and passed around.

lambdas.mux
Loading...

Try It Yourself

Want to experiment with these examples? Visit the Mux Playground to write and run your own code!