Created
February 24, 2025 01:14
-
-
Save gdonald/f994637625144b31cb6c1a84d69293c6 to your computer and use it in GitHub Desktop.
Rust boxit! macro
This file contains hidden or 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
#[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