-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
From this reddit thread [1], it became clearer that /u/tsion_'s solution is more useful. p!(x) now do the same as println!("x = {:?}", x); The old mode can be recovered by placing a colon before the value. [1] https://www.reddit.com/r/rust/comments/3fxf71
- Loading branch information
Showing
3 changed files
with
79 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,25 @@ | ||
#[macro_use] extern crate p_macro; | ||
|
||
#[derive(Debug)] | ||
struct Point { x: i32, y: i32 } | ||
|
||
fn main() { | ||
let a = [0, 1, 2, 3, 4, 5]; | ||
|
||
let b = ["This", "is", ""]; | ||
|
||
p!(a; b); | ||
let point = Point { x: 100, y: -50 }; | ||
|
||
p!(:a; :b); | ||
|
||
p!(); | ||
|
||
p!(point.x, point.y + 30); | ||
|
||
p!(_ b[0], _":", a[2] * 42); | ||
p!(_ b[0], _"=>", a[2] * 42); | ||
|
||
p! { | ||
_"Never changes", _ a[0], a[0]; | ||
b[1], b[2]; | ||
"Qq"; | ||
_"The value is:", point.x + a[2]; | ||
b[0], :a[2], :a[3], :a[1] | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,72 @@ | ||
#[macro_export] | ||
macro_rules! __p_c { | ||
($y:expr) => { print!("{:?}", $y); } | ||
} | ||
|
||
#[macro_export] | ||
macro_rules! __p_u { | ||
($y:expr) => { print!("{}", $y); } | ||
} | ||
|
||
#[macro_export] | ||
macro_rules! __p_b { | ||
($y:expr) => { print!("{} = {:?}", stringify!($y), $y); } | ||
} | ||
|
||
#[macro_export] | ||
macro_rules! p { | ||
() => { | ||
println!(""); | ||
}; | ||
|
||
(_ $y:expr) => { | ||
println!("{}", $y); | ||
}; | ||
|
||
|
||
(_ $y:expr,) => { | ||
println!("{}", $y); | ||
|
||
|
||
(: $y:expr) => { __p_c!($y); p!(); }; | ||
(: $y:expr,) => { __p_c!($y); p!(); }; | ||
(: $y:expr;) => { __p_c!($y); p!(); }; | ||
|
||
(: $y:expr, $($x:tt)+) => { | ||
__p_c!($y); | ||
print!(" "); | ||
p!($($x)*); | ||
}; | ||
|
||
(_ $y:expr;) => { | ||
println!("{}", $y); | ||
|
||
(: $y:expr; $($x:tt)+) => { | ||
__p_c!($y); | ||
p!(); | ||
p!($($x)*); | ||
}; | ||
|
||
|
||
(_ $y:expr) => { __p_u!($y); p!(); }; | ||
(_ $y:expr,) => { __p_u!($y); p!(); }; | ||
(_ $y:expr;) => { __p_u!($y); p!(); }; | ||
|
||
(_ $y:expr, $($x:tt)+) => { | ||
print!("{} ", $y); | ||
__p_u!($y); | ||
print!(" "); | ||
p!($($x)*); | ||
}; | ||
|
||
(_ $y:expr; $($x:tt)+) => { | ||
println!("{}", $y); | ||
__p_u!($y); | ||
p!(); | ||
p!($($x)*); | ||
}; | ||
|
||
($y:expr) => { | ||
println!("{:?}", $y); | ||
}; | ||
|
||
($y:expr,) => { | ||
println!("{:?}", $y); | ||
}; | ||
|
||
($y:expr;) => { | ||
println!("{:?}", $y); | ||
}; | ||
($y:expr) => { __p_b!($y); p!(); }; | ||
($y:expr,) => { __p_b!($y); p!(); }; | ||
($y:expr;) => { __p_b!($y); p!(); }; | ||
|
||
($y:expr, $($x:tt)+) => { | ||
print!("{:?} ", $y); | ||
__p_b!($y); | ||
print!(", "); | ||
p!($($x)*); | ||
}; | ||
|
||
($y:expr; $($x:tt)+) => { | ||
println!("{:?}", $y); | ||
__p_b!($y); | ||
p!(); | ||
p!($($x)*); | ||
}; | ||
} |