Skip to content

Instantly share code, notes, and snippets.

@gdonald
Created February 24, 2025 01:14
Show Gist options
  • Save gdonald/f994637625144b31cb6c1a84d69293c6 to your computer and use it in GitHub Desktop.
Save gdonald/f994637625144b31cb6c1a84d69293c6 to your computer and use it in GitHub Desktop.
Rust boxit! macro
#[macro_export]
macro_rules! boxit {
($value:expr) => {
Box::new($value)
};
}
// Example usage:
#[cfg(test)]
mod tests {
#[test]
fn test_box_macro() {
let boxed = boxit!(42);
assert_eq!(*boxed, 42);
let boxed_string = boxit!(String::from("hello"));
assert_eq!(*boxed_string, "hello");
struct Point { x: i32, y: i32 }
let boxed_struct = boxit!(Point { x: 1, y: 2 });
assert_eq!(boxed_struct.x, 1);
assert_eq!(boxed_struct.y, 2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment