Created
March 13, 2023 17:40
-
-
Save henryobiaraije/6d877b20c9f78ff4d1ebf387035e7bba to your computer and use it in GitHub Desktop.
How to create rust macro - detailed beginner friendly examples - Youtube video tutorial series
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
// Detailed example on how to create a macro in Rust language. | |
// Part 1 - Youtube tutorial - https://youtu.be/N-_CMAXQ5hc | |
// Part 2 - Youtube tutorial - https://youtu.be/tzZ2o8Nkp7I | |
fn main() { | |
let sum = add(&[1, 2, 3, 4]); | |
let diff = diff(&[1, 2, 3, 4]); | |
let product = mult(&[4, 2]); | |
println!("sum = {}, diff = {}, product = {}", sum, diff, product); | |
} | |
// Generate a function call add using out custom macro. | |
special_fn!(add, +, 4, i32, 0); | |
// Generate a function call diff using out custom macro. | |
special_fn!(diff, -, 4, i32, 0); | |
// Generate a function call mult using out custom macro. | |
special_fn!(mult, *, 2, i32, 0); | |
// Definging out macro. | |
#[macro_export] // This makes our macro importable into other modules. | |
macro_rules! special_fn { | |
// Define the patterns. | |
($name:ident, $op:tt, $n:literal, $t:ty, $default:expr ) => { | |
fn $name(values: &[$t; $n]) -> $t { | |
// Define the generated code here. | |
if(values.len() < 1){ | |
return $default; | |
} | |
let mut result = values[0]; | |
for i in 1..$n { | |
result = result $op values[i]; | |
println!("i = {}, result = {}, val = {}",i,result,values[i]); | |
} | |
result | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment