# Composing a string during run time

**URL:** <https://ziggit.dev/t/composing-a-string-during-run-time/17327>\
**Category:** Help\
**Tags:** language, comptime\
**Created:** [August 22, 2026, 7:18am UTC](https://ziggit.dev/t/composing-a-string-during-run-time/17327 "2026-08-22T07:18:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gDator](https://ziggit.dev/user_avatar/ziggit.dev/gdator/32/5732_2.png) [@gDator](https://ziggit.dev/u/gDator)\
**Post date:** [August 22, 2026, 7:18am UTC](https://ziggit.dev/t/composing-a-string-during-run-time/17327/1 "2026-08-22T07:18:35Z")

</div>

I tried composing a string during comptime. The string is dependent on a type given. Currently i m trying somthing like this:

```zig
            const size: usize = str: {
                comptime {
                    var length: usize = calculateLength();
                    break :str length;
                }
            };
            comptime var string: [size]u8 = undefined;
            const actual_length = len: {
                comptime {
                    @memset(string[0..], 0);
                    var string_builder: StringBuilder = .{ .buffer = string[0..] };
                    _ = string_builder.addSlice(create).addSlice(table_name).addSlice("(").addSlice(create_2);
                    buildStringFields(&string_builder, .{ .include_id = false, .include_type = true });
                    _ = string_builder.addSlice(")");
                    break :len string_builder.position;
                }
            };
            @compileLog(string);
            someFunction(string[0..actual_length);

```

The compileLog gives me the correct result. But the compiler gives an error if im using the variable:

```zig
src/root.zig:135:36: error: runtime value contains reference to comptime var
           someFunction(string[0..actual_length]);

```

Is it even possible to compose a string like this (or differntly)?  
Thanks in advance

---

<div class="post-metadata">

**Author:** ![vulpesx](https://ziggit.dev/user_avatar/ziggit.dev/vulpesx/32/3989_2.png) [@vulpesx](https://ziggit.dev/u/vulpesx)\
**Post date:** [August 22, 2026, 7:22am UTC](https://ziggit.dev/t/composing-a-string-during-run-time/17327/2 "2026-08-22T07:22:33Z")

</div>

`var`’s cannot be used across the comptime-runtime boundary. Just make a `const` copy, that will be able to be used at runtime.

---

<div class="post-metadata">

**Author:** ![gDator](https://ziggit.dev/user_avatar/ziggit.dev/gdator/32/5732_2.png) [@gDator](https://ziggit.dev/u/gDator)\
**Post date:** [August 22, 2026, 10:25am UTC](https://ziggit.dev/t/composing-a-string-during-run-time/17327/3 "2026-08-22T10:25:32Z")

</div>

Thanks for your answer. Do you know how exactly? Adding the following results in the same error:

```zig
const result: [size]u8 = undefined;
@memcpy(@constCast(result[0..string.len]), string);

```

---

<div class="post-metadata">

**Author:** ![vulpesx](https://ziggit.dev/user_avatar/ziggit.dev/vulpesx/32/3989_2.png) [@vulpesx](https://ziggit.dev/u/vulpesx)\
**Post date:** [August 22, 2026, 10:49am UTC](https://ziggit.dev/t/composing-a-string-during-run-time/17327/4 "2026-08-22T10:49:16Z")

</div>

Arrays are value types, i.e you copy them the same way you would an integer

```zig
const copy = string;

```

---

<div class="post-metadata">

**Author:** ![castholm](https://ziggit.dev/user_avatar/ziggit.dev/castholm/32/1330_2.png) [@castholm](https://ziggit.dev/u/castholm)\
**Post date:** [August 22, 2026, 5:12pm UTC](https://ziggit.dev/t/composing-a-string-during-run-time/17327/5 "2026-08-22T17:12:27Z")

</div>

You can also use `&(buf ++ .{})` or `&buf ++ ""` as an alternative to making an explicit copy assignment to a second `const`, e.g.:

```zig
const string: []const u8 = comptime string: {
    var buf: [26]u8 = undefined;
    for (&buf, 0..) |*c, i| {
        c.* = 'A' + i;
    }
    break :string &(buf ++ .{});
};

```
