Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hello world to samples page #397

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

leighmcculloch
Copy link

What

Add a hello world example to the samples page.

Why

The learn samples showcase some advanced features and concerns like memory leaks, but as a someone who is new to Zig the first thing I need to see is hello world. i.e. The minimal application that runs and gives me some sense of where the entry point is and how to log so I can debug using some basic principles.

I think it would be helpful to new folks to add a hello world example to the samples page.

Close #117

@leighmcculloch
Copy link
Author

For the other language pages, is it best to leave this off those pages until someone translates it? Or should I add it to the other pages without text? How does the translation process work with this repo?

@chrboesch
Copy link
Contributor

chrboesch commented Sep 19, 2024

Previously, the index page displayed a "Hello World". Personally, I like the current example better. And for a 'Hello World', we might consider whether the debug version or a release version would be more appropriate:

const std = @import("std");
const stdout = std.io.getStdOut();

pub fn main() !void {
    try stdout.writeAll("Hello, World!\n");
}

@Atomk
Copy link
Contributor

Atomk commented Sep 19, 2024

I see that you renamed all sample files to keep the order, if maintainers are ok with it I would remove that prefix since these files are not order-sensitive,. Having to keep the order of the filesin sync with the order in the page feels like an unnecessary complication.

@leighmcculloch
Copy link
Author

I see that you renamed all sample files to keep the order, if maintainers are ok with it I would remove that prefix since these files are not order-sensitive,. Having to keep the order of the filesin sync with the order in the page feels like an unnecessary complication.

+1. Initially I just added hello world as number 6 and put it at the beginning so they were out of order, but I wasn't sure if the numbering was important. I'll remove the numbers, sounds good to me.

@leighmcculloch
Copy link
Author

leighmcculloch commented Sep 20, 2024

I've updated the example to use stdout, but chose to use print, since it allows printing values in the text.

@chrboesch
Copy link
Contributor

You could do that, but why if you don't have a variable to print?

    try stdout.print("Hello World!\n", .{});

@leighmcculloch
Copy link
Author

leighmcculloch commented Sep 20, 2024

I think using the print fn is more similar to the hello world programs I see in other languages, which also means increased familiarity for folks coming from other ecosystems. Often a function is used that supports plain text and substitution variables like the zig print function.

For e.g. in Go hello worlds you'll often see fmt.Println(...) be used which supports variables, even though you'll often see no variables in those examples. It's rare to see folks use os.Stdout.Write(...).

For e.g. in Rust hello worlds you'll often see println!(...) be used which supports variables. It's rare to see folks use std::io::stdout().write_all(...).

@chrboesch
Copy link
Contributor

I completely understand what you mean. While the old "Hello World" example is well-known, it often results in new visitors just taking a quick look and thinking, 'Wow, that looks complicated,' without really grasping the philosophy behind it. However, it’s crucial to understand Zig’s philosophy in order to appreciate the example fully — especially when it comes to the print case, where it’s essential to include even empty parameters. That's why I personally find the current example much better. It really showcases Zig's elegance and encourages curious visitors to explore a bit deeper.

@leighmcculloch
Copy link
Author

That's why I personally find the current example much better. It really showcases Zig's elegance and encourages curious visitors to explore a bit deeper.

👍🏻. If folks are happy with the current example, can we merge this PR, or anything that should be tweaked, etc?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add hello world example to samples page
3 participants