Live data from Hacker News

Let's write a macro in Rust

hackeryarn.com

41–50 of 50 posts

Re: Let's write a macro in Rust

#41
post #24

One neat thing about the referenced chapter on macros from Practical Common Lisp is that it's chapters 7 and 8 of a 30+ chapter book. You're only learning about variables in the chapter before and then macros. I like to think about that when conversations about macros always devolve into talking about how you should never use macros. Now apologies for the aside—and I know I'm likely wading into a very expansive topic…

Those "things that look at a lot like strings already" are literals and thus constants, with the type `&'static str` which is how Rust spells an immutable reference to a string slice which lives forever. So, Rust is promising that the string slices "Hate Me" and "Blue October" exist, and in practice probably if you look inside the resulting executable it says "Hate MeBlue October" or similar.

On the other hand the String type is a growable array of bytes used to assemble strings, it's an owning type, so Song will own those two Strings, they can't go away or be altered via some other force. Owning a string would allow you to add to it, change it, or discard it entirely, you obviously can't do that to the text baked into the executable, so if you want to own a String it will have to be allocated at runtime.

You can also write "Some Words".to_owned() for example to the same effect.

Re: Let's write a macro in Rust

#42

Earlier quoted context omitted.

Maybe "don't write your own macros". I guess it's like C++ template meta programming. If you find yourself using that a lot, it's probably a bad sign. But also Eigen is pretty great.

I dunno, the C++ template implementation of this macro would be far easier to read as it is just normal C++.

C++ templates are totally different feature. You couldn't do this with C++ templates.

It was just an example of something that gets very hairy for library implementers but is still quite nice for users.

Re: Let's write a macro in Rust

#43
post #31

Earlier quoted context omitted.

> I agree that decl macros usually make me sigh and open the docs though Declarative macros are very like regex's: both in that at first they seem incredibly dense and arcane but once you work out how to read them they're actually very simple, and in that the syntax is literally similar in that it's a list of tokens that must match sequentially.

If regexp was simple there would not exist universities dedicating a semester to its theory and various implementation algorithms, alongside a famous quote. :)

Universities are teaching about the Regular languages and about Finite automata.

So if we're saying this means regex isn't simple then - having observed that my University's Math course has two semester long Number Theory courses we must conclude that the integers aren't simple either, right ?

And yet, the integers are simple enough that's where we start five year olds, by counting things.

Simple and Shallow are not the same thing.

Re: Let's write a macro in Rust

#44
post #37

Earlier quoted context omitted.

Because to_string allocates. And if a function requires a String (owned), it cannot accept a str reference (borrowed), it would defeat the purpose of the strong type system. String is moved, while str is passed by reference. There is the exception of Deref. If the function requires type A, and you pass it type B, which Derefs into type A, the compiler will Deref it for you. But that is zero cost and panic free, where…

If only one could construct a macro to solve the boilerplate of AsRef etc ;)

To me, there’s not much difference between:

  fn foo(name: &str) {}
and*:

  fn foo(name: impl AsRef) {}

Re: Let's write a macro in Rust

#45
post #31

Earlier quoted context omitted.

If regexp was simple there would not exist universities dedicating a semester to its theory and various implementation algorithms, alongside a famous quote. :)

Universities are teaching about the Regular languages and about Finite automata. So if we're saying this means regex isn't simple then - having observed that my University's Math course has two semester long Number Theory courses we must conclude that the integers aren't simple either, right ? And yet, the integers are simple enough that's where we start five year olds, by counting things. Simple and Shallow are not…

Indeed they aren't, that is why math has such a bad reputation in most countries, already at basic school levels.

Recent article this week in German press, as per IQB-Bildungstrend 2024 results, one third of students fail in math exams.

Which reinforces the common culture that math is hard, and it is to be expected not to have great grades at it.

Re: Let's write a macro in Rust

#48

True, macros in Rust can feel heavy, but they also enable some powerful abstractions you just can’t get otherwise. I guess the “don’t write macros” advice is more about avoiding overuse than rejecting them completely.

The only macro I ever use is json! or for applying 10 derives to more than 1 struct

vec!? println!?

Re: Let's write a macro in Rust

#49

Earlier quoted context omitted.

The only macro I ever use is json! or for applying 10 derives to more than 1 struct

vec!? println!?

those are language macros iirc (hardcoded in the language) hence I consider those the same as when, fn etc.

Re: Let's write a macro in Rust

#50
post #3

They should try writing a spellchecker first, I found the article difficult to read because of the high frequency of typos.

Thanks for pointing that out. This is really embarrassing, but I recently switched to Helix as my editor and had a conflicting config that partially broke the spellchecker in my blog.

Should be a lot better now.

Post reply on HN