Basics
Nim echo
Printing with echo
Nim echo outputs to console with string formatting.
Introduction to Nim's echo
The echo
command in Nim is used to output strings and other data types to the console. It is a fundamental feature for displaying information, debugging, and logging. One of the powerful aspects of echo
is its ability to handle string formatting, allowing developers to create well-structured output easily.
Basic Usage of echo
Using echo
is straightforward. You can print strings directly or combine multiple strings and variables in a single statement. Here's a simple example:
In the example above, the string "Hello, Nim!"
is printed directly to the console.
Combining Strings and Variables
Nim's echo
can concatenate strings and variables effortlessly. You can use it to display values stored in variables along with text:
In this example, the variable name
is combined with other strings to produce the output Hello, World!
.
Using echo with Different Data Types
The echo
command can handle various data types seamlessly, converting them to strings as needed. Here's how you can work with integers, floats, and more:
In this case, echo
prints both integer and float values by automatically converting them to strings, resulting in outputs like Age: 30 years
and Height: 1.75 meters
.
Advanced String Formatting
For more sophisticated string formatting, Nim supports the use of format specifiers within echo
. This allows you to control the appearance of numerical data and strings. Here's an example:
In this example, the float pi
is formatted to show only two decimal places, demonstrating the power of format specifiers in Nim.
Conclusion
The echo
command is a versatile tool in Nim for outputting data to the console. Whether you're printing simple strings, combining variables, or formatting complex data types, echo
provides the functionality you need to create clear and informative console outputs.