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
String(version)turns anIntinto text before concatenation.byte_length()measures the result; never calllenon aString.- Multiplying a string repeats it, which makes quick separators.
Keywords and builtins used here
Stringdefmainprintvar
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.
Step 1 of 5 in Strings, step 18 of 26 in Language basics.