I’m extremely naive about rust still and I’ve painted myself into a corner, so any advice on the best approach/fix for this would be welcome. It’s a bit long sorry, but I got some simplified examples
Essentially my problem is I need to return an instance of a struct, but that struct is 1 of 5 possible types, but all will implement a specific trait.
My first attempt worked fine until I added the parse file method with the BufRead parameter, at this point rust complains about the trait cannot be made into an object.
The second attempt compiled and I was happy, until I try and actually use the buf, at that point I get the error the size for values of type R cannot be known at compilation time.
Is what I’m trying to do possible? My only rational for wanting this to accept the buf is so I could potentially write a unit test for it and be able to pass in the buf for the test.
If you plan to work only with files, then you can make parse_file non-generic method, which will accept &Path and will handle opening files inside itself.
I thought I was returning a Box, but still had the issues, I did wrap in Option if that made a difference? I’ll look into enum variants however as a better solution, thanks!
vitalyd
That actually does work! However I will also look into the enums, as that seems to be the suggested best approach.