this post was submitted on 28 Sep 2024
43 points (97.8% liked)

Rust

5807 readers
118 users here now

Welcome to the Rust community! This is a place to discuss about the Rust programming language.

Wormhole

[email protected]

Credits

  • The icon is a modified version of the official rust logo (changing the colors to a gradient and black background)

founded 1 year ago
MODERATORS
 

A short post on how variable names can leak out of macros if there is a name collision with a constant. I thought this was a delightful read!

top 7 comments
sorted by: hot top controversial new old
[–] [email protected] 12 points 23 hours ago

That was such a cute lil post

[–] [email protected] 7 points 22 hours ago (1 children)

This was a great post, but is the last state of the macro actually bad for performance in any way? I get that it's ugly (and we should only choose to make code less readable like this when there's actually an issue) but is it worse for runtime performance?

[–] [email protected] 1 points 11 hours ago (1 children)

Runtime performance is entirely unaffected by the use of macros. It can have a negative impact on compile-time performance though, if you overdo it.

[–] [email protected] 1 points 5 hours ago (1 children)

I understand that the macro only affects compile time but I'm talking about the extra function that's included in the resulting source code when the macro is expanded during compile. Based on other feedback, it looks like the unused function is optimized away.

[–] [email protected] 1 points 3 hours ago

Ah yes, exactly.

[–] [email protected] -2 points 18 hours ago (1 children)

Maybe a good idea for a post. But the amount of reaches required makes this icky.

  • Pretending people write:
    let Ok(x) = read_input() else { return Err(Error) };
    
    instead of
     let x = read_input().map_err(|_| ...)?;
    
  • Pretending people write:
     const x: &str = "...";
    
    instead of
     const X: &str = "...";
    
  • Pretending there exist people who have such knowledge of rust macros hygiene, ident namespaces, etc, but somehow don't know about how macro code expands (the "shock" about the compile error).

Maybe there is a reason after all why almost no one (maybe no one, period) was ever in that situation.

[–] [email protected] 4 points 18 hours ago

Also:

A short post on how variable names can leak out of macros

I don't think you understood the blog OP!