Starting up with Zig

Hello fellow programmers,

After a complete crashdown with Rust, I decided to leave that language and give it a try with Zig.

Setting up things I will probably manage.
What I would like to know is what IDE I could use and what people are mostly using.
I have some experience with vscode, but debugging was hardly usable (with Rust).

Any ideas on this?
Thanks and regards,
Eric

Any editor/IDE which supports LSP (language server protocol) should be fine to get started with, since the main way to get zig autocomplete/linting is via zls (zig language server).

After you have zls running and maybe a bit of experience, you might also look into Improving Your Zig Language Server Experience | Loris Cro's Blog.

Also, I suggest using a tagged release of Zig (0.13.0 right now) and the matching tagged zls version. You can still switch to the latest stuff from the master branch whenever you feel like it.

I use VSCode. Thereā€™s a plugin for zls. You can debug with the C++ debugger.

For learning a langue, Iā€™d suggest to not worry about IDEs too much. Smart tooling is great at making a language expert faster in navigating large and complex code bases, but it doesnā€™t generally help with learning.

So, any text editor with Zig syntax highlighting should be enough to get started.

12 Likes

I use VSCode. https://code.visualstudio.com/

There is an extension for zig:

Name: Zig Language
Id: ziglang.vscode-zig
Description: Language support for the Zig programming language
Version: 0.5.9
Publisher: ziglang
VS Marketplace Link: Zig Language - Visual Studio Marketplace

From there it can manage the zig compiler installation for you (press ctrl + shift +p and ā€œinstall zigā€) (if you wish) but it is also very easy to install:
https://ziglang.org/learn/getting-started/

So far, the only thing I have needed is VSCode + the Zig extension for VSCode.
I havenā€™t used debuggers for any language really, perhaps I am missing out.

My suggestions would be to use Vim or Helix, if you like modal editing, I think both editor pair really well with Zig, I go very bare with both, just a theme, the lsp mostly for navigation, and voila. The stdlib is a great place to read and look at idiomatic Zig, you can also feel free to ask here if you have any trouble, this is how Iā€™ve learned the language, for the most part.

And if you really want to immerse yourself, you can use an editor written in zig :smile:

4 Likes

And a terminal emulator!

(private beta though, havenā€™t tried it)

3 Likes

Okidoki, Thanks for the answers. I will certainly look at some alternatives and ideas, but probably go for vscode at first.
First I will setup everythingā€¦ Be back later, probably next week.

Ok got zig 0.13 installed, in vscode added ā€˜Zig languageā€™.
Hello world is executingā€¦

Now why does this not compile??

pub const Rnd = struct
{
    pub seed: u64, // error: expected function or variable declaration after pub

    pub fn init(seed: u64) Rnd
    {
        return Rnd
        {
            .seed = seed,
        };
    }
};

Struct members do not have a visibility specifier in Zig.
Removing the ā€˜pubā€™ from the seed member definition should be the first fix.

Edit:

See also the Language Reference:
https://ziglang.org/documentation/0.13.0/#toc-struct

The struct examples show, that you could declare a variable inside a struct like

// Structs can have declarations.
// Structs can have 0 fields.
const Empty = struct {
    pub const PI = 3.14;
};

Thatā€™s why the compiler tells you about the ā€˜variable declarationā€™ option when using ā€˜pubā€™.

2 Likes

Ok thank you clear, so every struct field is public (!).
In the same directory as my main.zig is now the valid declaration inside rnd.zig.
Why canā€™t I import
in main.zig

const std = @import("std");  // works
const rnd = @import("rnd"); // does not work

EDIT:
ok it should be:

const rnd = @import("./rnd.zig");

Ok thank you clear, so every struct field is public (!).

Yes, there are not private struct members in zig. Granted modules can be private, so if you are exporting a library, not everything will be visible to the consumer of the library.

One of the reasons is that when you pass around a struct instance, the data is there, so thereā€™s no real guarantee that someone wonā€™t go in and fiddle with it. As such, rather than provide a somewhat empty promise of privacy, it is on the part of the developer to declare through documentation what is private and what is public.

FWIW, I donā€™t 100% agree with these arguments, but once you get used to it, itā€™s not a huge deal. It however can be a problem when working with a data structure and not knowing that the internals should not be messed with directly. ArrayList is a common example, where iā€™ve seen many help questions raised because someone was directly modifying the internal buffer of an arraylist rather than using the apiā€™s available.


If you want to see Andrewā€™s reasoning about this, you can read this comment.

2 Likes

A good resource for walking through the language features is ziglings:

1 Like

Okā€¦ Stuff is working but a lot of stuff is not.
In vscode I can run main
and also build zig build-exe main.zig -O ReleaseFast

I also installed ā€œZig Languageā€
vscode has an insane amount of options and I cannot distinguish what is vscode and what is Zigā€¦

Still missing:

  1. I cannot debug. Is it possible? And how?

  2. Is zig format built in into my zig installation?
    I donā€™t use any formatting because the code gets asymmetrical. (yes the format everyone seems to prefer).
    Is it possible to adjust something so that braces are always on new lines?

  3. What is the best way to output the assember code next to my exe? And Is it possible to find the methods in between all the asm statements?

  4. Refactoring / renaming is buggy. Which piece of software is responsible for that?

I would be most grateful if these things can be solved.
After that I will probably have to dive into creating a ā€˜build.zigā€™ file.

I cannot debug. Is it possible? And how?

I donā€™t know about how to set it up on vscode, but Iā€™ve used codelldb (which was made for vscode) to debug zig in neovim.

Is zig format built in into my zig installation?
I donā€™t use any formatting because the code gets asymmetrical. (yes the format everyone seems to prefer).
Is it possible to adjust something so that braces are always on new lines?

Zig fmt is part of the zig compiler. Itā€™s not required like go, but there are no configurations for it. You either take it or leave it. You might have to turn off auto format in vscode.

Refactoring / renaming is buggy. Which piece of software is responsible for that?

generally language servers handle code actions like rename/etc. Iā€™ve not had many issues with the rename feature, what problems are you having?

1 Like

what problems are you having?

Refactor rename produces some random nonsense inside the file and it forgets lots of instances in other files.

Yes i have turned it off