In Mojo, variables are simply names you give to store values or objects. Think of them as labels on jars, where each jar can hold something different. These variables are mutable, meaning you can change what's inside the jar after you've put something in it.

Constants vs. Variables

Quick Note on Immutability

Types of Variables

  1. Undeclared Variables: You can make a variable on the fly inside a def function or when playing around in the Mojo interactive environment (REPL). Just give it a name and a value, like name = "Sam". But, these casual variables are a no-go inside fn functions or as part of a structure's blueprint.
  2. Declared Variables: Use var when you want to formally declare a variable. It's like saying, "I'm going to use this jar for something specific." You can specify what type of thing the jar will hold, like an integer with var user_id: Int, or you can fill it immediately with something, like var name = "Sam".

Rules for Declared Variables

Going Deeper with Types

Understanding Variable Scopes