When you allocate the string using `create`, it is initialized with zeroes. So
its capacity and its length are null as if it was assigned `""`. Then, if you
assign globally the string, it works, but not if you assign each element
individually.
But this works:
# test.nim
proc main =
let str = "hello"
var sptr = string.create(str.len)
#copyMem(sptr, unsafeAddr str, str.len) # this works
#sptr[] = str # this also works
# but let's try manual copy
for i in 0 ..< str.len:
sptr[].add(str[i])
echo sptr[]
when isMainModule:
main()
Run
and this also works:
# test.nim
proc main =
let str = "hello"
var sptr = string.create(str.len)
#copyMem(sptr, unsafeAddr str, str.len) # this works
#sptr[] = str # this also works
# but let's try manual copy
sptr[].setLen(str.len)
for i in 0 ..< str.len:
sptr[][i] = str[i]
echo sptr[]
when isMainModule:
main()
Run