I want to write a macro that return a [u8; some size] something akin to vec!.
macro_rules! buf{
($a:expr)=>{
{
[0u8; $a]
}
}
}
This works but I need to pass a const to buf!(). How can I change it to take a variable? For instance I want to use it as follows:
let len = get_some_size();
let buf = buf!(len);
Is this possible? Thanks in advance.
kpreid
February 25, 2023, 6:10am
2
An array, by definition, has a size set at compile time, so you cannot use a variable to pick the size. This is true whether or not macros are involved.
1 Like
To expand on that, from the docs for the primitive type array :
A fixed-size array, denoted [T; N]
, for the element type, T
, and the non-negative compile-time constant size, N
.
Perhaps you want to use a vector instead?
let len = get_some_size();
let buf = vec![0u8; len];
system
Closed
May 26, 2023, 7:35am
5
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.