Basics

Nim Running Code

Running Nim Code

Nim code runs via nim c or nim r with .nim files.

Introduction to Running Nim Code

Nim is a versatile and efficient programming language that can be compiled to C, C++, or JavaScript. Running Nim code involves compiling it first using the nim c command or running it directly with the nim r command. In this tutorial, we will explore both methods to execute Nim files.

Using the nim c Command

The nim c command is used to compile a Nim source file into an executable. This is the most common way to run Nim programs when you want to distribute or deploy the executable.

To compile a Nim file named example.nim, use the following command in your terminal:

This command compiles the example.nim file and immediately runs the resulting executable. The -r flag is a convenient addition that saves you from having to run the executable separately.

Using the nim r Command

The nim r command is a shortcut that compiles and runs the Nim code in one step. It is particularly useful during development when you need to test your code frequently.

To run a Nim file directly, use:

This command compiles the example.nim file and executes it without leaving an intermediate executable file. The nim r command is ideal for quick testing and development purposes.

Conclusion

Both nim c and nim r offer efficient ways to run your Nim code. Use nim c when you need a standalone executable and nim r for rapid development cycles. Understanding these commands will help streamline your Nim development process.