-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Guide macros and unsafe #16331
Guide macros and unsafe #16331
Conversation
@@ -3628,5 +3628,113 @@ guide](http://doc.rust-lang.org/guide-pointers.html#rc-and-arc). | |||
|
|||
# Macros | |||
|
|||
One of Rust's most advanced features is is system of **macro**s. While |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/is is/is its
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dammmit, thank you
@jakub- see if the extra words i added fixes your issue about the mutex. |
be able to tell where a macro starts and ends. The `!(...)` helps with this. | ||
|
||
An example of even more advanced macro usage is in Rust's `regex` crate. This | ||
implements **regular expressions* for Rust. Regular expressions provide a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing *.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks
@jakub- thanks, fixed! |
|
||
fn main() { | ||
let date_matcher = regex!(r"^\d{4}-\d{2}-\d{2}$"); | ||
println!("Does our expression match? {}", date_matcher.is_match("2014-01-01")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is supposed to showcase the power of the macro system but it doesn't seem that way to me. It just looks like regex is implemented with macros rather than functions. With functions you could something like this where the returned structure has a function called is_match
to test with.
let date_matcher = regex(r"^\d{4}-\d{2}-\d{2}$");
println!("{}", date_matcher.is_match("2014-01-01"));
Is it easy to show why it is much better implemented as a macro than a function? If so, this seems like a good place for it. Otherwise, the conclusion might be that it's another variant of a function but why should we care.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My point is mostly to show that macros exist, but it is true that it would be good to mention why it is implemented this way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Discussing println!
might be a good example? It does relatively complicated computation (compared to a C-like macro anyway), e.g. parsing & processing "{foo:<5.8t} {0:03d}", x, foo = y
, and results in something that has a clear benefit: compile-time-checked flexible format strings.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good idea, especially since it's the macro we've been using already, and doesn't assume knowledge of something else, like regular expressions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is assert!
simple? It might be another alternative.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is an alternative, but it's not often used. At least, maybe assert_eq!
or something would be better.
I've updated this PR to talk about |
The `println!` macro does a few things: | ||
|
||
1. It parses the string to find any `{}`s | ||
2. It checks that the number of `{}`s matches the numer of other arguments. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/numer/number/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i really, really, really need to figure out how to get my spell checker to not throw errors in code blocks, it's how i keep missing things like this
Much better macro intro. |
@mdinger awesome 😄 |
@huonw do you have anything to say here? You reviewed the initial draft. |
I'll try to look over it again in the next day or so. |
1664a97
to
b23fd8d
Compare
@brson : I reabased this to fix the merge error. |
b23fd8d
to
1395166
Compare
@brson: sigh, the very last example doesn't work. I've fixed them both now. |
(worth knowing, when we expand the macro, it throws a warning, heh.) |
…rson The last two sections of the guide, and a small conclusion. I suck at conclusions. I also realized I never covered strings, so I'm going to put that section up before we're actually 'done.'
The last two sections of the guide, and a small conclusion. I suck at conclusions.
I also realized I never covered strings, so I'm going to put that section up before we're actually 'done.'