# Joshua - Zig Assert Strategies (Highlights)

## Metadata
**Review**:: [readwise.io](https://readwise.io/bookreview/56194661)
**Source**:: #from/readwise #from/reader
**Zettel**:: #zettel/fleeting
**Status**:: #x
**Authors**:: [[Joshua]]
**Full Title**:: Zig Assert Strategies
**Category**:: #articles #readwise/articles
**Category Icon**:: 📰
**URL**:: [joshuao.com](https://joshuao.com/posts/zig-assert-strategies.html)
**Host**:: [[joshuao.com]]
**Highlighted**:: [[2025-11-11]]
**Created**:: [[2025-11-15]]
## Highlights
- `std.debug.assert` is a normal function, and it may evaluate its parameter even in `ReleaseFast` mode. ([View Highlight](https://read.readwise.io/read/01k9thb7fdhd2ahahawn73d18e)) ^956591286
- Use `if (@import("builtin").mode == .Debug)` when you're feeling paranoid about slow asserts, or you want to mimic C assert macros ([View Highlight](https://read.readwise.io/read/01k9thbavkcxd4s9j4bfk5jmtj)) ^956591290
- The default panic handler is [here](https://ziglang.org/documentation/master/std/#std.debug.defaultPanic) , it nicely prints the stack trace and aborts. You can define your own by declaring a `const panic` in your root, it's described in good detail [here in the Zig Reference](https://ziglang.org/documentation/master/#Panic-Handler) ([View Highlight](https://read.readwise.io/read/01k9the9etj8gkhjaab9nt5dqq)) ^956591412
- Let's try to make a minimal panic handler that stops as close to the action as possible ([View Highlight](https://read.readwise.io/read/01k9thjyane94n4570zmf0e22y)) ^956591711
↩︎
```
pub const panic = PanicHandler;
pub const PanicHandler = struct {
pub fn call(_: []const u8, _: ?usize) noreturn {
@branchHint(.cold);
@breakpoint();
unreachable;
}
pub fn sentinelMismatch(_: anytype, _: anytype) noreturn {
@branchHint(.cold);
@breakpoint();
unreachable;
}
// ... Many more functions to add here, see std.debug.simple_panic for the "interface" you have to present.
};
```
- if (comptime dbg_mode) std.debug.assert(super_expensive_check()); ([View Highlight](https://read.readwise.io/read/01k9ths7189zrtwnp58er34pvg)) ^956592031
- Alternatively, you can explicitly turn on the safety checking features with `@setRuntimeSafe(true)`, so using that we could use the standard assert and still get a panic ([View Highlight](https://read.readwise.io/read/01k9thzm0y54zy3h6b4krmygj3)) ^956592464
Assert panics only in safe mode, while `@panic` always panics.
- If you really want the perfect debugger experience then you can have a special assert just for that ([View Highlight](https://read.readwise.io/read/01k9tj271pwveb6kwe6m4pxtqg)) ^956592550
```
pub inline fn debugAssertOrSkip(ok: bool) {
if (dbg_mode and !ok) @breakpoint();
}
```