#string literal defaults to sentinel

1 messages · Page 1 of 1 (latest)

hollow blaze
#

why does @as(*const []u8, "test"); error

winter fern
#

slices are already pointers so that type is a pointer to a pointer

hollow blaze
#

@as([]u8, "test"); also fails

wet zealot
#

string literals are immutable

#

[]const u8 should work

hollow blaze
#

it says type of "test" is *const [4:0]u8

#

but isnt that constant?

wet zealot
#

that can coerce to [:0]const u8 or []const u8

wet zealot
hollow blaze
#

var string: []u8 = "test"; doesnt work either, sorry for this nooby questions

#

i cant seem to get my head around it

wet zealot
#

"test" is a pointer to immutable data stored in your program's executable

hollow blaze
#

oh. that makes sense

wet zealot
#

you can't turn it into any non-const pointer type because the data itself cannot be modified

#

what you could do is var string = "test".* which will copy the array that "test" points to into a different one which is mutable

#

string there will be a [4:0]u8 (not a pointer)

hollow blaze
#

im trying to right a parser and this is the function

pub fn parse(ally: Allocator, string: *const []u8) !Sequence {...}```
#

*const []u8 gives me information to the length and ptr, so why cant i use it?

wet zealot
#

*const []u8 is a constant pointer to a mutable string

#

like a double pointer cuz the string itself is a pointer

#

if you want an immutable string just use []const u8

hollow blaze
#

and when i use []const u8 as a parameter the compiler wants everything to become comptime, so im guessing it is the case

native atlas
wet zealot
#

[]T (slice) is pointer, [n]T (array) is value

hollow blaze
#

ah so that was it, thats what was confusing me thank you,

hollow blaze