Skip to content

Commit

Permalink
first init
Browse files Browse the repository at this point in the history
  • Loading branch information
jiacai2050 committed Nov 24, 2023
0 parents commit 3d120cf
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .gitignore
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
22 changes: 22 additions & 0 deletions 01-zig-files-are-struct/build.zig
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);
}
3 changes: 3 additions & 0 deletions 01-zig-files-are-struct/src/foo.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub var a: usize = 1;

b: usize,
12 changes: 12 additions & 0 deletions 01-zig-files-are-struct/src/main.zig
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 });
}
32 changes: 32 additions & 0 deletions README.md
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
```

0 comments on commit 3d120cf

Please sign in to comment.