Am I learning zig wrong?

Hi folks. I’m a pretty early career engineer who works primarily in Matlab and C# at my job–combo of data analysis and working with a lot of senior engineers whose job is to know things, not write pretty code–with some C++ for performance critical stuff and occasional Julia on the side. I’d been seeing a lot of hype around Zig lately and wanting to sort of branch out my skillsets some, but after going through some of the initial tutorial and reference stuff on ziglang.org and zig.guide, I went to start solving AoC 23 problems since I typically like learning by trying stuff out + wanted to force myself to write things on my own rather than work through example projects. But I’m finding a lot of friction with how the early problems tend be almost more string parsing exercises than the kind of stuff I’d typically expect to use the language for, including maybe a bug with the zig-regex library I found when I tried that. Trying to figure out if

  1. This is pure skill issue that I need to power through/stop trying to write smarter code and just do whatever works 'til I get more familiar
  2. This isn’t a great way to learn the language, at least with my background, and I should stick to reading docs/books/ziglings for now
  3. Maybe I should just try something like Go first

I do find it kind of tough, coming from working in generally much more established languages, seeing things like very inconsistent amounts of detail/information in the zig standard library documentation (e.g. some pages will show test code that effectively gives a lot of usage examples while others are completely barren; I guess you could say just look at the raw source code, but I find that slower/less intuitive to search through), although I think the base tools are quite good with clear compiler errors and ZLS being refreshing compared to my experiences with .NET having very slow and unreliable dev tools when not working in JetBrains Rider (I like using VS Code).

4 Likes

I recommend Ziglings first, before AOC. Better focus on Zig.

Docs and std lib are immature – Zig is a long way from 1.0. These will come, but for now tests and source are important.

7 Likes

Consider writing down each little bit of friction you hit, and then we can brainstorm how to make that easier for new users!

I think the docs are only useful if you are solving a problem. Personally, I don’t remember the information unless I actually use it.

I started with ziglings and then jumped into a rather complex peoject, but you can bite off a little at a time.

Some things that caused friction for me:

  1. Learning how to use build.zig.zon to add a dependency to my project
  2. Parsing command line parameters
  3. The differences between std.posix and std.os.linux, maybe I should have just been using std.os.linux.
  4. Learning wtf root.zig is vs main.zig is in the zig init generated project

Here is a pretty good intro to the standard library:

3 Likes

For what it’s worth, the way I learned Zig was pretty unusual compared to other language. As you may have noticed the language is extremely readable, even when complex meta-programming is involved, as such I invite you to leverage that perk, and actually read the standard library, it might seem silly but most of what I’ve learned about the language, came from reading the documentation, and exploring how things were done in the standard library. This is something almost impossible in other languages, because you ether don’t have access to the std, or it’s so unreadable and complex that there’s quite literally no point trying to understand what’s going on in there.

But the complete roadmap for me was :

  • Reading the language reference, and compiling the snippets / playing around with them.
  • Doing all the ziglings
  • Implemented a few very simple programs, using a variety of language features, like comptime, reflection, slice, allocators, containers, and std.
  • Rewriting few of my projects, from C to Zig, and solving issues by reading code of the std. Asking question here along the way.

You can obviously skip step 4 and replace it with AOC, but I think it’s a solid start. As for the string processing, I think that using arena and slice/ArrayList helps a lot.

My recommendation for you would be to get really familiar, with :

Those are going to be what you will want to use for string processing. If you play around enough with them and use them a lot, understand how they work and which patterns they use, I think you will be much more comfortable for AOC.

Here’s also a list of learning resources compiled by the forum’s members. link

8 Likes

The language itself is tiny and simple.
I use surprisingly little of the std library. I recommend not worrying about the std library and just wiriting your own functions. Eventually, you’ll find some std functions that are similar to what you wrote, in which case you can compare your implementation to theirs. Either your function are better than theirs, and you can submit a PR to improve it, or theirs is better than yours, and you’learn something. Also, a lot of times the function you wrote is better for your code, because you are leveraging assumptions about your code that the std library can’t rely on.
Just learn ArrayList, BoundedArray, Allocator, a couple of allocators in heap, and you can get pretty far.
Some of the things I also use from the library are the reader and writer interfaces and fmt, but you can learn them later. For starting out, std.debug.print is more than fine.

I think trying to write your own stuff is a great way to get started. I think picking something you’re familiar with and trying to re-implement it in Zig is a good way to start, so that you aren’t trying to learn some new domain and new technology at the same time. If you’re familiar with socket programming, for example, you could try writing a simple HTTP client.

You might want to check out Learning Zig. You can read the whole thing in less than an hour, and it might help bridge some of the gap you’ll have coming from a GC’d language (C#).

4 Likes

Learning any new programming language will take time and be difficult. Yes you understand C#, but there was a time when C# was just as foreign to you as Zig is now.

Along with everyone else’s suggestions, I’d recommend finding something you’re excited to make. For me that was using Zig’s build system to compile C libraries and attempting to use them in Zig rather than C. Something like ray lib, for example. Once you have a project you’re interested in, learning becomes so much more bearable and the time you spend will fly by.

I also think you posting in this forum is a great first step! The Ziggit community has been extremely helpful in furthering my understanding of Zig, by both posting my own projects to gather feedback, to reading through the mountain of information already available here.

Good luck!

1 Like

i’d like to understand what’s the reason to learn zig (or another language)
instead of improving c# knowledge?

I think this is a pretty typical response to trying to learn Zig through AoC problems – I myself have felt this friction when I tried to work through AoC problems. If you like AoC problems, here’s a suggestion:

  1. Pick just one AoC problem, perhaps from days 10-20. The idea is that you pick something with enough difficulty that you really need to sit with the problem for maybe more than a day or two.
  2. Build out everything from scratch using as few libraries and even std data structures as possible. Be extremely naive in your own implementations.
  3. Once you complete the problem, pick a data structure and replace it with one the std or even a library you can find. Read the code. Think about what was done differently and why.
  4. Post any other questions back here on ziggit!
4 Likes

My understanding is that zig-regex remains a work in progress. mvzr is missing some features one might be used to, but for what it does, people are finding it useful (and I fix bugs as they arise).

You’ve gotten a lot of good advice here and I find I have little else to add other than this specific tip. Good luck! Learning Zig is eminently worthwhile.

3 Likes

Thanks for all the replies folks, definitely a lot of good tips! I like the ideas of reimplementing a project in Zig or tackling one of the later/more algorithmic AoCs with minimal std interaction as a learning experience, will probably try something like that after going back through the ziglings.

i’d like to understand what’s the reason to learn zig (or another language)
instead of improving c# knowledge?

A fair question. I will say I only used that at work while I was on a team working with a fairly extensive Unity project, but it didn’t give me the best impression of the language and I just haven’t had any real use case to drive working with the language further. Like I mentioned, I work with and write C++ code occasionally as well, but that’s all super functional stuff (think implementing coordinate transforms, matrix operations, and some algorithms on them like linear assignment problems over cost matrices), but other than that I haven’t worked with low-level stuff since using C in my computer architecture course in uni. So, basically wanted to try Zig out of personal interest and maybe to swap it into some of those use cases at work in the future due to the easy C interoperability.

6 Likes

Oh, I’ll also add for myself, based on looking around a bit more, it definitely seems like I should be using the built in testing tools a lot more. This is just me being too used to easy debugging in interpreted languages to solve my problems, so bit of a learning curve there, but sorta feels like eating my vegetables to grow big and strong.

2 Likes

By the way I did see and read the thread about your project when checking out this forum. It seems cool! I’ll probably give it a spin soon.

1 Like

It’s what I had to do, Zig is not very searchable and it took me a while to get comfortable. I found reading the standard library really helped quite a lot.

2 Likes

Am I learning Zig wrong?

No, you are not.

The point is that a programming language
(I mean it’s syntax, it’s grammar, it’s “idioms”, a language as it’s seen)
is a very tiny piece of knowledge of a programmer.

It’s just a tip of an iceberg, and below, deep down in the very cold water, there are:

  • tools (editors, debuggers, language servers etc)
  • hardware (note, for ex. the size of STM manual and also endless talking about “cache lines”)
  • data structures and algorithms (lists, hashes/maps/dictionaries, sets etc)
  • libraries (hundreds of thousends of them, you can spend the whole of your life to learn all that stuff)
  • OS API, be it Unix-like or Windows-like
  • “world”, in your program you have to describe it somehow (just structs, OOP or other facilities) and there is no a programming language that can express everything

If you are more inclined to “systems programming” (i/o, hardware, operating systems etc) then Zig is good choice (it’s future C after all :slight_smile: ), but then do not pay much attention to purely computational (only CPU + RAM required) stuff in it’s standard library, maybe you will be able to implement something better! :slight_smile:

And if you more math inclined person then… then also yes, numpy (for instance) is written in C, why not to write similar libs in Zig?

1 Like

Yeah, actually in parallel to general reading and practice examples, I’m working on a little interpolation library as I thought it would be a neat math project (and specifically at work lately ran into some stuff where I was needing to optimize interpolation of some 1-D and 2-D datasets to make some plots faster so it was on my mind). Will hopefully post about it here soon-ish once it’s more presentable!

I’ve been learning zig for about a month now.

Yes, I started with ziglings and zig.guide… but as soon as possible I switch to solving problems that actually matter to me. Whenever I ran to a skill issue, I asked questions in here. People have been super friendly and helpful.

Just reading the examples and docs would never get me to where I’m now (and I’m not saying that I’ve mastered zig… but I can use it to solve real life problems with it):

  • writing my own stuff
  • asking questions in here

Your mileage my vary, but it keeps me motivated to write stuff that I feel passionate about and having a community who can support me when I fail to understand the docs.

6 Likes