I'm a Rust beginner (I mainly have experience with C & Python).
How would one go about making a tabular data structure in Rust?
Specifically one that allows the user to specify the number of columns & their corresponding data types?
Would this be implemented as a pointer which points to a slice of pointers (whose size is equal to the number of columns specified), and then each of these pointers further point to a slice each for the specified datatypes?
The question is... why do you need such structure?
Is it so that the user of your library can store an unknown amount of values of unknown types?
Vec<Box<dyn Any>>, wrapped into some API which allows the user to specify exact types themselves.
Is it so that the user of your library can store a single value of unknown type (or a fixed amount of them)? Just make the table generic.
Is it so you can do some common processing on all this data (e.g. serialize it and send over the wire)? Make a trait which fully describes the behaviour you need, then use it instead of
That's only the three cases I was able to think of immediately - your goal might be different entirely.
My application is closest to the first case you described.
Is it not possible to use
Vec<Vec<DataType>> instead, where each inner vector represents a column & the inner
DataType is different for each vector (column)?
Here I could make a DataType an
enum to allow many different types, and in order to allow any arbitrary types use
But wouldn't there be some serious overhead with this implementation?
I'm basically considering writing the next iteration of my library (for financial analysis/trading) in Rust, it is currently written in C (& provides an API for Python).
Not directly; a
Vec requires all of its elements to have the same type, and that applies to the outer
Vec as well. There are a couple of other alternatives you can consider, though:
- Make a
Column enum, where each variant holds a
- If the column types will be known at compile time, you can use a mix of tuples and vectors:
(Vec<T1>, Vec<T2>, Vec<T3>, ...)
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.