Questions tagged [zig]

Zig is an open-source programming language designed for robustness, optimality, and clarity.

Zig is a general-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.

Website:

Documentation:

Zig has many features for low-level programming, notably: packed structs (structs without padding between fields), arbitrary width integers and multiple pointer types. Zig has no hidden control flow, no hidden memory alocation, no macros (but therefor compile time code execution)

212 questions
15
votes
4 answers

How to read a file in zig?

How can I read a file in zig, and run over it line by line? I did found os.File.openRead, but it seems old cause it says that container 'std.os' has no member called 'File'.
clankill3r
  • 9,146
  • 20
  • 70
  • 126
15
votes
1 answer

struct definition with var instead of const in zig language

I am now learning zig language. I have seen definitions of structs with const keyword like const X = struct { n: i32, }; My understanding is that const is a kind of complementary to var, the latter allows change, the former does not. But what…
11
votes
1 answer

In Zig, how do you tell if a frame represents a fully executed function?

In Zig (currently using 0.7.1), suppose for some reason you don't have any good way to design code which always has exactly one resume for every suspend. Is there any supported way to detect at runtime that a given frame has or has not been executed…
Hans Musgrave
  • 6,613
  • 1
  • 18
  • 37
10
votes
1 answer

Why is infix const required for arrays of strings?

I am learning zig slowly, but I don't understand const and how it interacts with arrays/types - I'm going through https://ziglang.org/documentation/0.6.0/#Introduction but they use const a lot for strings. This compiles: var n = [_][]const u8…
Neal Fultz
  • 9,282
  • 1
  • 39
  • 60
10
votes
2 answers

Generation of types in zig (zig language)

Is it possible to create a comptime function in zig that would generate a new struct type? The function would receive an array of strings and an array of types. The strings are the names of subsequent struct fields.
az5112
  • 590
  • 2
  • 11
9
votes
2 answers

How do I include one .zig file from another .zig file

Just exploring Zig... I have one .zig file with a bunch of comptime functions and constants, and I want to use those in other .zig programs. Equivalent to #include "my.h" in C.
Dave Mason
  • 669
  • 6
  • 15
8
votes
1 answer

How to incorporate the C++ standard library into a Zig program?

In reading the documentation for zig, I was under the impression that zig could compile both C and C++ code. Consequently, I thought you could import a C++ file's header via @cImport and have zig build succeed. However, I can't seem to get this to…
8
votes
2 answers

How to import zig modules dynamically?

I'm using zig 0.7.0. and I'm trying to import a list of zig source files from an array. Each source file has a main function (whose return type is !void) that I would like to call. The array module_names is known at compile time. Here is what I…
jackdbd
  • 4,583
  • 3
  • 26
  • 36
7
votes
2 answers

Current Way to Get User Input in Zig

I'm following this blog post on 'comptime' in Zig. The following line no longer compiles in Zig 0.6.0. const user_input = try io.readLineSlice(buf[0..]); Below is the full function: fn ask_user() !i64 { var buf: [10]u8 = undefined; …
user3475861
  • 73
  • 1
  • 4
5
votes
1 answer

How do I mutate a Zig function argument?

I have discovered that Zig function parameters are constant. That means my naive function for freeing a HashMap doesn't work. You can see an example of the code here. I am wondering if the most correct Zig way is to pass dict as a function or if…
Erik Engheim
  • 8,182
  • 4
  • 36
  • 51
5
votes
1 answer

How to pass a C string into a Zig function expecting a Zig string?

Trying to use a Zig library expecting a string... but I get a string buffer from a C library. That means that I need to pass a value of type [*c]u8 to a function that accepts [:0]const u8. How to do that? I found this way so far: const buffer:…
Renato
  • 12,940
  • 3
  • 54
  • 85
5
votes
2 answers

zig structs, pointers, field access

I was trying to implement vector algebra with generic algorithms and ended up playing with iterators. I have found two examples of not obvious and unexpected behaviour: if I have pointer p to a struct (instance) with field fi, I can access the…
osetr
  • 55
  • 1
  • 5
5
votes
1 answer

zig creates a C library but not usable by C

I'm able to get Zig to create a C library but when I attempt to use said library from a C program, it fails to find the definition of the included function. My library definition: const std = @import("std"); export fn removeAll(name: [*]const u8,…
dangeroushobo
  • 1,291
  • 2
  • 16
  • 28
4
votes
0 answers

Using LLVM-based tools (clang, Rust, Zig) to compile for N64

I’m trying to use the LLVM-based zig cc to compile C code (and eventually other LLVM-based languages like Rust/Zig) for the R4300i / VR4300 MIPS CPU, which is the processor in the N64. But I don't see any evidence or examples of compiling for VR4300…
CrepeGoat
  • 2,315
  • 20
  • 24
4
votes
3 answers

are dynamic strings possible in zig?

I'm just getting started with Zig and come from C++ and Rust; I've been struck early with a difficult issue that I cannot seem to solve. Or find anywhere on the internet. This is what I have: // this doesn't work pub const User = struct { bot: …
gavin
  • 1,224
  • 5
  • 17
1
2 3
14 15