{

Learn free nim tutorials


Nim example - Convert String to/from the Int

May 9, 2023 ·  1 min read

This tutorials explains about convert String to int and int to string in Nim language with examples. How to convert String to Int in Nim parseInt in strutils module convert given string to integer. It throws an ValueError if string is invalid number. Here is an example import strutils const value = parseInt("12") echo typeof(value) # int echo value # 12 How to convert Int to String in Nim Multiple ways we can convert int to string in nim....


How to get length of an array and sequence in Nim?

May 8, 2023 ·  1 min read

This tutorial explains about following things Find the size of an array in Nim Find length of an sequence in Nim Array and sequence are data structures used to store(container) collection of elements. Size of these containers contains number of elements in it. How do you find the length of an array in Nim? Please following below steps to find the length of an array. Array contains len procedure to return an size of an array....


How to convert from single character to/from string in Nim?

May 5, 2023 ·  1 min read

This tutorials explains about how to concatenate Strings in Nim language. Nim String Concatenate example There are multiple ways, we do string append in NIM language. using & operator & operator appends one string into end of another string. var str = "one " var str1 = "two " var result= str & str1 echo result using strutils join function strutils module has join procedure that joins the openarray of elements to append Syntax:...


Nim environment variables - read, set, delete, exists, and iterate examples?

May 5, 2023 ·  2 min read

This tutorial explains Environment variables in NIM. How to read environment variable The os module contains the getEnv procedure that returns the environment variable value for a given variable. syntax: if the environment variable is not found, return the default value proc getEnv(key: string; default = ""): string {.....} import os echo getEnv("HOME") HOME is equivalent to $HOME in Linux(Unix) and %HOME% in Windows Another example import os echo getEnv("NIM_HOME","default") It returns either value or default if not found....


How to convert a string into a sequence of characters in Nim?

May 4, 2023 ·  2 min read

This tutorial explains how to convert a String into a Sequence of characters in the Nim Programming language The string is a type that contains a collection of characters, enclosed in double quotes. The sequence is a collection of elements with dynamic in size. Both are different data types and automatic conversion is not required. Convert String into a sequence of characters in Nim with an example Multiple ways we can convert a String into a sequence...


Nim How to get Variable datatype with example?

May 4, 2023 ·  2 min read

This tutorial explains How to find the variable data type in Nim With examples Nim typeof Operator Example typeOf(variable) returns the datatype of a variable Here is an examples int type: var number=12 echo typeOf(number) #int Float types var number1=12.123 echo typeOf(number1) #float64 char type: var character='a' echo typeOf(character) #char String type: var str="name" echo typeOf(str) # string Object type type Employee = object id: int name: string echo typeOf(Employee) # Employee bool var isFlag = false; echo typeOf(isFlag) # bool sequence types let sequence = @['a', 'b', 'c'] echo typeOf(sequence) # seq[char] let numbers = @[1,2,3] echo typeOf(numbers) # seq[int] let numbers1 = @["one", "two"] echo typeOf(numbers1) # seq[string] Array types var arr: array[3, int] = [7, 8, 9,] echo typeof(arr) # array[0....


How to read command line arguments in Nim with examples?

May 3, 2023 ·  1 min read

This tutorial explains how to read command line arguments in NIm code For example, You have hello.nim code, that you want to run the code by passing arguments separated by space. nim compile --run hello.nim arguments How to read command line arguments Multiple ways to read command line arguments in Nim programming language using the os commandLineParams procedure commandLineParams returns the Sequence of a string that passed to the command line Here is an example...


Nim Generate Random Numbers with Examples?

May 3, 2023 ·  1 min read

This tutorial explains Nim Random Generators Nim Random Number Generator Example random is a nim module that provides a procedure to generate Random numbers. It is based on the xoroshiro128 library. Import random library into a code Next, call randomize() method to initialize a random generator. This is required to generate a random number for every call Rand(n) procedure returns the random number between 1 to n. Here is an example...


Nim example - Convert JSOn to/from the object

May 2, 2023 ·  1 min read

This tutorial shows NIM object and json string object conversion examples $$: to serialize Nim Object to JSOn string. to: deserialize json string to Nim Object How to convert Nim Object to JSOn Nim has a marshal module that contains the procedure to serialize and deserialize data from NIM objects. $$ is used to convert Nim Object to JSOn string. Here is an example Create an Employee object Variable created by adding data using the constructor $$variable returns the JSOn string import std/marshal type Employee = object id: int name: string var emp = Employee(id: 1, name: "john") echo($$emp) echo(typeOf($$emp)) Output:...


Nim example - epoch unix timestamp, Local and Utc datetime

May 2, 2023 ·  1 min read

This tutorial explains multiple ways to get Datetime in Local and UTC as well as EPOCH timestamp. How to get the Current Epoch unix timestamp in NIm. times module hasepochTime() proc that returns a number of milliseconds since the 1970 date. It returns a floating number. import std/[times] let currentTimestamp = epochTime() echo currentTimestamp Output 1682999883.740629 How to get Current DateTime in Local and UTC NIm. now() in the times module returns the current DateTime in Local time....


How to read Data from a standard input stream console in Nim?

May 1, 2023 ·  2 min read

Nim language provides a Stdin library to read input data from a user. This tutorial explains multiple ways to read input data from a user and solve the below questions. How to read from the stdin with the nim script? How to get input from the console in Nim? Nim How to read input data from a user? The readLine procedure works with stdin. stdin is a variable system module that refers standard input stream such as a file or console....


NIM http client example| http GET, POST API tutorial?

May 1, 2023 ·  2 min read

This tutorial explains multiple examples of HTTP client examples in NIm HTTP requests are issued synchronously and asynchronously to make API calls. HttpClient is an object in the std/httpclient module to make a synchronous request Nim HTTP GET request example Synchronous GET Request example: First, Import the std/httpclient module create an object of HttpClient i.e client variable call getContent() method on variable print to console import std/httpclient var httpClient = newHttpClient() echo httpClient....


Subscribe
You'll get a notification every time a post gets published here.