Skip to content
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

Merged
merged 2 commits into from Aug 21, 2014
Merged

Conversation

steveklabnik
Copy link
Member

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.'

@@ -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
Copy link

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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dammmit, thank you

@steveklabnik
Copy link
Member Author

@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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing *.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks

@steveklabnik
Copy link
Member Author

@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"));
Copy link
Contributor

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.

Copy link
Member Author

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.

Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Contributor

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.

Copy link
Member Author

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.

@steveklabnik
Copy link
Member Author

I've updated this PR to talk about println! instead of regexes. I like it better, but I'm unsure as to how much of a good idea it is to show the expanded output. I kinda like the shock-and-awe effect, heh.

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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/numer/number/

Copy link
Member Author

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

@mdinger
Copy link
Contributor

mdinger commented Aug 12, 2014

Much better macro intro.
Before - it was: "Macros do cool things but I'm not gonna show any of them to you"
Now - it's: "Macros are cool. Check this out!"

@steveklabnik
Copy link
Member Author

@mdinger awesome 😄

@steveklabnik
Copy link
Member Author

@huonw do you have anything to say here? You reviewed the initial draft.

@huonw
Copy link
Member

huonw commented Aug 14, 2014

I'll try to look over it again in the next day or so.

@steveklabnik
Copy link
Member Author

@brson : I reabased this to fix the merge error.

@steveklabnik
Copy link
Member Author

@brson: sigh, the very last example doesn't work. I've fixed them both now.

@steveklabnik
Copy link
Member Author

(worth knowing, when we expand the macro, it throws a warning, heh.)

bors added a commit that referenced this pull request Aug 21, 2014
…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.'
@bors bors closed this Aug 21, 2014
@bors bors merged commit 1395166 into rust-lang:master Aug 21, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants