Skip to main content
Filter by
Sorted by
Tagged with
20 votes
5 answers
29k views

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's user avatar
  • 9,411
20 votes
1 answer
5k views

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 ...
HiFile.app - best file manager's user avatar
15 votes
1 answer
6k views

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 ...
Neal Fultz's user avatar
  • 9,617
13 votes
2 answers
3k views

Generation of types in zig

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 ...
az5112's user avatar
  • 630
13 votes
3 answers
6k views

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's user avatar
13 votes
1 answer
8k views

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 ...
Ambiguous Illumination's user avatar
12 votes
2 answers
12k views

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() !...
user3475861's user avatar
12 votes
1 answer
655 views

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 ...
Hans Musgrave's user avatar
9 votes
1 answer
7k views

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 ...
Renato's user avatar
  • 13.5k
9 votes
1 answer
4k views

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 ...
Erik Engheim's user avatar
  • 8,442
9 votes
1 answer
4k views

Mutating a value in an array list in Zig

Noob question: I want to mutate a value that exists in an array list. I initially tried to just grab the indexed item and directly change its field value. const Foo = struct { const Self = @...
aqui8's user avatar
  • 560
9 votes
2 answers
2k views

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 ...
jackdbd's user avatar
  • 4,953
8 votes
2 answers
7k views

How to concat two string literals at compile time in Zig?

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 + ...
Himujjal's user avatar
  • 2,069
8 votes
1 answer
3k views

How to create an empty slice of slices

How do I construct []const []const u8 without using an allocator? I can do var slice: []const []const u8 = undefined; slice.len = 0; // use slice But there surely must be a better way.
sigod's user avatar
  • 5,808
8 votes
2 answers
7k views

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 ...
osetr's user avatar
  • 85
8 votes
1 answer
3k views

How can I pass a Zig String literal to C

A Zig String literal is a single-item pointer to a null-terminated byte array, which is perfect to be used as a C's char * String! However, when I try to use this simple C function from Zig: int ...
Renato's user avatar
  • 13.5k
7 votes
1 answer
4k views

zig print float precision

In zig it is possible to print float values in decimal notation by using "{d}". This will automatically print the value at full precision. Is there way to specify the number of digits? ...
B_old's user avatar
  • 1,231
7 votes
1 answer
5k views

How do I split a string in zig by a specific character?

I have a simple sentence in a string. I want to print each word on a new line or otherwise just do some calculation on each word? Is there something similar to python's "Hello World".split() ...
Stanislav Nikolov's user avatar
7 votes
1 answer
3k views

Can I pass commandline arguments when invoking "zig build run"?

I'm just starting with the new programming language Zig and finding documentation pretty sparse. I can build and run the current project by invoking zig build run. I can also do zig run src/main.zig ...
hippietrail's user avatar
  • 16.7k
7 votes
3 answers
5k views

Looping over an integer range in Zig

Is a while-loop like this the idiomatic way to loop over an integer range in Zig? var i: i32 = 5; while (i<10): (i+=1) { std.debug.print("{}\n", .{i}); } I first tried the python-...
xpqz's user avatar
  • 3,697
7 votes
1 answer
2k views

Formating strings with comptime values in Zig

I am trying to create a polymorphic function that accepts only built-in vectors (as in @Vector) of length 4. From what I understand, the length of vectors are comptime known, and I would like to add ...
loonatick's user avatar
  • 1,081
7 votes
1 answer
3k views

Declaring pointer to function type, with a function name

I found a bit strange feature of Zig when I played with function pointers. Here is a simple example of a function pointer type: const std = @import("std"); const print = std.debug.print; ...
dee0xeed's user avatar
  • 105
6 votes
2 answers
5k views

How to pass Zig function pointers to other functions?

I have the following error when I pass in a function to another function? const std = @import("std"); const St = struct { a: usize }; fn returnFunc(print: fn (str: []const u8, st: St) void)...
Himujjal's user avatar
  • 2,069
6 votes
1 answer
2k views

Create a struct with String parameter

I just want to create a struct with variable String (utf-8 text). const Person = struct { name: [_]u8, }; Is it possible? Or I have to set maximum length of string (e.g. name: [255]u8;)? When I ...
somenxavier's user avatar
  • 1,449
6 votes
1 answer
5k views

Malloc to a list of struct in Zig?

How can I dynamically allocate a memory space and get a pointer to a list of structs in Zig. Like in C : struct Foo* my_array_of_foo = (struct Foo*) malloc(10*sizeof(Foo));
the duck's user avatar
  • 435
6 votes
1 answer
1k views

Convert [*]u8 to []u8 in Zig

When I have a [*]u8 pointer and a usize length, how do I convert the pointer to a []u8 slice with the specified length?
Dull Bananas's user avatar
  • 1,032
6 votes
1 answer
760 views

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(&...
dangeroushobo's user avatar
6 votes
2 answers
1k views

Check if element exists in zig array

Is there a more idiomatic way of finding if some value is contained in an array than fn valueInArray(value: u32, array: []const u32) bool { for (array) |num| { if (value == num) { ...
Simon Berens's user avatar
6 votes
1 answer
2k views

Convert i32 into f32

How can I convert a i32 into a f32 in Zig Language? I want to count appearances of values in a for loop and afterwards get the percentages in a smooth floating number. var partial : i32 = 0; var total ...
Marian Klösler's user avatar
6 votes
1 answer
2k views

How do I get the full path of a `std.fs.Dir`?

Is there any way to access the full path of a std.fs.Dir struct? I've looked through all of the methods in the source but I can't find anything that gets path-related information on the directory.
sno2's user avatar
  • 4,105
6 votes
2 answers
3k views

Zig: How to handle an optional error union type? (e.g., ?std.process.NextError![:0]u8)

Context Very new to the language, so bear with me. I'm writing a super basic function to print out the command line arguments passed to the program. Here is the critical logic: // already created ...
j_kennon's user avatar
6 votes
1 answer
4k views

How to format strings in Zig using a runtime format string

I'm a total noob to learning Zig so my question may be naïve. I'm trying to create a variety of strings in my code using the string formatting functions in the standard library such as std.fmt....
hippietrail's user avatar
  • 16.7k
5 votes
1 answer
3k views

How do I print a UTF-16 string in Zig?

I've been trying to code a UTF-16 string structure, and although the standard library provides a unicode module, it doesn't seem to provide a way to print out a slice of u16. I've tried this: const ...
Sapphire_Brick's user avatar
5 votes
2 answers
3k views

How do I pass a stream or writer parameter to a function in Zig?

I'm trying to pass the output stream to a function but can't get it right. This sample code shows a couple of the things I've tried // Attempts to pass stream or writer to a function const std = @...
Adrian Savage's user avatar
5 votes
4 answers
5k views

How can you create a buffer of the same size as a file?

I would like to avoid making a set size buffer because of things like a file being too big, or small enough that there is empty space in the buffer. An ArenaAllocator sounds promising since you can ...
dxbednarczyk's user avatar
5 votes
1 answer
8k views

How can I parse an integer from a string in Zig?

What is the best way to parse an integer from a string in Zig and specify the resulting integer type? const foo = "22"; How would I convert foo to an i32, for example?
JPlusPlus's user avatar
  • 442
5 votes
1 answer
2k views

How to do generics in a struct field in zig

I'm very new to zig and I'm wondering how to create a struct field that can have a compile time known type for example something similar to the comptime keyword when used with a function parameter, I ...
Mhhany's user avatar
  • 301
5 votes
2 answers
2k views

How to use the zig compiler in order to compile nim code?

Nim turns its own code into C code and compiles that using C-compilers. Zig has its own compiler that has many nice features that would make you want to use it, such as allowing you to choose which ...
Philipp Doerner's user avatar
5 votes
1 answer
2k views

Search ArrayList of Structs in zig

I expect this is a question with a very simple answer about how to do this well in zig. I want to search an ArrayList of some struct to find a record by one of the fields. In C++ I would consider ...
jcoder's user avatar
  • 29.9k
5 votes
1 answer
4k views

How to include (msvc) libc when building c code with the Zig compiler

I've recently discovered zig and find it very interesting. I'm now trying to learn how to use zig as a cross compiler and, the following builds and runs fine (on Windows) zig cc -Wno-everything src/...
JacobP's user avatar
  • 601
5 votes
4 answers
3k views

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. I would like to know if it's possible to have dynamic strings ...
gavin's user avatar
  • 1,334
5 votes
2 answers
4k views

zig link: how to link against shared (system) libraries?

I want zig to link in shared (dynamic) system libraries (like pthread). No matter what I try, Zig links in static libs. Now, generally I prefer that, but not in this case. How/where do I communicate ...
ecwdw 23e3e23e's user avatar
5 votes
1 answer
486 views

What is the type of `std.io.getStdOut().writer();` in zig?

I cannot initialize a global variable with the stdout in zig: var out = std.io.getStdOut().writer(); Then I tried to initialize in the fn main and declare the global variables as optional (...
Emilio Platzer's user avatar
5 votes
0 answers
452 views

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 ...
CrepeGoat's user avatar
  • 2,485
5 votes
2 answers
802 views

How can I utilize SIMD in Zig explicitly?

After reading the Zig language reference, I found that the vector section states that a @Vector will use SIMD instructions if possible. There is an example following up as const a = @Vector(4, i32){ 1,...
YuChan Tai's user avatar
4 votes
2 answers
2k views

How to change the local cache directory for the zig build system?

Motivation: my source files are on a network (samba) share. zig build fails with error: Unexpected . To overcome it (otherwise I have to delete the ./zig-cache/ folder before every build) and to ...
MKaama's user avatar
  • 1,868
4 votes
1 answer
2k views

Zig "translate c" doesn't translate main function

I created a C file: int main() { return 1; } I used Zig's translate-c command line option to generate a zig file, and I only get some global variable declarations like pub const ...
ice1000's user avatar
  • 6,560
4 votes
1 answer
3k views

How to import zig module from another module?

Assuming following folder structure: src/ lib.zig module_a/ file_a.zig file_a_test.zig module_b/ file_b.zig file_b_test.zig tools/ tools.zig tools_test....
Piotr Tutak's user avatar
4 votes
1 answer
1k views

How to get the field value in comptime in zig

I want to make a deserializer in Zig. Now I could iterate the field information in comptime. But I want to get the field default value in struct. Is there a way to get them? For example, I want to get ...
Hanaasagi's user avatar
  • 349
4 votes
1 answer
2k views

Pointer to array, returning an array in Zig

I need to create an array in some function (having size as an arg) then return it for easy declare in a single clean line. It already causes a lot of questions as: As I know, Zig has no hidden ...
varikoz272's user avatar

1
2 3 4 5
8