-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 3d120cf
Showing
5 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Created by https://www.toptal.com/developers/gitignore/api/zig | ||
# Edit at https://www.toptal.com/developers/gitignore?templates=zig | ||
|
||
### zig ### | ||
# Zig programming language | ||
|
||
zig-cache/ | ||
zig-out/ | ||
build/ | ||
build-*/ | ||
docgen_tmp/ | ||
|
||
# End of https://www.toptal.com/developers/gitignore/api/zig |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const std = @import("std"); | ||
|
||
pub fn build(b: *std.Build) void { | ||
const target = b.standardTargetOptions(.{}); | ||
const optimize = b.standardOptimizeOption(.{}); | ||
|
||
const exe = b.addExecutable(.{ | ||
.name = "01-zig-files-are-struct", | ||
.root_source_file = .{ .path = "src/main.zig" }, | ||
.target = target, | ||
.optimize = optimize, | ||
}); | ||
|
||
b.installArtifact(exe); | ||
const run_cmd = b.addRunArtifact(exe); | ||
run_cmd.step.dependOn(b.getInstallStep()); | ||
if (b.args) |args| { | ||
run_cmd.addArgs(args); | ||
} | ||
const run_step = b.step("run", "Run"); | ||
run_step.dependOn(&run_cmd.step); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub var a: usize = 1; | ||
|
||
b: usize, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const std = @import("std"); | ||
const Foo = @import("foo.zig"); | ||
|
||
pub fn main() !void { | ||
const foo = Foo{ .b = 100 }; | ||
std.debug.print("Type of Foo is {any}, foo is {any}\n", .{ | ||
@TypeOf(Foo), | ||
@TypeOf(foo), | ||
}); | ||
|
||
std.debug.print("foo = {any}, Foo.a = {d}, foo.b = {d}\n", .{ foo, Foo.a, foo.b }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Zig idioms | ||
|
||
This repository collects tips for writing clean, idiomatic Zig code. | ||
|
||
> This could be used as guide for developer with other programming languages background. | ||
## 01. Zig files are structs | ||
|
||
> Source: https://ziglang.org/documentation/master/#import | ||
Zig 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. | ||
```zig | ||
// foo.zig | ||
pub var a: usize = 1; | ||
b: usize, | ||
// main.zig | ||
const Foo = @import("foo.zig"); | ||
pub fn main() !void { | ||
const foo = Foo{ .b = 100 }; | ||
std.debug.print("Type of Foo is {any}, foo is {any}\n", .{ | ||
@TypeOf(Foo), | ||
@TypeOf(foo), | ||
}); | ||
} | ||
``` | ||
This will output | ||
``` | ||
Type of Foo is type, foo is foo | ||
``` |