Skip to content

Instantly share code, notes, and snippets.

@Mr-Byte
Created March 26, 2015 19:17
Show Gist options
  • Save Mr-Byte/90ea3c88073ddf2402f4 to your computer and use it in GitHub Desktop.
Save Mr-Byte/90ea3c88073ddf2402f4 to your computer and use it in GitHub Desktop.
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