#what does []const u8 mean?

1 messages · Page 1 of 1 (latest)

lapis grotto
#

I am confused by the type []const u8, from the official document the statements regarding const is few. []const u8 is a slice, and is a ptr and length,but what's the diff from []u8. the length is const or the ptr is const or both. another point is each element of slice is const u8, but what is const u8,it is a type or else. I know what is const identifier and pointer, but confused by the type with const.

except the const in the slice type, any other usage?

prisma leaf
#

const is a modification of the pointer, it means that the values pointed to are const

#

[]u8 is a slice of mutable u8s (you can change them)
[]const u8 is a slice of constant u8s (you can't change them)

#

the ptr and length may or may not be mutable depending on whether you store the slice as a var or const

fiery shadow
#
var var_const: []const u8 = ...;
var_const = other;
var_const.ptr = other_ptr;
var_const.len = other_len;
// var_const[i] = 0;

const const_mut: []u8 = ...;
// const_mut = other;
// const_mut.ptr = other_ptr;
// const_mut.len = other_len;
const_mut[i] = 0;
#

And then mix and match

#

Btw, to respond to another part of the question: const u8 isn't a type that exists. See, the way it works is that const and similar qualifiers aren't attributes of the type, but rather attributes of the memory representing the type

#

It's sort of like the difference between describing a category vs an instance of that category. It isn't correct to say "human eyes are blue", but it would be correct to say "these humans in particular have blue eyes"