Skip to content

Instantly share code, notes, and snippets.

@stivenson
Created July 3, 2019 16:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stivenson/631ad1cea9bb4697ead8715f25d6a81a to your computer and use it in GitHub Desktop.
Save stivenson/631ad1cea9bb4697ead8715f25d6a81a to your computer and use it in GitHub Desktop.
Example macro in rust-lang
const VALUES_COP: &'static [i32;4] = &[50000, 20000, 10000, 2000];
// macro called cashier
macro_rules! cashier {
() => { // without arguments
println!("Now is necessary that you enter a quantity.");
};
($($x: expr),+) => { // 1 to n arguments
{
let mut total: u64 = 0;
let mut _i: usize = 0;
$( // cycle
let number: u64 = $x
.trim()
.parse()
.expect("Wanted a number");
total = total + (number * VALUES_COP[_i] as u64); // logic of operation
_i += 1; // "_" to omit alert of use
)+
println!("Current Total {:?}", &total);
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment