Variable Declaration and Assignment

Almost every Nim program makes use of variable bindings to associate some value to a name that can be used elsewhere in the program.

Variables have both a name and a type that they are associated with. In some languages (such as Python, Ruby or PHP), variables can change their type between assignments. However, in Nim a variable is always of a single type. You cannot, for instance, create a variable with a numerical value and then assign a string to it. In order to do this, you would require a new variable declaration for the string value.

Declaring a variable is simple:

var some_variable_name: int

Here we defined a variable called some_variable_name which has the type int, meaning it can hold an integer value. The type of the variable is described after a colon (:).

Assigning a value to a variable

Declaring a variable on its own is fairly useless without actually assigning a value to that variable. You can assign a value to a variable by saying that a variable equals some value:

var meaning_of_life_the_universe_and_everything = 42

Here we defined a variable called meaning_of_life_the_universe_and_everything and assigned the value 42 to it.

Note: We didn't define a type for the variable! The Nim compiler is smart here and knows that you're assigning an integer value to the variable, so the variable must be an int type! This can save on some typing, but you can still include the type should you want to be extra explicit:

var meaning_of_life_the_universe_and_everything: int = 42

Note: Variables that are declared but not yet assigned to have a default value. The value depends on the type of the variable, but int types will default to 0, boolean types to false and string types to "". The default values for the different primitive types will be described later on in this section.

You can also assign a value to a variable after declaring it, rather than doing both at once:

var hello: string
hello = "World!"

Variable mutability

Nim actually has two ways of declaring variables. The type we've already covered makes use of the var keyword. These types of variables are mutable variables - they can be assigned to multiple times. However, there are also immutable variables which cannot be overwritten or re-assigned to after initial assignment. Immutable variables are defined using the let keyword:

let meaning_of_life_the_universe_and_everything = 42

Making use of immutable variables with let can make sure you don't accidentally overwrite a variable that should not change. This can help prevent you from making basic mistakes in your programs and should be used whenever possible.

Declaring multiple variables

Nim also allows you to assign multiple variables at once, saving typing. You can assign multiple variables on new lines after the var or let keyword, with each line one level of indentation further indented than the original keyword:

var
  a: int
  b: int
  c: int

The same also applies for variables defined using the let keyword:

let
  x: string
  y: string
  z: string

Assigning the same value to multiple variables

You can also declared and assign to multiple variables at once, with all of the declared variables having the same value. The variable names in this case are comma separated:

let life, the_universe, everything = 42

This defines three variables: life, the_universe and everything. Each of these variables has the type int and the value 42. The variables are all immutable (using let, rather than var, though var works the same way).