Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> For me the value in Zig is its simplicity as a language

I was reading an article about rust posted here yesterday and i realized: i have no clue what is going on in this code (also i presume "templage" is a typo):

    #[derive(Debug, Default)]
    pub struct Context {
        authed: bool,
    }

    #[derive(Template)]
    #[templage = "layout.jinja"]
    struct HomeTemplate {
        ctx: Context
    };

    pub async fn home() -> impl 
    IntoResponse {
        HtmlTemplate(
            HomeTemplate { ctx: 
    Context::default() }
        ).into_response()
    }


Looks like a small request handler for an HTTP framework using a page written with the jinja templating engine. I'm a bit rusty (pun intended) so I can't immediately place which HTTP framework that is though.


You're correct and the HTTP framework looks like Axum.


i mean i got that part :). my best guess is that the async function is some sort of factory? why you would need a factory to be async i have no idea.


Yes, while macros simplify some tasks, they also make it harder to really understand the code. Well, technically they hide the code in most languages as they're metaprogramming done in a separate language.

I love Rust and do some advanced code sometimes, but very rarely have I created a macro. I still see it more or less like "magic" that "super-human" library developers master to make their life easier. Typically not my use case.

This is another advantage of Zig. As you may know, metaprogramming in Zig doesn't use any special syntax, but rather the Zig language itself. This is often known as comptime and while not necessarily easy to grasp, as you're reasoning about compile time stuff, at least it's done in pure Zig.


to be fair you can generate a type in zig using a function that results in kinda obscure methods etc. i do think zig devs shy away from that kind of patterm though.


I personally think that the discovery of rust API is awfully as soon as macro is involve. It's impossible to know what argument is taken by macro. For example the "simple" `println!`, without example in the docstring it's impossible to guess what are the argument even when we have an IDE that show you the implementation.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: