Strings and Runes in Nim
This Nim code demonstrates working with strings and runes, which are similar to Go’s concept of strings and runes. Here’s a breakdown of the code:
We import the
unicode
module, which provides utilities for working with Unicode strings.The
examineRune
procedure is defined to demonstrate passing a rune value to a procedure.In the
main
procedure:- We define a constant string
s
containing Thai characters. - We print the length of the string, which gives the number of bytes.
- We iterate over the string’s bytes and print their hexadecimal values.
- We use
runeLen
to count the number of runes in the string. - We use a
for
loop with therunes
iterator to iterate over each rune in the string. - We demonstrate manual iteration over runes using
runeAt
.
- We define a constant string
The
examineRune
procedure shows how to compare rune values.
Nim strings are similar to Go strings in that they are sequences of bytes. However, Nim provides utilities in the unicode
module to work with Unicode characters (runes).
To run this program, save it as strings_and_runes.nim
and use the Nim compiler:
This will compile and run the program, displaying output similar to the Go version, demonstrating how Nim handles Unicode strings and runes.