Created
July 3, 2019 16:45
-
-
Save stivenson/631ad1cea9bb4697ead8715f25d6a81a to your computer and use it in GitHub Desktop.
Example macro in rust-lang
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
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