{"id":14990796,"url":"https://github.com/zigcc/zig-idioms","last_synced_at":"2025-04-12T03:24:24.453Z","repository":{"id":209000318,"uuid":"722879493","full_name":"zigcc/zig-idioms","owner":"zigcc","description":"Common idioms used in Zig","archived":false,"fork":false,"pushed_at":"2024-05-03T12:00:44.000Z","size":15,"stargazers_count":44,"open_issues_count":0,"forks_count":2,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-03-25T23:01:38.662Z","etag":null,"topics":["zig","zig-lang","ziglang"],"latest_commit_sha":null,"homepage":"","language":"Zig","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zigcc.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-11-24T07:05:32.000Z","updated_at":"2025-03-18T05:45:17.000Z","dependencies_parsed_at":"2023-12-22T02:46:21.602Z","dependency_job_id":"176f729a-7eb9-42d7-8e38-ca70519c50b1","html_url":"https://github.com/zigcc/zig-idioms","commit_stats":{"total_commits":7,"total_committers":2,"mean_commits":3.5,"dds":0.1428571428571429,"last_synced_commit":"2c201e7f78f0d8e27450f4255c664cd03c9e9d97"},"previous_names":["zigcc/zig-idioms"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zigcc%2Fzig-idioms","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zigcc%2Fzig-idioms/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zigcc%2Fzig-idioms/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zigcc%2Fzig-idioms/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zigcc","download_url":"https://codeload.github.com/zigcc/zig-idioms/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248511041,"owners_count":21116339,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["zig","zig-lang","ziglang"],"created_at":"2024-09-24T14:20:52.315Z","updated_at":"2025-04-12T03:24:24.415Z","avatar_url":"https://github.com/zigcc.png","language":"Zig","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Zig idioms\n\n[![](https://github.com/zigcc/zig-idioms/actions/workflows/ci.yml/badge.svg)](https://github.com/zigcc/zig-idioms/actions/workflows/ci.yml)\n\n\u003e Zig, despite its simplicity, harbors unique features rarely found in other programming languages. This project aims to collect these techniques, serving as a valuable complement to the [Zig Language Reference](https://ziglang.org/documentation/master).\n\nEach idiom is accompanied by an illustrative example named after its corresponding sequence number. These examples can be executed using the command `zig build run-{number}`, or `zig build run-all` to execute all.\n\n## 01. Zig files are structs\n\n\u003e Source: https://ziglang.org/documentation/master/#import\n\nZig source files are implicitly structs, with a name equal to the file's basename with the extension truncated. `@import` returns the struct type corresponding to the file.\n\n```zig\n// Foo.zig\npub var a: usize = 1;\n\nb: usize,\n\nconst Self = @This();\n\npub fn inc(self: *Self) void {\n    self.b += 1;\n}\n\n// main.zig\nconst Foo = @import(\"Foo.zig\");\n\npub fn main() !void {\n    const foo = Foo{ .b = 100 };\n    std.debug.print(\"Type of Foo is {any}, foo is {any}\\n\", .{\n        @TypeOf(Foo),\n        @TypeOf(foo),\n    });\n    foo.inc();\n    std.debug.print(\"foo.b = {d}\\n\", .{foo.b});\n}\n```\n\nThis will output\n\n```text\nType of Foo is type, foo is foo\nfoo.b = 101\n```\n\n## 02. Naming Convention\n\n\u003e Source: https://www.openmymind.net/Zig-Quirks/\n\nIn general:\n\n- Functions are `camelCase`\n- Types are `PascalCase`\n- Variables are `lowercase_with_underscores`\n\nSo we know `file_path` is mostly a variable, and `FilePath` is mostly a type.\n\nOne exception to those rules is functions that return types. They are `PascalCase`, eg:\n\n```zig\npub fn ArrayList(comptime T: type) type {\n    return ArrayListAligned(T, null);\n}\n```\n\nNormally, file names are `lowercase_with_underscore`. However, files that expose a type directly (like our first example), follow the type naming rule. Thus, the file should be named `Foo.zig`, not `foo.zig`.\n\n## 03. Dot Literals\n\nIn Zig `.{ ... }` is everywhere, it can be used to initialize struct/tuple/enum, depending on its context.\n\n```zig\nconst Rect = struct {\n    w: usize,\n    h: usize,\n};\n\nconst Color = enum { Red, Green, Blue };\n\nfn mySquare(x: usize) usize {\n    return x * x;\n}\n\npub fn main() !void {\n    const rect: Rect = .{ .w = 1, .h = 2 };\n    const rect2 = .{ .w = 1, .h = 2 };\n    std.debug.print(\"Type of rect is {any}\\n\", .{@TypeOf(rect)});\n    std.debug.print(\"Type of rect2 is {any}\\n\", .{@TypeOf(rect2)});\n\n    const c: Color = .Red;\n    const c2 = .Red;\n    std.debug.print(\"Type of c is {any}\\n\", .{@TypeOf(c)});\n    std.debug.print(\"Type of c2 is {any}\\n\", .{@TypeOf(c2)});\n\n    // We can use .{ ... } to construct a tuple of tuples\n    // This can be handy when test different inputs of functions.\n    inline for (.{\n        .{ 1, 1 },\n        .{ 2, 4 },\n        .{ 3, 9 },\n    }) |case| {\n        try std.testing.expectEqual(mySquare(case.@\"0\"), case.@\"1\");\n    }\n}\n```\n\nThis will output\n\n```text\nType of rect is main.Rect\nType of rect2 is struct{comptime w: comptime_int = 1, comptime h: comptime_int = 2}\nType of c is main.Color\nType of c2 is @TypeOf(.enum_literal)\n```\n\n# Other learning materials\n\n- [Problems of C, and how Zig addresses them](https://avestura.dev/blog/problems-of-c-and-how-zig-addresses-them)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzigcc%2Fzig-idioms","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzigcc%2Fzig-idioms","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzigcc%2Fzig-idioms/lists"}