How to create a memory region at compile time and have part of it embedded in the program binary?
ziggit.dev·2d
Rust MaybeUninit
Preview
Report Post

January 9, 2026, 5:15pm 1

just a case


fn do() void {
const str = comptime str: {
errdefer unreachable;
var buf: [256]u8 = undefined;
const result = try std.fmt.bufPrint(&buf, "{s}", .{"xxx"}); // edit memory region
break :str result;
};
std.debug.print("{s}\n", .{str});
}

but compile failed

error: runtime value contains reference to comptime var
std.debug.print("{s}\n", .{str});

UPDATE: This is just a simple example to get the code running—don’t worry about how to print strings at compile time.

I’ve found a way that works. Is this the right thing to do?

fn do() void {
const str = comptime str: {
errdefer unreachable;
var buf: [256]u8 = undefined;
const buf_str = try std.fmt.bufPrint(&buf, "{s}", .{"xxx"});
var result: [buf_str.len]u8 = undefined;
std.mem....

Similar Posts

Loading similar posts...

Keyboard Shortcuts

Navigation
Next / previous item
j/k
Open post
oorEnter
Preview post
v
Post Actions
Love post
a
Like post
l
Dislike post
d
Undo reaction
u
Recommendations
Add interest / feed
Enter
Not interested
x
Go to
Home
gh
Interests
gi
Feeds
gf
Likes
gl
History
gy
Changelog
gc
Settings
gs
Browse
gb
Search
/
General
Show this help
?
Submit feedback
!
Close modal / unfocus
Esc

Press ? anytime to show this help