Hi guys, I want to know the layout of a Sample structure by calling Layout::new instead of calculating it by myself. So I try to define this part of code to do so:
But I got complain from compiler that attempt to use a non-constant value in a constant. I understood that rustc requires all array sizes must be known during compiling, because the other code might use the structure definition.
However, the struct here is defined for layout calculating only and won't escape from the function scope. I wonder whether I have a way to bypass the limitation from rustc, or do I have a better way to do so?
I'm having trouble understanding what you mean. But I think you want to know how to fix the compiler error, so that you can create a Layout from the Header type, the Payload type, and an array size N. To do that, instead of passing n as a runtime parameter, pass N as a const generic parameter.
You cannot use a runtime value n to do this, because the Sample struct type must be known at compile time.
Hi @jumpnbrownweasel , thanks for your answer, but that's not my direction because the N cannot be changed to a const. It's known until the runtime. But thanks for your helping, I believe @Bruecki answer is what I want.