Skip to content
Discussion options

You must be logged in to vote

在 ziggit 上已经有人回复了,答案也很简单:

var line: []const u8 = "foo";

The reason why string literals are not slices by default is that a pointer to an array preserves more compile-time information than a slice because a slice has a run-time known length.

之前主要是搞混淆了 const 的位置,

  • var line: []const u8 表示 line 可以改变指向
  • const line: [] u8 表示 line 不可以改变指向,但是可以改变指向值的内容

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by jiacai2050
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant