-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Macros #200
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
Macros #200
Conversation
Some of the examples seem repetitive. |
Any recommendations? I could probably turn it into a match like below but I was avoiding matching till the later match example: #![feature(macro_rules)]
macro_rules! echo {
// Match a series of expressions separated by `,`; return the same
// and convert the result into a string. `stringify!` causes
// evaluation to be skipped.
($($x:expr),+) => { stringify!($($x),+) };
// Same. Whitespace is ignored. This won't run because the first will match.
//($($x:expr) , +) => { stringify!($($x) , +) };
// Split on `|`. `stringify` is important here.
($($x:expr)|+) => { stringify!($($x)|+) };
// `$` isn't valid. It's a special macro key.
($($x:expr)$+) => { stringify!($($x)$+) };
// Split on `,` with trailing comma.
($($x:expr),+,) => { stringify!($($x),+,) };
// Split on `any_word`. `stringify` is important here.
($($x:expr) any_word +) => { stringify!($($x) any_word +) };
// Error: only allows one symbol between `$(...)` and `+`
//($($x:expr),,+) => { stringify!($($x),,+) };
// Error: only allows one word between `$(...)` and `+`
//($($x:expr) two words +) => { stringify!($($x) two words +) };
}
fn main() {
println!("{}", echo!(1u, 2u, 3u));
// Error: Only a single symbol allowed
//println!("{}", echo!(1u,, 2u,, 3u));
println!("{}", echo!(1u | 2u | 3u));
// Error: not valid. Don't split on `$`.
//println!("{}", echo!(1u $ 2u $ 3u));
println!("{}", echo!(1u, 2u, 3u,));
// Trailing comma ^
println!("{}", echo!(1u any_word 2u any_word 3u));
//Error: Only a single word allowed
//println!("{}", echo!(1u two words 2u two words 3u));
} The intent is to highlight that the symbol you split with matters. |
Hmmm interesting, I like the more concise example you posted, but it would be important to explain matches first. Either of the two paths I'd avoid duplicate examples like: macro_rules! echo_word {
// Split on `word`. `stringify` is important here.
($($x:expr) word +) => { stringify!($($x) word +) };
}
macro_rules! echo_and {
// Split on `and`. `stringify` is important here.
($($x:expr) and +) => { stringify!($($x) and +) };
} |
|
@@ -1,3 +1,2 @@ | |||
bin/* | |||
stage/* | |||
*~ |
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.
this change seems unrelated
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.
This was because I originally thought automatically hiding gedit temp files might be a good thing. Japaric didn't like it though so I removed it.
I'll close for now. If I decide to work on it, I'll rebase. |
Japaric's branch: #193. My original PR: #185. I'm hoping the comments will merge onto the end of his...
Target's helping fix this comment. Hopefully it's a little better at showing how repetition works and what is allowed.