logoalt Hacker News

slopinthebag • today at 5:21 AM • 5 replies • view on HN

one thing i love about rust's macros is that you can accomplish this without needing a separate file type or inventing syntax

   #[component]
   pub fn HomePage(user: Option<User>) -> Element {
      rsx! {
         Layout { 
            title: "Home",
            if let Some(u) = user {
               h1 { "Welcome back, {u.name}!" }
               a { href: "/users/{u.id}", "Your profile" }
            } else {
               h1 { "Welcome, guest!" }
               a { href: "/login", "Log in" }
            }
         }
      }
   }

Replies

skavi • today at 6:32 AM

but you have invented syntax

asimovDev • today at 5:49 AM

This is just Twig templates, crazy. It's cool that Rust can do this out of a box

➕ show 1 reply
dnautics • today at 6:00 AM

> you can accomplish this

That's not HTML though, you have to learn a whole new dsl that must conform to rust parser rules. So, not the same thing at all.

➕ show 2 replies
sroerick • today at 5:38 AM

That is super cool actually

eggsandbeer • today at 5:47 AM

[dead]