Added Enums & Structs
This commit is contained in:
parent
06ff9369ab
commit
14b2b76120
|
@ -0,0 +1,34 @@
|
|||
const std = @import("std");
|
||||
|
||||
pub fn build(b: *std.build.Builder) void {
|
||||
// Standard target options allows the person running `zig build` to choose
|
||||
// what target to build for. Here we do not override the defaults, which
|
||||
// means any target is allowed, and the default is native. Other options
|
||||
// for restricting supported target set are available.
|
||||
const target = b.standardTargetOptions(.{});
|
||||
|
||||
// Standard release options allow the person running `zig build` to select
|
||||
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
|
||||
const mode = b.standardReleaseOptions();
|
||||
|
||||
const exe = b.addExecutable("Enums", "src/main.zig");
|
||||
exe.setTarget(target);
|
||||
exe.setBuildMode(mode);
|
||||
exe.install();
|
||||
|
||||
const run_cmd = exe.run();
|
||||
run_cmd.step.dependOn(b.getInstallStep());
|
||||
if (b.args) |args| {
|
||||
run_cmd.addArgs(args);
|
||||
}
|
||||
|
||||
const run_step = b.step("run", "Run the app");
|
||||
run_step.dependOn(&run_cmd.step);
|
||||
|
||||
const exe_tests = b.addTest("src/main.zig");
|
||||
exe_tests.setTarget(target);
|
||||
exe_tests.setBuildMode(mode);
|
||||
|
||||
const test_step = b.step("test", "Run unit tests");
|
||||
test_step.dependOn(&exe_tests.step);
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
const std = @import("std");
|
||||
|
||||
// can also be pub like structs
|
||||
// can define size with enum(u8) for example
|
||||
const Month = enum {
|
||||
January,
|
||||
February,
|
||||
March,
|
||||
April,
|
||||
May,
|
||||
June,
|
||||
July,
|
||||
August,
|
||||
September,
|
||||
October,
|
||||
November,
|
||||
December,
|
||||
|
||||
// bound functions
|
||||
pub inline fn isNovemeber(self: @This()) bool {
|
||||
return self == .November;
|
||||
}
|
||||
};
|
||||
|
||||
pub fn main() !void {
|
||||
var month: Month = .February;
|
||||
std.log.info("The month is: {}, size of month: {}", .{ @enumToInt(month), @sizeOf(Month) });
|
||||
std.log.info("Is November? {}", .{month.isNovemeber()});
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
const std = @import("std");
|
||||
|
||||
pub fn build(b: *std.build.Builder) void {
|
||||
// Standard target options allows the person running `zig build` to choose
|
||||
// what target to build for. Here we do not override the defaults, which
|
||||
// means any target is allowed, and the default is native. Other options
|
||||
// for restricting supported target set are available.
|
||||
const target = b.standardTargetOptions(.{});
|
||||
|
||||
// Standard release options allow the person running `zig build` to select
|
||||
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
|
||||
const mode = b.standardReleaseOptions();
|
||||
|
||||
const exe = b.addExecutable("Structs", "src/main.zig");
|
||||
exe.setTarget(target);
|
||||
exe.setBuildMode(mode);
|
||||
exe.install();
|
||||
|
||||
const run_cmd = exe.run();
|
||||
run_cmd.step.dependOn(b.getInstallStep());
|
||||
if (b.args) |args| {
|
||||
run_cmd.addArgs(args);
|
||||
}
|
||||
|
||||
const run_step = b.step("run", "Run the app");
|
||||
run_step.dependOn(&run_cmd.step);
|
||||
|
||||
const exe_tests = b.addTest("src/main.zig");
|
||||
exe_tests.setTarget(target);
|
||||
exe_tests.setBuildMode(mode);
|
||||
|
||||
const test_step = b.step("test", "Run unit tests");
|
||||
test_step.dependOn(&exe_tests.step);
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
const std = @import("std");
|
||||
|
||||
// Name, Age
|
||||
// packed struct cannot contain strings
|
||||
// extern struct allows for compatibility with gcc
|
||||
// as with packed struct string not allowed
|
||||
const UserInfo = struct {
|
||||
name: []const u8 = "Not defined",
|
||||
age: u32,
|
||||
|
||||
pub fn init(name: []const u8, age: u32) UserInfo {
|
||||
return UserInfo{
|
||||
.name = name,
|
||||
.age = age,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn printName(self: @This()) !void {
|
||||
std.log.info("{s}", .{self.name});
|
||||
}
|
||||
};
|
||||
|
||||
pub fn main() !void {
|
||||
//const user = UserInfo{
|
||||
// .age = 100,
|
||||
//};
|
||||
|
||||
const user = UserInfo.init("Eddie", 100);
|
||||
try user.printName();
|
||||
}
|
Loading…
Reference in New Issue