How to start using `!` operator?

Related Signature of an executable's `main` function

The main function is special because it interacts with the operating system. The return type of the main function is an approximation of how your program will interact with the OS.

If you return an error from the main function, your process with exit with a non-zero exit code, indicating an error to the OS and user.

If you do not desire to tell this to the user, for example, if you know your program will always succeed, feel free to omit the !.

By using only an !, you are asking the compiler to create an inferred error set for you: Documentation - The Zig Programming Language

You can choose what you want to happen! For example, would you want your database to kill itself if someone sent a malformed query? Probably not. So the language is providing you some features to help you handle errors, or not.

Tests are primarily to help you develop your software, not let a user run it. Tests help you ensure that your software behaves correctly. Typically a user will get a binary from you (like a game executable). Tests help you ensure that the software you write won’t crash or have bugs before you ship the executable to your users.

Typically you put the tests in your build script so you can run them many times while you are writing your code.

Here’s some resources on how to do that:

https://ziglang.org/learn/build-system/

2 Likes