This code does not work as we can not return a ref to a temporary. How can we fix this? My problem is that for some of the enum Constructors, we have a Vec, in which case we just return a ref. In other situations, we have a finite # of values, in which case we need to construct a Vec to return.
Pre emptive questions:
Can you make B, C, D take Vec? No, I want compile time guarantee of exactly 43/1 items.
I like @birkenfeld’s suggestion if you’re ok with using arrays. You can use an array for the f32s, and different types for the other fields - you can still return a slice from them in that case.
BTW, &Vec<f32> is a useless type. You should almost always use &[f32] instead. The difference between [] and Vec is that the Vec can grow, but &Vec is a read-only vec that can't grow.
There should be another difference: &[f32] is stack-allocated and can cause a stack overflow if it's too big, whereas &Vec<f32> is heap-allocated.
Unfortunately building a Vec is done on the stack in debug builds, so both can segfault your executable if they're too big.
Regardless that &[f32] is stack allocated pointer, I guess you meant, that [f32] is stack allocated which also is not true - its !Sized, so it is impossible to allocate it directly on stack at all. [f32; N] is "stack allocated" (which is not exactly true - where is it allocated is context dependent, but as a local variable it is allocated fully on stack, without any heap allocations).