Skip to content

Instantly share code, notes, and snippets.

@estshorter
Last active September 19, 2021 13:51
Show Gist options
  • Save estshorter/92d776046ef9781d9f5a9948f5dcdab0 to your computer and use it in GitHub Desktop.
Save estshorter/92d776046ef9781d9f5a9948f5dcdab0 to your computer and use it in GitHub Desktop.
rust snippets
// https://maguro.dev/debug-macro/
macro_rules! debug {
($($a:expr),* $(,)*) => {
#[cfg(debug_assertions)]
eprintln!(concat!($("| ", stringify!($a), "={:?} "),*, "|"), $(&$a),*);
};
}
// https://kuretchi.hateblo.jp/entry/rust_nested_vec
macro_rules! nested_vec {
($e:expr; $n:expr) => {
vec![$e; $n]
};
($e:expr; $n:expr $(; $m:expr)+) => {
vec![nested_vec!($e $(; $m)+); $n]
};
}
// https://maguro.dev/chmin-chmax-macro/
#[macro_export]
macro_rules! chmin {
($base:expr, $($cmps:expr),+ $(,)*) => {{
let cmp_min = min!($($cmps),+);
if $base > cmp_min {
$base = cmp_min;
true
} else {
false
}
}};
}
#[macro_export]
macro_rules! chmax {
($base:expr, $($cmps:expr),+ $(,)*) => {{
let cmp_max = max!($($cmps),+);
if $base < cmp_max {
$base = cmp_max;
true
} else {
false
}
}};
}
#[macro_export]
macro_rules! min {
($a:expr $(,)*) => {{
$a
}};
($a:expr, $b:expr $(,)*) => {{
std::cmp::min($a, $b)
}};
($a:expr, $($rest:expr),+ $(,)*) => {{
std::cmp::min($a, min!($($rest),+))
}};
}
#[macro_export]
macro_rules! max {
($a:expr $(,)*) => {{
$a
}};
($a:expr, $b:expr $(,)*) => {{
std::cmp::max($a, $b)
}};
($a:expr, $($rest:expr),+ $(,)*) => {{
std::cmp::max($a, max!($($rest),+))
}};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment