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
2 answers

How to test multiple files in Zig?

In main.zig I am importing some file lexer.zig that contains some tests const lxr = @import("lexer"); some code test { std.testing.refAllDecls(@This()); } However when running zig build test, the tests in lexer.zig are ignored. Here are the…
user20654379
1
vote
0 answers

Is there a way to merge struct types / extend a struct in Zig?

I have two struct types: const BaseStruct = struct { foo: f64, bar: f64 }; const AdditionalFields = struct { baz: u64, }; Is there a – preferably simple – way to create (at comptime) a third struct type from the former two which is…
balu
  • 3,500
  • 4
  • 34
  • 35
1
vote
1 answer

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 : i32 = 2000; for (arr[0..total]) |value| { …
Marian Klösler
  • 620
  • 8
  • 22
1
vote
1 answer

Checking if a pointer in NULL

Is there a nicer way of achieving the same result as the following code in a nicer way? if (window == @intToPtr(?*c.GLFWwindow, 0)) I want to check if a pointer to any kind of object (in this case, a nullable [of course] pointer to a GLFWwindow) is…
Snail5008
  • 59
  • 1
  • 9
1
vote
1 answer

How can I create a HashMap on the heap in Zig?

The conventional way to make an object on the heap is to make a create fn: const Something = struct { a:Allocator, b:[1000]u8, pub fn create(a:Allocator) !*Something { var mem = try a.create(Something) mem.* = { .a =a, …
nic ferrier
  • 1,573
  • 13
  • 14
1
vote
1 answer

How to insert a u32 into a u8 array in Zig?

I have a u8 buffer that stores a series of values including the RGB components from two u32 values (of which only 24 bits are used). I'm currently using bitwise operations to extract the individual components, then using @truncate to drop the…
Dan Prince
  • 29,491
  • 13
  • 89
  • 120
1
vote
1 answer

Toggle a boolean within a struct in zig, possible?

To toggle* a boolean I normally use boolean = !boolean like this: var boolean: bool = true; boolean = !boolean; std.debug.print("My bool is: {}\n", .{boolean}); //My bool is: false But how do I achieve this for booleans within a struct? Or is it…
Gaai
  • 98
  • 1
  • 8
1
vote
1 answer

Could someone explain how this works: const print = @import("std").debug.print;

How can this const print variable behave like a function? const print = @import("std").debug.print; print("hello, world", .{}); I understand you can assign expressions to variables. But this seems to behave like a precompiler macro in c/c++, I …
Gaai
  • 98
  • 1
  • 8
1
vote
2 answers

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 = @import("std"); pub fn main() !void { // #1 try…
1
vote
1 answer

pointer casting (ptrCast) of child type embed in Zig

I wonder if it's safe in Zig to cast a pointer from a child struct to a parent struct embedded in the child. Something like that: const std = @import("std"); const Parent = struct { age: u8, fn init(age: u8) Parent { return .{ .age…
umcisou
  • 11
  • 1
1
vote
1 answer

How to select version of function with a particular comptime parameter in gdb

For example how to add a breakpoint to function foo(true) in line 100, which is if (a) cat(), of src.zig in gdb? fn foo(comptime a: bool) void { if (a) cat() else mouse(); }
1
vote
0 answers

Must a zig function called at compile-time be pure?

https://ziglang.org/documentation/master/#struct // Functions called at compile-time are memoized. This means you can // do this: try expect(LinkedList(i32) == LinkedList(i32)); IIUC, if functions called at compile-time are memoized, they should be…
YouJiacheng
  • 449
  • 3
  • 11
1
vote
1 answer

Compile Time HashMap in Zig

Take for example the Rust library lazy_static's example: use lazy_static::lazy_static; use std::collections::HashMap; lazy_static! { static ref HASHMAP: HashMap = { let mut m = HashMap::new(); m.insert(0,…
CircArgs
  • 570
  • 6
  • 16
1
vote
2 answers

Zig std.log.info not printing anything with cross-compiled to AARCH64 binary

I have copied a hello world Zig program and it runs fine locally on my Mac: const std = @import("std"); pub fn main() anyerror!void { // Note that info level log messages are by default printed only in Debug // and ReleaseSafe build modes. …
Renato
  • 12,940
  • 3
  • 54
  • 85
1
vote
1 answer

Calling GMP from Zig

I'm new to Zig and wanted to use the cImport to call out to the GNU Multiprecision library GMP. I got this to work but it feels awkward because Zig apparently doesn't cast from arrays to pointers by default and GMP uses this approach to pass by…