026_hello2.zig 1.1 KB

123456789101112131415161718192021222324252627
  1. //
  2. // Great news! Now we know enough to understand a "real" Hello World
  3. // program in Zig - one that uses the system Standard Out resource...which
  4. // can fail!
  5. //
  6. const std = @import("std");
  7. // Take note that this main() definition now returns "!void" rather
  8. // than just "void". Since there's no specific error type, this means
  9. // that Zig will infer the error type. This is appropriate in the case
  10. // of main(), but can make a function harder (function pointers) or
  11. // even impossible to work with (recursion) in some situations.
  12. //
  13. // You can find more information at:
  14. // https://ziglang.org/documentation/master/#Inferred-Error-Sets
  15. //
  16. pub fn main() !void {
  17. // We get a Writer for Standard Out so we can print() to it.
  18. var stdout = std.fs.File.stdout().writer(&.{});
  19. // Unlike std.debug.print(), the Standard Out writer can fail
  20. // with an error. We don't care _what_ the error is, we want
  21. // to be able to pass it up as a return value of main().
  22. //
  23. // We just learned of a single statement which can accomplish this.
  24. stdout.interface.print("Hello world!\n", .{});
  25. }