aboutsummaryrefslogtreecommitdiff
path: root/exercises/036_enums2.zig
diff options
context:
space:
mode:
authorDave Gauer <dave@ratfactor.com>2021-03-12 18:59:46 -0500
committerDave Gauer <dave@ratfactor.com>2021-03-12 18:59:46 -0500
commit6ad9774189fbd64b2f2c9519f4513ab34b0c3809 (patch)
treed6c90700131d5b28e898881f13e2a05612e4703f /exercises/036_enums2.zig
parentbe36352572ddb18218e1830e49316c259dea5e8c (diff)
downloadziglings-6ad9774189fbd64b2f2c9519f4513ab34b0c3809.tar.gz
ziglings-6ad9774189fbd64b2f2c9519f4513ab34b0c3809.tar.bz2
ziglings-6ad9774189fbd64b2f2c9519f4513ab34b0c3809.tar.xz
ziglings-6ad9774189fbd64b2f2c9519f4513ab34b0c3809.zip
"999 is enough for anybody" triple-zero padding (#18)
When I hit 999 exercises, I will finally have reached the ultimate state of soteriological release and no more exercises will be needed. The cycle will be complete. All that will be left is perfect quietude, freedom, and highest happiness.
Diffstat (limited to 'exercises/036_enums2.zig')
-rw-r--r--exercises/036_enums2.zig61
1 files changed, 61 insertions, 0 deletions
diff --git a/exercises/036_enums2.zig b/exercises/036_enums2.zig
new file mode 100644
index 0000000..0ddc4a5
--- /dev/null
+++ b/exercises/036_enums2.zig
@@ -0,0 +1,61 @@
+//
+// Enums are really just a set of numbers. You can leave the
+// numbering up to the compiler, or you can assign them
+// explicitly. You can even specify the numeric type used.
+//
+// const Stuff = enum(u8){ foo = 16 };
+//
+// You can get the integer out with a built-in function:
+//
+// var my_stuff: u8 = @enumToInt(Stuff.foo);
+//
+// Note how that built-in function starts with "@" just like the
+// @import() function we've been using.
+//
+const std = @import("std");
+
+// Zig lets us write integers in hexadecimal format:
+//
+// 0xf (is the value 15 in hex)
+//
+// Web browsers let us specify colors using a hexadecimal
+// number where each byte represents the brightness of the
+// Red, Green, or Blue component (RGB) where two hex digits
+// are one byte with a value range of 0-255:
+//
+// #RRGGBB
+//
+// Please define and use a pure blue value Color:
+const Color = enum(u32) {
+ red = 0xff0000,
+ green = 0x00ff00,
+ blue = ???,
+};
+
+pub fn main() void {
+ // Remember Zig's multi-line strings? Here they are again.
+ // Also, check out this cool format string:
+ //
+ // {x:0>6}
+ // ^
+ // x type ('x' is lower-case hexadecimal)
+ // : separator (needed for format syntax)
+ // 0 padding character (default is ' ')
+ // > alignment ('>' aligns right)
+ // 6 width (use padding to force width)
+ //
+ // Please add this formatting to the blue value.
+ // (Even better, experiment without it, or try parts of it
+ // to see what prints!)
+ std.debug.print(
+ \\<p>
+ \\ <span style="color: #{x:0>6}">Red</span>
+ \\ <span style="color: #{x:0>6}">Green</span>
+ \\ <span style="color: #{}">Blue</span>
+ \\</p>
+ , .{
+ @enumToInt(Color.red),
+ @enumToInt(Color.green),
+ @enumToInt(???), // Oops! We're missing something!
+ });
+}