打印 Nim 地址的简单语法

Simple syntax to print out Nim address

如何打印出Nim地址 就像在 C:

 int array[] = { 7, 8, 9 };
 printf(" %p ", (void *)&array);

?尝试:

 var
   arr = newSeq[array[2,int]](2)
   refVar = addr arr

 echo refVar

给了:

Error: type mismatch: got <ptr seq[array[0..1, int]]>
but expected one of: 
proc echo(x: varargs[typed, `$`])
  first type mismatch at position: 1
  required type for x: varargs[typed]
  but expression 'refVar' is of type: ptr seq[array[0..1, int]

请大家帮忙!

使用repr获取一个值的字符串表示,该值还包含内存地址:

var
  arr = newSeq[array[2,int]](2)
  refVar = addr arr

echo arr.repr  # 0x7f5c2fcbd050@[[0, 0], [0, 0]]
echo refVar.repr  # ptr 0x564bd37d7528 --> 0x7f5c2fcbd050@[[0, 0], [0, 0]]

如果你只想要内存地址(而不是 repr 给你的其他东西),最简单的方法是:

echo cast[int](arr.addr).toHex