-
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
1 parent
b1322e0
commit 1297481
Showing
9 changed files
with
211 additions
and
5 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
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 |
---|---|---|
@@ -1,3 +1,9 @@ | ||
pub var a: usize = 1; | ||
|
||
b: usize, | ||
|
||
const Self = @This(); | ||
|
||
pub fn inc(self: *Self) void { | ||
self.b += 1; | ||
} |
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
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 @@ | ||
const std = @import("std"); | ||
|
||
pub fn build(b: *std.Build) void { | ||
const exe = b.addExecutable(.{ | ||
.name = "02-name-convertion", | ||
.root_source_file = .{ .path = "src/main.zig" }, | ||
.target = .{}, | ||
.optimize = .Debug, | ||
}); | ||
|
||
b.installArtifact(exe); | ||
b.step("run", "Run").dependOn(&b.addRunArtifact(exe).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,29 @@ | ||
const std = @import("std"); | ||
|
||
const Rect = struct { | ||
w: usize, | ||
h: usize, | ||
|
||
fn init(w: usize, h: usize) Rect { | ||
return .{ .w = w, .h = h }; | ||
} | ||
|
||
fn getArea(self: Rect) usize { | ||
return self.w * self.h; | ||
} | ||
}; | ||
|
||
pub fn main() !void { | ||
const rect = Rect.init(1, 2); | ||
std.debug.print("Area of rect is {d}\n", .{rect.getArea()}); | ||
|
||
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); | ||
defer arena.deinit(); | ||
const allocator = arena.allocator(); | ||
|
||
var list = std.ArrayList(Rect).init(allocator); | ||
defer list.deinit(); | ||
|
||
try list.append(rect); | ||
std.debug.print("Len of list is {d}\n", .{list.items.len}); | ||
} |
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 @@ | ||
const std = @import("std"); | ||
|
||
pub fn build(b: *std.Build) void { | ||
const exe = b.addExecutable(.{ | ||
.name = "03-dot-literals", | ||
.root_source_file = .{ .path = "src/main.zig" }, | ||
.target = .{}, | ||
.optimize = .Debug, | ||
}); | ||
|
||
b.installArtifact(exe); | ||
b.step("run", "Run").dependOn(&b.addRunArtifact(exe).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,40 @@ | ||
const std = @import("std"); | ||
|
||
const Rect = struct { | ||
w: usize, | ||
h: usize, | ||
}; | ||
|
||
const Color = enum { Red, Green, Blue }; | ||
|
||
fn mySquare(x: usize) usize { | ||
return x * x; | ||
} | ||
|
||
pub fn main() !void { | ||
const rect: Rect = .{ .w = 1, .h = 2 }; | ||
const rect2 = .{ .w = 1, .h = 2 }; | ||
std.debug.print("Type of rect is {any}\n", .{@TypeOf(rect)}); | ||
std.debug.print("Type of rect2 is {any}\n", .{@TypeOf(rect2)}); | ||
// inline for (@typeInfo(@TypeOf(rect)).Struct.fields) |fld| { | ||
// std.debug.print("field name:{s}, field:{any}\n", .{ fld.name, fld }); | ||
// } | ||
// inline for (@typeInfo(@TypeOf(rect2)).Struct.fields) |fld| { | ||
// std.debug.print("field name:{s}, field:{any}\n", .{ fld.name, fld }); | ||
// } | ||
|
||
const c: Color = .Red; | ||
const c2 = .Red; | ||
std.debug.print("Type of c is {any}\n", .{@TypeOf(c)}); | ||
std.debug.print("Type of c2 is {any}\n", .{@TypeOf(c2)}); | ||
|
||
// We can rely on .{ ... } to construct a tuple of tuples | ||
// This can be handy when testing different inputs of functions. | ||
inline for (.{ | ||
.{ 1, 1 }, | ||
.{ 2, 4 }, | ||
.{ 3, 9 }, | ||
}) |case| { | ||
try std.testing.expectEqual(mySquare(case.@"0"), case.@"1"); | ||
} | ||
} |
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
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,9 @@ | ||
#!/bin/bash | ||
|
||
|
||
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) | ||
|
||
for dir in `find . -maxdepth 1 -type d -not -name '.*'`; do | ||
echo "Run example ${dir}..." | ||
cd "${SCRIPT_DIR}/${dir}" && zig build run | ||
done |