Variables and Constants
Mux supports both explicit type declarations and type inference with the auto keyword.
Variable Declarations
Explicit Typing
Type Inference with auto
Important Rules
All declarations require either an explicit type or auto with an initializer. Semicolons are not used.
Constants
Constants are immutable values that cannot be reassigned or modified after initialization:
Const Enforcement
- Cannot reassign:
const_var = new_value-> ERROR - Cannot use compound assignment:
const_var += 1-> ERROR - Cannot increment/decrement:
const_var++orconst_var---> ERROR - Applies to both identifiers and class fields
- Use
constwhen you want a value that won't change after initialization
When to Use auto
Recommended
- Local variables with obvious initialization
- Complex generic types that are clear from context
- Temporary variables in calculations
- Iterator variables in loops
Explicit Types Recommended
Using Underscore for Unused Values
The underscore _ is a placeholder for values you don't need:
Best Practice: Use _ when a value is required by syntax but not needed in your code. Don't overuse it when descriptive names would improve readability.
Variable Scope
Variables are scoped to the block in which they are declared:
Unless you create a closure, then you can capture variables from the enclosing scope: