# std: Handle field struct defaults in std.mem.zeroInit · gitcafe/zig

[View on GitCafe](https://git.cafe/gitcafe/zig/commit/31a2b8c3642f1240a70d78203d568051d4dbcd3f)

Repository: [gitcafe/zig](https://git.cafe/gitcafe/zig)

Visibility: public

Requested revision: 31a2b8c3642f1240a70d78203d568051d4dbcd3f

Requested commit: 31a2b8c3642f1240a70d78203d568051d4dbcd3f

Commit: 31a2b8c3642f1240a70d78203d568051d4dbcd3f

Tree: 00164723421c72b1feb39dada34bd705736045ce

Author: Michael Bartnett

Committer: GitHub

## Message

```
std: Handle field struct defaults in std.mem.zeroInit

I originally started monkeying with this because std.mem.zeroes doesn't support sentinel-terminated const slices even with defaults in 0.10.x.

I see that std.mem.zeroes was modified in #13256 to allow setting these slices to "".

That got me partway to where I wanted, but there was still an issue fields whose types are structs, they wouldn't get their defaults.

So when iterating struct fields looking for default values, when there is no default value and the type is .Struct, it will delegate to a call to zeroInit.

* Initialize struct fields in zeroInit exactly once

In my changes, similar to the previous implementation, the priority order for fields being initialized is:

1. If the `init` argument is a tuple, the nth element corresponding to the nth field of the struct.
2. Otherwise, if the `init` argument is not a tuple, try to find a matching field name on `init` and use that field.
3. Is the field has a default value, initalize with that value.
4. Fall back to what the field would have been initialized to via a recursive call to `std.mem.zeroInit`.

But instead of initializing a default instance of the struct and then running multiple passes over it, the init method is chosen per-field and each field is initialized exactly once.
```

## Parents

- [99febb54d331ded5026024a29f8407c2d5149762](https://git.cafe/gitcafe/zig/commit/99febb54d331ded5026024a29f8407c2d5149762?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zig/tree/31a2b8c3642f1240a70d78203d568051d4dbcd3f?format=markdown)
