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
1
vote
1 answer

using mem.eql - unable to evaluate constant expression

Can someone explain me why this piece of code can't be compiled? const std = @import("std"); const ParseError = error { NotAValidField }; const TestEnum = enum { field_1, field_2, pub fn fromString(str: []const u8) !TestEnum { …
ArashM
  • 1,379
  • 13
  • 18
1
vote
2 answers

How to initialize variadic function arguments in Zig?

What is the proper way to use and initialise variadic arguments in Zig functions? fn variadicFunc(val: u8, variadicArg: ...u8) { for (variadicArg) |arg| { // ... work on the arg _ = arg; } }
Himujjal
  • 1,581
  • 1
  • 14
  • 16
1
vote
2 answers

Method definition on struct fails with "use of undeclared identifier error"

i am learning zig and i have some simple code where i try to declare a method on a struct and try to call that method here is the code pub fn main() void { const Example = struct { a: i8, b: i8, c: u6 = 5, fn…
Palash Nigam
  • 1,653
  • 3
  • 14
  • 26
1
vote
0 answers

How to produce a armv5 binary by zig builed-exec

Host Here is a running binary in target system readelf -h asterisk ELF Header: Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 Class: ELF32 Data: 2's complement, little endian …
wener
  • 7,191
  • 6
  • 54
  • 78
1
vote
1 answer

Build variable length arguments array for @call

I've recently started learning Zig. As a little project I wanted to implement a small QuickCheck [1] style helper library for writing randomized tests. However, I can't figure out how to write a generic way to call a function with an arbitrary…
fav
  • 13
  • 2
1
vote
2 answers

Non determinism in the output when using ArrayList and Slices

I'm working on Advent of Code with Zig and I'm at day 3. I've uploaded the code I wrote. The puzzle description and the code is here: https://github.com/secondspass/adventofcodeday3 . The code is in day3_part2.zig, the input file is day3.in. I run…
dragsubil
  • 13
  • 4
1
vote
2 answers

Is it possible to use the standard C++17 facilities with Zig in C++ compiler mode?

I am just getting started with the Zig. I am using Zig 0.9.0 on Windows 10. One of the features that attracts me is using Zig as a C or C++ compiler or cross-compiler. I have used Zig successfully for some toy programs, but my luck ran out when I…
Bill Forster
  • 6,137
  • 3
  • 27
  • 27
1
vote
2 answers

What literal expression has type `void`?

In Zig 0.9, I need a literal expression that has type void, for use as the context parameter to std.sort.sort, so that my lessThan function signature is semantically accurate. Is there some? I tried these but to no avail: const ek =…
1
vote
1 answer

zig: if statement in print function produces wrong output

I have this code: const std = @import("std"); const print = std.debug.print; pub fn main() void { var n: u8 = 1; print("{s}\n", .{if (n == 0) "0" else "1"}); } But it does not do what I want. The output is "0". Could anyone tell me what…
snoire
  • 13
  • 2
1
vote
1 answer

How to free keys of StringHashMap?

I was trying test "foo" { var map = std.StringHashMap(void).init(std.testing.allocator); defer { while (map.keyIterator().next()) |key| { std.testing.allocator.free(key); } map.deinit(); } } But got…
Helin Wang
  • 4,002
  • 1
  • 30
  • 34
1
vote
2 answers

Converting a slice to an array

I have a slice that I have guaranteed (in runtime) that its length is at least 8. I want to convert that slice to an array because I need to use std.mem.bytesAsValue() in order to create an f64 from raw bytes (for context, I'm implementing a binary…
André Staltz
  • 13,304
  • 9
  • 48
  • 58
1
vote
2 answers

Zig slice to C pointer of void pointers

I have a Zig slice of bytes, i.e. []u8, and a C dependency expects a void** as an argument. How do I cast or convert the former into the latter?
André Staltz
  • 13,304
  • 9
  • 48
  • 58
1
vote
1 answer

Need help using a C library in zig

I am trying to port the microUI library from C into zig. I have tried using this port attempt https://gitdab.com/luna/zig-microui as a guide post, but it does not seem to work. Here is a breakdown of my attempt so far: MicroUI is a very simple…
Kallo
  • 35
  • 1
  • 7
1
vote
1 answer

Errors with compiling Zig to wasm32-freestanding

I am trying to compile Zig functions to a freestanding WebAssembly module using the wasm32-freestanding target. The official documentation has a section explaining how to do this, but with a recent version of Zig (0.8.0) I get errors when trying to…
Challenger5
  • 959
  • 6
  • 26
1
vote
1 answer

zig maxValue of integer and float types

zig used to have @maxValue to query the max value for integer types, but I think it was removed several versions ago. Is there a replacement? I can´t find it.
B_old
  • 1,141
  • 3
  • 12
  • 26