What Are (Rust) Macros
(not azriel's) Definition: Copy-paste code without the pasta. And varargs.
-
Shortcode:
Original:
#![allow(unused_variables)] fn main() { use std::ops::Add; #[derive(Clone, Copy, PartialEq)] pub struct HealthPoints(pub u32); impl Add for HealthPoints { type Output = HealthPoints; fn add(self, other: HealthPoints) -> HealthPoints { HealthPoints(self.0 + other.0) } } }
Macro-treated:
#[derive(derive_more::Add, Clone, Copy, PartialEq)] pub struct HealthPoints(pub u32);
-
Varargs:
#![allow(unused_variables)] fn main() { let (a, b) = (3., 2.); println!(); println!("hello"); println!( "{a:.1} รท {b:.1} = {answer:.2}", a = a, b = b, answer = a / b ); }