Variables and types in Mojo
Typed bindings, and what reassigning them looks like.
def main():
var count: Int = 3
var ratio: Float64 = 0.75
var label: String = "batch"
var ready: Bool = count > 0
count += 1
ratio = ratio * 2.0
label = label + "-2"
print(label, count, ratio, ready)
How it works
var name: Type = valuedeclares a binding with an explicit type.Int,Float64,String, andBoolcover most everyday values.- Each binding is reassigned before the final
printreads it.
Keywords and builtins used here
Float64IntStringdefmainprintvar
The run, in numbers
- Lines
- 9
- Characters to type
- 198
- Tokens
- 57
- Three-star pace
- 90 tpm
At the three-star pace of 90 tokens a minute, this run takes about 38 seconds.
Step 2 of 6 in Variables & functions, step 2 of 26 in Language basics.