Skip to main content

Variables

The video for “Variables” is in the works!

Variables store data that your program can use and modify.

Declaring Variables

Mux has explicit typing with type inference. You can declare variables with explicit types or let Mux infer the type:

Try it yourself
Loading...

When to Use Explicit Types

The auto keyword works well when the type is obvious from the value:

  • auto name = "Mux" - string inferred
  • auto count = 42 - int inferred
  • auto numbers = [1, 2, 3] - type inferred from contents

Some cases require an explicit type because the type can't be inferred:

  • Empty collections: auto empty = [] (error - can't infer element type)
  • Use explicit types: List<int> numbers = [], Map<string, int> ages = {}
Try it yourself
Loading...

Constants

Use const to declare values that never change:

Try it yourself
Loading...

Incrementing and Decrementing

Variables can be incremented or decremented using ++ and --:

Try it yourself
Loading...

Previous: Hello World | Next: Basic Types