Created
March 26, 2015 19:17
-
-
Save Mr-Byte/90ea3c88073ddf2402f4 to your computer and use it in GitHub Desktop.
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
macro_rules! update { | |
($source:ident with { $($field:ident: $newValue:expr),* }) => { | |
{ | |
let mut result = $source.clone(); | |
$( | |
result.$field = $newValue; | |
)* | |
result | |
} | |
} | |
} | |
#[derive(Debug, Clone)] | |
struct Point { | |
x: f32, | |
y: f32 | |
} | |
fn main() { | |
let a = Point { x: 1.0, y: 1.0 }; | |
let b = update!(a with { y: 2.0 }); | |
let c = update!(b with { x: 3.0, y: -4.0 }); | |
println!("{:?}", a); | |
println!("{:?}", b); | |
println!("{:?}", c); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment