Formatting curly braces

Hello,
I need some guidance regarding formatting curly braces in Zig. here’s my code:

                            const search = try allocPrint(
                                self.allocator,
                                "${{s}}",
                                .{clean_var_name},
                            );

What I’m trying to do is to format the string like ${{s}} => ${some value}, but the compiler is telling me:

/archive/ARCHIVE/zig-bootstrap-0.10.1/zig-bootstrap-0.10.1/out/host/lib/zig/std/fmt.zig:201:18: error: unused argument in '${{s}}'
            1 => @compileError("unused argument in '" ++ fmt ++ "'"),
                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
referenced by:
    count__anon_8427: /archive/ARCHIVE/zig-bootstrap-0.10.1/zig-bootstrap-0.10.1/out/host/lib/zig/std/fmt.zig:1934:5
    allocPrint__anon_5847: /archive/ARCHIVE/zig-bootstrap-0.10.1/zig-bootstrap-0.10.1/out/host/lib/zig/std/fmt.zig:1941:35
    remaining reference traces hidden; use '-freference-trace' to see all reference traces

/archive/ARCHIVE/zig-bootstrap-0.10.1/zig-bootstrap-0.10.1/out/host/lib/zig/std/fmt.zig:201:18: error: unused argument in '${{s}}'
            1 => @compileError("unused argument in '" ++ fmt ++ "'"),
                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: test...
error: The following command exited with error code 1:
/archive/ARCHIVE/zig-bootstrap-0.10.1/zig-bootstrap-0.10.1/out/host/bin/zig test /archive/ARCHIVE/sources/zeb/src/server.zig -lc --cache-dir /archive/ARCHIVE/sources/zeb/zig-cache --global-cache-dir /home/kamil/.cache/zig --name test --pkg-begin network /archive/ARCHIVE/sources/zeb/zig-network/network.zig --pkg-end --enable-cache
error: the following build command failed with exit code 1:
/archive/ARCHIVE/sources/zeb/zig-cache/o/39542eb356c7023c26f8f9fb9a9fad05/build /archive/ARCHIVE/zig-bootstrap-0.10.1/zig-bootstrap-0.10.1/out/host/bin/zig /archive/ARCHIVE/sources/zeb /archive/ARCHIVE/sources/zeb/zig-cache /home/kamil/.cache/zig test

Any help will be appreciated

Thanks!

To escape a literal { you have to double it, so in your case it would be "${{{s}}}" ; where the outer double curly braces are the literal braces that will show up in the output, and the inner curly braces around the s are part of the format specifier.

5 Likes