#Concise hello world

1 messages · Page 1 of 1 (latest)

blissful prism
#

Hi, I've just begun my zig journey. I've got a very beginner question - I'm wondering if zig has anything that's built-in that resembles rust's println macro, specifically in that it also:

  • Takes one or more arguments
  • Locks and unlocks stdout as required
  • Appends a \n
  • Can be called from any context without specifying its namespace or explicitly importing the standard library

If not, why? Does this go against zig principles? Is it due to limitation of the language?

glad knot
#

There is nothing that does precisely all of the things you're asking for.

std.debug.print and the various functions in std.log print to stderr, not stdout. std.log appends a newline, but it also prefixes the log message with the log level so it might not be what you want.

You could technically override the implementation of std.log by declaring the function std_options.logFn in your root source file and have it both write to stdout and without the log level prefix, but this is roughly the same amount of work as writing your own custom function that locks and prints to stderr in the way you want.

Can be called from any context without specifying its namespace or explicitly importing the standard library
[...] If not, why? Does this go against zig principles?

Zig prioritizes readability and tries to make where symbols comes from as explicit as possible, which is why there isn't any way to automatically make certain types or functions available everywhere without first importing them. No symbol should ever appear from out of nowhere and you should be able to tell from where everything comes from simply by reading an isolated source file from top to bottom.

Only the @-prefixed builtins can be used without importing a library, and as a general rule the only things that are promoted to builtins are intrinsics that can't be accomplished (effectively) using just regular zig code.