Skip to content

Mojo Error: implicit declaration of 'x' is not allowed

error: implicit declaration of 'x' is not allowed; add 'var' to declare a new name means you assigned a value to a name that doesn’t exist yet. In Python that’s how you create a variable. In Mojo, a new variable needs var in front of it. If you’re coming from Python, this is very likely the first Mojo error you’ll see. For the other errors in this series, see Mojo Errors Explained.

def main():
count = 0
print(count)
count.mojo:2:5: error: implicit declaration of 'count' is not allowed; add 'var' to declare a new name
count = 0
^~~~~
var
mojo: error: failed to parse the provided Mojo source module

The line under the ^~~~~ marks shows the fix: Mojo would put var in front of count. The last line only says the build stopped; the first error: line is the one to read.

A declaration creates a new name. An implicit declaration creates one without saying so, just by assigning to it, like count = 0 in Python. Mojo turned that off. Every new variable has to be declared explicitly with var:

  • var count = 0 creates count.
  • count = 5 changes a count that already exists.

So when Mojo sees name = value and there’s no name yet, it stops and asks you to say what you meant.

It protects you from a classic Python bug. Say you have a total and you mistype it once:

total = 0
for n in range(4):
totl = total + n # Python: quietly makes a second variable
print(total) # prints 0, not 6

Python runs this without complaint and prints the wrong answer. In Mojo, totl = ... is an error, so the typo is caught before the program even runs. The variables page of the Mojo manual explains declared variables in more detail.

Fix 1: Add var the first time you use the name

Section titled “Fix 1: Add var the first time you use the name”

This is the right fix almost every time. Put var in front of the first assignment, and only there:

def main():
var count = 0
count = count + 1
count += 1
print(count)

Output:

2

Line 2 creates count. Lines 3 and 4 change it, so they don’t get var.

If you meant to change an existing variable, the name on the left is probably misspelled. Mojo can’t tell that you meant total, so it suggests var, which would be wrong here:

def main():
var total = 0
for n in range(4):
totl = total + n
print(total)
sum.mojo:4:9: error: implicit declaration of 'totl' is not allowed; add 'var' to declare a new name
totl = total + n
^~~~
var
mojo: error: failed to parse the provided Mojo source module

Before you add var, look at the name. Here the fix is the spelling, not var:

def main():
var total = 0
for n in range(4):
total = total + n
print(total)

Output:

6

A quick test: if the variable should still exist after the loop or the if, it should have been declared before it, so the line inside only needs =.

A type annotation or tuple unpacking doesn’t change the rule. age: Int = 36 and x, y = 1, 2 both give this error when the names are new. Put one var at the start of the line:

def main():
var age: Int = 36
var x, y = 1, 2
print(age, x, y)

Output:

36 1 2

Once x and y exist, swapping them works just like in Python, without var:

def main():
var x = 1
var y = 2
x, y = y, x
print(x, y)

Output:

2 1

You can also declare a variable first and give it a value later. var age: Int on one line and age = 36 on the next is fine, as long as you set it before you read it. If you don’t, you get use of uninitialized value.

  • Code pasted from Python. Every first assignment needs var. Go through the function and add it where each name first appears.
  • A typo inside a loop or an if. As in Fix 2, the typo makes a “new” name. Fix the spelling.
  • Code at the top of a file. Even with var, a variable outside any function is a different error, global variables are not supported. Put the code inside main, or use comptime for a constant.
  • A typo with +=. totl += 1 doesn’t create anything, so it gives use of unknown declaration ‘totl’ instead.

This is Python code pasted into a Mojo main. Make it build and print Ada scored 27.

def main():
name = "Ada"
scores: List[Int] = [9, 8, 10]
total = 0
for s in scores:
total += s
print(name, "scored", total)

Hint: three names are new. The loop variable s is created by for, so it doesn’t need var.

Show solution
def main():
var name = "Ada"
var scores: List[Int] = [9, 8, 10]
var total = 0
for s in scores:
total += s
print(name, "scored", total)

Output:

Ada scored 27

name, scores and total are each created once, so each gets one var. total += s changes an existing variable, and for declares s for you.

  • The error means you assigned to a new name without var.
  • Write var once, the first time a name appears. Later changes use plain =.
  • If you meant to change an existing variable, check the spelling before adding var.
  • Annotations and unpacking need it too: var age: Int = 36, var x, y = 1, 2.
  • Declaring the same name twice is a different error, invalid redefinition.

Next up: expressions must not appear at file scope, the error for code that isn’t inside a function.

Quick check

1. Which line creates a new variable price in Mojo?
2. You have var total = 0 and later write totl = total + 1. What should you change?
3. After var x = 1 and var y = 2, what does x, y = y, x do?