I didn’t use `create` but reading the documentation it is clear that the
parameter `size`, whose default value is 1, is the number of elements to
allocate. That is, for a string, 8 bytes for a pointer and, under the hood, 8
bytes for the length, 8 bytes for the capacity and 0 bytes for the actual
content as the capacity is null.
So your example is wrong. You have indeed created memory for `str.len` strings,
that is 5 strings. And, as I have said in my previous comment, each string
capacity is null so there is no room to write directly into them. You have
either to make room using `strLen` or to use `add`.
The corrected code will be, for instance:
# test.nim
proc main =
let str = "hello"
var sptr = string.create() # Allocate one string.
sptr[].setLen(str.len) # Make room to store the chars.
for i in 0 ..< str.len:
sptr[][i] = str[i]
echo sptr[]
when isMainModule:
main()
Run