summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarol (Nichols || Goulding) <carol.nichols@gmail.com>2017-03-19 10:08:42 -0400
committerGitHub <noreply@github.com>2017-03-19 10:08:42 -0400
commit133892fc4efc41b229baecf7fc12c7efeaecd024 (patch)
tree584cc00908e03dbbd5bb55ed215142da4cace876
parent2ee6e22e5ca2da45a38e96d3c61396fe264084cd (diff)
parent2b70161b4f5961c313439ef8a132c4dfcd597216 (diff)
Merge pull request #57 from QuietMisdreavus/macros
add macros examples
-rw-r--r--macros/macros1.rs62
-rw-r--r--macros/macros2.rs72
-rw-r--r--macros/macros3.rs74
-rw-r--r--macros/macros4.rs76
4 files changed, 284 insertions, 0 deletions
diff --git a/macros/macros1.rs b/macros/macros1.rs
new file mode 100644
index 0000000..b2f48e4
--- /dev/null
+++ b/macros/macros1.rs
@@ -0,0 +1,62 @@
+// Make me compile! Scroll down for hints :)
+
+macro_rules! my_macro {
+ () => {
+ println!("Check out my macro!");
+ };
+}
+
+fn main() {
+ my_macro();
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+// When you call a macro, you need to add something special compared to a regular function call. If
+// you're stuck, take a look at what's inside `my_macro`.
diff --git a/macros/macros2.rs b/macros/macros2.rs
new file mode 100644
index 0000000..040dc7f
--- /dev/null
+++ b/macros/macros2.rs
@@ -0,0 +1,72 @@
+// Make me compile! Scroll down for hints :)
+
+fn main() {
+ my_macro!();
+}
+
+macro_rules! my_macro {
+ () => {
+ println!("Check out my macro!");
+ };
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+// Macros don't quite play by the same rules as the rest of Rust, in terms of what's available
+// where.
+
+
+
+
+
+
+
+
+// Unlike other things in Rust, the order of "where you define a macro" versus "where you use it"
+// actually matters.
diff --git a/macros/macros3.rs b/macros/macros3.rs
new file mode 100644
index 0000000..8d8da38
--- /dev/null
+++ b/macros/macros3.rs
@@ -0,0 +1,74 @@
+// Make me compile, without taking the macro out of the module! Scroll down for hints :)
+
+mod macros {
+ macro_rules! my_macro {
+ () => {
+ println!("Check out my macro!");
+ };
+ }
+}
+
+fn main() {
+ my_macro!();
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+// In order to use a macro outside of its module, you need to do something special to the module to
+// lift the macro out into its parent.
+
+
+
+
+
+
+
+
+// The same trick also works on "extern crate" statements for crates that have exported macros, if
+// you've seen any of those around.
diff --git a/macros/macros4.rs b/macros/macros4.rs
new file mode 100644
index 0000000..57dc431
--- /dev/null
+++ b/macros/macros4.rs
@@ -0,0 +1,76 @@
+// Make me compile! Scroll down for hints :)
+
+macro_rules! my_macro {
+ () => {
+ println!("Check out my macro!");
+ }
+ ($val:expr) => {
+ println!("Look at this other macro: {}", $val);
+ }
+}
+
+fn main() {
+ my_macro!();
+ my_macro!(7777);
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+// You only need to add a single character to make this compile.
+
+
+
+
+
+
+
+
+
+// The way macros are written, it wants to see something between each "macro arm", so it can
+// separate them.