Mojo keywords reference
Keywords are reserved identifiers with special meaning in the language. They cannot be used as ordinary variable names unless escaped with backticks.
Caution
Core language keywords
| Keyword | Category | Purpose | Example |
|---|---|---|---|
and | Logical | Boolean AND operator | if x > 0 and y > 0: |
or | Logical | Boolean OR operator | if valid or debug: |
not | Logical | Boolean NOT operator | if not found: |
if | Control flow | Conditional execution | if condition: |
elif | Control flow | Additional condition in if statement | elif other_condition: |
else | Control flow | Default branch in conditionals or loops | else: |
for | Control flow | Iteration loop | for item in items: |
while | Control flow | Conditional loop | while count < 10: |
break | Control flow | Exit innermost loop | break |
continue | Control flow | Skip to next loop iteration | continue |
return | Control flow | Return from function | return value |
pass | Control flow | No-op placeholder statement | pass |
raise | Exception | Raise an exception | raise ValueError("error") |
try | Exception | Begin exception handling block | try: |
except | Exception | Exception handler clause | except e: |
finally | Exception | Always-execute block in try statement | finally: |
with | Context | Context manager statement | with open("file") as f: |
as | Binding | Import aliasing and exception binding | import math as m |
from | Import | Selective import | from math import pi |
import | Import | Module import | import collections |
in | Membership | Membership test operator | if x in list: |
is | Identity | Identity test operator | if x is None: |
Mojo-specific keywords
| Keyword | Purpose | Example |
|---|---|---|
fn | Function declaration (non-raising by default) | fn compute() -> Int: |
def | Function declaration (can raise) | def process(): |
struct | Structure type declaration | struct Point: |
trait | Trait interface declaration | trait Comparable: |
comptime | Compile-time evaluation | comptime SIZE = 1024 |
var | Variable/field declaration | var x: Int |
ref | Reference argument convention | fn modify(ref x: Int): |
Type introspection keywords
| Keyword | Purpose | Example |
|---|---|---|
origin_of | Returns origin (lifetime) of reference | origin_of(x) |
type_of | Returns type of expression at compile time | type_of(x) |
conforms_to | Tests trait conformance at compile time | conforms_to(T, Trait) |
Literal keywords
| Keyword | Type | Value |
|---|---|---|
True | Bool | Boolean true value |
False | Bool | Boolean false value |
None | NoneType | Absence of value |
Self | Type reference | Reference to enclosing struct or trait type |
_ | Discard | Discard pattern in destructuring |
Escaped keywords
Use backticks to use keywords as identifiers:
`fn` = 42 # Valid - escaped keyword as variable name
`struct` = "text" # Valid - escaped keyword
obj.fn() # Valid - keywords after dot don't need escaping
point.struct # Valid - member access doesn't require escapingCase sensitivity
All Mojo keywords are case-sensitive:
True # Keyword - boolean literal
true # Not a keyword - would be treated as identifier
None # Keyword - None literal
none # Not a keyword - would be treated as identifier
Self # Keyword - type reference
self # Not a keyword - conventional name for instance argumentWas this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!