Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

String section is wrong; []const u8 is not null terminated #83

Closed
scottredig opened this issue Oct 21, 2024 · 1 comment · Fixed by #86
Closed

String section is wrong; []const u8 is not null terminated #83

scottredig opened this issue Oct 21, 2024 · 1 comment · Fixed by #86

Comments

@scottredig
Copy link

As Zig user who saw a link to this book, I randomly chose the string section. Just reading the first bit, I don't think this is a good introduction to the concept of working with text in Zig, but there's definitely a specific error:

A string object in Zig is an object of type []const u8, and, this object always contains two things: the same null-terminated array of bytes that you would find in a string literal value, plus a length value.

Nothing about the type []const u8 indicates that it will be null terminated. String literals are null terminated, but if that's a trait which is important to you (eg, c interopt), then you should be using a type which is actually null terminated such as [:0]u8.

Also, []const u8 doesn't contain an array, it contains a pointer to bytes. It having a pointer (and not the bytes themselves) is an important bit, but also as these bytes may come from any source (eg, an allocator) the pointer's destination may never have actually been a value of an array type.

@pedropark99
Copy link
Owner

Hey @scottredig ! Thank you for your comment!

About the "[]const u8 is not a null-terminated array of bytes", you are right, this is clearly an error, therefore, I'm going to fix this.

Now, when you say:

I don't think this is a good introduction to the concept of working with text in Zig

Are you saying that the section "How strings work in Zig?" as a hole is not a good introduction? Or maybe you are saying just about the []const u8 thing? Anyway, this is a bit too broad for me to understand what you are thinking. Thus, it would be nice if you describe what specifically is not good, and give a suggestion of improvement maybe.

@pedropark99 pedropark99 linked a pull request Oct 23, 2024 that will close this issue
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants