Values in Nim

Nim has various value types including strings, integers, floats, booleans, etc. Here are a few basic examples.

import strformat

# Strings, which can be added together with `&`.
echo "nim" & "lang"

# Integers and floats.
echo fmt"1+1 = {1+1}"
echo fmt"7.0/3.0 = {7.0/3.0}"

# Booleans, with boolean operators as you'd expect.
echo true and false
echo true or false
echo not true

To run the program, save it as values.nim and use the Nim compiler:

$ nim c -r values.nim
nimlang
1+1 = 2
7.0/3.0 = 2.3333333333333335
false
true
false

In this Nim code:

  1. We use the strformat module to format strings, which is similar to Go’s fmt package.
  2. String concatenation in Nim is done with the & operator instead of +.
  3. Nim uses echo for printing to the console, which is similar to Go’s fmt.Println.
  4. Boolean operators in Nim are written as and, or, and not, instead of &&, ||, and ! in Go.
  5. Nim uses # for single-line comments instead of //.

The output is identical to the original example, demonstrating that Nim handles these basic value types in a similar manner to other programming languages.