Hello i try to implement fill array macro in Rust! And i have a problem!
My macro code:
macro_rules! fill_tensor {
($elem:expr; ($dim:expr, $($dims:expr),+)) => {
vec![fill_tensor!($elem; ($($dims),+)); $dim]
};
($elem:expr; $dim:expr) => {
vec![$elem; $dim]
};
}
When i call my macro like this
fill_tensor!(4; (3_usize, 3_usize))
All works correctly (output is 3 by 3 matrix)
But when i call this macro like this
let shape: (usize, usize) = (3_usize, 3_usize)
fill_tensor!(4; shape)
I got error
rustc: mismatched typesexpected type usize found tuple (usize, usize)
And my question is Why?
Link To Reproduce problem (Rust Playground)
I i try to see hows $dim
looks in this block of code
($elem:expr; $dim:expr) => {
vec![$elem; $dim]
};
I make tiny change in code
macro_rules! fill_tensor {
($elem:expr; ($dim:expr, $($dims:expr),+)) => {
vec![fill_tensor!($elem; ($($dims),+)); $dim]
};
($elem:expr; $dim:expr) => {
assert_eq!($shape, (3_usize, 3_usize))
};
}
And i have no error assertion correct
My final question is Why this pattern $elem:expr; ($dim:expr, $($dims:expr),+)
match to (3_usize, 3_usize)
but not match to this value in variable? And how to fix it?
Proger is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.