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
4
votes
1 answer

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, 2, 3, 4 }; const b = @Vector(4, i32){ 5, 6, 7,…
YuChan Tai
  • 73
  • 3
4
votes
2 answers

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 glibc version to dynamically link against, or…
Philipp Doerner
  • 1,090
  • 7
  • 24
4
votes
0 answers

Declaring pointer to function

I found some (a bit strange) feature of zig when played with function pointers. Here is an example which I beleive is written as implied. const std = @import("std"); const print = std.debug.print; const aFunc = fn (x: i32) void; fn theFunc(x: i32)…
dee0xeed
  • 61
  • 5
4
votes
1 answer

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 count_bytes(const char *str) { int count = 0; …
Renato
  • 12,940
  • 3
  • 54
  • 85
4
votes
1 answer

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
  • 892
  • 7
  • 29
4
votes
2 answers

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 allocator (std.heap.ArenaAllocator) and iterator…
j_kennon
  • 41
  • 2
4
votes
1 answer

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 using std::find_if and a lambda but there doesn't…
jcoder
  • 29,554
  • 19
  • 87
  • 130
4
votes
1 answer

Allocating a generic struct in Zig lang

Is it possible to create allocation for struct that contains type as an attribute ? For example Example struct is const Content = struct { content: type, name: []const u8, }; Then, i would like to allocate memory for example of 2 *…
Tonis
  • 111
  • 5
4
votes
1 answer

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
  • 375
  • 3
  • 13
4
votes
1 answer

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 = @This(); foo: u8, }; pub fn main() anyerror!void…
aqui8
  • 511
  • 3
  • 11
4
votes
1 answer

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/ctest.c however, when I use the build-exe command…
JacobP
  • 561
  • 3
  • 21
3
votes
1 answer

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() in zig? Something like this: var arr =…
3
votes
1 answer

How to refer to a C symbol from Zig which is a keyword?

I want to call the error(3) function from Zig. I can do this by defining a new symbol with a name that is not a Zig keyword: @cInclude("error.h"); @cDefine("_error", "error"); Is this the recommended way to do this, or is there a different way to…
Reuben Thomas
  • 647
  • 6
  • 13
3
votes
4 answers

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 allocate more space as needed. Is there a "proper"…
steamsy
  • 104
  • 1
  • 8
3
votes
0 answers

Why does `error.Foo catch {};` compiles, error.Foo is error, not error union

This code compiles: error.Foo catch {}; But not: error.Foo catch |bar| { std.debug.print("{s}", .{bar}); }; Why is that? Does catch without capturing payload works with error too? I thought catch only works with error union.
Helin Wang
  • 4,002
  • 1
  • 30
  • 34
1
2
3
14 15