typestar

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

  1. var name: Type = value declares a binding with an explicit type.
  2. Int, Float64, String, and Bool cover most everyday values.
  3. Each binding is reassigned before the final print reads it.

Keywords and builtins used here

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.

Type this snippet

Step 2 of 6 in Variables & functions, step 2 of 26 in Language basics.

← Previous Next →

Variables and types in other languages