typestar

Building strings in Mojo

+ joins strings, and String() brings other types along.

def main():
    var name = String("Mojo")
    var version = 1
    var line = name + " " + String(version) + ".0"
    print(line)
    print(line + " is " + String(line.byte_length()) + " bytes")
    print(String("-") * 20)

How it works

  1. String(version) turns an Int into text before concatenation.
  2. byte_length() measures the result; never call len on a String.
  3. Multiplying a string repeats it, which makes quick separators.

Keywords and builtins used here

The run, in numbers

Lines
7
Characters to type
197
Tokens
66
Three-star pace
85 tpm

At the three-star pace of 85 tokens a minute, this run takes about 47 seconds.

Type this snippet

Step 1 of 5 in Strings, step 18 of 26 in Language basics.

← Previous Next →

Building strings in other languages