Skip to content

Update macro_rules! formatting. #1205

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

Merged
merged 1 commit into from
Jun 17, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/macros.md
Original file line number Diff line number Diff line change
@@ -14,10 +14,10 @@ Macros are created using the `macro_rules!` macro.
// This is a simple macro named `say_hello`.
macro_rules! say_hello {
// `()` indicates that the macro takes no argument.
() => (
() => {
// The macro will expand into the contents of this block.
println!("Hello!");
)
};
}

fn main() {
8 changes: 4 additions & 4 deletions src/macros/designators.md
Original file line number Diff line number Diff line change
@@ -8,13 +8,13 @@ macro_rules! create_function {
// This macro takes an argument of designator `ident` and
// creates a function named `$func_name`.
// The `ident` designator is used for variable/function names.
($func_name:ident) => (
($func_name:ident) => {
fn $func_name() {
// The `stringify!` macro converts an `ident` into a string.
println!("You called {:?}()",
stringify!($func_name));
}
)
};
}

// Create functions named `foo` and `bar` with the above macro.
@@ -25,12 +25,12 @@ macro_rules! print_result {
// This macro takes an expression of type `expr` and prints
// it as a string along with its result.
// The `expr` designator is used for expressions.
($expression:expr) => (
($expression:expr) => {
// `stringify!` will convert the expression *as it is* into a string.
println!("{:?} = {:?}",
stringify!($expression),
$expression);
)
};
}

fn main() {
10 changes: 5 additions & 5 deletions src/macros/dry.md
Original file line number Diff line number Diff line change
@@ -10,18 +10,18 @@ use std::ops::{Add, Mul, Sub};
macro_rules! assert_equal_len {
// The `tt` (token tree) designator is used for
// operators and tokens.
($a:ident, $b:ident, $func:ident, $op:tt) => (
($a:ident, $b:ident, $func:ident, $op:tt) => {
assert!($a.len() == $b.len(),
"{:?}: dimension mismatch: {:?} {:?} {:?}",
stringify!($func),
($a.len(),),
stringify!($op),
($b.len(),));
)
};
}

macro_rules! op {
($func:ident, $bound:ident, $op:tt, $method:ident) => (
($func:ident, $bound:ident, $op:tt, $method:ident) => {
fn $func<T: $bound<T, Output=T> + Copy>(xs: &mut Vec<T>, ys: &Vec<T>) {
assert_equal_len!(xs, ys, $func, $op);

@@ -30,7 +30,7 @@ macro_rules! op {
// *x = x.$method(*y);
}
}
)
};
}

// Implement `add_assign`, `mul_assign`, and `sub_assign` functions.
@@ -54,7 +54,7 @@ mod test {
assert_eq!(x, z);
}
}
}
};
}

// Test `add_assign`, `mul_assign`, and `sub_assign`.
8 changes: 4 additions & 4 deletions src/macros/overload.md
Original file line number Diff line number Diff line change
@@ -9,19 +9,19 @@ In that regard, `macro_rules!` can work similarly to a match block:
macro_rules! test {
// Arguments don't need to be separated by a comma.
// Any template can be used!
($left:expr; and $right:expr) => (
($left:expr; and $right:expr) => {
println!("{:?} and {:?} is {:?}",
stringify!($left),
stringify!($right),
$left && $right)
);
};
// ^ each arm must end with a semicolon.
($left:expr; or $right:expr) => (
($left:expr; or $right:expr) => {
println!("{:?} or {:?} is {:?}",
stringify!($left),
stringify!($right),
$left || $right)
);
};
}

fn main() {
2 changes: 1 addition & 1 deletion src/macros/repeat.md
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@ macro_rules! find_min {

fn main() {
println!("{}", find_min!(1u32));
println!("{}", find_min!(1u32 + 2 , 2u32));
println!("{}", find_min!(1u32 + 2, 2u32));
println!("{}", find_min!(5u32, 2u32 * 3, 4u32));
}
```