2

How to do concat the following strings whose length are known at compile time in Zig?

const url = "https://github.com/{}/reponame";
const user = "Himujjal";
const final_url = url + user; // ??
Himujjal
  • 1,581
  • 1
  • 14
  • 16

2 Answers2

10

Array concatenation operator, for two comptime-known strings:

const final_url = "https://github.com/" ++ user ++ "/reponame";

std.fmt.comptimePrint for comptime-known strings and numbers and other formattable things:

const final_url = comptime std.fmt.comptimePrint("https://github.com/{s}/reponame", .{user});

Runtime, with allocation:

const final_url = try std.fmt.allocPrint(alloc, "https://github.com/{s}/reponame", .{user});
defer alloc.free(final_url);

Runtime, no allocation, with a comptime-known maximum length:

var buffer = [_]u8{undefined} ** 100;
const printed = try std.fmt.bufPrint(&buffer, "https://github.com/{s}/reponame", .{user});
pfg
  • 2,348
  • 1
  • 16
  • 37
1

Its an easy thing to do. Lack of research produced this question. But for anyone wondering.

const final_url = "https://github.com/" ++ user ++ "/reponame";

For more information go through: comptime in zig.

Himujjal
  • 1,581
  • 1
  • 14
  • 16