#array literal requires address-of operator (&) to coerce to slice type '[]u8'

1 messages · Page 1 of 1 (latest)

violet stump
#

i have a struct that contains a field []u8. when i try to init it by using [_]u8{...} i get this error

eternal fable
#

it's like the error says: you have an array literal, to make it a slice you have to prefix it with &

hexed fox
#

if the field is of type []u8, that still won't work; did you meant to use []const u8?

violet stump
#

tried with const too

sacred zealot
#

This error has confused quite a few people over the past couple of years, and should probably read more like:

error: array literal cannot coerce into slice type
note: use '&[_]u8{}' or '&.{}' if you mean to make a slice.
eternal fable
#

using const and & should work

#

and yeah that error message is a much clearer explanation

sacred zealot
#

It will also work with a mutable slice type, if the array literal has no elements.