#Getting a []const u8 from a Struct with format defined?

1 messages · Page 1 of 1 (latest)

stable coral
#
std.debug.print("{}\n", .{snippet});

I was wondering if we could get this into a []const u8 - or if I need to define a separate method to do this

strong folio
#

use an ArrayLists’s writer’s print method

#
var arr = ArrayList(u8).init(alloc);
const writer = arr.writer();
try writer.print(“{}\n”, {thing});
stable coral
#

I used printAlloc is there benefits to doing ArrayList instead?

strong folio
#

oh no thats probably better lol, forgot about that

unique escarp
#

If you have many prints that should end up in a single slice ArrayList is the way

mortal juniper
#

Also, printAlloc runs format twice (the first one is to figure out the exact required allocation size), so if your format method is expensive it could be worth considering the ArrayList approach

sudden vault
#

maybe that's slower somehow than running format twice 🤔
i doubt that

brittle fossil
#

id guess so its only 1 allocation
not sure thats a great reason tho

sudden vault
#

im gonna benchmark perf :P

sudden vault
#

there was infact a reason

#

i still dont like that second format >:C

brittle fossil
#

30ns?

#

for allocating and formatting twice?

sudden vault
#

it's not right i think

#

gimme a sec

brittle fossil
#

i also think

sudden vault
#

ah yes chat

#

i am silly and forgot doNotOptimizeAway