Catalog / Nim Programming Language Cheatsheet
Nim Programming Language Cheatsheet
A concise reference for the Nim programming language, covering syntax, data types, control flow, and advanced features for quick lookup and efficient coding.
Basic Syntax and Data Types
Fundamental Syntax
Example:
|
Example:
|
Example:
|
Basic Data Types
|
Integer (platform-dependent size). Example: |
|
Signed integers of specific sizes. Example: |
|
Unsigned integer (platform-dependent size). Example: |
|
Unsigned integers of specific sizes. Example: |
|
Floating-point numbers. Example: |
|
Boolean (true or false). Example: |
|
Single character. Example: |
|
Sequence of characters. Example: |
String Literals
Example:
|
Control Flow and Procedures
Conditional Statements
Example:
|
Example:
|
Loops
|
Iterating over a range or collection.
|
|
Looping while a condition is true.
|
|
Exits the current loop.
|
|
Skips the current iteration and continues with the next.
|
Procedures
Syntax:
|
Parameters can have default values:
|
Discarding return values:
|
Collections and Data Structures
Arrays and Sequences
Example:
|
Array access:
|
Sequence length:
|
Tuples and Objects
|
Collection of named fields with different types.
|
|
User-defined type with fields and methods (similar to classes in other languages).
|
Sets and Dictionaries
Example:
|
Advanced Features
Generics
Generic procedures and types allow you to write code that works with multiple types. Example:
|
Metaprogramming
|
Evaluate code at compile time. Useful for generating code or performing calculations at compile time.
|
|
Code generation mechanism. Templates are expanded at compile time.
|
|
More powerful than templates. Macros can manipulate the abstract syntax tree (AST) of the code.
|
Error Handling
Example:
|
Example:
|